PLearn 0.1
Array_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: Array_impl.h 8247 2007-11-12 20:22:12Z nouiz $
00041  * This file is part of the PLearn library.
00042  ******************************************************* */
00043 
00044 
00047 #ifndef Array_impl_INC
00048 #define Array_impl_INC
00049 
00050 #include "Array_decl.h"
00051 
00052 namespace PLearn {
00053 using std::string;
00054 
00055 template <class T>
00056 void swap(Array<T>& a1, Array<T>& a2)
00057 {
00058     T* a1d = a1.data();
00059     T* a2d = a2.data();
00060     T tmp;
00061 #ifdef BOUNDCHECK
00062     if (a1.size()!=a2.size())
00063         PLERROR("Array::swap expects two same-size arguments");
00064 #endif
00065     for(int i=0; i<a1.size(); i++)
00066     {
00067         tmp = a1d[i];
00068         a1d[i]=a2d[i];
00069         a2d[i]=tmp;
00070     }
00071 }
00072 
00073 template <class T>
00074 inline PStream & operator>>(PStream &in, Array<T> &a)
00075 { readSequence(in, a); return in; }
00076 
00077 template <class T>
00078 inline PStream & operator<<(PStream &out, const Array<T> &a)
00079 { writeSequence(out, a); return out; }
00080 
00081 template<class T>
00082 ostream& operator<<(ostream& out, const Array<T>& a)
00083 { a.print(out); return out; }
00084 
00085 template <class T>
00086 inline void deepCopyField(Array<T>& field, CopiesMap& copies)
00087 { field.makeDeepCopyFromShallowCopy(copies); }
00088 
00089 template<class T>
00090 Array<T> operator&(const T& elem, const Array<T>& a)
00091 { return Array<T>(elem) & a; }
00092 
00093 template<class T>
00094 Array<T>& operator&=(Array<T>& a, const T& elem)
00095 { a.append(elem); return a; }
00096 
00097 template<class T>
00098 Array<T>& operator&=(Array<T>& a, const Array<T>& ar)
00099 { a.append(ar); return a; }
00100 
00101 template<class T>
00102 Array<T>& operator&=(Array<T>& a, const vector<T> &ar)
00103 { a.append(ar); return a; }
00104 
00105 template<class T>
00106 Array<T> operator&(const Array<T>& a, const T& elem)
00107 {
00108     Array<T> newarray(a.size(), a.size()+1);
00109     newarray = a;
00110     newarray.append(elem);
00111     return newarray;
00112 }
00113 
00114 template<class T>
00115 Array<T> operator&(const Array<T>& a, const Array<T>& ar)
00116 {
00117     Array<T> newarray(a.size(), a.size()+ar.size());
00118     newarray = a;
00119     newarray.append(ar);
00120     return newarray;
00121 }
00122 
00123 template<class T>
00124 Array<T> operator&(const Array<T>& a, const vector<T> &ar)
00125 {
00126     Array<T> newarray(a.size(), a.size() + ar.size());
00127     newarray = a;
00128     newarray.append(ar);
00129     return newarray;
00130 }
00131 
00132 inline string join(const Array<string>& s, const string& separator)
00133 {
00134     string result;
00135     for(int i=0; i<s.size(); i++)
00136     {
00137         result += s[i];
00138         if(i<s.size()-1)
00139             result += separator;
00140     }
00141     return result;
00142 }
00143 
00145 template<class T>
00146 inline Array< TVec<T> > operator&(const TVec<T>& m1, const TVec<T>& m2) { return Array< TVec<T> >(m1,m2); } 
00147 
00148 template<class T>
00149 TVec<T> concat(const Array< TVec<T> >& varray)
00150 {
00151     int l = 0;
00152     for(int k=0; k<varray.size(); k++)
00153         l += varray[k].length();
00154  
00155     TVec<T> result(l);
00156     real* resdata = result.data();
00157     for(int k=0; k<varray.size(); k++)
00158     {
00159         const TVec<T>& v = varray[k];
00160         real* vdata = varray[k].data();
00161         for(int i=0; i<v.length(); i++)
00162             resdata[i] = vdata[i];
00163         resdata += v.length();
00164     }
00165     return result;
00166 }
00167 
00168 template<class T>
00169 TMat<T> vconcat(const Array< TMat<T> >& ar)
00170 {
00171     int l = 0;
00172     int w = ar[0].width();
00173     for(int n=0; n<ar.size(); n++)
00174     {
00175         if(ar[n].width() != w)
00176             PLERROR("In Mat vconcat(Array<Mat> ar) all Mats do not have the same width()!");
00177         l += ar[n].length();
00178     }
00179     TMat<T> result(l, w);
00180     int pos = 0;
00181     for(int n=0; n<ar.size(); n++)
00182     {
00183         result.subMatRows(pos, ar[n].length()) << ar[n];
00184         pos+=ar[n].length();  // do not put this line after the n++ in the for loop, or it will cause a bug!
00185     }
00186     return result;
00187 }
00188 
00189 template<class T>
00190 TMat<T> hconcat(const Array< TMat<T> >& ar)
00191 {
00192     int w = 0;
00193     int l = ar[0].length();
00194     for(int n=0; n<ar.size(); n++)
00195     {
00196         if(ar[n].length() != l)
00197             PLERROR("In Mat hconcat(Array<Mat> ar) all Mats do not have the same length()!");
00198         w += ar[n].width();
00199     }
00200     TMat<T> result(l, w);
00201     int pos = 0;
00202     for(int n=0; n<ar.size(); n++)
00203     {
00204         result.subMatColumns(pos, ar[n].width()) << ar[n];
00205         pos+=ar[n].width(); // do not put this line after the n++ in the for loop, or it will cause a bug!
00206     }
00207     return result;
00208 }
00209 
00210 template<class T>
00211 inline TMat<T> vconcat(const TMat<T>& m1, const TMat<T>& m2) { return vconcat(Array< TMat<T> >(m1,m2)); }
00212 
00213 template<class T>
00214 inline TMat<T> hconcat(const TMat<T>& m1, const TMat<T>& m2) { return hconcat(Array< TMat<T> >(m1,m2)); }
00215 
00217 template<class T>
00218 inline Array< TMat<T> > operator&(const TMat<T>& m1, const TMat<T>& m2) { return Array< TMat<T> >(m1,m2); } 
00219 
00220 // Deep copying
00221 template<class T>
00222 void Array<T>::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00223 {
00224     // Shallow copy of an array, contrarily to TVec, already makes a shallow copy of the elements, so we
00225     // don't want to call deepCopyField(storage, copies) as TVec does, but simply deepCopyField()
00226     // of each of the storage's elements.
00227     if (storage.isNotNull())
00228         for (int i = 0; i < storage->size(); i++)
00229             deepCopyField(storage->data[i], copies);
00230 }
00231 
00232 
00233 } // end of namespace PLearn
00234 
00235 #endif
00236 
00237 
00238 /*
00239   Local Variables:
00240   mode:c++
00241   c-basic-offset:4
00242   c-file-style:"stroustrup"
00243   c-file-offsets:((innamespace . 0)(inline-open . 0))
00244   indent-tabs-mode:nil
00245   fill-column:79
00246   End:
00247 */
00248 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines