2 // Copyright 2008-2009 Daniel James.
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 // Gratuitous single linked list.
8 // Sadly some STL implementations aren't up to scratch and I need a simple
9 // cross-platform container. So here it is.
11 #if !defined(UNORDERED_TEST_LIST_HEADER)
12 #define UNORDERED_TEST_LIST_HEADER
14 #include <boost/limits.hpp>
19 template <typename It1, typename It2>
20 bool equal(It1 begin, It1 end, It2 compare)
22 for (; begin != end; ++begin, ++compare)
23 if (*begin != *compare)
28 template <typename It1, typename It2, typename Pred>
29 bool equal(It1 begin, It1 end, It2 compare, Pred predicate)
31 for (; begin != end; ++begin, ++compare)
32 if (!predicate(*begin, *compare))
37 template <typename T> class list;
39 namespace test_detail {
40 template <typename T> class list_node;
41 template <typename T> class list_data;
42 template <typename T> class list_iterator;
43 template <typename T> class list_const_iterator;
45 template <typename T> class list_node
47 list_node(list_node const&);
48 list_node& operator=(list_node const&);
54 list_node(T const& v) : value_(v), next_(0) {}
55 list_node(T const& v, list_node* n) : value_(v), next_(n) {}
58 template <typename T> class list_data
61 typedef list_node<T> node;
62 typedef unsigned int size_type;
68 list_data() : first_(0), last_ptr_(&first_), size_(0) {}
74 first_ = first_->next_;
80 list_data(list_data const&);
81 list_data& operator=(list_data const&);
86 : public std::iterator<std::forward_iterator_tag, T, int, T*, T&>
88 friend class list_const_iterator<T>;
89 friend class test::list<T>;
90 typedef list_node<T> node;
91 typedef list_const_iterator<T> const_iterator;
96 list_iterator() : ptr_(0) {}
97 explicit list_iterator(node* x) : ptr_(x) {}
99 T& operator*() const { return ptr_->value_; }
100 T* operator->() const { return &ptr_->value_; }
101 list_iterator& operator++()
106 list_iterator operator++(int)
108 list_iterator tmp = *this;
112 bool operator==(const_iterator y) const { return ptr_ == y.ptr_; }
113 bool operator!=(const_iterator y) const { return ptr_ != y.ptr_; }
116 template <typename T>
117 class list_const_iterator : public std::iterator<std::forward_iterator_tag,
118 T, int, T const*, T const&>
120 friend class list_iterator<T>;
121 friend class test::list<T>;
122 typedef list_node<T> node;
123 typedef list_iterator<T> iterator;
124 typedef list_const_iterator<T> const_iterator;
129 list_const_iterator() : ptr_(0) {}
130 list_const_iterator(list_iterator<T> const& x) : ptr_(x.ptr_) {}
132 T const& operator*() const { return ptr_->value_; }
133 T const* operator->() const { return &ptr_->value_; }
135 list_const_iterator& operator++()
141 list_const_iterator operator++(int)
143 list_const_iterator tmp = *this;
148 bool operator==(const_iterator y) const { return ptr_ == y.ptr_; }
150 bool operator!=(const_iterator y) const { return ptr_ != y.ptr_; }
154 template <typename T> class list
156 typedef test::test_detail::list_data<T> data;
157 typedef test::test_detail::list_node<T> node;
161 typedef T value_type;
162 typedef value_type& reference;
163 typedef value_type const& const_reference;
164 typedef unsigned int size_type;
166 typedef test::test_detail::list_iterator<T> iterator;
167 typedef test::test_detail::list_const_iterator<T> const_iterator;
171 list(list const& other) : data_() { insert(other.begin(), other.end()); }
173 template <class InputIterator>
174 list(InputIterator i, InputIterator j) : data_()
179 list& operator=(list const& other)
182 insert(other.begin(), other.end());
186 iterator begin() { return iterator(data_.first_); }
187 iterator end() { return iterator(); }
188 const_iterator begin() const { return iterator(data_.first_); }
189 const_iterator end() const { return iterator(); }
190 const_iterator cbegin() const { return iterator(data_.first_); }
191 const_iterator cend() const { return iterator(); }
193 template <class InputIterator> void insert(InputIterator i, InputIterator j)
199 void push_front(value_type const& v)
201 data_.first_ = new node(v, data_.first_);
203 data_.last_ptr_ = &(*data_.last_ptr_)->next_;
207 void push_back(value_type const& v)
209 *data_.last_ptr_ = new node(v);
210 data_.last_ptr_ = &(*data_.last_ptr_)->next_;
216 while (data_.first_) {
217 node* tmp = data_.first_;
218 data_.first_ = data_.first_->next_;
222 data_.last_ptr_ = &data_.first_;
225 void erase(const_iterator i, const_iterator j)
227 node** ptr = &data_.first_;
229 while (*ptr != i.ptr_) {
230 ptr = &(*ptr)->next_;
233 while (*ptr != j.ptr_) {
234 node* to_delete = *ptr;
235 *ptr = (*ptr)->next_;
241 data_.last_ptr_ = ptr;
244 bool empty() const { return !data_.size_; }
246 size_type size() const { return data_.size_; }
248 void sort() { sort(std::less<T>()); }
250 template <typename Less> void sort(Less less = Less())
254 &data_.first_, (std::numeric_limits<size_type>::max)(), less);
257 bool operator==(list const& y) const
259 return size() == y.size() && test::equal(begin(), end(), y.begin());
262 bool operator!=(list const& y) const { return !(*this == y); }
265 template <typename Less>
266 node** merge_sort(node** l, size_type recurse_limit, Less less)
268 node** ptr = &(*l)->next_;
269 for (size_type count = 0; count < recurse_limit && *ptr; ++count) {
270 ptr = merge_adjacent_ranges(l, ptr, merge_sort(ptr, count, less), less);
275 template <typename Less>
276 node** merge_adjacent_ranges(
277 node** first, node** second, node** third, Less less)
283 if (less((*second)->value_, (*first)->value_))
285 first = &(*first)->next_;
288 swap_adjacent_ranges(first, second, third);
289 first = &(*first)->next_;
291 // Since the two ranges we just swapped, the order is now:
292 // first...third...second
297 if (!less((*first)->value_, (*third)->value_))
299 first = &(*first)->next_;
302 swap_adjacent_ranges(first, third, second);
303 first = &(*first)->next_;
307 void swap_adjacent_ranges(node** first, node** second, node** third)
314 data_.last_ptr_ = second;