Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
list.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 
8 Copyright(c) 2014 jwellbelove
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files(the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions :
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 ******************************************************************************/
28 
29 #ifndef __ETL_LIST__
30 #define __ETL_LIST__
31 
32 #include <stddef.h>
33 
34 #include "ilist.h"
35 #include "container.h"
36 
37 //*****************************************************************************
41 //*****************************************************************************
42 
43 namespace etl
44 {
45  //*************************************************************************
49  //*************************************************************************
50  template <typename T, const size_t MAX_SIZE_>
51  class list : public ilist<T>
52  {
53  public:
54 
55  static const size_t MAX_SIZE = MAX_SIZE_;
56 
57  public:
58 
59  typedef T value_type;
60  typedef T* pointer;
61  typedef const T* const_pointer;
62  typedef T& reference;
63  typedef const T& const_reference;
64  typedef size_t size_type;
65 
66  //*************************************************************************
68  //*************************************************************************
69  list()
70  : ilist<T>(&node_pool[0], MAX_SIZE)
71  {
72  }
73 
74  //*************************************************************************
76  //*************************************************************************
77  explicit list(size_t initialSize, typename ilist<T>::parameter_t value = T())
78  : ilist<T>(&node_pool[0], MAX_SIZE)
79  {
80  ilist<T>::assign(initialSize, value);
81  }
82 
83  //*************************************************************************
85  //*************************************************************************
86  explicit list(const list& other)
87  : ilist<T>(&node_pool[0], MAX_SIZE)
88  {
89  ilist<T>::assign(other.cbegin(), other.cend());
90  }
91 
92  //*************************************************************************
94  //*************************************************************************
95  template <typename TIterator>
96  list(TIterator first, TIterator last)
97  : ilist<T>(&node_pool[0], MAX_SIZE)
98  {
99  ilist<T>::assign(first, last);
100  }
101 
102  //*************************************************************************
104  //*************************************************************************
105  list& operator = (const list& rhs)
106  {
107  ilist<T>::assign(rhs.cbegin(), rhs.cend());
108 
109  return *this;
110  }
111 
112  //*************************************************************************
114  //*************************************************************************
115  void swap(list& other)
116  {
117  // Re-align the node pointers for this list.
118  if (this->terminal_node.previous != 0)
119  {
120  size_t index = std::distance(&node_pool[0], static_cast<typename ilist<T>::Data_Node*>(this->terminal_node.previous));
121  this->terminal_node.previous = &node_pool[index];
122  }
123 
124  if (this->terminal_node.next != 0)
125  {
126  size_t index = std::distance(&node_pool[0], static_cast<typename ilist<T>::Data_Node*>(this->terminal_node.next));
127  this->terminal_node.next = &node_pool[index];
128  }
129 
130  for (size_t i = 0; i < MAX_SIZE; ++i)
131  {
132  typename ilist<T>::Data_Node& data_node = node_pool[i];
133 
134  if (!data_node.is_free())
135  {
136  size_t index;
137 
138  index = std::distance(&node_pool[0], static_cast<typename ilist<T>::Data_Node*>(data_node.previous));
139  data_node.previous = &other.node_pool[index];
140 
141  index = std::distance(&node_pool[0], static_cast<typename ilist<T>::Data_Node*>(data_node.next));
142  data_node.next = &other.node_pool[index];
143  }
144  }
145 
146  // Re-align the node pointers for the other list.
147  if (other.terminal_node.previous != 0)
148  {
149  size_t index = std::distance(&other.node_pool[0], static_cast<typename ilist<T>::Data_Node*>(other.terminal_node.previous));
150  other.terminal_node.previous = &other.node_pool[index];
151  }
152 
153  if (other.terminal_node.next != 0)
154  {
155  size_t index = std::distance(&other.node_pool[0], static_cast<typename ilist<T>::Data_Node*>(other.terminal_node.next));
156  other.terminal_node.next = &other.node_pool[index];
157  }
158 
159  for (size_t i = 0; i < MAX_SIZE; ++i)
160  {
161  typename ilist<T>::Data_Node& data_node = other.node_pool[i];
162 
163  if (!data_node.is_free())
164  {
165  size_t index;
166 
167  index = std::distance(&other.node_pool[0], static_cast<typename ilist<T>::Data_Node*>(data_node.previous));
168  data_node.previous = &node_pool[index];
169 
170  index = std::distance(&other.node_pool[0], static_cast<typename ilist<T>::Data_Node*>(data_node.next));
171  data_node.next = &node_pool[index];
172  }
173  }
174 
175  // Swap the data.
176  std::swap_ranges(etl::begin(node_pool), etl::end(node_pool), etl::begin(other.node_pool));
177  std::swap(this->next_free, other.next_free);
178  std::swap(this->current_size, other.current_size);
179  }
180 
181  private:
182 
184  typename ilist<T>::Data_Node node_pool[MAX_SIZE];
185  };
186 
187  //*************************************************************************
189  //*************************************************************************
190  template <typename T, const size_t MAX_SIZE>
192  {
193  first.swap(second);
194  }
195 }
196 
197 #endif
void assign(TIterator first, TIterator last)
If ETL_THROW_EXCEPTIONS & _DEBUG are defined throws list_iterator if the iterators are reversed...
Definition: ilist.h:501
list()
Default constructor.
Definition: list.h:69
void swap(etl::bitset< N > &lhs, etl::bitset< N > &rhs)
swap
Definition: bitset.h:1200
size_type current_size
The number of the used nodes.
Definition: list_base.h:148
Definition: list.h:51
Definition: algorithm.h:43
TContainer::iterator end(TContainer &container)
Definition: container.h:95
TContainer::iterator begin(TContainer &container)
Definition: container.h:45
void swap(list &other)
Swap.
Definition: list.h:115
list(const list &other)
Copy constructor.
Definition: list.h:86
list(TIterator first, TIterator last)
Construct from range.
Definition: list.h:96
const_iterator cend() const
Gets the end of the list.
Definition: ilist.h:418
size_type next_free
The index of the next free node.
Definition: list_base.h:147
list & operator=(const list &rhs)
Assignment operator.
Definition: list.h:105
list(size_t initialSize, typename ilist< T >::parameter_t value=T())
Construct from size and value.
Definition: list.h:77
Node terminal_node
The node that acts as the list start and end.
Definition: ilist.h:122
bool is_free() const
Checks if the node is free.
Definition: ilist.h:96
Definition: ilist.h:54
const_iterator cbegin() const
Gets the beginning of the list.
Definition: ilist.h:410
The data node element in the list.
Definition: ilist.h:116