Go to the documentation of this file.00001
00002
00003
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef _CHOMP_CUBES_CUBEVAR_H_
00037 #define _CHOMP_CUBES_CUBEVAR_H_
00038
00039 #include "chomp/system/config.h"
00040 #include "chomp/system/textfile.h"
00041 #include "chomp/cubes/pointset.h"
00042 #include "chomp/struct/bitfield.h"
00043 #include "chomp/struct/integer.h"
00044 #include "chomp/struct/hashsets.h"
00045 #include "chomp/cubes/pointbas.h"
00046 #include "chomp/cubes/cubemain.h"
00047
00048 #include <iostream>
00049 #include <fstream>
00050 #include <cstdlib>
00051 #include <cstring>
00052
00053
00054 namespace chomp {
00055 namespace homology {
00056
00057
00058
00059 template <class coordtype>
00060 class tCellVar;
00061
00062
00063
00064
00065
00066
00071 template <class coordtype>
00072 class tCubeVar
00073 {
00074 public:
00076 typedef coordtype CoordType;
00077
00079 typedef tCellVar<coordtype> CellType;
00080
00082 static const int MaxDim = 50;
00083
00085 typedef tWrapBase<coordtype> PointBase;
00086
00088 tCubeVar ();
00089
00091 tCubeVar (const coordtype *c, int dim);
00092
00094 tCubeVar (int number, int dim);
00095
00097 tCubeVar (const tCubeVar<coordtype> &c);
00098
00100 tCubeVar<coordtype> &operator = (const tCubeVar<coordtype> &c);
00101
00103 ~tCubeVar ();
00104
00106 int dim () const;
00107
00109
00110
00111 coordtype *coord (coordtype *c) const;
00112
00114 int_t hashkey1 () const;
00115
00117 int_t hashkey2 () const;
00118
00120 static const char *name ();
00121
00123 static const char *pluralname ();
00124
00125
00126
00128 friend inline int operator == (const tCubeVar<coordtype> &c1,
00129 const tCubeVar<coordtype> &c2)
00130 {
00131 if (!c1. tab)
00132 return c2. tab ? false : true;
00133 if (!c2. tab)
00134 return false;
00135 return thesame (c1. tab, c2. tab, c1. tab [0] + 1);
00136 }
00137
00138
00139 friend class tCellVar<coordtype>;
00140
00141 private:
00144 coordtype *tab;
00145
00148 coordtype *initialize (int dim);
00149
00150 };
00151
00152
00153
00154 template <class coordtype>
00155 inline coordtype *tCubeVar<coordtype>::initialize (int d)
00156 {
00157 tab = new coordtype [d + 1];
00158 if (!tab)
00159 throw "Not enough memory for a cube.";
00160 *tab = d;
00161 return (tab + 1);
00162 }
00163
00164
00165
00166 template <class coordtype>
00167 inline tCubeVar<coordtype>::tCubeVar ()
00168 {
00169 tab = NULL;
00170 return;
00171 }
00172
00173 template <class coordtype>
00174 inline tCubeVar<coordtype>::tCubeVar (const coordtype *c, int d)
00175 {
00176 if (d < 0)
00177 throw "Negative dimension of a cube.";
00178 if (d)
00179 PointBase::wrapcopy (initialize (d), c, d);
00180 else
00181 tab = NULL;
00182 return;
00183 }
00184
00185 template <class coordtype>
00186 inline tCubeVar<coordtype>::tCubeVar (int, int)
00187 {
00188 throw "Unable to construct a cube from a number.";
00189 }
00190
00191 template <class coordtype>
00192 inline tCubeVar<coordtype>::tCubeVar (const tCubeVar<coordtype> &c)
00193 {
00194 if (!c. dim ())
00195 tab = NULL;
00196 else
00197 {
00198 initialize (c. dim ());
00199 copycoord (tab + 1, c. tab + 1, c. dim ());
00200 }
00201 return;
00202 }
00203
00204 template <class coordtype>
00205 inline tCubeVar<coordtype> &tCubeVar<coordtype>::operator =
00206 (const tCubeVar<coordtype> &c)
00207 {
00208 if (dim () == c. dim ())
00209 copycoord (tab + 1, c. tab + 1, dim ());
00210 else
00211 {
00212 if (tab)
00213 delete [] tab;
00214 if (c. dim ())
00215 {
00216 initialize (c. dim ());
00217 copycoord (tab + 1, c. tab + 1, c. dim ());
00218 }
00219 else
00220 tab = NULL;
00221 }
00222 return *this;
00223 }
00224
00225 template <class coordtype>
00226 inline tCubeVar<coordtype>::~tCubeVar<coordtype> ()
00227 {
00228 if (tab)
00229 delete [] tab;
00230 return;
00231 }
00232
00233 template <class coordtype>
00234 inline int tCubeVar<coordtype>::dim () const
00235 {
00236 return tab ? *tab : 0;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 template <class coordtype>
00251 inline coordtype *tCubeVar<coordtype>::coord (coordtype *c) const
00252 {
00253 if (!tab)
00254 return 0;
00255 for (int i = 0; i < *tab; ++ i)
00256 c [i] = tab [i + 1];
00257 return c;
00258 }
00259
00260 template <class coordtype>
00261 inline int_t tCubeVar<coordtype>::hashkey1 () const
00262 {
00263 int d = dim ();
00264 switch (d)
00265 {
00266 case 0:
00267 return 0;
00268 case 1:
00269 return static_cast<int_t> (tab [0]) << 12;
00270 case 2:
00271 return ((static_cast<int_t> (tab [0])) << 18) +
00272 ((static_cast<int_t> (tab [1])) << 6);
00273 default:
00274 return ((static_cast<int_t> (tab [0])) << 18) +
00275 ((static_cast<int_t> (tab [1])) << 6) +
00276 ((static_cast<int_t> (tab [2])) >> 6);
00277 }
00278 }
00279
00280 template <class coordtype>
00281 inline int_t tCubeVar<coordtype>::hashkey2 () const
00282 {
00283 int d = dim ();
00284 switch (d)
00285 {
00286 case 0:
00287 return 1;
00288 case 1:
00289 return static_cast<int_t> (tab [0]) << 3;
00290 case 2:
00291 return (static_cast<int_t> (tab [0]) >> 1) +
00292 (static_cast<int_t> (tab [1]) << 13);
00293 default:
00294 return ((static_cast<int_t> (tab [d - 1])) << 20) +
00295 ((static_cast<int_t> (tab [d - 2])) << 9) +
00296 ((static_cast<int_t> (tab [d - 3])) >> 1);
00297 }
00298 }
00299
00300 template <class coordtype>
00301 inline const char *tCubeVar<coordtype>::name ()
00302 {
00303 return "cube";
00304 }
00305
00306 template <class coordtype>
00307 inline const char *tCubeVar<coordtype>::pluralname ()
00308 {
00309 return "cubes";
00310 }
00311
00312
00313
00315 template <class coordtype>
00316 inline int operator != (const tCubeVar<coordtype> &c1,
00317 const tCubeVar<coordtype> &c2)
00318 {
00319 return !(c1 == c2);
00320 }
00321
00322
00323
00325 template <class coordtype>
00326 inline tCubeVar<coordtype> operator * (const tCubeVar<coordtype> &c1,
00327 const tCubeVar<coordtype> &c2)
00328 {
00329 int dim1 = c1. dim (), dim2 = c2. dim ();
00330 if (dim1 + dim2 >= tCubeVar<coordtype>::MaxDim)
00331 throw "Dimension too high to concatenate coordinates.";
00332 coordtype coord [tCubeVar<coordtype>::MaxDim];
00333 c1. coord (coord);
00334 c2. coord (coord + dim1);
00335 return tCubeVar<coordtype> (coord, dim1 + dim2);
00336 }
00337
00338
00339
00343 template <class coordtype>
00344 inline std::ostream &operator << (std::ostream &out,
00345 const tCubeVar<coordtype> &c)
00346 {
00347 return WriteCube (out, c);
00348 }
00349
00353 template <class coordtype>
00354 inline std::istream &operator >> (std::istream &in, tCubeVar<coordtype> &c)
00355 {
00356 return ReadCube (in, c);
00357 }
00358
00360 template <class coordtype>
00361 inline std::istream &operator >> (std::istream &in,
00362 hashedset<tCubeVar<coordtype> > &s)
00363 {
00364 return ReadCubes (in, s);
00365 }
00366
00368 template <class coordtype>
00369 inline std::istream &operator >> (std::istream &in,
00370 mvmap<tCubeVar<coordtype>,tCubeVar<coordtype> > &m)
00371 {
00372 return ReadCubicalMap (in, m);
00373 }
00374
00375
00376 }
00377 }
00378
00379 #endif // _CHOMP_CUBES_CUBEVAR_H_
00380
00382