cubebase.h

Go to the documentation of this file.
00001 
00002 
00003 
00016 
00017 // Copyright (C) 1997-2010 by Pawel Pilarczyk.
00018 //
00019 // This file is part of the Homology Library.  This library is free software;
00020 // you can redistribute it and/or modify it under the terms of the GNU
00021 // General Public License as published by the Free Software Foundation;
00022 // either version 2 of the License, or (at your option) any later version.
00023 //
00024 // This library is distributed in the hope that it will be useful,
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 // GNU General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License along
00030 // with this software; see the file "license.txt".  If not, write to the
00031 // Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00032 // MA 02111-1307, USA.
00033 
00034 // Started in January 2002. Last revision: May 25, 2010.
00035 
00036 
00037 #ifndef _CHOMP_CUBES_CUBEBASE_H_
00038 #define _CHOMP_CUBES_CUBEBASE_H_
00039 
00040 #include "chomp/system/config.h"
00041 #include "chomp/system/textfile.h"
00042 #include "chomp/cubes/pointset.h"
00043 #include "chomp/struct/bitfield.h"
00044 #include "chomp/struct/integer.h"
00045 #include "chomp/struct/hashsets.h"
00046 #include "chomp/cubes/pointbas.h"
00047 #include "chomp/cubes/cubemain.h"
00048 
00049 #include <iostream>
00050 #include <fstream>
00051 #include <cstdlib>
00052 #include <cstring>
00053 
00054 namespace chomp {
00055 namespace homology {
00056 
00057 
00058 // a friend class template
00059 template <class coordtype>
00060 class tCellBase;
00061 
00062 
00063 // --------------------------------------------------
00064 // -------------- Cube with PointBase ---------------
00065 // --------------------------------------------------
00066 
00071 template <class coordtype>
00072 class tCubeBase
00073 {
00074 public:
00076         typedef coordtype CoordType;
00077 
00079         typedef tCellBase<coordtype> CellType;
00080 
00082         static const int MaxDim = MaxBasDim;
00083 
00085         typedef tPointBase<coordtype> PointBase;
00086 
00088         tCubeBase ();
00089         
00091         tCubeBase (const coordtype *c, int dim);
00092 
00095         tCubeBase (int_t number, int dim);
00096 
00098         tCubeBase (const tCubeBase<coordtype> &c);
00099 
00101         tCubeBase<coordtype> &operator =
00102                 (const tCubeBase<coordtype> &c);
00103 
00105         int dim () const;
00106 
00108 //      template <class intType>
00109 //      intType *coord (intType *c) const;
00110         coordtype *coord (coordtype *c) const;
00111 
00113         int_t hashkey1 () const;
00114 
00116         int_t hashkey2 () const;
00117 
00119         static const char *name ();
00120 
00122         static const char *pluralname ();
00123 
00124         // --- friends: ---
00125 
00127         friend inline int operator == (const tCubeBase<coordtype> &c1,
00128                 const tCubeBase<coordtype> &c2)
00129         {
00130                 return c1. n == c2. n;
00131         } /* operator == */
00132 
00133         // friend class: cubical cell
00134         friend class tCellBase<coordtype>;
00135 
00136 private:
00138         int_t num () const;
00139 
00143         int_t n;
00144 
00145 }; /* class tCubeBase */
00146 
00147 // --------------------------------------------------
00148 
00149 template <class coordtype>
00150 inline int_t tCubeBase<coordtype>::num () const
00151 {
00152         return (n & NumMask);
00153 } /* tCubeBase::num */
00154 
00155 // --------------------------------------------------
00156 
00157 template <class coordtype>
00158 inline tCubeBase<coordtype>::tCubeBase ()
00159 : n (0)
00160 {
00161         return;
00162 } /* tCubeBase::tCubeBase */
00163 
00164 template <class coordtype>
00165 inline tCubeBase<coordtype>::tCubeBase (const coordtype *c,
00166         int dim)
00167 {
00168         if (dim <= 0)
00169                 throw "Non-positive dimension of a cube.";
00170         if (dim >= MaxDim)
00171                 throw "Too high space dimension.";
00172         n = PointBase::number (c, dim);
00173         if (n < 0)
00174                 throw "Negative number of a cube.";
00175         n |= (static_cast<int_t> (dim) << NumBits);
00176         return;
00177 } /* tCubeBase::tCubeBase */
00178 
00179 template <class coordtype>
00180 inline tCubeBase<coordtype>::tCubeBase (int_t number, int dim)
00181 {
00182         n = number | (static_cast<int_t> (dim) << NumBits);
00183         return;
00184 } /* tCubeBase::tCubeBase */
00185 
00186 template <class coordtype>
00187 inline tCubeBase<coordtype>::tCubeBase (const tCubeBase<coordtype> &c)
00188 {
00189         n = c. n;
00190         return;
00191 } /* tCubeBase::tCubeBase */
00192 
00193 template <class coordtype>
00194 inline tCubeBase<coordtype> &tCubeBase<coordtype>::operator =
00195         (const tCubeBase<coordtype> &c)
00196 {
00197         n = c. n;
00198         return *this;
00199 } /* tCubeBase::operator = */
00200 
00201 template <class coordtype>
00202 inline int tCubeBase<coordtype>::dim () const
00203 {
00204         return static_cast<int> (n >> NumBits);
00205 } /* tCubeBase::dim */
00206 
00207 //template <class coordtype>
00208 //template <class intType>
00209 //inline intType *tCubeBase<coordtype>::coord (intType *c) const
00210 //{
00211 //      int d = dim ();
00212 //      const coordtype *tab = PointBase::coord (num (), d);
00213 //      for (int i = 0; i < d; ++ i)
00214 //              c [i] = static_cast<const intType> (tab [i]);
00215 //      return c;
00216 //} /* tCubeBase::coord */
00217 
00218 template <class coordtype>
00219 inline coordtype *tCubeBase<coordtype>::coord (coordtype *c) const
00220 {
00221         int d = dim ();
00222         const coordtype *tab = PointBase::coord (num (), d);
00223         for (int i = 0; i < d; ++ i)
00224                 c [i] = tab [i];
00225         return c;
00226 } /* tCubeBase::coord */
00227 
00228 template <class coordtype>
00229 inline int_t tCubeBase<coordtype>::hashkey1 () const
00230 {
00231         return static_cast<int_t>
00232                 (((n ^ 0x55555555u) << 17) ^ ((n ^ 0xAA00AA00u) << 7) ^
00233                 ((n ^ 0x00AA00AAu) >> 7));
00234 } /* tCubeBase::hashkey1 */
00235 
00236 template <class coordtype>
00237 inline int_t tCubeBase<coordtype>::hashkey2 () const
00238 {
00239         return static_cast<int_t>
00240                 (((n ^ 0xAAAAAAAAu) << 18) ^ ((n ^ 0x55005500u) >> 8) ^
00241                 ((n ^ 0x00550055u) << 10));
00242 } /* tCubeBase::hashkey2 */
00243 
00244 template <class coordtype>
00245 inline const char *tCubeBase<coordtype>::name ()
00246 {
00247         return "cube";
00248 } /* tCubeBase::name */
00249 
00250 template <class coordtype>
00251 inline const char *tCubeBase<coordtype>::pluralname ()
00252 {
00253         return "cubes";
00254 } /* tCubeBase::pluralname */
00255 
00256 // --------------------------------------------------
00257 
00259 template <class coordtype>
00260 inline int operator != (const tCubeBase<coordtype> &c1,
00261         const tCubeBase<coordtype> &c2)
00262 {
00263         return !(c1 == c2);
00264 } /* operator != */
00265 
00266 // --------------------------------------------------
00267 
00269 template <class coordtype>
00270 inline tCubeBase<coordtype> operator * (const tCubeBase<coordtype> &c1,
00271         const tCubeBase<coordtype> &c2)
00272 {
00273         int dim1 = c1. dim (), dim2 = c2. dim ();
00274         if (dim1 + dim2 >= tCubeBase<coordtype>::MaxDim)
00275                 throw "Dimension too high to concatenate coordinates.";
00276         coordtype coord [tCubeBase<coordtype>::MaxDim];
00277         c1. coord (coord);
00278         c2. coord (coord + dim1);
00279         return tCubeBase<coordtype> (coord, dim1 + dim2);
00280 } /* operator * */
00281 
00282 // --------------------------------------------------
00283 
00287 template <class coordtype>
00288 inline std::ostream &operator << (std::ostream &out,
00289         const tCubeBase<coordtype> &c)
00290 {
00291         return WriteCube (out, c);
00292 } /* operator << */
00293 
00297 template <class coordtype>
00298 inline std::istream &operator >> (std::istream &in, tCubeBase<coordtype> &c)
00299 {
00300         return ReadCube (in, c);
00301 } /* operator >> */
00302 
00304 template <class coordtype>
00305 inline std::istream &operator >> (std::istream &in,
00306         hashedset<tCubeBase<coordtype> > &s)
00307 {
00308         return ReadCubes (in, s);
00309 } /* operator >> */
00310 
00312 template <class coordtype>
00313 inline std::istream &operator >> (std::istream &in,
00314         mvmap<tCubeBase<coordtype>,tCubeBase<coordtype> > &m)
00315 {
00316         return ReadCubicalMap (in, m);
00317 } /* operator >> */
00318 
00319 
00320 } // namespace homology
00321 } // namespace chomp
00322 
00323 #endif // _CHOMP_CUBES_CUBEBASE_H_
00324 
00326