blocxx
List.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2004 Vintela, Inc. All rights reserved.
3* Copyright (C) 2005 Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* - Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10*
11* - Redistributions in binary form must reproduce the above copyright notice,
12* this list of conditions and the following disclaimer in the documentation
13* and/or other materials provided with the distribution.
14*
15* - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
16* contributors may be used to endorse or promote products derived from this
17* software without specific prior written permission.
18*
19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22* ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE
23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*******************************************************************************/
31
32
37
38#ifndef BLOCXX_LIST_HPP_INCLUDE_GUARD_
39#define BLOCXX_LIST_HPP_INCLUDE_GUARD_
40#include "blocxx/BLOCXX_config.h"
42#include <list>
43
44namespace BLOCXX_NAMESPACE
45{
46
47// forward declarations are necessary for template friends.
48template<class T> class List;
49
50template <class T>
51inline bool operator==(const List<T>& x, const List<T>& y);
52
53template <class T>
54inline bool operator<(const List<T>& x, const List<T>& y);
55
56
60template<class T> class List
61{
62private:
63 typedef std::list<T> L;
65public:
66 typedef typename L::value_type value_type;
67 typedef typename L::pointer pointer;
68 typedef typename L::const_pointer const_pointer;
69 typedef typename L::reference reference;
70 typedef typename L::const_reference const_reference;
71 typedef typename L::size_type size_type;
72 typedef typename L::difference_type difference_type;
73 typedef typename L::iterator iterator;
74 typedef typename L::const_iterator const_iterator;
75 typedef typename L::reverse_iterator reverse_iterator;
76 typedef typename L::const_reverse_iterator const_reverse_iterator;
77
81 List() : m_impl(new L) {}
86 explicit List(L* toWrap) : m_impl(toWrap)
87 {
88 }
89
94 template<class InputIterator>
95 List(InputIterator first, InputIterator last)
96 : m_impl(new L(first, last))
97 {
98 }
99
106 List(size_type n, const T& value) : m_impl(new L(n, value))
107 {
108 }
109
116 List(int n, const T& value) : m_impl(new L(n, value))
117 {
118 }
119
126 List(long n, const T& value) : m_impl(new L(n, value))
127 {
128 }
129
134 explicit List(size_type n) : m_impl(new L(n))
135 {
136 }
137
142 {
143 return &*m_impl;
144 }
145
151 {
152 return m_impl->begin();
153 }
154
160 {
161 return m_impl->begin();
162 }
163
169 {
170 return m_impl->end();
171 }
172
178 {
179 return m_impl->end();
180 }
181
187 {
188 return m_impl->rbegin();
189 }
190
196 {
197 return m_impl->rbegin();
198 }
199
205 {
206 return m_impl->rend();
207 }
208
214 {
215 return m_impl->rend();
216 }
217
220 bool empty() const
221 {
222 return m_impl->empty();
223 }
224
228 {
229 return m_impl->size();
230 }
231
235 {
236 return m_impl->max_size();
237 }
238
242 {
243 return m_impl->front();
244 }
245
249 {
250 return m_impl->front();
251 }
252
256 {
257 return m_impl->back();
258 }
259
263 {
264 return m_impl->back();
265 }
266
270 void swap(List<T>& x)
271 {
272 m_impl.swap(x.m_impl);
273 }
274
282 iterator insert(iterator position, const T& x)
283 {
284 return m_impl->insert(position, x);
285 }
286
294 {
295 return m_impl->insert(position);
296 }
297
304 template<class InputIterator>
305 void insert(iterator position, InputIterator first, InputIterator last)
306 {
307 m_impl->insert(position, first, last);
308 }
309
318 void insert(iterator pos, size_type n, const T& x)
319 {
320 m_impl->insert(pos, n, x);
321 }
322
331 void insert(iterator pos, int n, const T& x)
332 {
333 m_impl->insert(pos, n, x);
334 }
335
344 void insert(iterator pos, long n, const T& x)
345 {
346 m_impl->insert(pos, n, x);
347 }
348
353 void push_front(const T& x)
354 {
355 m_impl->push_front(x);
356 }
357
362 void push_back(const T& x)
363 {
364 m_impl->push_back(x);
365 }
366
374 {
375 return m_impl->erase(position);
376 }
377
387 {
388 return m_impl->erase(first, last);
389 }
390
396 void resize(size_type new_size, const T& x)
397 {
398 m_impl->resize(new_size, x);
399 }
400
406 void resize(size_type new_size)
407 {
408 m_impl->resize(new_size);
409 }
410
414 void clear()
415 {
416 m_impl->clear();
417 }
418
428 const_iterator last) const
429 {
430 for( ; first != end(); ++first)
431 {
432 if( x == *first)
433 return first;
434 if( first == last)
435 break;
436 }
437 return end();
438 }
439
445 const_iterator find(const T &x) const
446 {
447 return find(x, begin(), end());
448 }
449
458 iterator find(const T &x, iterator first, iterator last)
459 {
460 for( ; first != end(); ++first)
461 {
462 if( x == *first)
463 return first;
464 if( first == last)
465 break;
466 }
467 return end();
468 }
469
475 iterator find(const T &x)
476 {
477 return find(x, begin(), end());
478 }
479
488 bool contains(const T& x, const_iterator first,
489 const_iterator last) const
490 {
491 return find(x, first, last) != end();
492 }
493
498 bool contains(const T& x) const
499 {
500 return find(x, begin(), end()) != end();
501 }
502
506 {
507 m_impl->pop_front();
508 }
509
512 void pop_back()
513 {
514 m_impl->pop_back();
515 }
516
522 void splice(iterator position, List& x)
523 {
524 m_impl->splice(position, *x.m_impl);
525 }
526
534 void splice(iterator position, List& x, iterator i)
535 {
536 m_impl->splice(position, *x.m_impl, i);
537 }
538
547 void splice(iterator position, List& x, iterator first, iterator last)
548 {
549 m_impl->splice(position, *x.m_impl, first, last);
550 }
551
555 void remove(const T& value)
556 {
557 m_impl->remove(value);
558 }
559
562 void unique()
563 {
564 m_impl->unique();
565 }
566
571 void merge(List& x)
572 {
573 m_impl->merge(*x.m_impl);
574 }
575
578 void reverse()
579 {
580 m_impl->reverse();
581 }
582
585 void sort()
586 {
587 m_impl->sort();
588 }
589
594 template<class Predicate> void remove_if (Predicate p)
595 {
596 m_impl->remove_if (p);
597 }
598
603 template<class BinaryPredicate> void unique(BinaryPredicate bp)
604 {
605 m_impl->unique(bp);
606 }
607
612 template<class StrictWeakOrdering> void merge(List& x, StrictWeakOrdering swo)
613 {
614 m_impl->merge(*x.m_impl, swo);
615 }
616
620 template<class StrictWeakOrdering> void sort(StrictWeakOrdering swo)
621 {
622 m_impl->sort(swo);
623 }
624
632 friend bool operator== <>(const List<T>& x, const List<T>& y);
640 friend bool operator< <>(const List<T>& x, const List<T>& y);
641};
642template <class T>
643inline bool operator==(const List<T>& x, const List<T>& y)
644{
645 return *x.m_impl == *y.m_impl;
646}
647template <class T>
648inline bool operator<(const List<T>& x, const List<T>& y)
649{
650 return *x.m_impl < *y.m_impl;
651}
652template <class T>
653inline void swap(List<T>& x, List<T>& y)
654{
655 x.swap(y);
656}
657template <class T>
658std::list<T>* COWReferenceClone(std::list<T>* obj)
659{
660 return new std::list<T>(*obj);
661}
662
663} // end namespace BLOCXX_NAMESPACE
664
665#endif
COWReference A smart pointer that uses non-intrusive reference counting.
This class is a wrapper around std::list<> and adds COW capabilities.
Definition List.hpp:61
void resize(size_type new_size, const T &x)
Ensure the List has a given size.
Definition List.hpp:396
void reverse()
Reverse the order of elements in the list.
Definition List.hpp:578
iterator erase(iterator position)
Remove an element from the List specified with an iterator.
Definition List.hpp:373
void pop_front()
Remove the first element in the List.
Definition List.hpp:505
L::reference reference
Definition List.hpp:69
List(int n, const T &value)
Construct a List that consist of a specified number of elements that are copies of a given object.
Definition List.hpp:116
void insert(iterator pos, int n, const T &x)
Insert a specified number of elements that are copies of a given object before the given position in ...
Definition List.hpp:331
iterator insert(iterator position, const T &x)
Insert an element to the List before the element specified by the iterator.
Definition List.hpp:282
iterator find(const T &x, iterator first, iterator last)
Find element x in the list range specified by the first and last iterators.
Definition List.hpp:458
void unique(BinaryPredicate bp)
Remove all elements from the List for which the binary predicate bp is true.
Definition List.hpp:603
bool contains(const T &x, const_iterator first, const_iterator last) const
Determine if element x is contained in the list range specified by the first and last iterators.
Definition List.hpp:488
L::value_type value_type
Definition List.hpp:66
void insert(iterator pos, long n, const T &x)
Insert a specified number of elements that are copies of a given object before the given position in ...
Definition List.hpp:344
reverse_iterator rend()
Definition List.hpp:204
const_iterator end() const
Definition List.hpp:177
L::const_iterator const_iterator
Definition List.hpp:74
const_reference front() const
Definition List.hpp:248
L::difference_type difference_type
Definition List.hpp:72
L::const_pointer const_pointer
Definition List.hpp:68
List(size_type n)
Construct a List that consist of a specified number of elements that have be constructed using the de...
Definition List.hpp:134
bool empty() const
Definition List.hpp:220
void sort(StrictWeakOrdering swo)
Sort the list using the specified comparisation class.
Definition List.hpp:620
L::pointer pointer
Definition List.hpp:67
void pop_back()
Remove the last element in the List.
Definition List.hpp:512
reference back()
Definition List.hpp:255
List()
Default Constructor.
Definition List.hpp:81
void insert(iterator pos, size_type n, const T &x)
Insert a specified number of elements that are copies of a given object before the given position in ...
Definition List.hpp:318
iterator erase(iterator first, iterator last)
Remove elements from the List specified by a beginning and ending iterator.
Definition List.hpp:386
const_iterator begin() const
Definition List.hpp:159
L * getImpl()
@doctodo
Definition List.hpp:141
const_reverse_iterator rbegin() const
Definition List.hpp:195
void insert(iterator position, InputIterator first, InputIterator last)
Insert a range of elements before a given position in the List.
Definition List.hpp:305
void remove(const T &value)
Remove the specified element from the List.
Definition List.hpp:555
L::const_reference const_reference
Definition List.hpp:70
void merge(List &x)
Merge the current and specified lists, producing a combined list that is ordered with respect to the ...
Definition List.hpp:571
void sort()
Sort the list using the < operator to compare elements.
Definition List.hpp:585
friend bool operator==(const List< T > &x, const List< T > &y)
Determine equality of two Lists comparing the size of both lists and all elements in the same positio...
Definition List.hpp:643
void clear()
Remove all items from the List.
Definition List.hpp:414
void splice(iterator position, List &x, iterator i)
@doctodo Move the specified element from list x pointed to by iterator i into the current list at the...
Definition List.hpp:534
L::size_type size_type
Definition List.hpp:71
void resize(size_type new_size)
Ensure the List has a given size appending a default-constructed object to the end of the List if it ...
Definition List.hpp:406
void remove_if(Predicate p)
Removes all elements from the list for which the unary predicate p is true.
Definition List.hpp:594
iterator find(const T &x)
Find element x in the list.
Definition List.hpp:475
const_reference back() const
Definition List.hpp:262
List(InputIterator first, InputIterator last)
Construct a List from a range specified with InputIterators.
Definition List.hpp:95
const_reverse_iterator rend() const
Definition List.hpp:213
size_type size() const
Definition List.hpp:227
std::list< T > L
Definition List.hpp:63
void merge(List &x, StrictWeakOrdering swo)
Merge the current and specified list, producing a combined list that is ordered with respect to the s...
Definition List.hpp:612
List(size_type n, const T &value)
Construct a List that consist of a specified number of elements that are copies of a given object.
Definition List.hpp:106
const_iterator find(const T &x, const_iterator first, const_iterator last) const
Find element x in the list range specified by the first and last iterators.
Definition List.hpp:427
void unique()
Remove all duplicate elements from the List.
Definition List.hpp:562
void push_back(const T &x)
Append the specified element to the end of the List.
Definition List.hpp:362
void swap(List< T > &x)
Exchanges the elements of the current list with those of another.
Definition List.hpp:270
List(L *toWrap)
Constructor.
Definition List.hpp:86
size_type max_size() const
Definition List.hpp:234
L::const_reverse_iterator const_reverse_iterator
Definition List.hpp:76
reference front()
Definition List.hpp:241
void push_front(const T &x)
Prepend the specified element at the front of the List.
Definition List.hpp:353
L::iterator iterator
Definition List.hpp:73
bool contains(const T &x) const
Determine if element x is contained in the list.
Definition List.hpp:498
void splice(iterator position, List &x)
@doctodo Move the specified list into the current list at the given position.
Definition List.hpp:522
L::reverse_iterator reverse_iterator
Definition List.hpp:75
COWReference< L > m_impl
Definition List.hpp:64
reverse_iterator rbegin()
Definition List.hpp:186
void splice(iterator position, List &x, iterator first, iterator last)
@doctodo Move the elements from list x specified by first and last iterators into the current list at...
Definition List.hpp:547
const_iterator find(const T &x) const
Find element x in the list.
Definition List.hpp:445
iterator insert(iterator position)
Insert an default-constructed element to the List before the element specified by the iterator.
Definition List.hpp:293
List(long n, const T &value)
Construct a List that consist of a specified number of elements that are copies of a given object.
Definition List.hpp:126
Taken from RFC 1321.
T * COWReferenceClone(T *obj)
bool operator<(const Array< T > &x, const Array< T > &y)
bool operator==(const Array< T > &x, const Array< T > &y)
bool StrictWeakOrdering(const T1 &lhs1, const T1 &rhs1)
void swap(Array< T > &x, Array< T > &y)