PLearn 0.1
TMatRowsAsArraysIterator_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  * $Id: TMatRowsAsArraysIterator_decl.h 8235 2007-11-07 21:32:01Z nouiz $
00040  * AUTHORS: Pascal Vincent & Yoshua Bengio
00041  * This file is part of the PLearn library.
00042  ******************************************************* */
00043 
00044 
00047 #ifndef TMatRowsAsArraysIterator_decl_INC
00048 #define TMatRowsAsArraysIterator_decl_INC
00049 
00050 #include <plearn/base/Array_impl.h>
00051 
00052 namespace PLearn {
00053 using namespace std;
00054 
00058 
00059 
00060 template <class T>
00061 class TMatRowsAsArraysIterator
00062 {
00063 public:
00065     typedef random_access_iterator_tag iterator_category;
00066     typedef Array<T>                    value_type;
00067     typedef ptrdiff_t                  difference_type;
00068     typedef Array<T>*                   pointer;
00069     typedef Array<T>&                   reference;
00070   
00071 private:
00072     T* ptr;                                    
00073     int width;                                 
00074     int mod;                                   
00075     Array<T> v;
00076 
00077 public:
00078     TMatRowsAsArraysIterator()
00079         : ptr(), width(), mod() {}
00080 
00081     TMatRowsAsArraysIterator(T* p, int w, int m)
00082         : ptr(p), width(w), mod(m) {}
00083 
00084     // Implement trivial iterator functions
00085     bool operator==(const TMatRowsAsArraysIterator& other) const {
00086         return ptr == other.ptr && width == other.width && mod == other.mod;
00087     }
00088 
00089     bool operator!=(const TMatRowsAsArraysIterator& y)
00090     { return !operator==(y); }
00091 
00092     Array<T>& operator*() 
00093     {
00094         v.view(TVec<T>(width, ptr));
00095         return v;
00096     }
00097 
00098     // cannot define operator-> here since we cannot return a pointer to a
00099     // temporary (e.g. stack-based) vector and expect this to work properly...
00100   
00101     // Implement forward iterator functions
00102     TMatRowsAsArraysIterator& operator++() {
00103         ptr += mod;
00104         return *this;
00105     }
00106 
00107     TMatRowsAsArraysIterator operator++(int) {
00108         TMatRowsAsArraysIterator r(*this);
00109         ptr += mod;
00110         return r;
00111     }
00112 
00113     // Implement bidirectional iterator functions
00114     TMatRowsAsArraysIterator& operator--() {
00115         ptr -= mod;
00116         return *this;
00117     }
00118 
00119     TMatRowsAsArraysIterator operator--(int) {
00120         TMatRowsAsArraysIterator r(*this);
00121         ptr -= mod;
00122         return r;
00123     }
00124 
00125     // Implement random access iterator functions
00126     TMatRowsAsArraysIterator& operator+=(difference_type n) {
00127         ptr += n*mod;
00128         return *this;
00129     }
00130 
00131     TMatRowsAsArraysIterator operator+(difference_type n) {
00132         TMatRowsAsArraysIterator r(*this);
00133         r += n;
00134         return r;
00135     }
00136   
00137     TMatRowsAsArraysIterator& operator-=(difference_type n) {
00138         ptr -= n*mod;
00139         return *this;
00140     }
00141 
00142     TMatRowsAsArraysIterator operator-(difference_type n) {
00143         TMatRowsAsArraysIterator r(*this);
00144         r -= n;
00145         return r;
00146     }
00147 
00148     difference_type operator-(const TMatRowsAsArraysIterator& y) {
00149         return (ptr - y.ptr) / mod;
00150     }
00151   
00152     value_type operator[](difference_type n) {
00153         return TVec<T>(width, ptr + n*mod);
00154     }
00155 
00156     bool operator<(const TMatRowsAsArraysIterator& y) {
00157         return ptr < y.ptr;
00158     }
00159 
00160 };
00161 
00162 
00163 
00164 } // end of namesapce PLearn
00165 
00166 #endif
00167 
00168 
00169 /*
00170   Local Variables:
00171   mode:c++
00172   c-basic-offset:4
00173   c-file-style:"stroustrup"
00174   c-file-offsets:((innamespace . 0)(inline-open . 0))
00175   indent-tabs-mode:nil
00176   fill-column:79
00177   End:
00178 */
00179 // 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