list2.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Copyright (c) 2001 - 2008 Marcus Boerger.  All rights reserved.
00003 *
00004 * This library is free software; you can redistribute it and/or
00005 * modify it under the terms of the GNU Lesser General Public
00006 * License as published by the Free Software Foundation; either
00007 * version 2.1 of the License, or (at your option) any later version.
00008 *
00009 * This library is distributed in the hope that it will be useful,
00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 * Lesser General Public License for more details.
00013 *
00014 * You should have received a copy of the GNU Lesser General Public
00015 * License along with this library; if not, write to the Free Software
00016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017 * ========================================================================= */
00018 
00019 /* ------------------------------------------------------------------------ */
00020 /* Name:      list2.h
00021  *
00022  * Requires:  
00023  * - Mbo.h
00024  */
00036 /* ------------------------------------------------------------------------ */
00037 
00038 #ifndef _LIST2_H_
00039 #define _LIST2_H_
00040 
00041 #include "Mbo.h"
00042 
00043 #include <list>
00044 #include <stdexcept>
00045 
00046 namespace std
00047 {
00048 
00069 template <class _Ty, class _Alloc = allocator<_Ty> >
00070 class list2 : public list<_Ty, _Alloc>
00071 {
00072 typedef list2<_Ty, _Alloc>   _Myt;     
00073 typedef list<_Ty, _Alloc>    _Mybase;  
00074 public:
00075 
00080     virtual void limit(
00081             _IN size_type   _Limit
00082         )
00083     {
00084         if (size() > _Limit)
00085         {
00086             resize(_Limit);
00087         }
00088     }
00089 
00098     pair<size_type, bool> position(
00099             _IN  const _Ty  &_Val
00100         ) const
00101     {
00102         size_type _Index = 0;
00103         for (const_iterator iter = begin(); iter != end(); ++iter, ++_Index)
00104         {
00105             if (*iter == _Val)
00106             {
00107                 return pair<size_type, bool>(_Index, true);
00108             }
00109         }
00110         return pair<size_type, bool>(-1, false);
00111     }
00112 
00119     bool contains(
00120             _IN  const _Ty  &_Val
00121         ) const
00122     {
00123         return std::find(begin(), end(), _Val) != end();
00124     }
00125 
00133     bool contains_no(
00134             _IN  const _Ty  &_Val
00135         ) const
00136     {
00137         return std::find(begin(), end(), _Val) == end();
00138     }
00139 
00146     virtual bool add_if_new(
00147             _IN  const _Ty  &_Val
00148         )
00149     {
00150         if (!contains(_Val))
00151         {
00152             push_back(_Val);
00153             return true;
00154         }
00155         return false;
00156     }
00157 
00162     operator bool()
00163     {
00164         return size() > 0;
00165     }
00166 
00171     reference at(
00172             _IN size_type _Off
00173         )
00174     {   // subscript mutable sequence with checking
00175         if (size() <= _Off)
00176         {
00177             _Xran();
00178         }
00179         iterator it = begin();
00180         while(_Off--) ++it;
00181         return *it;
00182     }
00183 
00188     const_reference at(
00189             _IN size_type _Off
00190         ) const
00191     {   // subscript non mutable sequence with checking
00192         if (size() <= _Off)
00193         {
00194             _Xran();
00195         }
00196         const_iterator it = begin();
00197         while(_Off--) ++it;
00198         return *it;
00199     }
00200 
00205     reference operator[] (
00206             _IN size_type _Off
00207         )
00208     {   // subscript mutable sequence
00209         return at(_Off);
00210     }
00211 
00216     const_reference operator[] (
00217             _IN size_type _Off
00218         ) const
00219     {   // subscript nonmutable sequence
00220         return at(_Off);
00221     }
00222 
00228     static list2<_Ty, _Alloc> __cdecl dyn_create( 
00229             _IN size_type   _Count, 
00230             ...
00231         )
00232     {
00233         _Myt       _List;
00234         va_list    _Args;
00235 
00236         va_start(_Args, _Count);
00237         while(_Count--)
00238         {
00239             _List.push_back(va_arg(_Args, const _Ty));
00240         }
00241         va_end(_Args);
00242         return _List;
00243     }
00244 
00245 protected:
00246 
00249     void _Xran() const
00250     {   // report an out_of_range error
00251         _THROW(out_of_range, "invalid list2<T> subscript");
00252     }
00253 
00254 };
00255 
00271 template < class _Ty_a, class _Alloc_a
00272          , class _Ty_b, class _Alloc_b
00273          >
00274 list2<_Ty_a, _Alloc_a> & operator += (
00275         _IN list2<_Ty_a, _Alloc_a> & container,
00276         _IN const list<_Ty_b, _Alloc_b> & values
00277     )
00278 {
00279     // NO clear
00280     for (list2<_Ty_b, _Alloc_b>::const_iterator it = values.begin(); it != values.end(); ++it)
00281     {
00282         container.push_back((_Ty_a)*it);
00283     }
00284     return container;
00285 };
00286 
00302 template < class _Ty_a, class _Alloc_a
00303          , class _Ty_b, class _Alloc_b
00304          >
00305 list2<_Ty_a, _Alloc_a> & operator << (
00306         _IN list2<_Ty_a, _Alloc_a> & container,
00307         _IN const list<_Ty_b, _Alloc_b> & values
00308     )
00309 {
00310     container.clear();
00311     container += values;
00312     return container;
00313 };
00314 
00315 }; // namespace std
00316 
00317 #endif // _LIST2_H_

  Hosted on code.google.com  
© Marcus Börger
Generated on Fri Jan 18 21:21:07 2008 for MBO-lib by doxygen 1.5.4