Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

ring.inc
Go to the documentation of this file.
1 
29 #pragma once
30 
31 namespace abacus {
32 
33 
34 template <class Type>
35 inline AbaRing<Type>::AbaRing(int size)
36  :
37  ring_(size),
38  head_(0),
39  filled_(false)
40 { }
41 
42 
43 template <class Type>
44 std::ostream &operator<<(std::ostream &out, const AbaRing<Type> &rhs)
45 {
46  if(rhs.filled_) {
47  const int s = rhs.size();
48 
49  for(int i = rhs.head_; i < s; i++)
50  out << rhs.ring_[i] << " ";
51  }
52 
53  for (int i = 0; i < rhs.head_; i++)
54  out << rhs.ring_[i] << " ";
55 
56  out << std::endl;
57 
58  return out;
59 }
60 
61 
62 template <class Type>
63 inline Type& AbaRing<Type>::operator[](int i)
64 {
65  return ring_[i];
66 }
67 
68 
69 template <class Type>
70 inline const Type& AbaRing<Type>::operator[](int i) const
71 {
72  return ring_[i];
73 }
74 
75 
76 template <class Type>
77 void AbaRing<Type>::insert(Type elem)
78 {
79  ring_[head_] = elem;
80 
81  if (++head_ == size()) {
82  if (!filled_) filled_ = true;
83  head_ = 0;
84  }
85 }
86 
87 
88 template <class Type>
89 inline void AbaRing<Type>::clear()
90 {
91  head_ = 0;
92  filled_ = false;
93 }
94 
95 
96 template <class Type>
97 inline int AbaRing<Type>::size() const
98 {
99  return ring_.size();
100 }
101 
102 
103 template <class Type>
104 inline int AbaRing<Type>::number() const
105 {
106  if (filled_)
107  return size();
108  else
109  return head_;
110 }
111 
112 
113 template <class Type>
114 inline Type AbaRing<Type>::oldest() const
115 {
116  if(filled_) return ring_[head_];
117  else return ring_[0];
118 }
119 
120 
121 template <class Type>
122 inline int AbaRing<Type>::oldestIndex() const
123 {
124  if(filled_) return head_;
125  else return 0;
126 }
127 
128 
129 template <class Type>
130 inline Type AbaRing<Type>::newest() const
131 {
132  if (head_) return ring_[head_ - 1];
133  else return ring_[size() - 1];
134 }
135 
136 
137 template <class Type>
138 inline int AbaRing<Type>::newestIndex() const
139 {
140  if (head_) return head_ - 1;
141  else return size() - 1;
142 }
143 
144 
145 template <class Type>
146 int AbaRing<Type>::previous(int i, Type &p) const
147 {
148  int j = head_ - 1 - i;
149 
150  if (j >= 0) {
151  p = ring_[j];
152  return 0;
153  }
154  else if (filled_) {
155  p = ring_[size() + j];
156  return 0;
157  }
158  else return 1;
159 }
160 
161 
162 template <class Type>
163 inline bool AbaRing<Type>::empty() const
164 {
165  return !(head_ || filled_);
166 }
167 
168 
169 template <class Type>
170 inline bool AbaRing<Type>::filled() const
171 {
172  return filled_;
173 }
174 
175 
176 template <class Type>
177 void AbaRing<Type>::realloc(int newSize)
178 {
179  Array<Type> tmp = ring_;
180  int oldSize = size();
181  int oldHead = head_;
182  int i;
183 
184  ring_.realloc(newSize);
185 
186  if(newSize > oldSize) {
187  // increase ring
188  /* If the ring is increased yet has not been filled, nothing has to be
189  * done. Otherwise, the elements of the old ring are copied in the correct
190  * order.
191  */
192  if (filled_) {
193  head_ = 0;
194  for(i = oldHead; i < oldSize; i++) ring_[head_++] = tmp[i];
195  for(i = 0; i < oldHead; i++) ring_[head_++] = tmp[i];
196  filled_ = false;
197  }
198  }
199  else {
200  // decrease ring
201  /* If the ring is decreased and it is filled, then we copy the elements of the
202  * old ring in the correct order. If the ring is not filled we only have
203  * to copy the elements if by decreasing the ring elements are removed.
204  */
205  if (filled_) {
206  for (head_ = size() - 1, i = oldHead - 1; head_ >= 0; head_--, i--) {
207  if (i < 0) i = oldSize - 1;
208  ring_[head_] = tmp[i];
209  }
210  head_ = 0;
211  }
212  else if (oldHead > size()) {
213  for (head_ = size() - 1, i = oldHead - 1; head_ >= 0; --head_, --i)
214  ring_[head_] = tmp[i];
215  head_ = 0;
216  filled_ = true;
217  }
218  }
219 }
220 
221 }
abacus
Definition: abacusroot.h:48
ogdf::dot::Attribute::Type
@ Type
ogdf::tlp::Attribute::size
@ size
ogdf::operator<<
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:978
abacus::AbaRing::AbaRing
AbaRing(int size)
The constructor.