cubevar.h

Go to the documentation of this file.
00001 
00002 
00003 
00015 
00016 // Copyright (C) 1997-2010 by Pawel Pilarczyk.
00017 //
00018 // This file is part of the Homology Library.  This library is free software;
00019 // you can redistribute it and/or modify it under the terms of the GNU
00020 // General Public License as published by the Free Software Foundation;
00021 // either version 2 of the License, or (at your option) any later version.
00022 //
00023 // This library is distributed in the hope that it will be useful,
00024 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026 // GNU General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License along
00029 // with this software; see the file "license.txt".  If not, write to the
00030 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00031 // MA 02111-1307, USA.
00032 
00033 // Started in January 2002. Last revision: June 1, 2007.
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 // a friend class template
00059 template <class coordtype>
00060 class tCellVar;
00061 
00062 
00063 // --------------------------------------------------
00064 // ----------- Cube with allocated memory -----------
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 //      template <class intType>
00110 //      intType *coord (intType *c) const;
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         // --- friends: ---
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         } /* operator == */
00137 
00138         // friend class: cubical cell
00139         friend class tCellVar<coordtype>;
00140 
00141 private:
00144         coordtype *tab;
00145 
00148         coordtype *initialize (int dim);
00149 
00150 }; /* class tCubeVar */
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 } /* tCubeVar::initialize */
00163 
00164 // --------------------------------------------------
00165 
00166 template <class coordtype>
00167 inline tCubeVar<coordtype>::tCubeVar ()
00168 {
00169         tab = NULL;
00170         return;
00171 } /* tCubeVar::tCubeVar */
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 } /* tCubeVar::tCubeVar */
00184 
00185 template <class coordtype>
00186 inline tCubeVar<coordtype>::tCubeVar (int, int)
00187 {
00188         throw "Unable to construct a cube from a number.";
00189 } /* tCubeVar::tCubeVar */
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 } /* tCubeVar::tCubeVar */
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 } /* tCubeVar::operator = */
00224 
00225 template <class coordtype>
00226 inline tCubeVar<coordtype>::~tCubeVar<coordtype> ()
00227 {
00228         if (tab)
00229                 delete [] tab;
00230         return;
00231 } /* tCubeVar::~tCubeVar */
00232 
00233 template <class coordtype>
00234 inline int tCubeVar<coordtype>::dim () const
00235 {
00236         return tab ? *tab : 0;
00237 } /* tCubeVar::dim */
00238 
00239 //template <class coordtype>
00240 //template <class intType>
00241 //inline intType *tCubeVar<coordtype>::coord (intType *c) const
00242 //{
00243 //      if (!tab)
00244 //              return NULL;
00245 //      for (int i = 0; i < *tab; ++ i)
00246 //              c [i] = static_cast<const intType> (tab [i + 1]);
00247 //      return c;
00248 //} /* tCubeVar::coord */
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 } /* tCubeVar::coord */
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 } /* tCubeVar::hashkey1 */
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 } /* tCubeVar::hashkey2 */
00299 
00300 template <class coordtype>
00301 inline const char *tCubeVar<coordtype>::name ()
00302 {
00303         return "cube";
00304 } /* tCubeVar::name */
00305 
00306 template <class coordtype>
00307 inline const char *tCubeVar<coordtype>::pluralname ()
00308 {
00309         return "cubes";
00310 } /* tCubeVar::pluralname */
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 } /* operator != */
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 } /* operator * */
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 } /* operator << */
00349 
00353 template <class coordtype>
00354 inline std::istream &operator >> (std::istream &in, tCubeVar<coordtype> &c)
00355 {
00356         return ReadCube (in, c);
00357 } /* operator >> */
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 } /* operator >> */
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 } /* operator >> */
00374 
00375 
00376 } // namespace homology
00377 } // namespace chomp
00378 
00379 #endif // _CHOMP_CUBES_CUBEVAR_H_
00380 
00382