cubefix.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: June 1, 2007.
00035 
00036 
00037 #ifndef _CHOMP_CUBES_CUBEFIX_H_
00038 #define _CHOMP_CUBES_CUBEFIX_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 <int dimfix, class coordtype>
00060 class tCellFix;
00061 
00062 
00063 // --------------------------------------------------
00064 // ------------ Cube of fixed dimension -------------
00065 // --------------------------------------------------
00066 
00071 template <int dimfix, class coordtype>
00072 class tCubeFix
00073 {
00074 public:
00076         typedef coordtype CoordType;
00077 
00079         typedef tCellFix<dimfix,coordtype> CellType;
00080 
00082         static const int MaxDim = 512;
00083 
00085         typedef tWrapBase<coordtype> PointBase;
00086 
00088         tCubeFix ();
00089         
00091         tCubeFix (const coordtype *c, int dim = 0);
00092 
00094         tCubeFix (int number, int dim);
00095 
00097         tCubeFix (const tCubeFix<dimfix,coordtype> &c);
00098 
00100         tCubeFix<dimfix,coordtype> &operator =
00101                 (const tCubeFix<dimfix,coordtype> &c);
00102 
00104         int dim () const;
00105 
00107 //      template <class intType>
00108 //      intType *coord (intType *c) const;
00109         coordtype *coord (coordtype *c) const;
00110 
00112         int_t hashkey1 () const;
00113 
00115         int_t hashkey2 () const;
00116 
00118         static const char *name ();
00119 
00121         static const char *pluralname ();
00122 
00124         friend int operator == (const tCubeFix<dimfix,coordtype> &c1,
00125                 const tCubeFix<dimfix,coordtype> &c2)
00126         {
00127                 return thesame (c1. tab, c2. tab, dimfix);
00128         }
00129 
00130         // friend class: cubical cell
00131         friend class tCellFix<dimfix, coordtype>;
00132 
00133 private:
00135         coordtype tab [dimfix];
00136 
00137 }; /* class tCubeFix */
00138 
00139 // --------------------------------------------------
00140 
00141 template <int dimfix, class coordtype>
00142 inline tCubeFix<dimfix,coordtype>::tCubeFix ()
00143 {
00144         return;
00145 } /* tCubeFix::tCubeFix */
00146 
00147 template <int dimfix, class coordtype>
00148 inline tCubeFix<dimfix,coordtype>::tCubeFix
00149         (const coordtype *c, int dim)
00150 {
00151         if (dim && ((dim != dimfix) || (dim < 0)))
00152                 throw "Wrong dimension of a fixed-dim cube.";
00153         PointBase::wrapcopy (tab, c, dimfix);
00154         return;
00155 } /* tCubeFix::tCubeFix */
00156 
00157 template <int dimfix, class coordtype>
00158 inline tCubeFix<dimfix,coordtype>::tCubeFix (int, int)
00159 {
00160         throw "Unable to construct a cube from a number.";
00161 } /* tCubeFix::tCubeFix */
00162 
00163 template <int dimfix, class coordtype>
00164 inline tCubeFix<dimfix,coordtype>::tCubeFix
00165         (const tCubeFix<dimfix,coordtype> &c)
00166 {
00167         for (int i = 0; i < dimfix; ++ i)
00168                 tab [i] = c. tab [i];
00169         return;
00170 } /* tCubeFix::tCubeFix */
00171 
00172 template <int dimfix, class coordtype>
00173 inline tCubeFix<dimfix,coordtype> &tCubeFix<dimfix,coordtype>::operator =
00174         (const tCubeFix<dimfix,coordtype> &c)
00175 {
00176         for (int i = 0; i < dimfix; ++ i)
00177                 tab [i] = c. tab [i];
00178         return *this;
00179 } /* tCubeFix::operator = */
00180 
00181 template <int dimfix, class coordtype>
00182 inline int tCubeFix<dimfix,coordtype>::dim () const
00183 {
00184         return dimfix;
00185 } /* tCubeFix::dim */
00186 
00187 //template <int dimfix, class coordtype>
00188 //template <class intType>
00189 //inline intType *tCubeFix<dimfix,coordtype>::coord (intType *c) const
00190 //{
00191 //      for (int i = 0; i < dimfix; ++ i)
00192 //              c [i] = static_cast<const intType> (tab [i]);
00193 //      return c;
00194 //} /* tCubeFix::coord */
00195 
00196 template <int dimfix, class coordtype>
00197 inline coordtype *tCubeFix<dimfix,coordtype>::coord (coordtype *c) const
00198 {
00199         for (int i = 0; i < dimfix; ++ i)
00200                 c [i] = tab [i];
00201         return c;
00202 } /* tCubeFix::coord */
00203 
00204 template <int dimfix, class coordtype>
00205 inline int_t tCubeFix<dimfix,coordtype>::hashkey1 () const
00206 {
00207         switch (dimfix)
00208         {
00209         case 1:
00210                 return static_cast<int_t> (tab [0]) << 12;
00211         case 2:
00212                 return ((static_cast<int_t> (tab [0])) << 18) +
00213                         ((static_cast<int_t> (tab [1])) << 6);
00214         default:
00215                 return ((static_cast<int_t> (tab [0])) << 18) +
00216                         ((static_cast<int_t> (tab [1])) << 6) +
00217                         ((static_cast<int_t> (tab [2])) >> 6);
00218         }
00219 } /* tCubeFix::hashkey1 */
00220 
00221 template <int dimfix, class coordtype>
00222 inline int_t tCubeFix<dimfix,coordtype>::hashkey2 () const
00223 {
00224         switch (dimfix)
00225         {
00226         case 1:
00227                 return static_cast<int_t> (tab [0]) << 3;
00228         case 2:
00229                 return (static_cast<int_t> (tab [0]) >> 1) +
00230                         (static_cast<int_t> (tab [1]) << 13);
00231         default:
00232                 return ((static_cast<int_t> (tab [dimfix - 1])) << 20) +
00233                         ((static_cast<int_t> (tab [dimfix - 2])) << 9) +
00234                         ((static_cast<int_t> (tab [dimfix - 3])) >> 1);
00235         }
00236 } /* tCubeFix::hashkey2 */
00237 
00238 template <int dimfix, class coordtype>
00239 const char *tCubeFix<dimfix,coordtype>::name ()
00240 {
00241         return "cube";
00242 } /* tCubeFix::name */
00243 
00244 template <int dimfix, class coordtype>
00245 const char *tCubeFix<dimfix,coordtype>::pluralname ()
00246 {
00247         return "cubes";
00248 } /* tCubeFix::pluralname */
00249 
00251 template <int dim1, int dim2, class coordtype>
00252 inline int operator != (const tCubeFix<dim1,coordtype> &c1,
00253         const tCubeFix<dim2,coordtype> &c2)
00254 {
00255         return !(c1 == c2);
00256 } /* operator != */
00257 
00258 // --------------------------------------------------
00259 
00261 template <int dim1, int dim2, class coordtype>
00262 inline tCubeFix<dim1+dim2,coordtype> operator *
00263         (const tCubeFix<dim1,coordtype> &c1,
00264         const tCubeFix<dim2,coordtype> &c2)
00265 {
00266         coordtype coord [dim1 + dim2];
00267         c1. coord (coord);
00268         c2. coord (coord + dim1);
00269         return tCubeFix<dim1+dim2,coordtype> (coord);
00270 } /* operator * */
00271 
00272 // --------------------------------------------------
00273 
00277 template <int dimfix, class coordtype>
00278 inline std::ostream &operator << (std::ostream &out,
00279         const tCubeFix<dimfix,coordtype> &c)
00280 {
00281         return WriteCube (out, c);
00282 } /* operator << */
00283 
00287 template <int dimfix, class coordtype>
00288 inline std::istream &operator >> (std::istream &in,
00289         tCubeFix<dimfix,coordtype> &c)
00290 {
00291         return ReadCubeFix (in, c, dimfix);
00292 } /* operator >> */
00293 
00295 template <int dimfix, class coordtype>
00296 inline std::istream &operator >> (std::istream &in,
00297         hashedset<tCubeFix<dimfix,coordtype> > &s)
00298 {
00299         return ReadCubes (in, s);
00300 } /* operator >> */
00301 
00303 template <int dimfix, class coordtype>
00304 inline std::istream &operator >> (std::istream &in,
00305         mvmap<tCubeFix<dimfix,coordtype>,tCubeFix<dimfix,coordtype> > &m)
00306 {
00307         return ReadCubicalMap (in, m);
00308 } /* operator >> */
00309 
00310 
00311 } // namespace homology
00312 } // namespace chomp
00313 
00314 #endif // _CHOMP_CUBES_CUBEFIX_H_
00315 
00317