COOPY » Guide
version 0.6.5
|
00001 #ifndef COOPY_TYPEDSHEET 00002 #define COOPY_TYPEDSHEET 00003 00004 #include <coopy/DataSheet.h> 00005 00006 #include <vector> 00007 #include <string> 00008 00009 #include <stdio.h> 00010 00011 namespace coopy { 00012 namespace store { 00013 template <class T> class TypedSheet; 00014 template <class T> class EscapedTypedSheet; 00015 } 00016 } 00017 00018 template <class T> 00019 class coopy::store::TypedSheet : public DataSheet { 00020 public: 00021 using DataSheet::insertRow; 00022 std::vector<std::vector<T> > arr; 00023 int h, w; 00024 T zero; 00025 00026 TypedSheet() { 00027 h = w = 0; 00028 } 00029 00030 void clear() { 00031 arr.clear(); 00032 h = w = 0; 00033 } 00034 00035 virtual bool resize(int w, int h) { 00036 resize(w,h,zero); 00037 return true; 00038 } 00039 00040 void resize(int w, int h, const T& zero) { 00041 arr.clear(); 00042 for (int i=0; i<h; i++) { 00043 arr.push_back(std::vector<T>()); 00044 std::vector<T>& lst = arr.back(); 00045 for (int j=0; j<w; j++) { 00046 lst.push_back(zero); 00047 } 00048 } 00049 this->h = h; 00050 this->w = w; 00051 this->zero = zero; 00052 } 00053 00054 void nonDestructiveResize(int w, int h, const T& zero) { 00055 this->zero = zero; 00056 if (this->h==h && this->w==w) return; 00057 00058 this->h = h; 00059 this->w = w; 00060 while (arr.size()<h) { 00061 arr.push_back(std::vector<T>()); 00062 } 00063 for (int i=0; i<h; i++) { 00064 std::vector<T>& lst = arr[i]; 00065 while (lst.size()<w) { 00066 lst.push_back(zero); 00067 } 00068 } 00069 } 00070 00071 int width() const { 00072 return w; 00073 } 00074 00075 int height() const { 00076 return h; 00077 } 00078 00079 T& cell(int x, int y) { 00080 return arr[y][x]; 00081 } 00082 00083 const T& cell(int x, int y) const { 00084 return arr[y][x]; 00085 } 00086 00087 // still need to define how T is serialized 00088 virtual std::string cellString(int x, int y) const { 00089 return "IMPLEMENT SERIALIZATION"; 00090 } 00091 00092 virtual bool deleteColumn(const ColumnRef& column) { 00093 int offset = column.getIndex(); 00094 if (offset<0) return false; 00095 if (offset>=w) return false; 00096 for (int i=0; i<(int)arr.size(); i++) { 00097 arr[i].erase(arr[i].begin()+offset); 00098 } 00099 w--; 00100 return true; 00101 } 00102 00103 virtual ColumnRef insertColumn(const ColumnRef& base) { 00104 int offset = base.getIndex(); 00105 if (offset>=w) return ColumnRef(); 00106 for (int i=0; i<(int)arr.size(); i++) { 00107 if (offset>=0) { 00108 arr[i].insert(arr[i].begin()+offset,zero); 00109 } else { 00110 arr[i].push_back(zero); 00111 } 00112 } 00113 w++; 00114 return ColumnRef((offset>=0)?offset:ColumnRef(w-1)); 00115 } 00116 00117 virtual ColumnRef insertColumn(const ColumnRef& base, 00118 const ColumnInfo& info) { 00119 return insertColumn(base); 00120 } 00121 00122 virtual bool modifyColumn(const ColumnRef& base, 00123 const ColumnInfo& info) { 00124 return true; 00125 } 00126 00127 00128 virtual ColumnRef moveColumn(const ColumnRef& src, const ColumnRef& base) { 00129 int offset = base.getIndex(); 00130 if (offset>=(int)w) return ColumnRef(); 00131 int offset_src = src.getIndex(); 00132 int offset_del = offset_src; 00133 if (offset_del<0||offset_del>=w) return ColumnRef(); 00134 int final = offset; 00135 if (offset<=offset_del&&offset!=-1) { 00136 offset_del++; 00137 } else { 00138 final--; 00139 } 00140 //printf("Move col: insert %d from %d offset_del %d final %d\n", 00141 //offset, offset_src, offset_del, final); 00142 for (int i=0; i<(int)arr.size(); i++) { 00143 std::vector<T>& row = arr[i]; 00144 if (offset>=0) { 00145 row.insert(row.begin()+offset,row[offset_src]); 00146 } else { 00147 row.push_back(row[offset_src]); 00148 } 00149 row.erase(row.begin()+offset_del); 00150 } 00151 if (offset<0) { 00152 return ColumnRef(w-1); 00153 } 00154 return ColumnRef(final); 00155 } 00156 00157 virtual bool deleteRow(const RowRef& src) { 00158 int offset = src.getIndex(); 00159 if (offset<0||offset>=h) return false; 00160 arr.erase(arr.begin()+offset); 00161 h--; 00162 return true; 00163 } 00164 00165 virtual RowRef insertRow(const RowRef& base) { 00166 int offset = base.getIndex(); 00167 if (offset>=h) return RowRef(); 00168 if (offset<0) { 00169 arr.push_back(std::vector<T>()); 00170 offset = h; 00171 } else { 00172 arr.insert(arr.begin()+offset,std::vector<T>()); 00173 } 00174 h++; 00175 for (int i=0; i<w; i++) { 00176 arr[offset].push_back(zero); 00177 } 00178 return RowRef(offset); 00179 } 00180 00181 // move a row before base; if base is invalid move after all rows 00182 virtual RowRef moveRow(const RowRef& src, const RowRef& base) { 00183 int offset1 = src.getIndex(); 00184 if (offset1>=h) return RowRef(); 00185 int offset2 = base.getIndex(); 00186 if (offset2>=h) return RowRef(); 00187 //printf("Moving %d to %d\n", offset1, offset2); 00188 if (offset2!=-1) { 00189 int orig = offset1+((offset2<offset1)?1:0); 00190 arr.insert(arr.begin()+offset2, arr[offset1]); 00191 arr.erase(arr.begin()+orig); 00192 if (offset2>offset1) offset2--; 00193 } else { 00194 arr.push_back(arr[offset1]); 00195 arr.erase(arr.begin()+offset1); 00196 offset2 = (int)arr.size()-1; 00197 } 00198 //printf("Move resulted in %d\n", offset2); 00199 return RowRef(offset2); 00200 } 00201 00202 /* 00203 virtual Poly<SheetRow> insertRow() { 00204 Poly<SheetRow> pRow = DataSheet::insertRow(); 00205 arr.push_back(std::vector<T>()); 00206 std::vector<T>& lst = arr.back(); 00207 for (int j=0; j<w; j++) { 00208 lst.push_back(zero); 00209 } 00210 h++; 00211 return pRow; 00212 } 00213 */ 00214 00215 virtual bool applySchema(const SheetSchema& ss) { 00216 arr.clear(); 00217 h = 0; 00218 w = ss.getColumnCount(); 00219 return true; 00220 } 00221 00222 virtual std::string getDescription() const { 00223 return "typed"; 00224 } 00225 }; 00226 00227 00228 00229 template <class T> 00230 class coopy::store::EscapedTypedSheet : public DataSheet { 00231 public: 00232 using DataSheet::insertRow; 00233 typedef std::pair<T,bool> pairCellType; 00234 typedef T cellType; 00235 00236 TypedSheet<std::pair<T,bool> > s; 00237 //std::vector<std::vector<pairCellType> >& arr; 00238 //int& h; 00239 //int& w; 00240 00241 //EscapedTypedSheet() : arr(s.arr), h(s.h), w(s.w) { 00242 EscapedTypedSheet() { 00243 } 00244 00245 void clear() { 00246 s.clear(); 00247 } 00248 00249 void resize(int w, int h, const T& zero) { 00250 std::pair<T,bool> pzero(zero,true); 00251 s.resize(w,h,pzero); 00252 } 00253 00254 int width() const { 00255 return s.width(); 00256 } 00257 00258 int height() const { 00259 return s.height(); 00260 } 00261 00262 T& cell(int x, int y) { 00263 return s.cell(x,y).first; 00264 } 00265 00266 const T& cell(int x, int y) const { 00267 return s.cell(x,y).first; 00268 } 00269 00270 std::pair<T,bool>& pcell(int x, int y) { 00271 return s.cell(x,y); 00272 } 00273 00274 const std::pair<T,bool>& pcell(int x, int y) const { 00275 return s.cell(x,y); 00276 } 00277 00278 // still need to define how T is serialized 00279 //virtual std::string serializeCell(const T& cell) const = 0; 00280 00281 virtual std::string cellString(int x, int y) const = 0; 00282 //return serializeCell(s.cell(x,y).first); 00283 //} 00284 00285 virtual std::string cellString(int x, int y, bool& escaped) const = 0; 00286 00287 00288 virtual bool cellString(int x, int y, const std::string& str, 00289 bool escaped) = 0; 00290 00291 00292 /* 00293 const std::pair<T,bool>& p = s.cell(x,y); 00294 escaped = p.second; 00295 return serializeCell(p.first); 00296 } 00297 */ 00298 00299 virtual bool deleteColumn(const ColumnRef& column) { 00300 return s.deleteColumn(column); 00301 } 00302 00303 virtual ColumnRef insertColumn(const ColumnRef& base) { 00304 return s.insertColumn(base); 00305 } 00306 00307 virtual ColumnRef insertColumn(const ColumnRef& base, 00308 const ColumnInfo& info) { 00309 return s.insertColumn(base,info); 00310 } 00311 00312 virtual bool modifyColumn(const ColumnRef& base, 00313 const ColumnInfo& info) { 00314 return s.modifyColumn(base,info); 00315 } 00316 00317 virtual ColumnRef moveColumn(const ColumnRef& src, const ColumnRef& base) { 00318 return s.moveColumn(src,base); 00319 } 00320 00321 virtual bool deleteRow(const RowRef& src) { 00322 return s.deleteRow(src); 00323 } 00324 00325 virtual RowRef insertRow(const RowRef& base) { 00326 return s.insertRow(base); 00327 } 00328 00329 // move a row before base; if base is invalid move after all rows 00330 virtual RowRef moveRow(const RowRef& src, const RowRef& base) { 00331 return s.moveRow(src,base); 00332 } 00333 00334 virtual bool applySchema(const SheetSchema& ss) { 00335 return s.applySchema(ss); 00336 } 00337 00338 virtual std::string getDescription() const { 00339 return "escaped"; 00340 } 00341 }; 00342 00343 00344 #endif