Circular.h Source File

Simu5G: Circular.h Source File
Simu5G
Circular.h
1//
2// Simu5G
3//
4// Copyright (C) 2012-2021 Giovanni Nardini, Giovanni Stea, Antonio Virdis et al. (University of Pisa)
5// Copyright (C) 2022-2026 Giovanni Nardini, Giovanni Stea et al. (University of Pisa)
6//
7// This file is part of a software released under the license included in file
8// "license.pdf". Please read LICENSE and README files before using it.
9// The above files and the present reference are part of the software itself,
10// and cannot be removed from it.
11//
12
13#ifndef _LTE_CIRCULAR_H_
14#define _LTE_CIRCULAR_H_
15
16#include <list>
17#include <assert.h>
18
19namespace simu5g {
20
22template<typename T>
24{
26 std::list<T> list_;
27
29 typename std::list<T>::iterator cur_;
30
32 unsigned int size_ = 0;
33
34 public:
36 CircularList() : cur_(list_.begin())
37 {
38 }
39
41 CircularList(const CircularList<T>& cl) : list_(cl.list_), cur_(list_.begin()), size_(cl.size_)
42 {
43 auto it = cl.list_.begin();
44 if (size_ != 0) {
45 while (it != cl.cur_) {
46 ++it;
47 ++cur_;
48 }
49 }
50 }
51
54 {
55 list_ = cl.list_;
56 size_ = cl.size_;
57 auto it = cl.list_.begin();
58 cur_ = list_.begin();
59 if (size_ != 0) {
60 while (it != cl.cur_) {
61 ++it;
62 ++cur_;
63 }
64 }
65 return *this;
66 }
67
69 bool empty()
70 {
71 return size_ == 0;
72 }
73
75 unsigned int size()
76 {
77 return size_;
78 }
79
81 void clear()
82 {
83 list_.clear();
84 size_ = 0;
85 }
86
88 bool find(const T& t)
89 {
90 if (size_ == 0)
91 return false;
92 for (const auto& element : list_) {
93 if (element == t) {
94 return true;
95 }
96 }
97 return false;
98 }
99
101
104 T& find(T& t, bool& valid)
105 {
106 if (size_ == 0) {
107 valid = false;
108 return t;
109 }
110 for (const auto& element : list_) {
111 if (element == t) {
112 valid = true;
113 return const_cast<T&>(element);
114 }
115 }
116 valid = false;
117 return t;
118 }
119
121 void insert(const T& t)
122 {
123 if (size_ == 0) {
124 list_.push_back(t);
125 cur_ = list_.begin();
126 }
127 else
128 list_.insert(cur_, t);
129 ++size_;
130 }
131
133 void erase()
134 {
135 if (size_ == 0)
136 return;
137 typename std::list<T>::iterator drop = cur_++;
138 if (cur_ == list_.end())
139 cur_ = list_.begin();
140 list_.erase(drop);
141 --size_;
142 }
143
145 void eraseElem(T& t)
146 {
147 if (size_ == 0)
148 return;
149 for (auto it = list_.begin(); it != list_.end();) {
150 if (*it == t) {
151 if (cur_ == it) {
152 cur_++;
153 if (cur_ == list_.end())
154 cur_ = list_.begin();
155 }
156 it = list_.erase(it);
157 --size_;
158 return;
159 } else {
160 ++it;
161 }
162 }
163 }
164
166 void rewind()
167 {
168 cur_ = list_.begin();
169 }
170
172 void move()
173 {
174 if (size_ > 0 && ++cur_ == list_.end())
175 cur_ = list_.begin();
176 }
177
179
184 const T& current() const
185 {
186 assert(size_ > 0);
187 return *cur_;
188 }
189
192 {
193 assert(size_ > 0);
194 return *cur_;
195 }
196
198 void insertFront(const T& t)
199 {
200 if (size_ == 0) {
201 list_.push_back(t);
202 cur_ = list_.begin();
203 }
204 else {
205 typename std::list<T>::iterator temp = cur_;
206 ++temp;
207 list_.insert(temp, t);
208 }
209 ++size_;
210 }
211
212};
213
214} //namespace
215
216#endif // _LTE_CIRCULAR_H_
217
T & find(T &t, bool &valid)
Finds an element in the list and return it.
Definition Circular.h:104
void eraseElem(T &t)
Erases the specified element (eventually the current position is shifted)
Definition Circular.h:145
CircularList()
Create an empty circular list.
Definition Circular.h:36
bool empty()
Return true if the list is empty.
Definition Circular.h:69
const T & current() const
Return the current element.
Definition Circular.h:184
void move()
Moves the pointer to the next element in a circular fashion.
Definition Circular.h:172
unsigned int size()
Return the number of elements.
Definition Circular.h:75
CircularList(const CircularList< T > &cl)
Copy constructor.
Definition Circular.h:41
void insert(const T &t)
Insert a new element before the current position.
Definition Circular.h:121
void insertFront(const T &t)
Insert a new element after the current position.
Definition Circular.h:198
void rewind()
Goes back to the beginning of the circular list.
Definition Circular.h:166
CircularList & operator=(const CircularList< T > &cl)
Assignment operator.
Definition Circular.h:53
void clear()
Removes all the elements in the list.
Definition Circular.h:81
T & current()
Return the current element.
Definition Circular.h:191
bool find(const T &t)
Return true if a given element is in the list.
Definition Circular.h:88
void erase()
Removes the element at the current position.
Definition Circular.h:133
Definition AlertReceiver.h:24