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 
38 #ifndef BLOCXX_LIST_HPP_INCLUDE_GUARD_
39 #define BLOCXX_LIST_HPP_INCLUDE_GUARD_
40 #include "blocxx/BLOCXX_config.h"
41 #include "blocxx/COWReference.hpp"
42 #include <list>
43 
44 namespace BLOCXX_NAMESPACE
45 {
46 
47 // forward declarations are necessary for template friends.
48 template<class T> class List;
49 
50 template <class T>
51 inline bool operator==(const List<T>& x, const List<T>& y);
52 
53 template <class T>
54 inline bool operator<(const List<T>& x, const List<T>& y);
55 
56 
60 template<class T> class List
61 {
62 private:
63  typedef std::list<T> L;
65 public:
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  }
94  template<class InputIterator>
95  List(InputIterator first, InputIterator last)
96  : m_impl(new L(first, last))
97  {
98  }
106  List(size_type n, const T& value) : m_impl(new L(n, value))
107  {
108  }
116  List(int n, const T& value) : m_impl(new L(n, value))
117  {
118  }
126  List(long n, const T& value) : m_impl(new L(n, value))
127  {
128  }
134  explicit List(size_type n) : m_impl(new L(n))
135  {
136  }
142  {
143  return &*m_impl;
144  }
151  {
152  return m_impl->begin();
153  }
160  {
161  return m_impl->begin();
162  }
169  {
170  return m_impl->end();
171  }
178  {
179  return m_impl->end();
180  }
187  {
188  return m_impl->rbegin();
189  }
196  {
197  return m_impl->rbegin();
198  }
205  {
206  return m_impl->rend();
207  }
214  {
215  return m_impl->rend();
216  }
220  bool empty() const
221  {
222  return m_impl->empty();
223  }
227  size_type size() const
228  {
229  return m_impl->size();
230  }
235  {
236  return m_impl->max_size();
237  }
242  {
243  return m_impl->front();
244  }
249  {
250  return m_impl->front();
251  }
256  {
257  return m_impl->back();
258  }
263  {
264  return m_impl->back();
265  }
270  void swap(List<T>& x)
271  {
272  m_impl.swap(x.m_impl);
273  }
282  iterator insert(iterator position, const T& x)
283  {
284  return m_impl->insert(position, x);
285  }
294  {
295  return m_impl->insert(position);
296  }
304  template<class InputIterator>
305  void insert(iterator position, InputIterator first, InputIterator last)
306  {
307  m_impl->insert(position, first, last);
308  }
318  void insert(iterator pos, size_type n, const T& x)
319  {
320  m_impl->insert(pos, n, x);
321  }
331  void insert(iterator pos, int n, const T& x)
332  {
333  m_impl->insert(pos, n, x);
334  }
344  void insert(iterator pos, long n, const T& x)
345  {
346  m_impl->insert(pos, n, x);
347  }
353  void push_front(const T& x)
354  {
355  m_impl->push_front(x);
356  }
362  void push_back(const T& x)
363  {
364  m_impl->push_back(x);
365  }
374  {
375  return m_impl->erase(position);
376  }
387  {
388  return m_impl->erase(first, last);
389  }
396  void resize(size_type new_size, const T& x)
397  {
398  m_impl->resize(new_size, x);
399  }
406  void resize(size_type new_size)
407  {
408  m_impl->resize(new_size);
409  }
414  void clear()
415  {
416  m_impl->clear();
417  }
427  const_iterator find(const T &x, const_iterator first,
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  }
445  const_iterator find(const T &x) const
446  {
447  return find(x, begin(), end());
448  }
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  }
475  iterator find(const T &x)
476  {
477  return find(x, begin(), end());
478  }
488  bool contains(const T& x, const_iterator first,
489  const_iterator last) const
490  {
491  return find(x, first, last) != end();
492  }
498  bool contains(const T& x) const
499  {
500  return find(x, begin(), end()) != end();
501  }
505  void pop_front()
506  {
507  m_impl->pop_front();
508  }
512  void pop_back()
513  {
514  m_impl->pop_back();
515  }
522  void splice(iterator position, List& x)
523  {
524  m_impl->splice(position, *x.m_impl);
525  }
534  void splice(iterator position, List& x, iterator i)
535  {
536  m_impl->splice(position, *x.m_impl, i);
537  }
547  void splice(iterator position, List& x, iterator first, iterator last)
548  {
549  m_impl->splice(position, *x.m_impl, first, last);
550  }
555  void remove(const T& value)
556  {
557  m_impl->remove(value);
558  }
562  void unique()
563  {
564  m_impl->unique();
565  }
571  void merge(List& x)
572  {
573  m_impl->merge(*x.m_impl);
574  }
578  void reverse()
579  {
580  m_impl->reverse();
581  }
585  void sort()
586  {
587  m_impl->sort();
588  }
594  template<class Predicate> void remove_if (Predicate p)
595  {
596  m_impl->remove_if (p);
597  }
603  template<class BinaryPredicate> void unique(BinaryPredicate bp)
604  {
605  m_impl->unique(bp);
606  }
612  template<class StrictWeakOrdering> void merge(List& x, StrictWeakOrdering swo)
613  {
614  m_impl->merge(*x.m_impl, swo);
615  }
620  template<class StrictWeakOrdering> void sort(StrictWeakOrdering swo)
621  {
622  m_impl->sort(swo);
623  }
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 };
642 template <class T>
643 inline bool operator==(const List<T>& x, const List<T>& y)
644 {
645  return *x.m_impl == *y.m_impl;
646 }
647 template <class T>
648 inline bool operator<(const List<T>& x, const List<T>& y)
649 {
650  return *x.m_impl < *y.m_impl;
651 }
652 template <class T>
653 inline void swap(List<T>& x, List<T>& y)
654 {
655  x.swap(y);
656 }
657 template <class T>
658 std::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