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

TVec_impl.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: TVec_impl.h,v 1.4 2004/07/21 16:30:53 chrish42 Exp $ 00041 * AUTHORS: Pascal Vincent & Yoshua Bengio 00042 * This file is part of the PLearn library. 00043 ******************************************************* */ 00044 00045 00048 #ifndef TVec_impl_INC 00049 #define TVec_impl_INC 00050 00051 #include "TVec_decl.h" 00052 #include <plearn/io/pl_io.h> 00053 00054 namespace PLearn { 00055 using namespace std; 00056 00057 00058 // ***************************** 00059 // **** Fonctions pour TVec **** 00060 // ***************************** 00061 00062 // Deep copying 00063 template<class T> 00064 void TVec<T>::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00065 { 00066 deepCopyField(storage, copies); 00067 } 00068 00069 template<class T> 00070 TVec<T> TVec<T>::deepCopy(map<const void*, void*>& copies) const 00071 { 00072 // First do a shallow copy 00073 TVec<T> deep_copy = *this; 00074 // Transform the shallow copy into a deep copy 00075 deep_copy.makeDeepCopyFromShallowCopy(copies); 00076 // return the completed deep_copy 00077 return deep_copy; 00078 } 00079 00080 template <class T> 00081 inline TVec<T> deepCopy(const TVec<T>& source) 00082 { 00083 CopiesMap copies; 00084 return deepCopy(source, copies); 00085 } 00086 00087 template <class T> 00088 inline TVec<T> deepCopy(const TVec<T>& source, CopiesMap& copies) 00089 { 00090 return source.deepCopy(copies); 00091 } 00092 00093 template <class T> 00094 inline void deepCopyField(TVec<T>& field, CopiesMap& copies) 00095 { 00096 field.makeDeepCopyFromShallowCopy(copies); 00097 } 00098 00099 00100 template<class T> 00101 void swap( TVec<T>& a, TVec<T>& b) 00102 { swap_ranges(a.begin(), a.end(), b.begin()); } 00103 00105 template<class T> 00106 inline void operator<<(const TVec<T>& m1, const TVec<T>& m2) 00107 { 00108 #ifdef BOUNDCHECK 00109 if(m1.size()!=m2.size()) 00110 PLERROR("In operator<<(m1,m2) the 2 matrices must have the same number of elements (%d != %d)", m1.size(), m2.size()); 00111 #endif 00112 copy(m2.begin(), m2.end(), m1.begin()); 00113 } 00114 00116 template<class T, class U> 00117 void operator<<(const TVec<T>& m1, const TVec<U>& m2) 00118 { 00119 #ifdef BOUNDCHECK 00120 if(m1.size()!=m2.size()) 00121 PLERROR("In operator<<(m1,m2) the 2 matrices must have the same number of elements (%d != %d)", m1.size(), m2.size()); 00122 #endif 00123 copy_cast(m2.begin(), m2.end(), m1.begin()); 00124 } 00125 00127 template<class T, class U> 00128 inline void operator>>(const TVec<T>& m1, const TVec<U>& m2) 00129 { m2 << m1; } 00130 00131 // old .pvec format 00132 template<class T> 00133 void savePVec(const string& filename, const TVec<T>& vec) 00134 { PLERROR("savePVec only implemented for float and double"); } 00135 00136 template<class T> 00137 void loadPVec(const string& filename, TVec<float>& vec) 00138 { PLERROR("loadPVec only implemented for float and double"); } 00139 00140 00144 00145 template <class T> inline PStream & 00146 operator<<(PStream &out, const TVec<T> &v) 00147 { 00148 v.write(out); 00149 return out; 00150 } 00151 00152 template <class T> 00153 PStream & operator>>(PStream &in, TVec<T> &v) 00154 { 00155 v.read(in); 00156 return in; 00157 } 00158 00159 00160 template<class T> 00161 void binwrite(ostream& out, const TVec<T>& v) 00162 { 00163 int l = v.length(); 00164 PLearn::binwrite(out,l); 00165 if (l<200000) 00166 PLearn::binwrite(out,v.data(),l); 00167 else for (int i=0;i<l;i+=200000) 00168 PLearn::binwrite(out,&v[i],std::min(200000,l-i)); 00169 } 00170 00171 template<class T> 00172 void binread(istream& in, TVec<T>& v) 00173 { 00174 int l; 00175 PLearn::binread(in,l); 00176 v.resize(l); 00177 if (l<200000) 00178 PLearn::binread(in,v.data(),l); 00179 else for (int i=0;i<l;i+=200000) 00180 PLearn::binread(in,&v[i],std::min(200000,l-i)); 00181 } 00182 00183 template<class T> 00184 void binwrite_double(ostream& out, const TVec<T>& v) 00185 { 00186 int l = v.length(); 00187 PLearn::binwrite(out,l); 00188 if (l<200000) 00189 PLearn::binwrite_double(out,v.data(),l); 00190 else for (int i=0;i<l;i+=200000) 00191 PLearn::binwrite_double(out,&v[i],std::min(200000,l-i)); 00192 } 00193 00194 template<class T> 00195 void binread_double(istream& in, TVec<T>& v) 00196 { 00197 int l; 00198 PLearn::binread(in,l); 00199 v.resize(l); 00200 if (l<200000) 00201 PLearn::binread_double(in,v.data(),l); 00202 else for (int i=0;i<l;i+=200000) 00203 PLearn::binread_double(in,&v[i],std::min(200000,l-i)); 00204 } 00205 00206 00207 template<class T> 00208 inline ostream& operator<<(ostream& out, const TVec<T>& v) 00209 { 00210 v.print(out); 00211 return out; 00212 } 00213 00214 template<class T> 00215 inline istream& operator>>(istream& in, const TVec<T>& v) 00216 { 00217 v.input(in); 00218 return in; 00219 } 00220 00221 00227 template<class T, class I> 00228 void selectElements(const TVec<T>& source, const TVec<I>& indices, TVec<T>& destination); 00229 00231 template<class T> 00232 void elementsEqualTo(const TVec<T>& source, const T& value, const TVec<T>& destination); 00233 00234 template<class T> 00235 TVec<T> concat(const TVec<T>& v1, const TVec<T>& v2); 00236 00237 //template<class T> 00238 //TVec<T> concat(const Array< TVec<T> >& varray); 00239 00243 template<class T> 00244 TVec<T> removeElement(const TVec<T>& v, int elemnum); 00245 00246 00248 template <class T> 00249 bool operator<=(const TVec<T>& left, const TVec<T>& right) 00250 { 00251 if (left.size() != right.size()) 00252 PLERROR("Left and right vectors must have the same size in operator<="); 00253 return std::inner_product(left.begin(), left.end(), right.begin(), 00254 true, std::logical_and<bool>(), 00255 std::less_equal<T>()); 00256 } 00257 00258 template <class T> 00259 bool operator>=(const TVec<T>& left, const TVec<T>& right) 00260 { 00261 if (left.size() != right.size()) 00262 PLERROR("Left and right vectors must have the same size in operator>="); 00263 return std::inner_product(left.begin(), left.end(), right.begin(), 00264 true, std::logical_and<bool>(), 00265 std::greater_equal<T>()); 00266 } 00267 00268 // This is a lexicographical definition for operator< 00269 template <class T> 00270 bool operator<(const TVec<T>& left, const TVec<T>& right) 00271 { 00272 if (left.size() != right.size()) 00273 PLERROR("Left and right vectors must have the same size in operator<"); 00274 int size = left.size(); 00275 const T* ldata = left.data(); 00276 const T* rdata = right.data(); 00277 for ( ; size ; ++ldata, ++rdata, --size) { 00278 if (*ldata < *rdata) 00279 return true; 00280 if (*ldata > *rdata) 00281 return false; 00282 // Continue loop if both are equal 00283 } 00284 return false; // both vectors are equal at 00285 // this point; cannot be < 00286 } 00287 00288 // This is a lexicographical definition for operator> 00289 template <class T> 00290 bool operator>(const TVec<T>& left, const TVec<T>& right) 00291 { 00292 if (left.size() != right.size()) 00293 PLERROR("Left and right vectors must have the same size in operator>"); 00294 int size = left.size(); 00295 const T* ldata = left.data(); 00296 const T* rdata = right.data(); 00297 for ( ; size ; ++ldata, ++rdata, --size) { 00298 if (*ldata < *rdata) 00299 return false; 00300 if (*ldata > *rdata) 00301 return true; 00302 // Continue loop if both are equal 00303 } 00304 return false; // both vectors are equal at 00305 // this point; cannot be > 00306 } 00307 00308 } // end of namespace PLearn 00309 00310 #endif

Generated on Tue Aug 17 16:09:14 2004 for PLearn by doxygen 1.3.7