PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // CompactFileVMatrix.h 00004 // 00005 // Copyright (C) 2007 Olivier Breuleux 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 00035 00038 #ifndef CompactFileVMatrix_INC 00039 #define CompactFileVMatrix_INC 00040 00041 #include "RowBufferedVMatrix.h" 00042 #include <plearn/db/getDataSet.h> 00043 #include <plearn/vmat/VMat.h> 00044 00045 // While under development, we use this define to control 00046 // whether to use the NSPR 64 bit file access or the old std C FILE* 00047 #define USE_NSPR_FILE 00048 struct PRFileDesc; 00049 00050 #define COMPACTFILEVMATRIX_HEADERLENGTH_MAX 1024 00051 00052 namespace PLearn { 00053 using namespace std; 00054 00055 00059 struct GroupInfo { 00060 GroupInfo(): 00061 type('\0'), 00062 length(-1), 00063 max(-1), 00064 bits_per_value(-1), 00065 active(false), 00066 compact_length(-1) 00067 {} 00068 00069 GroupInfo(char type_, int length_, int max_, int bits_per_value_): 00070 type(type_), 00071 length(length_), 00072 max(max_), 00073 bits_per_value(bits_per_value_), 00074 active(false), 00075 compact_length(-1) 00076 {} 00077 00078 char type; 00079 int length; 00080 int max; 00081 00082 int bits_per_value; 00083 bool active; 00084 int compact_length; 00085 }; 00086 00087 00090 class CompactFileVMatrix: public RowBufferedVMatrix 00091 { 00092 00093 private: 00094 typedef RowBufferedVMatrix inherited; 00095 static VMatrixExtensionRegistrar* extension_registrar; 00096 00097 protected: 00098 PPath filename_; 00099 Vec active_list_; 00100 bool in_ram_; 00101 00102 00103 #ifdef USE_NSPR_FILE 00104 PRFileDesc* f; 00105 #else 00106 FILE* f; 00107 #endif 00108 00109 int header_length; 00110 int compact_width_; 00111 TVec<GroupInfo> info; 00112 TVec<unsigned char> cache_index; 00113 mutable string cache; 00114 00115 public: 00116 00117 CompactFileVMatrix(); 00118 CompactFileVMatrix(const PPath& filename); 00119 00120 protected: 00121 00122 static void declareOptions(OptionList & ol); 00123 virtual void getNewRow(int i, const Vec& v) const; 00124 00126 virtual void openCurrentFile(); 00128 virtual void closeCurrentFile(); 00129 00130 public: 00131 00133 virtual void updateHeader(); 00134 00136 virtual int nGroups() const; 00137 00139 virtual char groupEncoding(int group) const; 00140 00142 virtual int groupNFields(int group) const; 00143 00145 virtual int groupNValues(int group) const; 00146 00148 virtual int groupLength(int group) const; 00149 00150 virtual void put(int i, int j, real value); 00151 virtual void putSubRow(int i, int j, Vec v); 00152 virtual void appendRow(Vec v); 00153 virtual void flush(); 00154 00155 virtual void build(); 00156 00157 static VMat instantiateFromPPath(const PPath& filename) 00158 { 00159 return VMat(new CompactFileVMatrix(filename)); 00160 } 00161 00162 PLEARN_DECLARE_OBJECT(CompactFileVMatrix); 00163 00165 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00166 00168 virtual ~CompactFileVMatrix(); 00169 00170 private: 00171 00172 void build_(); 00173 GroupInfo& getGroup(int group) const; 00174 00175 // seek to element i,j in file 00176 void moveto(int i, int j=0) const; 00177 void cleanup(); 00178 00179 }; 00180 00181 DECLARE_OBJECT_PTR(CompactFileVMatrix); 00182 00183 } // end of namespace PLearn 00184 #endif 00185 00186 00187 /* 00188 Local Variables: 00189 mode:c++ 00190 c-basic-offset:4 00191 c-file-style:"stroustrup" 00192 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00193 indent-tabs-mode:nil 00194 fill-column:79 00195 End: 00196 */ 00197 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :