vector2.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:      vector2.h
00021  *
00022  * Requires:  
00023  * - Mbo.h
00024  */
00036 /* ------------------------------------------------------------------------ */
00037 
00038 #ifndef _VECOTR2_H_
00039 #define _VECOTR2_H_
00040 
00041 #include "Mbo.h"
00042 
00043 #include <algorithm>
00044 #include <vector>
00045 
00046 namespace std
00047 {
00048 
00072 template < class _Ty
00073          , class _Alloc    = allocator<_Ty> 
00074          >
00075 class vector2 : public vector<_Ty, _Alloc>
00076 {
00077 public:
00078     typedef vector2<_Ty, _Alloc>   _Myt;    
00079     typedef vector<_Ty, _Alloc>    _Mybase; 
00085     virtual void limit(
00086             _IN size_type   _Limit
00087         )
00088     {
00089         if (size() > _Limit)
00090         {
00091             resize(_Limit);
00092         }
00093     }
00094 
00103     pair<size_type, bool> position(
00104             _IN  const _Ty  &_Val
00105         ) const
00106     {
00107         size_type _Index = 0;
00108         for (const_iterator iter = begin(); iter != end(); ++iter, ++_Index)
00109         {
00110             if (*iter == _Val)
00111             {
00112                 return pair<size_type, bool>(_Index, true);
00113             }
00114         }
00115         return pair<size_type, bool>(-1, false);
00116     }
00117 
00124     bool contains(
00125             _IN  const _Ty  &_Val
00126         ) const
00127     {
00128         return std::find(begin(), end(), _Val) != end();
00129     }
00130 
00138     bool contains_no(
00139             _IN  const _Ty  &_Val
00140         ) const
00141     {
00142         return std::find(begin(), end(), _Val) == end();
00143     }
00144 
00151     virtual bool add_if_new(
00152             _IN  const _Ty  &_Val
00153         )
00154     {
00155         if (!contains(_Val))
00156         {
00157             push_back(_Val);
00158             return true;
00159         }
00160         return false;
00161     }
00162 
00167     operator bool()
00168     {
00169         return size() > 0;
00170     }
00171 
00174     void sort()
00175     {
00176         if (size() > 1)
00177         {
00178             std::sort(begin(), end());
00179         }
00180     }
00181 
00187     static _Myt __cdecl dyn_create( 
00188             _IN size_type   _Count, 
00189             ...
00190         )
00191     {
00192         _Myt       _Vector;
00193         va_list    _Args;
00194 
00195         va_start(_Args, _Count);
00196         while(_Count--)
00197         {
00198             _Vector.push_back(va_arg(_Args, const _Ty));
00199         }
00200         va_end(_Args);
00201         return _Vector;
00202     }
00203 };
00204 
00220 template < class _Ty_a, class _Alloc_a
00221          , class _Ty_b, class _Alloc_b
00222          >
00223 vector2<_Ty_a, _Alloc_a> & operator += (
00224         _IN vector2<_Ty_a, _Alloc_a> & container,
00225         _IN const vector<_Ty_b, _Alloc_b> & values
00226     )
00227 {
00228     // NO clear
00229     for (vector<_Ty_b, _Alloc_b>::const_iterator it = values.begin(); it != values.end(); ++it)
00230     {
00231         container.push_back((_Ty_a)*it);
00232     }
00233     return container;
00234 };
00235 
00251 template < class _Ty_a, class _Alloc_a
00252          , class _Ty_b, class _Alloc_b
00253          >
00254 vector2<_Ty_a, _Alloc_a> & operator << (
00255         _IN vector2<_Ty_a, _Alloc_a> & container,
00256         _IN const vector<_Ty_b, _Alloc_b> & values
00257     )
00258 {
00259     container.clear();
00260     container += values;
00261     return container;
00262 };
00263 
00264 }; // namespace std
00265 
00266 #endif //_VECTOR2_H_

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