FreeNOS
List.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIB_LIBSTD_LIST_H
19 #define __LIB_LIBSTD_LIST_H
20 
21 #include "Macros.h"
22 #include "Assert.h"
23 #include "Sequence.h"
24 
36 template <class T> class List : public Sequence<T>
37 {
38  public:
39 
43  class Node
44  {
45  public:
46 
50  Node(T t) : data(t)
51  {
52  prev = ZERO;
53  next = ZERO;
54  }
55 
57  T data;
58 
61 
64  };
65 
69  List()
70  {
71  m_head = ZERO;
72  m_tail = ZERO;
73  m_count = 0;
74  }
75 
81  List(const List<T> & lst)
82  {
83  m_head = ZERO;
84  m_tail = ZERO;
85  m_count = 0;
86 
87  for (Node *node = lst.m_head; node; node = node->next)
88  append(node->data);
89  }
90 
94  virtual ~List()
95  {
96  Node *node = m_head;
97 
98  while (node)
99  {
100  Node *tmp = node;
101  node = node->next;
102  delete tmp;
103  }
104  }
105 
111  void prepend(T t)
112  {
113 
114  // Create a new node with the item.
115  Node *node = new Node(t);
116 
117  // Connect the item to the list head, if set
118  if (m_head)
119  {
120  m_head->prev = node;
121  node->next = m_head;
122  }
123  // Make the new node head of the list.
124  m_head = node;
125 
126  // Also make it the tail, if not yet set
127  if (!m_tail)
128  m_tail = node;
129 
130  // Update node count
131  m_count++;
132  }
133 
139  void append(T t)
140  {
141  Node *node = new Node(t);
142  node->prev = m_tail;
143 
144  // Connect the item with the tail, if any.
145  if (m_tail)
146  m_tail->next = node;
147 
148  // Make the new Node the tail of the list.
149  m_tail = node;
150 
151  // Also make the item the head, if none.
152  if (!m_head)
153  m_head = node;
154 
155  // Update node count.
156  m_count++;
157  }
158 
166  virtual int remove(T t)
167  {
168  Node *node = m_head;
169  Node *next;
170  int num = 0;
171 
172  while (node)
173  {
174  next = node->next;
175 
176  if (node->data == t)
177  {
178  remove(node);
179  num++;
180  }
181  node = next;
182  }
183  return num;
184  }
185 
193  virtual int remove(Node *node)
194  {
195  if (node->prev)
196  node->prev->next = node->next;
197 
198  if (node == m_head)
199  m_head = node->next;
200 
201  if (node->next)
202  node->next->prev = node->prev;
203 
204  if (node == m_tail)
205  m_tail = node->prev;
206 
207  m_count--;
208  delete node;
209  return true;
210  }
211 
219  virtual bool contains(const T t) const
220  {
221 
222  for (Node *i = m_head; i; i = i->next)
223  if (i->data == t)
224  return true;
225 
226  return false;
227  }
228 
232  virtual void clear()
233  {
234  Node *node = m_head, *next = ZERO;
235 
236  // Delete all Node and optionally items too.
237  while (node)
238  {
239  next = node->next;
240  delete node;
241  node = next;
242  }
243  // Clear administration
244  m_head = ZERO;
245  m_tail = ZERO;
246  m_count = 0;
247  }
248 
254  Node * head()
255  {
256  return m_head;
257  }
258 
262  const Node * head() const
263  {
264  return m_head;
265  }
266 
272  Node * tail()
273  {
274  return m_tail;
275  }
276 
280  const Node * tail() const
281  {
282  return m_tail;
283  }
284 
292  T first()
293  {
294  return m_head->data;
295  }
296 
304  const T first() const
305  {
306  return m_head->data;
307  }
308 
316  T last()
317  {
318  return m_tail->data;
319  }
320 
328  const T last() const
329  {
330  return m_tail->data;
331  }
332 
340  virtual const T * get(Size position) const
341  {
342  Size num = 0;
343  Node *node = m_head;
344 
345  // Is the index within bounds of the list?
346  if (position >= m_count)
347  return ZERO;
348 
349  // Find the item and return it.
350  while (num++ < position)
351  node = node->next;
352 
353  return &node->data;
354  }
355 
365  virtual const T & at(Size position) const
366  {
367  Size num = 0;
368  Node *node = m_head;
369 
370  // Find the item and return it.
371  while (num++ < position)
372  node = node->next;
373 
374  return node->data;
375  }
376 
382  virtual bool isEmpty() const
383  {
384  return !m_head;
385  }
386 
392  Size size() const
393  {
394  return m_count;
395  }
396 
402  Size count() const
403  {
404  return m_count;
405  }
406 
411  {
412  append(t);
413  return (*this);
414  }
415 
419  bool operator == (const List<T> & lst) const
420  {
421  if (lst.count() != m_count)
422  return false;
423 
424  for (Node *n = m_head; n; n = n->next)
425  if (!lst.contains(n->data))
426  return false;
427 
428  return true;
429  }
430 
434  bool operator != (const List<T> & lst) const
435  {
436  if (lst.count() != m_count)
437  return true;
438 
439  for (Node *n = m_head; n; n = n->next)
440  if (!lst.contains(n->data))
441  return true;
442 
443  return false;
444  }
445 
446  private:
447 
449  Node *m_head;
450 
452  Node *m_tail;
453 
456 };
457 
463 #endif /* __LIB_LIBSTD_LIST_H */
Macros.h
List::head
const Node * head() const
Get the first Node on the List (read-only).
Definition: List.h:262
List::List
List()
Class constructor.
Definition: List.h:69
List::clear
virtual void clear()
Clears the entire List.
Definition: List.h:232
List::operator<<
List & operator<<(T t)
Append operator.
Definition: List.h:410
List::operator!=
bool operator!=(const List< T > &lst) const
Inequality operator.
Definition: List.h:434
List::Node::prev
Node * prev
Previous node.
Definition: List.h:60
Sequence.h
List::head
Node * head()
Get the first Node on the list.
Definition: List.h:254
List::at
virtual const T & at(Size position) const
Get a reference to the item at the given position.
Definition: List.h:365
Assert.h
List::count
Size count() const
Get the number of items on the list.
Definition: List.h:402
List::Node::next
Node * next
Next node.
Definition: List.h:63
List::isEmpty
virtual bool isEmpty() const
Check if the List is empty.
Definition: List.h:382
List::remove
virtual int remove(T t)
Remove all items which are equal to the given item.
Definition: List.h:166
List::m_head
Node * m_head
Head of the List.
Definition: List.h:449
Sequence
Sequences are containers that provide indexed based storage of items.
Definition: Sequence.h:37
List::first
const T first() const
Get the first value as constant.
Definition: List.h:304
List::tail
Node * tail()
Get the last Node on the list.
Definition: List.h:272
List::append
void append(T t)
Insert an item at the end of the list.
Definition: List.h:139
List::m_count
Size m_count
Number of items currently in the List.
Definition: List.h:455
List::remove
virtual int remove(Node *node)
Removes the given node from the list.
Definition: List.h:193
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
List::prepend
void prepend(T t)
Insert an item at the start of the list.
Definition: List.h:111
List::size
Size size() const
Get the size of the list.
Definition: List.h:392
List::~List
virtual ~List()
Class destructor.
Definition: List.h:94
List::get
virtual const T * get(Size position) const
Get a pointer to the item at the given position.
Definition: List.h:340
List::m_tail
Node * m_tail
Tail of the list.
Definition: List.h:452
List::first
T first()
Get the first value in the list.
Definition: List.h:292
List::Node::Node
Node(T t)
Constructor.
Definition: List.h:50
List::contains
virtual bool contains(const T t) const
Check whether an element is on the List.
Definition: List.h:219
List::tail
const Node * tail() const
Get the last Node on the List (read-only).
Definition: List.h:280
List
Simple linked list template class.
Definition: List.h:36
List::List
List(const List< T > &lst)
Copy constructor.
Definition: List.h:81
List::Node::data
T data
Item of this node.
Definition: List.h:57
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
List::last
const T last() const
Get the last value on the list as constant.
Definition: List.h:328
List::last
T last()
Get the last value on the list.
Definition: List.h:316
List::operator==
bool operator==(const List< T > &lst) const
Comparison operator.
Definition: List.h:419
List::Node
Represents an item on the List.
Definition: List.h:43