PLearn 0.1
|
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 8247 2007-11-12 20:22:12Z nouiz $ 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 <string> 00051 #include <vector> 00052 #include "TypeTraits.h" 00053 #include <plearn/math/TMat_decl.h> 00054 00055 #include "ms_hash_wrapper.h" 00056 namespace PLearn { 00057 using std::string; 00058 using std::vector; 00059 00060 00061 template <class T> 00062 class Array: public TVec<T> 00063 { 00064 public: 00065 00066 // norman: added scope for dependent name to resolve lookup 00067 // See chap. 9.4.2 of C++ Templates, The Complete Guide 00068 // by Vandevoorde and Josuttis 00069 using TVec<T>::length_; 00070 using TVec<T>::offset_; 00071 using TVec<T>::storage; 00072 00073 using TVec<T>::data; 00074 using TVec<T>::resize; 00075 00076 typedef T* iterator; 00077 00078 explicit Array<T>(int the_size=0, int extra_space = 10) 00079 : TVec<T>(the_size+extra_space) 00080 { length_ = the_size; } 00081 00082 Array<T>(const T& elem1) 00083 : TVec<T>(1) 00084 { (*this)[0] = elem1; } 00085 00086 Array<T>(const T& elem1, const T& elem2) 00087 : TVec<T>(2) 00088 { 00089 (*this)[0] = elem1; 00090 (*this)[1] = elem2; 00091 } 00092 00093 Array<T>(const Array<T>& other) 00094 : TVec<T>(other.length()) 00095 { 00096 length_ = other.size(); 00097 offset_ = other.offset(); 00098 iterator array = data(); 00099 for(int i=0; i<length_; i++) 00100 array[i] = other[i]; 00101 } 00102 00103 Array<T>(const TVec<T>& other) 00104 : TVec<T>(other.copy()) 00105 {} 00106 00107 Array<T>(const vector<T> &other) 00108 : TVec<T>(other.size()) 00109 { 00110 iterator array = data(); 00111 for (int i = 0; i < length_; ++i) 00112 array[i] = other[i]; 00113 } 00114 00116 operator bool() const 00117 { return length_>0; } 00118 00120 bool operator!() const 00121 { return length_==0; } 00122 00123 Array<T> subArray(int start, int len) 00124 { 00125 if (start+len>length_) 00126 PLERROR("Array::subArray start(%d)+len(%d)>size(%d)", start,len,length_); 00127 Array<T> newarray(len); 00128 iterator new_ar = newarray.data(); 00129 iterator array = data(); 00130 for (int i=0;i<len;i++) 00131 new_ar[i] = array[start+i]; 00132 return newarray; 00133 } 00134 00135 void clear() 00136 { length_ = 0; } 00137 00138 void operator=(const Array<T>& other) 00139 { 00140 resize(other.size()); 00141 iterator array = data(); 00142 for(int i=0; i<length_; i++) 00143 array[i] = other[i]; 00144 } 00145 00146 void operator=(const TVec<T>& other) 00147 { 00148 resize(other.size()); 00149 iterator array = data(); 00150 for(int i=0; i<length_; i++) 00151 array[i] = other[i]; 00152 } 00153 00154 void operator=(const vector<T> &other) 00155 { 00156 resize(other.size()); 00157 iterator array = data(); 00158 for(int i = 0; i < length_; ++i) 00159 array[i] = other[i]; 00160 } 00161 00163 void view(const TVec<T>& other) 00164 { 00165 TVec<T>::operator=(other); 00166 } 00167 00168 // EXPERIMENTALLY: PUT THOSE IN COMMENTS AND USE VERSION INHERITED FROM TVEC 00169 // 00170 // bool operator==(const Array<T>& other) const 00171 // { 00172 // #ifdef BOUNDCHECK 00173 // if (this->size()!=other.size()) 00174 // PLERROR("Array::operator== works on same-size arguments"); 00175 // #endif 00176 // iterator array = data(); 00177 // for(int i=0; i<length_; i++) 00178 // if (array[i] != other[i]) return false; 00179 // return true; 00180 // } 00181 // 00182 // bool operator<(const Array<T>& other) const 00183 // { 00184 // #ifdef BOUNDCHECK 00185 // if (this->size()!=other.size()) 00186 // PLERROR("Array::operator< works on same-size arguments"); 00187 // #endif 00188 // iterator array = data(); 00189 // for(int i=0; i<length_; i++) 00190 // { 00191 // if (array[i] < other[i]) return true; 00192 // else if (array[i] > other[i]) return false; 00193 // } 00194 // return false; // if == then not < 00195 // } 00196 // 00197 // bool operator<=(const Array<T>& other) const 00198 // { 00199 // #ifdef BOUNDCHECK 00200 // if (this->size()!=other.size()) 00201 // PLERROR("Array::operator< works on same-size arguments"); 00202 // #endif 00203 // iterator array = data(); 00204 // for(int i=0; i<length_; i++) 00205 // { 00206 // if (array[i] < other[i]) return true; 00207 // else if (array[i] > other[i]) return false; 00208 // } 00209 // return true; // if == then <= 00210 // } 00211 // 00212 // 00213 // bool operator>(const Array<T>& other) const 00214 // { 00215 // #ifdef BOUNDCHECK 00216 // if (this->size()!=other.size()) 00217 // PLERROR("Array::operator< works on same-size arguments"); 00218 // #endif 00219 // iterator array = data(); 00220 // for(int i=0; i<length_; i++) 00221 // { 00222 // if (array[i] > other[i]) return true; 00223 // else if (array[i] < other[i]) return false; 00224 // } 00225 // return false; // if == then not > 00226 // } 00227 // 00228 // bool operator>=(const Array<T>& other) const 00229 // { 00230 // #ifdef BOUNDCHECK 00231 // if (this->size()!=other.size()) 00232 // PLERROR("Array::operator< works on same-size arguments"); 00233 // #endif 00234 // iterator array = data(); 00235 // for(int i=0; i<length_; i++) 00236 // { 00237 // if (array[i] > other[i]) return true; 00238 // else if (array[i] < other[i]) return false; 00239 // } 00240 // return true; // if == then >= 00241 // } 00242 00243 void print(ostream& out) const 00244 { 00245 iterator array = data(); 00246 for(int i=0; i<length_; i++) 00247 out << array[i] << endl; 00248 } 00249 00250 int findFirstOccurence(const T& elem) 00251 { 00252 for(int i=0;i<this->array_size;i++) 00253 if(elem==this->array[i]) 00254 return i; 00255 return -1; 00256 } 00257 00260 void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00261 00262 00263 // DEPRECATED! Call PStream& << arr instead (This is for backward compatibility only) 00264 void write(ostream &out_) const 00265 { 00266 PStream out(&out_); 00267 out << *this; 00268 } 00269 00270 /* 00271 * NOTE: FIX_ME 00272 * If newread changes the state of the stream (e.g. eof), 00273 * the original stream will NOT reflect this state... 00274 * 'in' will have it's state changed, but not 'in_'. 00275 * This can be a major problem w/ 'asignstreams'... 00276 * - xsm 00277 */ 00278 00279 // DEPRECATED! Call PStream& >> arr instead (This is for backward compatibility only) 00280 void read(istream &in_) 00281 { 00282 PStream in(&in_); 00283 in >> *this; 00284 } 00285 00287 inline operator char*() const { if(this->isNull()) return 0; else return (char*)data(); } 00288 00289 // norman: removed const. With inline is useless (and .NET doesn't like it) 00290 // Old code: 00291 //inline const size_t byteLength() const { return length()*sizeof(T); } 00292 inline size_t byteLength() const { return this->size()*sizeof(T); } 00293 00294 /* PAS UTILISE 00295 void increaseCapacity(int increase = 10) 00296 { 00297 T* newarray = new T[array_capacity+increase]; 00298 for(int i=0; i<length_; i++) 00299 newarray[i] = array[i]; 00300 delete[] array; 00301 array = newarray; 00302 array_capacity += increase; 00303 } 00304 */ 00305 00306 }; 00307 00308 template<class T> 00309 class TypeTraits< Array<T> > 00310 { 00311 public: 00312 static inline string name() 00313 { return string("Array< ") + TypeTraits<T>::name()+" >"; } 00314 00315 static inline unsigned char little_endian_typecode() 00316 { return 0xFF; } 00317 00318 static inline unsigned char big_endian_typecode() 00319 { return 0xFF; } 00320 00321 }; 00322 00323 00324 template <class T> 00325 class Array2ArrayMap : public PPointable 00326 { 00327 public: 00328 multimap<Array<T>,Array<T> > map; 00329 }; 00330 00331 /*template <class T> 00332 struct hash_to_multimapArray { 00333 size_t operator()(const PP<multimap<Array<T>,Array<T> > > m) const 00334 { 00335 if (a) 00336 return hashbytes((char*)a->data(),a->size()*sizeof(T)); 00337 return 0; 00338 } 00339 }; 00340 */ 00341 00342 00343 } // end of namespace PLearn 00344 00345 00346 // define hash function (replace the below declaration) 00347 SET_HASH_FUNCTION(PLearn::Array<T>, T, a, PLearn::hashbytes((char*)a.data(),a.size()*sizeof(T)) ) 00348 00349 //template <class T> 00350 //struct hash_Array { 00351 // size_t operator()(const Array<T>& a) const 00352 // { 00353 // return hashbytes((char*)a.data(),a.size()*sizeof(T)); 00354 // } 00355 //}; 00356 00357 #endif 00358 00359 00360 /* 00361 Local Variables: 00362 mode:c++ 00363 c-basic-offset:4 00364 c-file-style:"stroustrup" 00365 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00366 indent-tabs-mode:nil 00367 fill-column:79 00368 End: 00369 */ 00370 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :