PLearn 0.1
DoubleAccessSparseMatrix.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1999-2002 Christian Jauvin
00005 //
00006 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // 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 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00037 #ifndef DoubleAccessSparseMatrix_INC
00038 #define DoubleAccessSparseMatrix_INC
00039 
00040 #include <plearn/base/general.h>
00041 
00042 namespace PLearn {
00043 using namespace std;
00044 
00073 #define ROW_WISE -1000
00074 #define COLUMN_WISE -2000
00075 
00076 template <class T>
00077 class SMat;
00078 class PSMat;
00079 
00080 template<class T>
00081 class DoubleAccessSparseMatrix : public PPointable
00082 {
00083 
00084 protected:
00085 
00086     vector<map<int, T> > rows;
00087 
00088     vector<map<int, T> > cols;
00089 
00090     string name;
00091 
00092     int mode;
00093 
00094     bool double_access;
00095 
00096     int height;
00097 
00098     int width;
00099 
00100     T null_elem;
00101 
00102 public:
00103 
00104     DoubleAccessSparseMatrix(int n_rows = 0, int n_cols = 0, string _name = "<no-name>", int _mode = ROW_WISE, bool _double_access = false, T _null_elem = 0);
00105 
00106     virtual ~DoubleAccessSparseMatrix() {}
00107 
00108     virtual void resize(int n_rows, int n_cols, bool clear_data=true);
00109   
00110     virtual void clear();
00111 
00112     virtual void clearRow(int i, bool force_synchro_if_double_accessible); // force_synchro is expensive!
00113 
00114     virtual void clearCol(int j, bool force_synchro_if_double_accessible); // force_synchro is expensive!
00115 
00116     virtual void clearElem(int i, int j);
00117 
00118     virtual T get(int i, int j) const;
00119 
00120     virtual T operator()(int i, int j)const { return get(i, j); }
00121 
00122     virtual bool exists(int i, int j) const;
00123 
00124     virtual void set(int i, int j, T value);
00125 
00126     virtual void incr(int i, int j, T inc);
00127 
00128     //virtual void operator=(SMat<T> m);
00129 
00130     virtual map<int, T>& getRow(int i);
00131   
00132     virtual const map<int, T>& getCol(int j)const;
00133 
00134     virtual void addRow(map<int, T>& row);
00135 
00136     virtual void addCol(map<int, T>& col);
00137 
00138     virtual int size();
00139 
00140     virtual T sumRow(int i);
00141 
00142     virtual T sumCol(int j);
00143 
00144     virtual T* getAsCompressedVec();
00145 
00146     virtual void getAsMaxSizedCompressedVecs(int max_size, vector<pair<T*, int> >& vectors);
00147 
00148     virtual void addCompressedVec(T* compressed_vec, int n_elems);
00149 
00150     virtual void setCompressedVec(T* compressed_vec, int n_elems);
00151  
00152     virtual T sumOfElements();
00153 
00154     virtual int getHeight() const { return height; }
00155 
00156     virtual int getWidth() const { return width; }
00157 
00158     virtual void setDoubleAccessible(bool da);
00159 
00160     virtual bool isDoubleAccessible() { return double_access; }
00161 
00162     virtual void setMode(int new_mode);
00163 
00164     virtual int getMode() { return mode; }
00165 
00166     virtual void setName(string n) { name = n; }
00167   
00168     virtual string getName() { return name; }
00169 
00170     virtual void write(PStream& out) const;
00171 
00172     virtual void read(PStream& in);
00173 
00174     virtual T getNullElem() { return null_elem; }
00175 
00176     virtual string getClassName() const { return "DoubleAccesSparseMatrix"; }
00177 
00178 };
00179 
00180 template <class T> 
00181 inline PStream& operator<<(PStream &out, const DoubleAccessSparseMatrix<T> &m)
00182 { 
00183     m.write(out); 
00184     return out;
00185 }
00186 
00187 template <class T> 
00188 inline PStream& operator>>(PStream &in, DoubleAccessSparseMatrix<T> &m)
00189 { 
00190     m.read(in); 
00191     return in;
00192 }
00193 
00194 template <class T>
00195 class SMat : public PP<DoubleAccessSparseMatrix<T> > //: public PP<PPointableDoubleAccessSparseMatrix<T> >
00196 {
00197 
00198 public:
00199 
00200     SMat(int n_rows = 0, int n_cols = 0, string name = "<no-name>", int mode = ROW_WISE, bool double_access = false) : PP<DoubleAccessSparseMatrix<T> >(new DoubleAccessSparseMatrix<T>(n_rows, n_cols, name, mode, double_access)) {}
00201 
00202     SMat(DoubleAccessSparseMatrix<T>* p) : PP<DoubleAccessSparseMatrix<T> >(p) {}
00203 
00204     DoubleAccessSparseMatrix<T>* getPtr() { return ptr; }
00205 
00206 };
00207 
00208 } // end of namespace PLearn
00209 
00210 #include "DoubleAccessSparseMatrix_impl.h"
00211 
00212 #endif
00213 
00214 
00215 /*
00216   Local Variables:
00217   mode:c++
00218   c-basic-offset:4
00219   c-file-style:"stroustrup"
00220   c-file-offsets:((innamespace . 0)(inline-open . 0))
00221   indent-tabs-mode:nil
00222   fill-column:79
00223   End:
00224 */
00225 // 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