Library of Assembled Shared Sources
slist.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43/** @class lass::stde::slist
44 * @brief a single linked list, just like the one in SGI STL (and STLport ...)
45 * @author Bram de Greve [Bramz]
46 */
47
48#ifndef LASS_GUARDIAN_OF_INCLUSION_STDE_SLIST_H
49#define LASS_GUARDIAN_OF_INCLUSION_STDE_SLIST_H
50
51#include "stde_common.h"
52#include "extended_io.h"
53#include "../meta/bool.h"
54#include "../meta/wrap.h"
55
56namespace lass
57{
58namespace stde
59{
60
61template <typename T, class Alloc = std::allocator<T> >
62class slist: private Alloc
63{
64private:
65
66 struct node_base_t
67 {
68 node_base_t* next;
69 };
70
71 struct node_t: public node_base_t
72 {
73 T value;
74 };
75
76public:
77
78 typedef T value_type;
79 typedef value_type& reference;
80 typedef const value_type& const_reference;
81 typedef typename std::allocator_traits<Alloc>::template rebind_alloc<value_type> allocator_type;
82 typedef typename std::allocator_traits<allocator_type>::pointer pointer;
83 typedef typename std::allocator_traits<allocator_type>::const_pointer const_pointer;
84 typedef typename std::allocator_traits<allocator_type>::size_type size_type;
85 typedef typename std::allocator_traits<allocator_type>::difference_type difference_type;
86 class const_iterator;
87 friend class const_iterator;
88 class iterator;
89 friend class iterator;
90
91 class iterator
92 {
93 public:
94 typedef typename slist<T, Alloc>::value_type value_type;
95 typedef typename slist<T, Alloc>::pointer pointer;
96 typedef typename slist<T, Alloc>::reference reference;
97 typedef typename slist<T, Alloc>::size_type size_type;
98 typedef typename slist<T, Alloc>::difference_type difference_type;
99 typedef std::forward_iterator_tag iterator_category;
100 iterator(): node_(0) {}
101 reference operator*() const
102 {
103 LASS_ASSERT(node_);
104 return static_cast<node_t* const>(node_)->value;
105 }
106 pointer operator->() const
107 {
108 LASS_ASSERT(node_);
109 return &static_cast<node_t* const>(node_)->value;
110 }
111 iterator& operator++() { node_ = node_->next; return *this; }
112 iterator operator++(int) { iterator result(*this); ++(*this); return result; }
113 bool operator==(const iterator& other) const { return node_ == other.node_; }
114 bool operator!=(const iterator& other) const { return !(*this == other); }
115 private:
116 friend class slist<T, Alloc>;
117 friend class const_iterator;
118 explicit iterator(node_base_t* node): node_(node) {}
119 node_base_t* node_;
120 };
121
122 class const_iterator
123 {
124 public:
125 typedef typename slist<T, Alloc>::value_type value_type;
126 typedef typename slist<T, Alloc>::pointer pointer;
127 typedef typename slist<T, Alloc>::reference reference;
128 typedef typename slist<T, Alloc>::size_type size_type;
129 typedef typename slist<T, Alloc>::difference_type difference_type;
130 typedef std::forward_iterator_tag iterator_category;
131 const_iterator(): node_(0) {}
132 const_iterator(iterator i): node_(i.node_) {}
133 const_reference operator*() const
134 {
135 LASS_ASSERT(node_);
136 return static_cast<const node_t* const>(node_)->value;
137 }
138 const_pointer operator->() const
139 {
140 LASS_ASSERT(node_);
141 return &static_cast<const node_t* const>(node_)->value;
142 }
143 const_iterator& operator++() { node_ = node_->next; return *this; }
144 const_iterator operator++(int) { const_iterator result(*this); ++(*this); return result; }
145 bool operator==(const const_iterator& other) const { return node_ == other.node_; }
146 bool operator!=(const const_iterator& other) const { return !(*this == other); }
147 private:
148 friend class slist<T, Alloc>;
149 explicit const_iterator(const node_base_t* node): node_(node) {}
150 const node_base_t* node_;
151 };
152
153 explicit slist(const Alloc& allocator = Alloc());
154 explicit slist(size_type n, const T& value = T(), const Alloc& allocator = Alloc());
155 template <typename InputIterator> slist(InputIterator first, InputIterator last,
156 const Alloc& allocator = Alloc());
157 slist(const slist<T, Alloc>& other);
159
161 template <typename InputIterator> void assign(InputIterator first, InputIterator last);
162 void assign(size_type n, const T& value = T());
163 allocator_type get_allocator() const;
164
165 iterator begin();
166 const_iterator begin() const;
167 iterator end();
168 const_iterator end() const;
169 iterator before_begin();
170 const_iterator before_begin() const;
171
172 iterator prior(iterator position) const;
173 const_iterator prior(const_iterator position) const;
174 iterator prior(iterator position, iterator start) const;
175 const_iterator prior(const_iterator position, const_iterator start) const;
176
177 bool empty() const;
178 size_type size() const;
179 size_type max_size() const;
180 void resize(size_type n, const T& value = T());
181
182 reference front();
183 const_reference front() const;
184 void push_front(const T& value);
185 void pop_front();
186
187 iterator insert(iterator position, const T& value);
188 void insert(iterator position, size_type n, const T& value);
189 template <typename InputIterator> void insert(iterator position, InputIterator first,
190 InputIterator last);
191
192 iterator insert_after(iterator position, const T& value);
193 void insert_after(iterator position, size_type n, const T& value);
194 template <typename InputIterator> void insert_after(iterator position, InputIterator first,
195 InputIterator last);
196
197 iterator erase(iterator position);
198 iterator erase(iterator position, iterator last);
199 void erase_after(iterator position);
200 void erase_after(iterator position, iterator last);
201 void swap(slist<T, Alloc>& other);
202 void clear();
203
204 void splice(iterator position, slist<T, Alloc>& other);
205 void splice(iterator position, slist<T, Alloc>& other, iterator x);
206 void splice(iterator position, slist<T, Alloc>& other, iterator first, iterator last);
207
208 void splice_after(iterator position, slist<T, Alloc>& other);
209 void splice_after(iterator position, slist<T, Alloc>& other, iterator before_x);
210 void splice_after(iterator position, slist<T, Alloc>& other, iterator before_first,
211 iterator before_last);
212
213 void remove(const T& value);
214 template <class UnaryPredicate> void remove_if(UnaryPredicate predicate);
215
216 void unique();
217 template <class BinaryPredicate> void unique(BinaryPredicate predicate);
218
219 void merge(slist<T, Alloc>& other);
220 template <class BinaryPredicate> void merge(slist<T, Alloc>& other, BinaryPredicate compare);
221
222 void sort();
223 template <class Compare> void sort(Compare compare);
224
225 void reverse();
226
227private:
228
229 typedef typename std::allocator_traits<allocator_type>::template rebind_alloc<node_t> node_allocator_type;
230
231 node_t* make_node(const T& value) const;
232 void unlink_and_destroy_after(node_base_t* position) const;
233 void link_after(node_base_t* position, node_base_t* node) const;
234 void splice_after(node_base_t* position, node_base_t* before_first,
235 node_base_t* before_last) const;
236
237 template <typename IntegerType>
238 void insert_after(iterator position, IntegerType n, IntegerType value,
239 meta::Wrap<meta::True> parameter_is_integral);
240 template <typename InputIterator> void insert_after(iterator position, InputIterator first,
241 InputIterator last, meta::Wrap<meta::False> parameter_is_iterator);
242
243 node_base_t head_;
244};
245
246template <typename T, class Alloc>
247bool operator==(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
248template <typename T, class Alloc>
249bool operator!=(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
250template <typename T, class Alloc>
251bool operator<(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
252template <typename T, class Alloc>
253bool operator>(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
254template <typename T, class Alloc>
255bool operator<=(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
256template <typename T, class Alloc>
257bool operator>=(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
258
259template <typename T, class Alloc, typename Char, typename Traits>
260std::basic_ostream<Char, Traits>&
261operator<<(std::basic_ostream<Char, Traits>& o_stream,
262 const slist<T, Alloc>& container);
263template <typename T, class Alloc, typename Char, typename Traits>
264std::basic_istream<Char, Traits>&
265operator>>(std::basic_istream<Char, Traits>& i_stream,
266 slist<T, Alloc>& container);
267
268}
269
270}
271
272#include "slist.inl"
273
274#endif
275
276// EOF
a single linked list, just like the one in SGI STL (and STLport ...)
Definition slist.h:63
iterator erase(iterator position, iterator last)
removes element in range from the list and return iterator to the next element.
Definition slist.inl:713
void clear()
removes all elements from the list
Definition slist.inl:783
void splice(iterator position, slist< T, Alloc > &other, iterator first, iterator last)
moves [ first , last ) from other to *this before position without copying.
Definition slist.inl:855
void sort()
Sorts *this according to operator<.
Definition slist.inl:1108
void splice_after(iterator position, slist< T, Alloc > &other)
moves all elements from other to *this after position without copying, and clears other.
Definition slist.inl:876
const_iterator prior(const_iterator position, const_iterator start) const
return the iterator that sits position and start searching for it from start.
Definition slist.inl:378
void merge(slist< T, Alloc > &other)
Merge two sorted containers to one big sorted.
Definition slist.inl:1056
bool operator==(const slist< T, Alloc > &a, const slist< T, Alloc > &b)
returns wether a and b are lexicographical idential.
Definition slist.inl:1305
void erase_after(iterator position, iterator last)
removes element in range from the list and return iterator to the next element.
Definition slist.inl:752
slist(const Alloc &allocator=Alloc())
Creates an empty list, using the provided allocator.
Definition slist.inl:82
iterator end()
returns an iterator for the position after the last element of the list.
Definition slist.inl:255
iterator before_begin()
returns an iterator for the position before the first element of the list.
Definition slist.inl:285
~slist()
destroys all elements and frees memory.
Definition slist.inl:150
reference front()
returns reference to first element in list.
Definition slist.inl:482
const_reference front() const
returns reference to first element in list.
Definition slist.inl:498
iterator begin()
returns an iterator to the first element of the list.
Definition slist.inl:229
void push_front(const T &value)
insert a copy of value at the front of the list so that the current list comes behind it.
Definition slist.inl:515
void unique()
removes all but the first element in every consecutive group of equal elements.
Definition slist.inl:987
iterator prior(iterator position, iterator start) const
return the iterator that sits position and start searching for it from start.
Definition slist.inl:353
size_type size() const
returns the N, the number of elements in the list.
Definition slist.inl:411
void splice_after(iterator position, slist< T, Alloc > &other, iterator before_x)
moves element before before_x from other to *this after position without copying.
Definition slist.inl:900
void assign(size_type n, const T &value=T())
replace *this by a list with n copies of value
Definition slist.inl:202
iterator erase(iterator position)
removes element at iterator position from the list and return iterator to the next element.
Definition slist.inl:687
const_iterator before_begin() const
returns an iterator for the position before the first element of the list.
Definition slist.inl:302
size_type max_size() const
returns a very rough estimation on what's the maximum number of elements you can ever have in an slis...
Definition slist.inl:432
bool empty() const
returns wether the list is empty.
Definition slist.inl:398
iterator insert(iterator position, const T &value)
inserts a new element before iterator position and returns iterator to it.
Definition slist.inl:553
void insert_after(iterator position, size_type n, const T &value)
inserts n copies of value after iterator position.
Definition slist.inl:640
void remove(const T &value)
removes all elements with iterarors i for which *i == value
Definition slist.inl:935
const_iterator end() const
returns an iterator for the position after the last element of the list.
Definition slist.inl:268
void unique(BinaryPredicate predicate)
removes all but the first element in every consecutive group of equivalent elements by a predicate.
Definition slist.inl:1021
void splice(iterator position, slist< T, Alloc > &other)
moves all elements from other to *this before position without copying, and clears other.
Definition slist.inl:806
bool operator!=(const slist< T, Alloc > &a, const slist< T, Alloc > &b)
returns wether a and b are not lexicographical idential.
Definition slist.inl:1337
void splice(iterator position, slist< T, Alloc > &other, iterator x)
moves x from other to *this before position without copying.
Definition slist.inl:829
const_iterator prior(const_iterator position) const
return the iterator that sits position and start searching for it from start.
Definition slist.inl:334
slist< T, Alloc > & operator=(slist< T, Alloc > other)
replace *this by a copy of other.
Definition slist.inl:167
void merge(slist< T, Alloc > &other, BinaryPredicate compare)
Merge two by compare sorted containers to one big sorted.
Definition slist.inl:1079
iterator prior(iterator position) const
return the iterator that sits position and start searching for it from start.
Definition slist.inl:318
slist(size_type n, const T &value=T(), const Alloc &allocator=Alloc())
Creates an list with n elements - each of which is a copy of value - and an allocator.
Definition slist.inl:100
void pop_front()
erases the first element in the list.
Definition slist.inl:532
slist(InputIterator first, InputIterator last, const Alloc &allocator=Alloc())
creates a list with as elements a copy of a range, and an allocator.
Definition slist.inl:120
void insert_after(iterator position, InputIterator first, InputIterator last)
inserts a copy of a range before iterator position.
Definition slist.inl:666
void splice_after(iterator position, slist< T, Alloc > &other, iterator before_first, iterator before_last)
moves ( before_first, before_last ] from other to *this after position without copying.
Definition slist.inl:920
iterator insert_after(iterator position, const T &value)
inserts a new element after iterator position and returns iterator to it.
Definition slist.inl:620
void resize(size_type n, const T &value=T())
at the end of the list, inserts copies of value or erases elements so that list size is n.
Definition slist.inl:450
void insert(iterator position, InputIterator first, InputIterator last)
inserts a copy of a range before iterator position.
Definition slist.inl:601
void erase_after(iterator position)
removes element after iterator position from the list.
Definition slist.inl:732
void swap(slist< T, Alloc > &other)
exchange all data between two lists.
Definition slist.inl:770
void remove_if(UnaryPredicate predicate)
removes all elements with iterarors i for which predicate(*i) yields true.
Definition slist.inl:961
const_iterator begin() const
returns an iterator to the first element of the list.
Definition slist.inl:242
allocator_type get_allocator() const
returns the memory model of the list.
Definition slist.inl:216
void insert(iterator position, size_type n, const T &value)
inserts n copies of value before iterator position.
Definition slist.inl:576
void reverse()
reverses the order of the elements.
Definition slist.inl:1171
slist(const slist< T, Alloc > &other)
copies an entire list and its elements and allocator.
Definition slist.inl:136
lass extensions to the standard library
Library for Assembled Shared Sources.
Definition config.h:53