libidx
/home/rex/ebltrunk/core/libidx/include/idxspec.h
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Yann LeCun and Pierre Sermanet *
00003  *   yann@cs.nyu.edu, pierre.sermanet@gmail.com *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source
00014  *       Initiative (http://www.opensource.org) must display the
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef IDXSPEC_H
00034 #define IDXSPEC_H
00035 
00036 #include "defines.h"
00037 
00038 #ifndef __NOSTL__
00039 #include <vector>
00040 #include <algorithm>
00041 #include <iterator>
00042 #include <iostream>
00043 #endif
00044 
00045 #include <stdio.h>
00046 #include "stl.h"
00047 #include "smart.h"
00048 
00049 namespace ebl {
00050 
00051   const int MAXDIMS=8;
00052 
00053   // forward declarations //////////////////////////////////////////////////////
00054 
00055   template <typename T = intg> class idxd;
00056   typedef idxd<intg> idxdim;
00057   typedef idxd<float> fidxdim;
00058 
00060 
00067   class EXPORT idxspec {
00068   public:
00070     virtual ~idxspec();
00072     const idxspec& operator=(const idxspec& src);
00074     void copy(const idxspec& src);
00076     idxspec(const idxspec& src);
00077 
00080 
00083     idxspec();
00085     idxspec(intg o);
00087     idxspec(intg o, intg size0);
00089     idxspec(intg o, intg size0, intg size1);
00091     idxspec(intg o, intg size0, intg size1, intg size2);
00093     idxspec(intg o, intg s0, intg s1, intg s2, intg s3,
00094             intg s4=-1, intg s5=-1, intg s6=-1, intg s7=-1);
00097     idxspec(intg o, int n, intg *ldim, intg *lmod);
00100     idxspec(intg o, const idxdim &d);
00101 
00102     // access methods //////////////////////////////////////////////////////////
00103 
00105     intg getoffset() const;
00107     void add_offset(intg n);
00109     int getndim() const;
00113     intg footprint() const;
00115     intg nelements() const;
00119     bool contiguousp() const;
00120 
00123 
00125     void pretty(FILE *f) const;
00126     void pretty(std::ostream& out) const;
00127 
00130 
00138     idxspec select(int d, intg i);
00141     intg select_into(idxspec *dst, int d, intg n) const;
00144     intg select_inplace(int d, intg i);
00145 
00153     idxspec narrow(int d, intg s, intg o);
00156     intg narrow_into(idxspec *dst, int d, intg s, intg o);
00159     intg narrow_inplace(int d, intg s, intg o);
00160 
00162     idxspec transpose(int d1, int d2);
00164     idxspec transpose(int *p);
00167     int transpose_into(idxspec *dst, int d1, int d2);
00170     int transpose_into(idxspec *dst, int *p);
00173     int transpose_inplace(int d1, int d2);
00176     int transpose_inplace(int *p);
00177 
00184     idxspec unfold(int d, intg k, intg s);
00187     intg unfold_into(idxspec *dst, int d, intg k, intg s);
00190     intg unfold_inplace(int d, intg k, intg s);
00191 
00194 
00195     friend class idxd<>;
00196     template <class T> friend class idxiter;
00197     template <class T> friend class idxlooper;
00198     template <class T> friend class contiguous_idxiter;
00199     template <class T> friend class noncontiguous_idxiter;
00200     template <class T> friend class idx;
00201 
00202     friend bool same_dim(idxspec &s1, idxspec &s2);
00204     friend EXPORT std::ostream& operator<<(std::ostream& out, idxspec& d);
00206     friend EXPORT std::ostream& operator<<(std::ostream& out, const idxspec& d);
00208     friend EXPORT std::string& operator<<(std::string& out, idxspec& d);
00209 
00210   private:
00215     int setndim(int n);
00216 
00219     int setndim(int n, intg *ldim, intg *lmod);
00220 
00222     int ndim;
00224     intg offset;
00226     intg *dim;
00228     intg *mod;
00229 
00237     intg resize(intg s0=-1, intg s1=-1, intg s2=-1, intg s3=-1,
00238                 intg s4=-1, intg s5=-1, intg s6=-1, intg s7=-1);
00242     intg resize(const idxdim &d);
00246     intg resize1(intg dimn, intg size);
00247     /* template<typename SizeIter> */
00248     /*   intg resize(SizeIter& dimsBegin, SizeIter& dimsEnd); */
00250     intg setoffset(intg o) { return offset = o; }
00252     void init_spec(intg o, intg s0, intg s1, intg s2, intg s3,
00253                    intg s4, intg s5, intg s6, intg s7);
00256     void init_spec(intg o, intg s0, intg s1, intg s2, intg s3,
00257                    intg s4, intg s5, intg s6, intg s7, uint n);
00258   };
00259 
00263   bool same_dim(idxspec &s1, idxspec &s2);
00264 
00270   template <typename T> class idxd : public smart_pointer {
00271   public:
00273     // constructors
00274 
00276     idxd();
00278     idxd(const idxspec &s);
00280     template <class T2> idxd(const idx<T2> &i);
00282     template <class T2> idxd(const idxd<T2> &i);
00284     idxd(const idxd<T> &s);
00286     idxd(T s0, T s1=-1, T s2=-1, T s3=-1, T s4=-1, T s5=-1, T s6=-1, T s7=-1);
00288     virtual ~idxd();
00289 
00291     // set dimensions
00292 
00295     void setdim(intg dimn, T size);
00297     template <class Tidx> void setdims(const idx<Tidx> &i);
00299     void setdims(const idxd<T> &s);
00301     void setdims(const idxspec &s);
00303     void setdims(T n);
00307     void insert_dim(intg pos, T dim_size);
00311     T remove_dim(intg pos);
00313     void setoffset(intg dimn, T offset);
00315     bool has_offsets() const;
00318     void set_max(const idxd<T> &other);
00320     void shift_dim(int d, int pos);
00321 
00323     // get dimensions
00324 
00326     bool empty() const;
00328     intg order() const;
00330     T dim(intg dimn) const;
00332     T maxdim() const;
00334     T offset(intg dimn) const;
00336     bool operator==(const idxd<T>& other);
00339     bool operator!=(const idxd<T>& other);
00341     intg nelements();
00342 
00344     // operators
00345 
00346     /* //! Assign idxd 'd2' into current idxd (copies offsets if they exist). */
00347     /* idxd<T>& operator=(const idxd<T> &d2); */
00349     idxd<T>& operator=(idxd<T> d2);
00352     template <typename T2> idxd<T>& operator=(const idxd<T2> &d2);
00355     idxd<T> operator*(const idxd<T> &d2) const;
00358     template <typename T2> idxd<T> operator*(const idxd<T2> &d2) const;
00361     template <typename T2> idxd<T> operator*(idxd<T2> &d2);
00363     template <typename T2> idxd<T> operator*(T2 d);
00365     template <typename T2> idxd<T> operator+(T2 d);
00367     idxd<T> operator+(idxd<T> &d2);
00370     bool operator<=(idxd<T> &d2);
00373     bool operator>=(idxd<T> &d2);
00374 
00376     // friends
00377 
00378     template <typename T2>
00379       friend std::string& operator<<(std::string& out, const idxd<T2>& d);
00380     friend class idxspec;
00381 
00383     // member variables
00384   protected:
00385     T           dims[MAXDIMS];  
00386     intg        ndim;           
00387     T           *offsets;       
00388   };
00389 
00391   template <typename T>
00392     EXPORT std::ostream& operator<<(std::ostream& out, const idxd<T>& d);
00394   template <typename T>
00395     EXPORT std::string& operator<<(std::string& out, const idxd<T>& d);
00396 
00398   template <typename T> class midxd : public svector<idxd<T> > {
00399   public:
00400     // constructors ////////////////////////////////////////////////////////////
00401 
00403     midxd();
00405     midxd(uint n);
00407     midxd(const idxd<T> &s);
00409     template <typename T2> midxd(const midxd<T2> &s);
00411     virtual ~midxd();
00412 
00413     // accessors ///////////////////////////////////////////////////////////////
00414 
00416     virtual midxd<T> narrow_copy(uint n, uint offset);
00418     template <typename T2> void push_back(const idxd<T2> &m);
00420     virtual void push_back(const midxd<T> &m);
00421     /* //! Appends all idxd contained in 'm' to this idxd. */
00422     /* virtual void push_back(midxd<T> &m); */
00425     virtual midxd<T> merge_all();
00427     template <typename T2> midxd<T>& operator=(const midxd<T2> &other);
00428 
00429     // member variables ////////////////////////////////////////////////////////
00430   protected:
00431   };
00432 
00433   // shortcut types
00434   typedef midxd<intg> midxdim;
00435   typedef midxd<float> mfidxdim;
00436 
00437   // printing //////////////////////////////////////////////////////////////////
00438 
00440   template<class T>
00441     EXPORT std::string& operator<<(std::string &o, const midxd<T> &v);
00443   template<class T>
00444     EXPORT std::ostream& operator<<(std::ostream &o, const midxd<T> &v);
00445 
00446 } // end namespace ebl
00447 
00448 #include "idxspec.hpp"
00449 
00450 #endif /* IDXSPEC_H_ */