libidx
|
00001 /*************************************************************************** 00002 * Copyright (C) 2011 by Pierre Sermanet * 00003 * pierre.sermanet@gmail.com * 00004 * All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Redistribution under a license not approved by the Open Source 00014 * Initiative (http://www.opensource.org) must display the 00015 * following acknowledgement in all advertising material: 00016 * This product includes software developed at the Courant 00017 * Institute of Mathematical Sciences (http://cims.nyu.edu). 00018 * * The names of the authors may not be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 00022 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00023 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00024 * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY 00025 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00026 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00027 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00028 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00029 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 ***************************************************************************/ 00032 00033 #ifndef SMART_H 00034 #define SMART_H 00035 00036 #include <stdio.h> 00037 #include "defines.h" 00038 #include "stl.h" 00039 00040 namespace ebl { 00041 00042 // smart_pointer ///////////////////////////////////////////////////////////// 00043 00046 class EXPORT smart_pointer { 00047 public: 00048 // basic constructors/destructor /////////////////////////////////////////// 00049 00050 smart_pointer(); 00051 smart_pointer(const smart_pointer &other); 00052 virtual ~smart_pointer(); 00053 00054 // reference counting ////////////////////////////////////////////////////// 00055 00057 virtual int unlock(); 00059 virtual int lock(); 00061 virtual int get_count(); 00062 00064 virtual void set_count(int count); 00065 00066 // friends ///////////////////////////////////////////////////////////////// 00067 template <class T> friend class svector; 00068 00069 // members variables /////////////////////////////////////////////////////// 00070 protected: 00071 int refcount; 00072 00073 // debug only ////////////////////////////////////////////////////////////// 00074 #ifdef __DEBUGMEM__ 00075 public: 00076 static intg locks; 00077 static intg spointers; 00078 #endif 00079 //#ifdef __DEBUG__ 00080 std::string debug_name; 00081 //#endif 00082 }; 00083 00084 // svector /////////////////////////////////////////////////////////////////// 00085 00089 template <class T> 00090 class EXPORT svector : public std::vector<T*>, public smart_pointer { 00091 public: 00093 svector(); 00095 svector(const svector<T> &other); 00097 svector(uint n); 00098 virtual ~svector(); 00099 00101 virtual void clear(); 00103 virtual void clear(uint i); 00104 00106 virtual T& operator[](uint i); 00107 /* //! Returns a reference to T at index 'i'. */ 00108 /* virtual const T& operator[](uint i) const; */ 00110 virtual const T& at_const(uint i) const; 00111 00113 virtual bool exists(uint i) const; 00115 virtual uint size_existing() const; 00116 00119 virtual void remove(uint i); 00121 virtual void remove(uint start, uint end); 00124 virtual void remove_empty(); 00125 00127 virtual void push_front(T &e); 00129 virtual void push_front_new(T &e); 00130 00132 virtual void push_back(T *e); 00134 virtual void push_back(T &e); 00136 virtual void push_back_new(const T &e); 00138 virtual void push_back_new(const T *e); 00140 virtual void push_back(std::vector<T*> &v); 00142 virtual void push_back(svector<T> &v); 00144 virtual void push_back_new(const svector<T> &v); 00146 virtual void push_back_empty(); 00147 00149 virtual void set(T& e, uint n); 00151 virtual void set_new(T& e, uint n); 00152 00153 /* //! Clear all elements, then lock and push all elements of 'v' to the end. */ 00154 /* virtual svector<T>& operator=(svector<T> &v); */ 00156 virtual void operator=(svector<T> v); 00157 00159 virtual void swap(uint i, uint j); 00160 00162 virtual svector<T> copy(); 00164 virtual void copy(const svector<T> &other); 00166 virtual void copy(svector<T> &other); 00168 virtual svector<T> narrow(uint n, uint offset); 00171 virtual void resize_default(uint n); 00172 00174 virtual void randomize(); 00175 00176 // iterators /////////////////////////////////////////////////////////////// 00177 00179 class iterator; 00180 class const_iterator; 00183 virtual iterator begin(uint offset = 0); 00186 virtual const_iterator begin(uint offset = 0) const; 00187 00188 // TEMPORARLY OUT 00191 void lock_all(); 00192 // internal methods //////////////////////////////////////////////////////// 00193 protected: 00195 void unlock_all(); 00197 void unlock_range(uint start, uint end); 00198 00199 // members ///////////////////////////////////////////////////////////////// 00200 protected: 00201 typename std::vector<T*>::iterator it; 00202 }; 00203 00204 // iterator ////////////////////////////////////////////////////////////////// 00205 00207 template <class T> 00208 class svector<T>::iterator : public std::vector<T*>::iterator { 00209 public: 00211 iterator(); 00212 iterator(typename std::vector<T*>::iterator &i); 00213 iterator(typename std::vector<T*>::iterator i); 00214 virtual ~iterator(); 00215 00217 inline virtual T& operator*(); 00219 inline virtual T* operator->(); 00221 inline virtual T* ptr(); 00223 inline virtual bool exists() const; 00224 }; 00225 00226 // const_iterator //////////////////////////////////////////////////////////// 00227 00229 template <class T> 00230 class svector<T>::const_iterator : public std::vector<T*>::const_iterator { 00231 public: 00233 const_iterator(); 00234 const_iterator(typename std::vector<T*>::const_iterator &i); 00235 const_iterator(typename std::vector<T*>::const_iterator i); 00236 virtual ~const_iterator(); 00237 00239 inline virtual const T& operator*() const; 00241 inline virtual const T* operator->() const; 00243 inline virtual const T* ptr() const; 00245 inline virtual bool exists() const; 00246 }; 00247 00248 // printing ////////////////////////////////////////////////////////////////// 00249 00251 template <class T, class stream> 00252 EXPORT stream& operator<<(stream &o, const svector<T> &v); 00254 template <class T, class stream> 00255 EXPORT stream& operator<<(stream &o, svector<T> &v); 00256 00257 } // end namespace ebl 00258 00259 #include "smart.hpp" 00260 00261 #endif /* SMART_H_ */