Embedded Template Library  1.0
 All Classes Files Functions Variables Typedefs Friends Modules Pages
forward_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 "iforward_list.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 forward_list : public iforward_list<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  //*************************************************************************
70  : iforward_list<T>(&node_pool[0], MAX_SIZE)
71  {
72  }
73 
74  //*************************************************************************
76  //*************************************************************************
77  explicit forward_list(size_t initialSize, typename iforward_list<T>::parameter_t value = T())
78  : iforward_list<T>(&node_pool[0], MAX_SIZE)
79  {
80  iforward_list<T>::assign(initialSize, value);
81  }
82 
83  //*************************************************************************
85  //*************************************************************************
86  explicit forward_list(const forward_list& other)
87  : iforward_list<T>(&node_pool[0], MAX_SIZE)
88  {
89  iforward_list<T>::assign(other.cbegin(), other.cend());
90  }
91 
92  //*************************************************************************
94  //*************************************************************************
95  template <typename TIterator>
96  forward_list(TIterator first, TIterator last)
97  : iforward_list<T>(&node_pool[0], MAX_SIZE)
98  {
99  iforward_list<T>::assign(first, last);
100  }
101 
102  //*************************************************************************
104  //*************************************************************************
106  {
107  iforward_list<T>::assign(rhs.cbegin(), rhs.cend());
108 
109  return *this;
110  }
111 
112  //*************************************************************************
114  //*************************************************************************
115  void swap(forward_list& other)
116  {
117  // Re-align the node pointers for this forward_list.
118  if (this->start_node.next != 0)
119  {
120  size_t index = std::distance(&node_pool[0], static_cast<typename iforward_list<T>::Data_Node*>(this->start_node.next));
121  this->start_node.next = &node_pool[index];
122  }
123 
124  if (this->end_node.next != 0)
125  {
126  size_t index = std::distance(&node_pool[0], static_cast<typename iforward_list<T>::Data_Node*>(this->end_node.next));
127  this->end_node.next = &node_pool[index];
128  }
129 
130  for (size_t i = 0; i < MAX_SIZE; ++i)
131  {
132  typename iforward_list<T>::Node& node = node_pool[i];
133 
134  if (!node.is_free())
135  {
136  size_t index = std::distance(&node_pool[0], static_cast<typename iforward_list<T>::Data_Node*>(node.next));
137  node.next = &other.node_pool[index];
138  }
139  }
140 
141  // Re-align the node pointers for the other forward_list.
142  if (other.start_node.next != 0)
143  {
144  size_t index = std::distance(&other.node_pool[0], static_cast<typename iforward_list<T>::Data_Node*>(other.start_node.next));
145  other.start_node.next = &other.node_pool[index];
146  }
147 
148  if (other.end_node.next != 0)
149  {
150  size_t index = std::distance(&other.node_pool[0], static_cast<typename iforward_list<T>::Data_Node*>(other.end_node.next));
151  other.end_node.next = &other.node_pool[index];
152  }
153 
154  for (size_t i = 0; i < MAX_SIZE; ++i)
155  {
156  typename iforward_list<T>::Node& node = other.node_pool[i];
157 
158  if (!node.is_free())
159  {
160  size_t index = std::distance(&other.node_pool[0], static_cast<typename iforward_list<T>::Data_Node*>(node.next));
161  node.next = &node_pool[index];
162  }
163  }
164 
165  // Swap the data.
166  std::swap_ranges(etl::begin(node_pool), etl::end(node_pool), etl::begin(other.node_pool));
167  std::swap(this->next_free, other.next_free);
168  std::swap(this->count, other.count);
169  }
170 
171  private:
172 
174  typename iforward_list<T>::Data_Node node_pool[MAX_SIZE];
175  };
176 
177  //*************************************************************************
179  //*************************************************************************
180  template <typename T, const size_t MAX_SIZE>
182  {
183  first.swap(second);
184  }
185 }
186 
187 #endif
const_iterator cbegin() const
Gets the beginning of the forward_list.
Definition: iforward_list.h:312
Node start_node
The node that acts as the forward_list start.
Definition: iforward_list.h:966
void swap(etl::bitset< N > &lhs, etl::bitset< N > &rhs)
swap
Definition: bitset.h:1200
size_type next_free
The index of the next free node.
Definition: forward_list_base.h:147
forward_list(size_t initialSize, typename iforward_list< T >::parameter_t value=T())
Construct from size and value.
Definition: forward_list.h:77
void swap(forward_list &other)
Swap.
Definition: forward_list.h:115
const_iterator cend() const
Gets the end of the forward_list.
Definition: iforward_list.h:336
Definition: forward_list.h:51
Definition: algorithm.h:43
void assign(TIterator first, TIterator last)
If ETL_THROW_EXCEPTIONS & _DEBUG are defined throws forward_list_iterator if the iterators are revers...
Definition: iforward_list.h:381
The data node element in the forward_list.
Definition: iforward_list.h:97
TContainer::iterator end(TContainer &container)
Definition: container.h:95
TContainer::iterator begin(TContainer &container)
Definition: container.h:45
forward_list()
Default constructor.
Definition: forward_list.h:69
forward_list & operator=(const forward_list &rhs)
Assignment operator.
Definition: forward_list.h:105
forward_list(const forward_list &other)
Copy constructor.
Definition: forward_list.h:86
Node end_node
The node that acts as the forward_list end.
Definition: iforward_list.h:967
forward_list(TIterator first, TIterator last)
Construct from range.
Definition: forward_list.h:96
size_type count
The number of the used nodes.
Definition: forward_list_base.h:148
Definition: iforward_list.h:74
Definition: iforward_list.h:54