Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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