Blender V2.61 - r43446

util_vector.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program 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
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #ifndef __UTIL_VECTOR_H__
00020 #define __UTIL_VECTOR_H__
00021 
00022 /* Vector */
00023 
00024 #include <string.h>
00025 #include <vector>
00026 
00027 CCL_NAMESPACE_BEGIN
00028 
00029 using std::vector;
00030 
00031 /* Array
00032  *
00033  * Simplified version of vector, serving two purposes:
00034  * - somewhat faster in that it does not clear memory on resize/alloc,
00035  *   this was actually showing up in profiles quite significantly
00036  * - if this is used, we are not tempted to use inefficient operations */
00037 
00038 template<typename T>
00039 class array
00040 {
00041 public:
00042     array()
00043     {
00044         data = NULL;
00045         datasize = 0;
00046     }
00047 
00048     array(size_t newsize)
00049     {
00050         if(newsize == 0) {
00051             data = NULL;
00052             datasize = 0;
00053         }
00054         else {
00055             data = new T[newsize];
00056             datasize = newsize;
00057         }
00058     }
00059 
00060     array(const array& from)
00061     {
00062         *this = from;
00063     }
00064 
00065     array& operator=(const array& from)
00066     {
00067         if(from.datasize == 0) {
00068             data = NULL;
00069             datasize = 0;
00070         }
00071         else {
00072             data = new T[from.datasize];
00073             memcpy(data, from.data, from.datasize*sizeof(T));
00074             datasize = from.datasize;
00075         }
00076 
00077         return *this;
00078     }
00079 
00080     array& operator=(const vector<T>& from)
00081     {
00082         datasize = from.size();
00083         data = NULL;
00084 
00085         if(datasize > 0) {
00086             data = new T[datasize];
00087             memcpy(data, &from[0], datasize*sizeof(T));
00088         }
00089 
00090         return *this;
00091     }
00092 
00093     ~array()
00094     {
00095         delete [] data;
00096     }
00097 
00098     void resize(size_t newsize)
00099     {
00100         if(newsize == 0) {
00101             clear();
00102         }
00103         else {
00104             T *newdata = new T[newsize];
00105             memcpy(newdata, data, ((datasize < newsize)? datasize: newsize)*sizeof(T));
00106             delete [] data;
00107 
00108             data = newdata;
00109             datasize = newsize;
00110         }
00111     }
00112 
00113     void clear()
00114     {
00115         delete [] data;
00116         data = NULL;
00117         datasize = 0;
00118     }
00119 
00120     size_t size() const
00121     {
00122         return datasize;
00123     }
00124 
00125     T& operator[](size_t i) const
00126     {
00127         return data[i];
00128     }
00129 
00130 protected:
00131     T *data;
00132     size_t datasize;
00133 };
00134 
00135 CCL_NAMESPACE_END
00136 
00137 #endif /* __UTIL_VECTOR_H__ */
00138