TaskSched
SimpleList.h
Go to the documentation of this file.
1 #ifndef SIMPLE_LIST_H
2 #define SIMPLE_LIST_H
3 
4 
10 template<typename T>
11 class SimpleList {
12 private:
16  struct Node {
17  T data;
18  Node* next;
19 
25  Node(const T& value) : data(value), next(nullptr) {}
26  };
27 
28  Node* head;
29  Node* tail;
30  mutable Node* read_position;
31  size_t size;
32 
33 public:
37  SimpleList() : head(nullptr), tail(nullptr), read_position(nullptr), size(0) {}
38 
43  clear();
44  }
45 
51  void push_back(const T& value) {
52  Node* new_node = new Node(value);
53  if (!head) {
54  head = tail = read_position = new_node;
55  } else {
56  tail->next = new_node;
57  tail = new_node;
58  }
59  ++size;
60  }
61 
65  void pop_front() {
66  if (head) {
67  Node* temp = head;
68  head = head->next;
69  if (read_position == temp) {
70  read_position = head;
71  }
72  delete temp;
73  --size;
74  if (!head) {
75  tail = nullptr;
76  read_position = nullptr;
77  }
78  }
79  }
80 
86  T read() const {
87  if (read_position) {
88  T value = read_position->data;
89  read_position = read_position->next;
90  return value;
91  }
92  return T(); // Return empty T
93  }
94 
98  void rewind() const {
99  read_position = head;
100  }
101 
107  bool is_exhausted() const {
108  return read_position == nullptr;
109  }
110 
116  size_t get_size() const {
117  return size;
118  }
119 
123  void clear() {
124  while (head) {
125  Node* temp = head;
126  head = head->next;
127  delete temp;
128  }
129  tail = nullptr;
130  read_position = nullptr;
131  size = 0;
132  }
133 
138  private:
139  const Node* current;
140 
141  public:
145  const_iterator() : current(nullptr) {}
146 
152  explicit const_iterator(const Node* node) : current(node) {}
153 
159  const T& operator*() const { return current->data; }
160 
166  const T* operator->() const { return &current->data; }
167 
173  const_iterator& operator++() { if (current) current = current->next; return *this; }
174 
180  const_iterator operator++(int) { const_iterator tmp = *this; ++(*this); return tmp; }
181 
188  bool operator!=(const const_iterator& other) const { return current != other.current; }
189 
196  bool operator==(const const_iterator& other) const { return current == other.current; }
197  };
198 
204  const_iterator cbegin() const { return const_iterator(head); }
205 
211  const_iterator cend() const { return const_iterator(nullptr); }
212 
213  class iterator {
214  private:
215  Node* current;
216 
217  public:
221  iterator() : current(nullptr) {}
222 
228  iterator(Node* node) : current(node) {}
229 
235  T& operator*() { return current->data; }
236 
242  T* operator->() { return &current->data; }
243 
249  iterator& operator++() { if (current) current = current->next; return *this; }
250 
257  bool operator!=(const iterator& other) const { return current != other.current; }
258 
265  bool operator==(const iterator& other) const { return current == other.current; }
266  };
267 
273  iterator begin() const { return iterator(head); }
274 
280  iterator end() const { return iterator(nullptr); }
281 };
282 
283 #endif // SIMPLE_LIST_H
Iterator class for SimpleList.
Definition: SimpleList.h:137
const_iterator()
Construct a new const_iterator object.
Definition: SimpleList.h:145
const T & operator*() const
Dereference operator.
Definition: SimpleList.h:159
bool operator!=(const const_iterator &other) const
Inequality comparison operator.
Definition: SimpleList.h:188
const T * operator->() const
Arrow operator.
Definition: SimpleList.h:166
const_iterator operator++(int)
Postfix increment operator.
Definition: SimpleList.h:180
const_iterator(const Node *node)
Construct a new const_iterator object.
Definition: SimpleList.h:152
bool operator==(const const_iterator &other) const
Equality comparison operator.
Definition: SimpleList.h:196
const_iterator & operator++()
Prefix increment operator.
Definition: SimpleList.h:173
bool operator==(const iterator &other) const
Equality comparison operator.
Definition: SimpleList.h:265
iterator(Node *node)
Construct a new iterator object.
Definition: SimpleList.h:228
iterator()
Construct a new iterator object.
Definition: SimpleList.h:221
T & operator*()
Dereference operator.
Definition: SimpleList.h:235
bool operator!=(const iterator &other) const
Inequality comparison operator.
Definition: SimpleList.h:257
iterator & operator++()
Prefix increment operator.
Definition: SimpleList.h:249
T * operator->()
Arrow operator.
Definition: SimpleList.h:242
A simple linked list implementation.
Definition: SimpleList.h:11
void push_back(const T &value)
Add a new element to the end of the list.
Definition: SimpleList.h:51
size_t get_size() const
Get the number of elements in the list.
Definition: SimpleList.h:116
bool is_exhausted() const
Check if all elements have been read.
Definition: SimpleList.h:107
const_iterator cbegin() const
Get a const_iterator pointing to the beginning of the list.
Definition: SimpleList.h:204
void pop_front()
Remove the first element from the list.
Definition: SimpleList.h:65
iterator begin() const
Get an iterator pointing to the beginning of the list.
Definition: SimpleList.h:273
void rewind() const
Reset the read position to the beginning of the list.
Definition: SimpleList.h:98
~SimpleList()
Destroy the SimpleList object and free all allocated memory.
Definition: SimpleList.h:42
SimpleList()
Construct a new empty SimpleList object.
Definition: SimpleList.h:37
T read() const
Read the current element and move the read position to the next element.
Definition: SimpleList.h:86
void clear()
Remove all elements from the list.
Definition: SimpleList.h:123
const_iterator cend() const
Get a const_iterator pointing to the end of the list.
Definition: SimpleList.h:211
iterator end() const
Get an iterator pointing to the end of the list.
Definition: SimpleList.h:280