GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/HMesh/ItemVector.h
00001 /* ----------------------------------------------------------------------- *
00002  * This file is part of GEL, www.imm.dtu.dk/GEL
00003  * Copyright (C) the authors (see AUTHORS.txt) and DTU Informatics
00004  *
00005  * Principal authors:
00006  *  Christian Thode Larsen (thode2d@gmail.com)
00007  *  J. Andreas Baerentzen (jab@imm.dtu.dk)
00008  *
00009  * See LICENSE.txt for licensing information
00010  * ----------------------------------------------------------------------- */
00011 
00012 #ifndef __HMESH_ITEMVECTOR_H__
00013 #define __HMESH_ITEMVECTOR_H__
00014 
00015 #include <cassert>
00016 #include <vector>
00017 
00018 namespace HMesh
00019 {
00020     template<typename ITEM>
00021     class ItemVector
00022     {
00023     public:
00025         ItemVector(size_t _size = 0, ITEM i = ITEM()); 
00026         
00028         ITEM& get(size_t i);
00030         const ITEM& get(size_t i) const;
00032         ITEM& operator[](size_t i);
00034         const ITEM& operator[](size_t i) const;
00035 
00037         size_t add(const ITEM& i);
00038 
00040         void remove(size_t i);
00041 
00043         void cleanup();
00044 
00046         size_t active_size() const;
00047 
00049         size_t total_size() const;
00050 
00052         void resize(size_t _size, ITEM i = ITEM());
00054         void reserve(size_t i);
00055 
00057         void clear();
00058 
00060         bool in_use(size_t i) const;
00061 
00063         size_t index_begin(bool skip = true) const;
00064 
00066         size_t index_end() const;
00067 
00069         size_t index_next(size_t index, bool skip = true) const;
00070 
00072         size_t index_prev(size_t index, bool skip = true) const;
00073 
00074     private:
00075 
00076         size_t size_active;
00077         std::vector<ITEM> items;
00078 
00080         std::vector<bool> active_items;
00081     };
00082 
00083     template<typename ITEM>
00084     inline ItemVector<ITEM>::ItemVector(size_t _size, ITEM i) 
00085         :   size_active(_size), 
00086             items(_size, i), 
00087             active_items(_size, true){}
00088 
00089     template<typename ITEM>
00090     inline ITEM& ItemVector<ITEM>::get(size_t i)
00091     {
00092         assert(i < items.size());
00093         return items[i];
00094     }
00095 
00096     template<typename ITEM>
00097     inline const ITEM& ItemVector<ITEM>::get(size_t i) const
00098     {
00099         assert(i < items.size());
00100         return items[i];
00101     }
00102 
00103     template<typename ITEM>
00104     inline ITEM& ItemVector<ITEM>::operator [](size_t i)
00105     {
00106         assert(i < items.size());
00107         return items[i];
00108     } 
00109 
00110     template<typename ITEM>
00111     inline const ITEM& ItemVector<ITEM>::operator [](size_t i) const
00112     {
00113         assert(i < items.size());
00114         return items[i];
00115     }
00116 
00117     template<typename ITEM>
00118     inline size_t ItemVector<ITEM>::add(const ITEM& i)
00119     {
00120         items.push_back(i);
00121         active_items.push_back(true);
00122         ++size_active;
00123         return items.size() - 1;
00124     }
00125 
00126     template<typename ITEM>
00127     inline void ItemVector<ITEM>::remove(size_t i)
00128     {
00129         if(active_items[i]){ 
00130             --size_active;
00131             active_items[i] = false;
00132         }
00133     }
00134 
00135     template<typename ITEM>
00136     inline void ItemVector<ITEM>::cleanup()
00137     {
00138         std::vector<ITEM> new_items;
00139         for(size_t i = 0; i < items.size(); ++i){
00140             if(active_items[i]) 
00141                 new_items.push_back(items[i]);          
00142         }
00143         std::swap(items, new_items);
00144         active_items = std::vector<bool>(items.size(), true);
00145         size_active = items.size();
00146     }
00147 
00148     template<typename ITEM>
00149     inline size_t ItemVector<ITEM>::active_size() const
00150     { return size_active; }
00151 
00152     template<typename ITEM>
00153     inline size_t ItemVector<ITEM>::total_size() const
00154     { return items.size(); }
00155 
00156     template<typename ITEM>
00157     inline void ItemVector<ITEM>::resize(size_t _size, ITEM i)
00158     {
00159         items.resize(_size, i);
00160         active_items.resize(_size, true);
00161         size_active = _size;
00162     }
00163 
00164     template<typename ITEM>
00165     inline void ItemVector<ITEM>::reserve(size_t i)
00166     {
00167         items.reserve(i);
00168         active_items.reserve(i);
00169     }
00170 
00171     template<typename ITEM>
00172     inline void ItemVector<ITEM>::clear()
00173     {
00174         items.clear();
00175         active_items.clear();
00176         size_active = 0;
00177     }
00178 
00179     template<typename ITEM>
00180     inline bool ItemVector<ITEM>::in_use(size_t i) const
00181     {
00182         if(i < active_items.size())
00183                         return active_items[i];
00184                 return false;
00185     }
00186 
00187     template<typename ITEM>
00188     inline size_t ItemVector<ITEM>::index_begin(bool skip) const
00189     {
00190         size_t i = 0;
00191 
00192         if(!skip)
00193             return i;
00194         
00195         while(i < active_items.size() && !active_items[i])
00196             ++i;
00197 
00198 
00199         return i;
00200     }
00201 
00202     template<typename ITEM>
00203     inline size_t ItemVector<ITEM>::index_end() const
00204     { return items.size(); }
00205 
00206     template<typename ITEM>
00207     inline size_t ItemVector<ITEM>::index_next(size_t index, bool skip) const
00208     {
00209         if(index < items.size())
00210             ++index;
00211 
00212         if(!skip)
00213             return index;
00214 
00215         while(index < items.size() && !active_items[index])
00216            ++index;
00217 
00218         return index;
00219     }
00220 
00221     template<typename ITEM>
00222     inline size_t ItemVector<ITEM>::index_prev(size_t index, bool skip) const
00223     {
00224         if(index > 0)
00225             --index;
00226 
00227         if(!skip)
00228             return index;
00229 
00230         while(!active_items[index] && index > 0)
00231             --index;
00232 
00233         return index;
00234     }
00235 
00236 }
00237 
00238 #endif 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations