SheafSystem  0.0.0.0
singly_linked_list.h
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 #ifndef SINGLY_LINKED_LIST_H
19 #define SINGLY_LINKED_LIST_H
20 
21 #define HAVE_FORWARD_LIST 1
22 
23 #ifndef SHEAF_DLL_SPEC_H
24 #include "sheaf_dll_spec.h"
25 #endif
26 
27 // Include the appropriate header file.
28 
29 #ifdef HAVE_FORWARD_LIST
30 #include <forward_list>
31 namespace list_ns = std;
32 #else
33 #include <ext/slist>
34 namespace list_ns = __gnu_cxx;
35 #endif
36 
37 namespace sheaf
38 {
39 
40 // Forward declarations to enable friend declaration.
41 
42 template< class T, class Alloc > class singly_linked_list;
43 
44 template< class T, class Alloc >
46  const singly_linked_list<T,Alloc>& rhs);
47 
48 template< class T, class Alloc >
49 bool operator<( const singly_linked_list<T,Alloc>& lhs,
50  const singly_linked_list<T,Alloc>& rhs);
51 
61 template <typename T, class A = list_ns::allocator<T> >
62 class SHEAF_DLL_SPEC singly_linked_list
63 {
64 
65  friend bool operator==<T, A>(const singly_linked_list<T,A>& lhs,
66  const singly_linked_list<T,A>& rhs);
67  friend bool operator< <T, A>(const singly_linked_list<T,A>& lhs,
68  const singly_linked_list<T,A>& rhs);
69 
70 public:
71 
72 #ifdef HAVE_FORWARD_LIST
73  typedef typename list_ns::forward_list<T, A>::value_type value_type;
74  typedef typename list_ns::forward_list<T, A>::size_type size_type;
75  typedef typename list_ns::forward_list<T, A>::difference_type difference_type;
76  typedef typename list_ns::forward_list<T, A>::reference reference;
77  typedef typename list_ns::forward_list<T, A>::const_reference const_reference;
78  typedef typename list_ns::forward_list<T, A>::pointer pointer;
79  typedef typename list_ns::forward_list<T, A>::iterator iterator;
80  typedef typename list_ns::forward_list<T, A>::const_iterator const_iterator;
81 #else
82  typedef typename list_ns::slist<T, A>::value_type value_type;
83  typedef typename list_ns::slist<T, A>::size_type size_type;
84  typedef typename list_ns::slist<T, A>::difference_type difference_type;
85  typedef typename list_ns::slist<T, A>::reference reference;
86  typedef typename list_ns::slist<T, A>::const_reference const_reference;
87  typedef typename list_ns::slist<T, A>::pointer pointer;
88  typedef typename list_ns::slist<T, A>::iterator iterator;
89  typedef typename list_ns::slist<T, A>::const_iterator const_iterator;
90 #endif
91 
96  {
97  };
98 
104  singly_linked_list(size_type count)
105  : _list(count)
106  {
107  };
108 
112  singly_linked_list(size_type count, const T& value)
113  : _list(count, value)
114  {
115  };
116 
121  : _list(other._list)
122  {
123  };
124 
129  {
130  };
131 
137  {
138  _list = other._list;
139 
140  return *this;
141  };
142 
150  {
151  _list = other._list;
152 
153  return *this;
154  };
155 
161  {
162  _list.swap(other._list);
163  };
164 
168  reference front()
169  {
170  return _list.front();
171  };
172 
176  const_reference front() const
177  {
178  return _list.front();
179  };
180 
184  iterator begin()
185  {
186  return _list.begin();
187  };
188 
192  const_iterator begin() const
193  {
194  return _list.begin();
195  };
196 
201  iterator end()
202  {
203  return _list.end();
204  };
205 
210  const_iterator end() const
211  {
212  return _list.end();
213  };
214 
218  bool empty() const
219  {
220  return _list.empty();
221  };
222 
227  size_type max_size() const
228  {
229  return _list.max_size();
230  };
231 
235  void clear()
236  {
237  _list.clear();
238  };
239 
240 #ifdef HAVE_FORWARD_LIST
241 
242  //
243  // Forward_list signatures for insert_after on Windows. Use the Windows
244  // signature because two of the insert_after after signatures have a
245  // void return time. The standard states that all insert_after signatures
246  // should return an iterator but we need this code to compile on Windows.
247  //
248 
252  iterator insert_after(const_iterator pos, const T& value)
253  {
254  return _list.insert_after(pos, value);
255  };
256 
260  iterator insert_after(const_iterator pos, T&& value)
261  {
262  return _list.insert_after(pos, value);
263  };
264 
268  void insert_after(const_iterator pos, size_type count, const T& value)
269  {
270  _list.insert_after(pos, count, value);
271  };
272 
277  template<class InputIt>
278  void insert_after(const_iterator pos, InputIt first, InputIt last)
279  {
280  _list.insert_after(pos, first, last);
281  };
282 
283  //
284  // Forward_list standard and Windows signatures for erase_after.
285  //
286 
290  iterator erase_after(const_iterator position)
291  {
292  return _list.erase_after(position);
293  };
294 
295 #else // HAVE_FORWARD_LIST
296 
297  //
298  // Slist signatures for insert_after and erase_after.
299  //
300 
304  iterator insert_after(iterator pos, const T& value)
305  {
306  return _list.insert_after(pos, value);
307  };
308 
312  iterator insert_after(iterator pos, T& value)
313  {
314  return _list.insert_after(pos, value);
315  };
316 
320  void insert_after(iterator pos, size_type count, const T& value)
321  {
322  _list.insert_after(pos, count, value);
323  };
324 
329  template<class InputIt>
330  void insert_after(iterator pos, InputIt first, InputIt last)
331  {
332  _list.insert_after(pos, first, last);
333  };
334 
338  iterator erase_after(iterator position)
339  {
340  return _list.erase_after(position);
341  };
342 
343 #endif // HAVE_FORWARD_LIST
344 
348  void push_front(const T& value)
349  {
350  _list.push_front(value);
351  };
352 
356  void push_front(T& value)
357  {
358  _list.push_front(value);
359  };
360 
364  template<class InputIt>
365  void push_front(InputIt first, InputIt last)
366  {
367 #ifdef HAVE_FORWARD_LIST
368  _list.insert_after(_list.before_begin(), first, last);
369 #else
370  _list.insert(_list.begin(), first, last);
371 #endif
372  };
373 
377  void pop_front()
378  {
379  _list.pop_front();
380  };
381 
386  void resize(size_type count)
387  {
388  _list.resize(count);
389  };
390 
395  void resize(size_type count, const value_type& value)
396  {
397  _list.resize(count, value);
398  };
399 
405  {
406  _list.merge(other._list);
407  };
408 
409 
413  void remove(const T& value)
414  {
415  _list.remove(value);
416  };
417 
421  void reverse()
422  {
423  _list.reverse();
424  };
425 
429  void unique()
430  {
431  _list.unique();
432  };
433 
437  void sort()
438  {
439  _list.sort();
440  };
441 
442 private:
443 
444 #ifdef HAVE_FORWARD_LIST
445  list_ns::forward_list<T, A> _list;
446 #else
447  list_ns::slist<T, A> _list;
448 #endif
449 
450 };
451 
457 template< class T, class Alloc >
459  const singly_linked_list<T,Alloc>& rhs)
460 {
461  return (lhs._list == rhs._list);
462 };
463 
464 template< class T, class Alloc >
465 bool operator<( const singly_linked_list<T,Alloc>& lhs,
466  const singly_linked_list<T,Alloc>& rhs)
467 {
468  return (lhs._list < rhs._list);
469 };
470 
471 } // namespace sheaf
472 
473 #endif // SINGLY_LINKED_LIST_H
void resize(size_type count)
Resizes the container to contain count elements. Additional value-initialized elements are appended...
void merge(singly_linked_list &other)
Merges two sorted lists into one. The lists should be sorted into ascending order.
void insert_after(const_iterator pos, InputIt first, InputIt last)
inserts elements from range [first, last) after the element pointed to by pos.
void resize(size_type count, const value_type &value)
Resizes the container to contain count elements. Additional copies of value are appended.
singly_linked_list(size_type count)
Constructs the container with count value-initialized (default constructed, for classes) instances of...
bool empty() const
Checks if the container has no elements, i.e. whether begin() == end().
const_iterator begin() const
Returns an iterator to the first element of the container.
long difference_type
A signed integral type used to represent the difference of two indices or iterators.
Definition: sheaf.h:47
iterator insert_after(const_iterator pos, T &&value)
Inserts value after the element pointed to by pos.
iterator erase_after(const_iterator position)
Removes the element following pos.
STL namespace.
reference front()
Returns a reference to the first element in the container.
void push_front(InputIt first, InputIt last)
Prepends the range [first, last) to the beginning of the containter.
iterator begin()
Returns an iterator to the first element of the container.
void insert_after(const_iterator pos, size_type count, const T &value)
Inserts count copies of the value after the element pointed to by pos.
const_iterator end() const
Returns an iterator to the element following the last element of the container.
void pop_front()
Removes the first element of the container.
size_type max_size() const
Returns the maximum number of elements the container is able to hold due to system or library impleme...
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
singly_linked_list & operator=(const singly_linked_list &other)
Copy assignment operator. Replaces the contents with a copy of the contents of other.
singly_linked_list(size_type count, const T &value)
Constructs the container with count copies of elements with value value.
iterator end()
Returns an iterator to the element following the last element of the container.
singly_linked_list & operator=(singly_linked_list &other)
Move assignment operator. Replaces the contents with those of other using move semantics (i...
const_reference front() const
Returns a reference to the first element in the container.
bool operator==(const singly_linked_list< T, Alloc > &lhs, const singly_linked_list< T, Alloc > &rhs)
Checks if the contents of lhs and rhs are equal, that is, whether lhs.size() == rhs.size() and each element in lhs compares equal with the element in rhs at the same position.
void push_front(const T &value)
Prepends the given element value to the beginning of the container.
singly_linked_list(const singly_linked_list &other)
Copy Constructor.
void swap(singly_linked_list &other)
Echanges the contents of the container with those of other. Does not invoke any move, copy, or swap operations on individual elements.
void clear()
Removes all elements from the container.
void sort()
Sorts the elements in ascending order.
Namespace for the sheaves component of the sheaf system.
iterator insert_after(const_iterator pos, const T &value)
Inserts value after the element pointed to by pos.
Interface for class sheaf_dll_spec.
void push_front(T &value)
Prepends the given element value to the beginning of the container.
void unique()
Removes all consecutive duplicate elements from the container.
Wrapper class for forward_list or slist depending on compiler. The class replicates the minimum subse...
singly_linked_list()
Default Constructor.
void reverse()
Reverses the order of the elements in the container.