AndroMeta  2.0.0
AndroMeta/include/AndroMeta/MList.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2009-2012 AndroMeta LLC. All rights reserved.
00003  *
00004  * AndroMeta LLC retains all intellectual property and proprietary 
00005  * rights to this software and related documentation and any modifications 
00006  * thereto.  Any use, reproduction, disclosure, or distribution of this 
00007  * software and related documentation without an express license agreement 
00008  * from AndroMeta LLC is strictly prohibited.
00009  * 
00010  */
00011 
00019 #include <list>
00020 #include <iterator>
00021 #include <sstream>
00022 #include <iostream>
00023 
00024 #include <AndroMeta/MError.h>
00025 
00026 #ifndef ANDROMETA_M_LIST_H
00027 #define ANDROMETA_M_LIST_H
00028 
00029 namespace Meta{
00030   
00042   template<typename T, class Allocator = std::allocator<T> >
00043   class MList{
00044   public:
00045     typedef std::list<T, Allocator> List_;
00046     
00047     typedef typename List_::reference reference;
00048     typedef typename List_::const_reference const_reference;
00049     typedef typename List_::iterator iterator;
00050     typedef typename List_::const_iterator const_iterator;
00051     typedef typename List_::allocator_type allocator_type;
00052     typedef typename List_::reverse_iterator reverse_iterator;
00053     typedef typename List_::const_reverse_iterator const_reverse_iterator;
00054     
00055     explicit MList(const Allocator& allocator=Allocator())
00056     : list_(allocator),
00057     i_(-1){
00058       
00059     }
00060     
00061     explicit MList(size_t n,
00062                    const T& value=T(),
00063                    const Allocator& allocator=Allocator())
00064     : list_(n, value, allocator),
00065     i_(-1){
00066       
00067     }
00068     
00069     template<class InputIterator>
00070     MList(InputIterator first,
00071           InputIterator last,
00072           const Allocator& allocator=Allocator())
00073     : list_(first, last, allocator),
00074     i_(-1){
00075       
00076     }
00077     
00078     MList(const MList<T, Allocator>& x)
00079     : list_(x.list_),
00080     i_(-1){
00081       
00082     }
00083     
00084     virtual ~MList(){
00085       
00086     }
00087     
00088     const List_ list() const{
00089       return list_;
00090     }
00091     
00092     List_ list(){
00093       return list_;
00094     }  
00095     
00096     template<class InputIterator>
00097     void assign(InputIterator first, InputIterator last){
00098       list_.assign(first, last);
00099       i_ = -1;
00100     }
00101     
00102     void assign(size_t n, const T& u){
00103       list_.assign(n, u);
00104       i_ = -1;
00105     }
00106     
00107     reference back(){
00108       return list_.back();
00109     }
00110     
00111     const_reference back() const{
00112       return list_.back();
00113     }
00114     
00115     iterator begin(){
00116       return list_.begin();
00117     }
00118     
00119     const_iterator begin () const{
00120       return list_.begin();
00121     }
00122     
00123     void clear(){
00124       list_.clear();
00125       i_ = -1;
00126     }
00127     
00128     bool empty() const{
00129       return list_.empty();
00130     }
00131     
00132     bool isEmpty() const{
00133       return list_.isEmpty();
00134     }
00135     
00136     iterator end(){
00137       return list_.end();
00138     }
00139     
00140     const_iterator end() const{
00141       return list_.end();
00142     }
00143     
00144     iterator erase(iterator position){
00145       i_ = -1;
00146       return list_.erase(position);
00147     }
00148     
00149     iterator erase(iterator first, iterator last){
00150       i_ = -1;
00151       return list_.erase(first, last);
00152     }
00153     
00154     reference front(){
00155       return list_.front();
00156     }
00157     
00158     const_reference front() const{
00159       return list_.front();
00160     }
00161     
00162     allocator_type get_allocator() const{
00163       return list_.get_allocator();
00164     }
00165     
00166     allocator_type getAllocator() const{
00167       return list_.get_allocator();
00168     }
00169     
00170     iterator insert(iterator position, const T& x){
00171       i_ = -1;
00172       return list_.insert(position, x);
00173     }
00174     
00175     void insert(iterator position, size_t n, const T& x){
00176       i_ = -1;
00177       return list_.insert(position, n, x);
00178     }
00179     
00180     template <class InputIterator>
00181     void insert(iterator position, InputIterator first, InputIterator last){
00182       i_ = -1;
00183       list_.insert(position, first, last);
00184     }
00185     
00186     void append(const MList<T> l){
00187       list_.insert(list_.end(), l.begin(), l.end());
00188     }
00189     
00190     template<class S>
00191     void append(const MList<S> l){
00192       list_.insert(list_.end(), l.begin(), l.end());
00193     }
00194     
00195     size_t max_size() const{
00196       return list_.max_size();
00197     }
00198     
00199     size_t maxSize() const{
00200       return list_.max_size();
00201     }
00202     
00203     void merge(MList<T, Allocator>& x){
00204       i_ = -1;
00205       list_.merge(x.list_);
00206     }
00207     
00208     template<class Compare>
00209     void merge(MList<T, Allocator>& x, Compare comp){
00210       i_ = -1;
00211       list_.merge(x.list_, comp);
00212     }
00213     
00214     MList<T, Allocator>& operator=(const MList<T, Allocator>& x){
00215       i_ = -1;
00216       list_ = x.list_;
00217       return *this;
00218     }
00219     
00220     void pop_back(){
00221       list_.pop_back();
00222     }
00223     
00224     T popBack(){
00225       T ret = *(--list_.end());
00226       list_.pop_back();
00227       return ret;
00228     }
00229     
00230     void pop_front(){
00231       i_ = -1;
00232       list_.pop_front();
00233     }
00234     
00235     T popFront(){
00236       i_ = -1;
00237       T ret = *list_.begin();
00238       list_.pop_front();
00239       return ret;
00240     }
00241     
00242     void push_back(const T& x){
00243       list_.push_back(x);
00244     }
00245     
00246     void pushBack(const T& x){
00247       list_.push_back(x);
00248     }
00249     
00250     void push_front(const T& x){
00251       i_ = -1;
00252       list_.push_front(x);
00253     }
00254     
00255     void pushFront(const T& x){
00256       i_ = -1;
00257       list_.push_front(x);
00258     }
00259     
00260     reverse_iterator rbegin(){
00261       return list_.rbegin();
00262     }
00263     
00264     const_reverse_iterator rbegin() const{
00265       return list_.rbegin();
00266     }
00267     
00268     void remove(const T& value){
00269       i_ = -1;
00270       list_.remove(value);
00271     }
00272     
00273     template <class Predicate>
00274     void remove_if(Predicate pred){
00275       i_ = -1;
00276       list_.remove_if(pred);
00277     }
00278     
00279     template <class Predicate>
00280     void removeIf(Predicate pred){
00281       i_ = -1;
00282       list_.remove_if(pred);
00283     }
00284     
00285     reverse_iterator rend(){
00286       return list_.rend();
00287     }
00288     
00289     const_reverse_iterator rend() const{
00290       return list_.rend();
00291     }
00292     
00293     void resize(size_t sz, T c=T()){
00294       i_ = -1;
00295       list_.resize(sz, c);
00296     }
00297     
00298     void reverse(){
00299       list_.reverse();
00300     }
00301     
00302     size_t size() const{
00303       return list_.size();
00304     }
00305     
00306     void sort(){
00307       list_.sort();
00308     }
00309     
00310     template<class Compare>
00311     void sort(Compare comp){
00312       list_.sort(comp);
00313     }
00314     
00315     void splice(iterator position, MList<T, Allocator>& x){
00316       i_ = -1;
00317       list_.splice(position, x);
00318     }
00319     
00320     void splice(iterator position, MList<T,Allocator>& x, iterator i){
00321       i_ = -1;
00322       list_.splice(position, x, i);
00323     }
00324     
00325     void splice(iterator position,
00326                 MList<T,Allocator>& x,
00327                 iterator first,
00328                 iterator last){
00329       i_ = -1;
00330       list_.splice(position, x, first, last);
00331     }
00332     
00333     void swap(MList<T, Allocator>& lst){
00334       i_ = -1;
00335       list_.swap(lst);
00336     }
00337     
00338     void unique(){
00339       i_ = -1;
00340       list_.unique();
00341     }
00342     
00343     template<class BinaryPredicate>
00344     void unique(BinaryPredicate binary_pred){
00345       i_ = -1;
00346       list_.unique(binary_pred);
00347     }
00348     
00349     MList<T,Allocator>& operator<<(const T& x){
00350       list_.push_back(x);
00351       return *this;
00352     }
00353     
00354     reference operator[](size_t i){
00355       if(i >= list_.size()){
00356         std::stringstream ostr;
00357         ostr << "MList::operator[]: invalid index: " << i;
00358         throw MError(ostr.str());
00359       }
00360       
00361       if(i_ < 0){
00362         itr_ = list_.begin();
00363         advance(itr_, i);
00364       }
00365       else{
00366         advance(itr_, i - i_);
00367       }
00368       
00369       i_ = i;
00370       
00371       return *itr_;
00372     }
00373     
00374     const_reference operator[](size_t i) const{
00375       MList* l = const_cast<MList*>(this);
00376       return l->operator[](i);
00377     }
00378     
00379   private:
00380     List_ list_;
00381     iterator itr_;
00382     int64_t i_;
00383   };
00384   
00385   template<class T, class Allocator>
00386   bool operator==(const MList<T,Allocator>& x,
00387                   const MList<T,Allocator>& y){
00388     return x.list() == y.list();
00389   }
00390   
00391   template<class T, class Allocator>
00392   bool operator<(const MList<T,Allocator>& x,
00393                  const MList<T,Allocator>& y){
00394     return x.list() < y.list();
00395   }
00396   
00397   template<class T, class Allocator>
00398   bool operator!=(const MList<T,Allocator>& x,
00399                   const MList<T,Allocator>& y){
00400     return x.list() != y.list();
00401   }
00402   
00403   template<class T, class Allocator>
00404   bool operator>(const MList<T,Allocator>& x,
00405                  const MList<T,Allocator>& y){
00406     return x.list() > y.list();
00407   }
00408   
00409   template<class T, class Allocator>
00410   bool operator>=(const MList<T,Allocator>& x,
00411                   const MList<T,Allocator>& y){
00412     return x.list() >= y.list();
00413   }
00414   
00415   template <class T, class Allocator>
00416   bool operator<=(const MList<T,Allocator>& x,
00417                   const MList<T,Allocator>& y){
00418     return x.list() <= y.list();
00419   }
00420   
00421   template<typename T>
00422   std::ostream& operator<<(std::ostream& ostr, const MList<T>& v){
00423     typename MList<T>::const_iterator itr = v.begin();
00424     size_t i = 0;
00425     bool index = v.size() > 10;
00426     ostr << "(";
00427     while(itr != v.end()){
00428       if(index){
00429         ostr << i << ":";
00430       }
00431       if(i > 0){
00432         ostr << ", ";
00433       }
00434       ostr << *itr;
00435       ++i;
00436       ++itr;
00437     }
00438     ostr << ")";
00439     return ostr;
00440   }
00441   
00442 } // end namespace Meta
00443 
00444 #endif // ANDROMETA_M_LIST_H