Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Array_decl.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 00039 /* ******************************************************* 00040 * $Id: Array_decl.h,v 1.5 2004/08/04 13:28:17 tihocan Exp $ 00041 * This file is part of the PLearn library. 00042 ******************************************************* */ 00043 00044 00047 #ifndef Array_decl_INC 00048 #define Array_decl_INC 00049 00050 //#include <map> 00051 //#include <set> 00052 #include <string> 00053 #include <vector> 00054 #include "TypeTraits.h" 00055 //#include "general.h" 00056 //#include "fileutils.h" 00057 #include <plearn/math/TMat_decl.h> 00058 00059 #include "ms_hash_wrapper.h" 00060 namespace PLearn { 00061 using namespace std; 00062 00063 00064 template <class T> 00065 class Array: public TVec<T> 00066 { 00067 public: 00068 00069 // norman: added scope for dependent name to resolve lookup 00070 // See chap. 9.4.2 of C++ Templates, The Complete Guide 00071 // by Vandevoorde and Josuttis 00072 using TVec<T>::length_; 00073 using TVec<T>::offset_; 00074 using TVec<T>::storage; 00075 00076 using TVec<T>::data; 00077 using TVec<T>::resize; 00078 00079 typedef T* iterator; 00080 00081 explicit Array<T>(int the_size=0, int extra_space = 10) 00082 : TVec<T>(the_size+extra_space) 00083 { length_ = the_size; } 00084 00085 Array<T>(const T& elem1) 00086 : TVec<T>(1) 00087 { (*this)[0] = elem1; } 00088 00089 Array<T>(const T& elem1, const T& elem2) 00090 : TVec<T>(2) 00091 { 00092 (*this)[0] = elem1; 00093 (*this)[1] = elem2; 00094 } 00095 00096 Array<T>(const Array<T>& other) 00097 : TVec<T>(other.length()) 00098 { 00099 length_ = other.size(); 00100 offset_ = other.offset(); 00101 iterator array = data(); 00102 for(int i=0; i<length_; i++) 00103 array[i] = other[i]; 00104 } 00105 00106 Array<T>(const TVec<T>& other) 00107 : TVec<T>(other.copy()) 00108 {} 00109 00110 Array<T>(const vector<T> &other) 00111 : TVec<T>(other.size()) 00112 { 00113 iterator array = data(); 00114 for (int i = 0; i < length_; ++i) 00115 array[i] = other[i]; 00116 } 00117 00119 operator bool() const 00120 { return length_>0; } 00121 00123 bool operator!() const 00124 { return length_==0; } 00125 00126 Array<T> subArray(int start, int len) 00127 { 00128 if (start+len>length_) 00129 PLERROR("Array::subArray start(%d)+len(%d)>size(%d)", start,len,length_); 00130 Array<T> newarray(len); 00131 iterator new_ar = newarray.data(); 00132 iterator array = data(); 00133 for (int i=0;i<len;i++) 00134 new_ar[i] = array[start+i]; 00135 return newarray; 00136 } 00137 00138 void clear() 00139 { length_ = 0; } 00140 00141 void operator=(const Array<T>& other) 00142 { 00143 resize(other.size()); 00144 iterator array = data(); 00145 for(int i=0; i<length_; i++) 00146 array[i] = other[i]; 00147 } 00148 00149 void operator=(const TVec<T>& other) 00150 { 00151 resize(other.size()); 00152 iterator array = data(); 00153 for(int i=0; i<length_; i++) 00154 array[i] = other[i]; 00155 } 00156 00158 void view(const TVec<T>& other) 00159 { 00160 TVec<T>::operator=(other); 00161 } 00162 00163 bool operator==(const Array<T>& other) const 00164 { 00165 #ifdef BOUNDCHECK 00166 if (this->size()!=other.size()) 00167 PLERROR("Array::operator== works on same-size arguments"); 00168 #endif 00169 iterator array = data(); 00170 for(int i=0; i<length_; i++) 00171 if (array[i] != other[i]) return false; 00172 return true; 00173 } 00174 00175 bool operator<(const Array<T>& other) const 00176 { 00177 #ifdef BOUNDCHECK 00178 if (this->size()!=other.size()) 00179 PLERROR("Array::operator< works on same-size arguments"); 00180 #endif 00181 iterator array = data(); 00182 for(int i=0; i<length_; i++) 00183 { 00184 if (array[i] < other[i]) return true; 00185 else if (array[i] > other[i]) return false; 00186 } 00187 return false; // if == then not < 00188 } 00189 00190 bool operator<=(const Array<T>& other) const 00191 { 00192 #ifdef BOUNDCHECK 00193 if (this->size()!=other.size()) 00194 PLERROR("Array::operator< works on same-size arguments"); 00195 #endif 00196 iterator array = data(); 00197 for(int i=0; i<length_; i++) 00198 { 00199 if (array[i] < other[i]) return true; 00200 else if (array[i] > other[i]) return false; 00201 } 00202 return true; // if == then <= 00203 } 00204 00205 00206 bool operator>(const Array<T>& other) const 00207 { 00208 #ifdef BOUNDCHECK 00209 if (this->size()!=other.size()) 00210 PLERROR("Array::operator< works on same-size arguments"); 00211 #endif 00212 iterator array = data(); 00213 for(int i=0; i<length_; i++) 00214 { 00215 if (array[i] > other[i]) return true; 00216 else if (array[i] < other[i]) return false; 00217 } 00218 return false; // if == then not > 00219 } 00220 00221 bool operator>=(const Array<T>& other) const 00222 { 00223 #ifdef BOUNDCHECK 00224 if (this->size()!=other.size()) 00225 PLERROR("Array::operator< works on same-size arguments"); 00226 #endif 00227 iterator array = data(); 00228 for(int i=0; i<length_; i++) 00229 { 00230 if (array[i] > other[i]) return true; 00231 else if (array[i] < other[i]) return false; 00232 } 00233 return true; // if == then >= 00234 } 00235 00236 void operator=(const vector<T> &other) 00237 { 00238 resize(other.size()); 00239 iterator array = data(); 00240 for(int i = 0; i < length_; ++i) 00241 array[i] = other[i]; 00242 } 00243 00244 void print(ostream& out) const 00245 { 00246 iterator array = data(); 00247 for(int i=0; i<length_; i++) 00248 out << array[i] << endl; 00249 } 00250 00251 int findFirstOccurence(const T& elem) 00252 { 00253 for(int i=0;i<this->array_size;i++) 00254 if(elem==this->array[i]) 00255 return i; 00256 return -1; 00257 } 00258 00261 void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00262 00263 00264 // DEPRECATED! Call PStream& << arr instead (This is for backward compatibility only) 00265 void write(ostream &out_) const 00266 { 00267 PStream out(&out_); 00268 out << *this; 00269 } 00270 00271 /* 00272 * NOTE: FIX_ME 00273 * If newread changes the state of the stream (e.g. eof), 00274 * the original stream will NOT reflect this state... 00275 * 'in' will have it's state changed, but not 'in_'. 00276 * This can be a major problem w/ 'asignstreams'... 00277 * - xsm 00278 */ 00279 00280 // DEPRECATED! Call PStream& >> arr instead (This is for backward compatibility only) 00281 void read(istream &in_) 00282 { 00283 PStream in(&in_); 00284 in >> *this; 00285 } 00286 00288 inline operator char*() const { if(this->isNull()) return 0; else return (char*)data(); } 00289 00290 // norman: removed const. With inline is useless (and .NET doesn't like it) 00291 // Old code: 00292 //inline const size_t byteLength() const { return length()*sizeof(T); } 00293 inline size_t byteLength() const { return this->size()*sizeof(T); } 00294 00295 /* PAS UTILISE 00296 void increaseCapacity(int increase = 10) 00297 { 00298 T* newarray = new T[array_capacity+increase]; 00299 for(int i=0; i<length_; i++) 00300 newarray[i] = array[i]; 00301 delete[] array; 00302 array = newarray; 00303 array_capacity += increase; 00304 } 00305 */ 00306 00307 }; 00308 00309 template<class T> 00310 class TypeTraits< Array<T> > 00311 { 00312 public: 00313 static inline string name() 00314 { return string("Array< ") + TypeTraits<T>::name()+" >"; } 00315 00316 static inline unsigned char little_endian_typecode() 00317 { return 0xFF; } 00318 00319 static inline unsigned char big_endian_typecode() 00320 { return 0xFF; } 00321 00322 }; 00323 00324 00325 template <class T> 00326 class Array2ArrayMap : public PPointable 00327 { 00328 public: 00329 multimap<Array<T>,Array<T> > map; 00330 }; 00331 00332 /*template <class T> 00333 struct hash_to_multimapArray { 00334 size_t operator()(const PP<multimap<Array<T>,Array<T> > > m) const 00335 { 00336 if (a) 00337 return hashbytes((char*)a->data(),a->size()*sizeof(T)); 00338 return 0; 00339 } 00340 }; 00341 */ 00342 00343 00344 } // end of namespace PLearn 00345 00346 00347 // define hash function (replace the below declaration) 00348 SET_HASH_FUNCTION(PLearn::Array<T>, T, a, PLearn::hashbytes((char*)a.data(),a.size()*sizeof(T)) ) 00349 00350 //template <class T> 00351 //struct hash_Array { 00352 // size_t operator()(const Array<T>& a) const 00353 // { 00354 // return hashbytes((char*)a.data(),a.size()*sizeof(T)); 00355 // } 00356 //}; 00357 00358 #endif

Generated on Tue Aug 17 15:48:32 2004 for PLearn by doxygen 1.3.7