TaskSched
Classes | Public Member Functions | List of all members
SimpleList< T > Class Template Reference

A simple linked list implementation. More...

#include <SimpleList.h>

Classes

class  const_iterator
 Iterator class for SimpleList. More...
 
class  iterator
 

Public Member Functions

 SimpleList ()
 Construct a new empty SimpleList object. More...
 
 ~SimpleList ()
 Destroy the SimpleList object and free all allocated memory. More...
 
void push_back (const T &value)
 Add a new element to the end of the list. More...
 
void pop_front ()
 Remove the first element from the list. More...
 
read () const
 Read the current element and move the read position to the next element. More...
 
void rewind () const
 Reset the read position to the beginning of the list. More...
 
bool is_exhausted () const
 Check if all elements have been read. More...
 
size_t get_size () const
 Get the number of elements in the list. More...
 
void clear ()
 Remove all elements from the list. More...
 
const_iterator cbegin () const
 Get a const_iterator pointing to the beginning of the list. More...
 
const_iterator cend () const
 Get a const_iterator pointing to the end of the list. More...
 
iterator begin () const
 Get an iterator pointing to the beginning of the list. More...
 
iterator end () const
 Get an iterator pointing to the end of the list. More...
 

Detailed Description

template<typename T>
class SimpleList< T >

A simple linked list implementation.

Template Parameters
TThe type of elements stored in the list

Definition at line 11 of file SimpleList.h.

Constructor & Destructor Documentation

◆ SimpleList()

template<typename T >
SimpleList< T >::SimpleList ( )
inline

Construct a new empty SimpleList object.

Definition at line 37 of file SimpleList.h.

37 : head(nullptr), tail(nullptr), read_position(nullptr), size(0) {}

◆ ~SimpleList()

template<typename T >
SimpleList< T >::~SimpleList ( )
inline

Destroy the SimpleList object and free all allocated memory.

Definition at line 42 of file SimpleList.h.

42  {
43  clear();
44  }
void clear()
Remove all elements from the list.
Definition: SimpleList.h:123

References SimpleList< T >::clear().

Member Function Documentation

◆ begin()

template<typename T >
iterator SimpleList< T >::begin ( ) const
inline

Get an iterator pointing to the beginning of the list.

Returns
iterator Iterator to the first element

Definition at line 273 of file SimpleList.h.

273 { return iterator(head); }

Referenced by Sched::run().

◆ cbegin()

template<typename T >
const_iterator SimpleList< T >::cbegin ( ) const
inline

Get a const_iterator pointing to the beginning of the list.

Returns
const_iterator Const iterator to the first element

Definition at line 204 of file SimpleList.h.

204 { return const_iterator(head); }

◆ cend()

template<typename T >
const_iterator SimpleList< T >::cend ( ) const
inline

Get a const_iterator pointing to the end of the list.

Returns
const_iterator Const iterator to the element after the last element

Definition at line 211 of file SimpleList.h.

211 { return const_iterator(nullptr); }

◆ clear()

template<typename T >
void SimpleList< T >::clear ( )
inline

Remove all elements from the list.

Definition at line 123 of file SimpleList.h.

123  {
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  }

Referenced by SimpleList< T >::~SimpleList().

◆ end()

template<typename T >
iterator SimpleList< T >::end ( ) const
inline

Get an iterator pointing to the end of the list.

Returns
iterator Iterator to the element after the last element

Definition at line 280 of file SimpleList.h.

280 { return iterator(nullptr); }

Referenced by Sched::run().

◆ get_size()

template<typename T >
size_t SimpleList< T >::get_size ( ) const
inline

Get the number of elements in the list.

Returns
size_t The number of elements

Definition at line 116 of file SimpleList.h.

116  {
117  return size;
118  }

Referenced by Sched::getSize().

◆ is_exhausted()

template<typename T >
bool SimpleList< T >::is_exhausted ( ) const
inline

Check if all elements have been read.

Returns
true if read_position is null, false otherwise

Definition at line 107 of file SimpleList.h.

107  {
108  return read_position == nullptr;
109  }

◆ pop_front()

template<typename T >
void SimpleList< T >::pop_front ( )
inline

Remove the first element from the list.

Definition at line 65 of file SimpleList.h.

65  {
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  }

◆ push_back()

template<typename T >
void SimpleList< T >::push_back ( const T &  value)
inline

Add a new element to the end of the list.

Parameters
valueThe value to be added

Definition at line 51 of file SimpleList.h.

51  {
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  }

Referenced by Sched::addTask().

◆ read()

template<typename T >
T SimpleList< T >::read ( ) const
inline

Read the current element and move the read position to the next element.

Returns
T The current element (empty T if read_position is null)

Definition at line 86 of file SimpleList.h.

86  {
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  }

◆ rewind()

template<typename T >
void SimpleList< T >::rewind ( ) const
inline

Reset the read position to the beginning of the list.

Definition at line 98 of file SimpleList.h.

98  {
99  read_position = head;
100  }

The documentation for this class was generated from the following file: