library of assembled shared sources

http://lass.cocamware.com

slist.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 /** @class lass::stde::slist
00044  *  @brief a single linked list, just like the one in SGI STL (and STLport ...)
00045  *  @author Bram de Greve [Bramz]
00046  */
00047 
00048 #ifndef LASS_GUARDIAN_OF_INCLUSION_STDE_SLIST_H
00049 #define LASS_GUARDIAN_OF_INCLUSION_STDE_SLIST_H
00050 
00051 #include "stde_common.h"
00052 #include "extended_io.h"
00053 #include "../meta/bool.h"
00054 #include "../meta/wrap.h"
00055 
00056 namespace lass
00057 {
00058 namespace stde
00059 {
00060 
00061 template <typename T, class Alloc = std::allocator<T> >
00062 class slist: private Alloc
00063 {
00064 private:
00065 
00066     struct node_base_t
00067     {
00068         node_base_t* next;
00069     };
00070 
00071     struct node_t: public node_base_t
00072     {
00073         T value;
00074     };
00075 
00076 public:
00077 
00078     typedef typename Alloc::template rebind<T>::other allocator_type;
00079     typedef T value_type;
00080     typedef typename allocator_type::pointer pointer;
00081     typedef typename allocator_type::const_pointer const_pointer;
00082     typedef typename allocator_type::reference reference;
00083     typedef typename allocator_type::const_reference const_reference;
00084     typedef typename allocator_type::size_type size_type;
00085     typedef typename allocator_type::difference_type difference_type;
00086 
00087     class const_iterator;
00088     friend class const_iterator;
00089     class iterator;
00090     friend class iterator;
00091 
00092     class iterator
00093     {
00094     public:
00095         typedef T value_type;
00096         typedef typename allocator_type::pointer pointer;
00097         typedef typename allocator_type::reference reference;
00098         typedef typename allocator_type::size_type size_type;
00099         typedef typename allocator_type::difference_type difference_type;
00100         typedef std::forward_iterator_tag iterator_category;
00101         iterator(): node_(0) {}
00102         reference operator*() const
00103         {
00104             LASS_ASSERT(node_);
00105             return static_cast<node_t* const>(node_)->value;
00106         }
00107         pointer operator->() const
00108         {
00109             LASS_ASSERT(node_);
00110             return &static_cast<node_t* const>(node_)->value;
00111         }
00112         iterator& operator++() { node_ = node_->next; return *this; }
00113         iterator operator++(int) { iterator result(*this); ++(*this); return result; }
00114         bool operator==(const iterator& other) const { return node_ == other.node_; }
00115         bool operator!=(const iterator& other) const { return !(*this == other); }
00116     private:
00117         friend class slist<T, Alloc>;
00118         friend class const_iterator;
00119         explicit iterator(node_base_t* node): node_(node) {}
00120         node_base_t* node_;
00121     };
00122 
00123     class const_iterator
00124     {
00125     public:
00126         typedef const T value_type;
00127         typedef typename allocator_type::const_pointer pointer;
00128         typedef typename allocator_type::const_reference reference;
00129         typedef typename allocator_type::size_type size_type;
00130         typedef typename allocator_type::difference_type difference_type;
00131         typedef std::forward_iterator_tag iterator_category;
00132         const_iterator(): node_(0) {}
00133         const_iterator(iterator i): node_(i.node_) {}
00134         const_reference operator*() const
00135         {
00136             LASS_ASSERT(node_);
00137             return static_cast<const node_t* const>(node_)->value;
00138         }
00139         const_pointer operator->() const
00140         {
00141             LASS_ASSERT(node_);
00142             return &static_cast<const node_t* const>(node_)->value;
00143         }
00144         const_iterator& operator++() { node_ = node_->next; return *this; }
00145         const_iterator operator++(int) { const_iterator result(*this); ++(*this); return result; }
00146         bool operator==(const const_iterator& other) const { return node_ == other.node_; }
00147         bool operator!=(const const_iterator& other) const { return !(*this == other); }
00148     private:
00149         friend class slist<T, Alloc>;
00150         explicit const_iterator(const node_base_t* node): node_(node) {}
00151         const node_base_t* node_;
00152     };
00153 
00154     explicit slist(const Alloc& allocator = Alloc());
00155     explicit slist(size_type n, const T& value = T(), const Alloc& allocator = Alloc());
00156     template <typename InputIterator> slist(InputIterator first, InputIterator last,
00157         const Alloc& allocator = Alloc());
00158     slist(const slist<T, Alloc>& other);
00159     ~slist();
00160 
00161     slist<T, Alloc>& operator=(slist<T, Alloc> other);
00162     template <typename InputIterator> void assign(InputIterator first, InputIterator last);
00163     void assign(size_type n, const T& value = T());
00164     allocator_type get_allocator() const;
00165 
00166     iterator begin();
00167     const_iterator begin() const;
00168     iterator end();
00169     const_iterator end() const;
00170     iterator before_begin();
00171     const_iterator before_begin() const;
00172 
00173     iterator prior(iterator position) const;
00174     const_iterator prior(const_iterator position) const;
00175     iterator prior(iterator position, iterator start) const;
00176     const_iterator prior(const_iterator position, const_iterator start) const;
00177 
00178     bool empty() const;
00179     size_type size() const;
00180     size_type max_size() const;
00181     void resize(size_type n, const T& value = T());
00182 
00183     reference front();
00184     const_reference front() const;
00185     void push_front(const T& value);
00186     void pop_front();
00187 
00188     iterator insert(iterator position, const T& value);
00189     void insert(iterator position, size_type n, const T& value);
00190     template <typename InputIterator> void insert(iterator position, InputIterator first,
00191         InputIterator last);
00192 
00193     iterator insert_after(iterator position, const T& value);
00194     void insert_after(iterator position, size_type n, const T& value);
00195     template <typename InputIterator> void insert_after(iterator position, InputIterator first,
00196         InputIterator last);
00197 
00198     iterator erase(iterator position);
00199     iterator erase(iterator position, iterator last);
00200     void erase_after(iterator position);
00201     void erase_after(iterator position, iterator last);
00202     void swap(slist<T, Alloc>& other);
00203     void clear();
00204 
00205     void splice(iterator position, slist<T, Alloc>& other);
00206     void splice(iterator position, slist<T, Alloc>& other, iterator x);
00207     void splice(iterator position, slist<T, Alloc>& other, iterator first, iterator last);
00208 
00209     void splice_after(iterator position, slist<T, Alloc>& other);
00210     void splice_after(iterator position, slist<T, Alloc>& other, iterator before_x);
00211     void splice_after(iterator position, slist<T, Alloc>& other, iterator before_first,
00212         iterator before_last);
00213 
00214     void remove(const T& value);
00215     template <class UnaryPredicate> void remove_if(UnaryPredicate predicate);
00216 
00217     void unique();
00218     template <class BinaryPredicate> void unique(BinaryPredicate predicate);
00219 
00220     void merge(slist<T, Alloc>& other);
00221     template <class BinaryPredicate> void merge(slist<T, Alloc>& other, BinaryPredicate compare);
00222 
00223     void sort();
00224     template <class Compare> void sort(Compare compare);
00225 
00226     void reverse();
00227 
00228 private:
00229 
00230     typedef typename allocator_type::template rebind<node_t>::other node_allocator_type;
00231 
00232     node_t* make_node(const T& value) const;
00233     void unlink_and_destroy_after(node_base_t* position) const;
00234     void link_after(node_base_t* position, node_base_t* node) const;
00235     void splice_after(node_base_t* position, node_base_t* before_first,
00236         node_base_t* before_last) const;
00237 
00238     void insert_after(iterator position, size_type n, const value_type& value,
00239         meta::Wrap<meta::True> parameter_is_integral);
00240     template <typename InputIterator> void insert_after(iterator position, InputIterator first,
00241         InputIterator last, meta::Wrap<meta::False> parameter_is_iterator);
00242 
00243     node_base_t head_;
00244 };
00245 
00246 template <typename T, class Alloc>
00247 bool operator==(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
00248 template <typename T, class Alloc>
00249 bool operator!=(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
00250 template <typename T, class Alloc>
00251 bool operator<(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
00252 template <typename T, class Alloc>
00253 bool operator>(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
00254 template <typename T, class Alloc>
00255 bool operator<=(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
00256 template <typename T, class Alloc>
00257 bool operator>=(const slist<T, Alloc>& a, const slist<T, Alloc>& b);
00258 
00259 template <typename T, class Alloc, typename Char, typename Traits>
00260 std::basic_ostream<Char, Traits>&
00261 operator<<(std::basic_ostream<Char, Traits>& o_stream,
00262            const slist<T, Alloc>& container);
00263 template <typename T, class Alloc, typename Char, typename Traits>
00264 std::basic_istream<Char, Traits>&
00265 operator>>(std::basic_istream<Char, Traits>& i_stream,
00266            slist<T, Alloc>& container);
00267 
00268 }
00269 
00270 }
00271 
00272 #include "slist.inl"
00273 
00274 #endif
00275 
00276 // EOF

Generated on Mon Nov 10 14:21:34 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo