The Original CHomP Software
cubevar.h
Go to the documentation of this file.
1
3
15
16// Copyright (C) 1997-2020 by Pawel Pilarczyk.
17//
18// This file is part of my research software package. This is free software:
19// you can redistribute it and/or modify it under the terms of the GNU
20// General Public License as published by the Free Software Foundation,
21// either version 3 of the License, or (at your option) any later version.
22//
23// This software is distributed in the hope that it will be useful,
24// but WITHOUT ANY WARRANTY; without even the implied warranty of
25// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26// GNU General Public License for more details.
27//
28// You should have received a copy of the GNU General Public License
29// along with this software; see the file "license.txt". If not,
30// please, see <https://www.gnu.org/licenses/>.
31
32// Started in January 2002. Last revision: October 24, 2013.
33
34
35#ifndef _CHOMP_CUBES_CUBEVAR_H_
36#define _CHOMP_CUBES_CUBEVAR_H_
37
38#include "chomp/system/config.h"
46
47#include <iostream>
48#include <fstream>
49#include <cstdlib>
50#include <cstring>
51
52
53namespace chomp {
54namespace homology {
55
56
57// a friend class template
58template <class coordtype>
59class tCellVar;
60
61
62// --------------------------------------------------
63// ----------- Cube with allocated memory -----------
64// --------------------------------------------------
65
70template <class coordtype>
72{
73public:
75 typedef coordtype CoordType;
76
79
81 static const int MaxDim = 50;
82
85
87 tCubeVar ();
88
90 tCubeVar (const coordtype *c, int dim);
91
93 tCubeVar (int number, int dim);
94
97
100
102 ~tCubeVar ();
103
105 int dim () const;
106
108// template <class intType>
109// intType *coord (intType *c) const;
110 coordtype *coord (coordtype *c) const;
111
113 int_t hashkey1 () const;
114
116 int_t hashkey2 () const;
117
119 static const char *name ();
120
122 static const char *pluralname ();
123
124 // --- friends: ---
125
127 friend inline int operator == (const tCubeVar<coordtype> &c1,
128 const tCubeVar<coordtype> &c2)
129 {
130 if (!c1. tab)
131 return c2. tab ? false : true;
132 if (!c2. tab)
133 return false;
134 return thesame (c1. tab, c2. tab, c1. tab [0] + 1);
135 } /* operator == */
136
137 // friend class: cubical cell
138 friend class tCellVar<coordtype>;
139
140private:
143 coordtype *tab;
144
147 coordtype *initialize (int dim);
148
149}; /* class tCubeVar */
150
151// --------------------------------------------------
152
153template <class coordtype>
154inline coordtype *tCubeVar<coordtype>::initialize (int d)
155{
156 tab = new coordtype [d + 1];
157 if (!tab)
158 throw "Not enough memory for a cube.";
159 *tab = d;
160 return (tab + 1);
161} /* tCubeVar::initialize */
162
163// --------------------------------------------------
164
165template <class coordtype>
167{
168 tab = NULL;
169 return;
170} /* tCubeVar::tCubeVar */
171
172template <class coordtype>
173inline tCubeVar<coordtype>::tCubeVar (const coordtype *c, int d)
174{
175 if (d < 0)
176 throw "Negative dimension of a cube.";
177 if (d)
178 PointBase::wrapcopy (initialize (d), c, d);
179 else
180 tab = NULL;
181 return;
182} /* tCubeVar::tCubeVar */
183
184template <class coordtype>
186{
187 throw "Unable to construct a cube from a number.";
188} /* tCubeVar::tCubeVar */
189
190template <class coordtype>
192{
193 if (!c. dim ())
194 tab = NULL;
195 else
196 {
197 initialize (c. dim ());
198 copycoord (tab + 1, c. tab + 1, c. dim ());
199 }
200 return;
201} /* tCubeVar::tCubeVar */
202
203template <class coordtype>
205 (const tCubeVar<coordtype> &c)
206{
207 if (dim () == c. dim ())
208 copycoord (tab + 1, c. tab + 1, dim ());
209 else
210 {
211 if (tab)
212 delete [] tab;
213 if (c. dim ())
214 {
215 initialize (c. dim ());
216 copycoord (tab + 1, c. tab + 1, c. dim ());
217 }
218 else
219 tab = NULL;
220 }
221 return *this;
222} /* tCubeVar::operator = */
223
224template <class coordtype>
226{
227 if (tab)
228 delete [] tab;
229 return;
230} /* tCubeVar::~tCubeVar */
231
232template <class coordtype>
233inline int tCubeVar<coordtype>::dim () const
234{
235 return tab ? *tab : 0;
236} /* tCubeVar::dim */
237
238//template <class coordtype>
239//template <class intType>
240//inline intType *tCubeVar<coordtype>::coord (intType *c) const
241//{
242// if (!tab)
243// return NULL;
244// for (int i = 0; i < *tab; ++ i)
245// c [i] = static_cast<const intType> (tab [i + 1]);
246// return c;
247//} /* tCubeVar::coord */
248
249template <class coordtype>
250inline coordtype *tCubeVar<coordtype>::coord (coordtype *c) const
251{
252 if (!tab)
253 return 0;
254 for (int i = 0; i < *tab; ++ i)
255 c [i] = tab [i + 1];
256 return c;
257} /* tCubeVar::coord */
258
259template <class coordtype>
261{
262 int d = dim ();
263 switch (d)
264 {
265 case 0:
266 return 0;
267 case 1:
268 return static_cast<int_t> (tab [0]) << 12;
269 case 2:
270 return ((static_cast<int_t> (tab [0])) << 18) +
271 ((static_cast<int_t> (tab [1])) << 6);
272 default:
273 return ((static_cast<int_t> (tab [0])) << 18) +
274 ((static_cast<int_t> (tab [1])) << 6) +
275 ((static_cast<int_t> (tab [2])) >> 6);
276 }
277} /* tCubeVar::hashkey1 */
278
279template <class coordtype>
281{
282 return c. hashkey1 ();
283} /* hashkey1 */
284
285template <class coordtype>
287{
288 int d = dim ();
289 switch (d)
290 {
291 case 0:
292 return 1;
293 case 1:
294 return static_cast<int_t> (tab [0]) << 3;
295 case 2:
296 return (static_cast<int_t> (tab [0]) >> 1) +
297 (static_cast<int_t> (tab [1]) << 13);
298 default:
299 return ((static_cast<int_t> (tab [d - 1])) << 20) +
300 ((static_cast<int_t> (tab [d - 2])) << 9) +
301 ((static_cast<int_t> (tab [d - 3])) >> 1);
302 }
303} /* tCubeVar::hashkey2 */
304
305template <class coordtype>
307{
308 return c. hashkey2 ();
309} /* hashkey2 */
310
311template <class coordtype>
312inline const char *tCubeVar<coordtype>::name ()
313{
314 return "cube";
315} /* tCubeVar::name */
316
317template <class coordtype>
319{
320 return "cubes";
321} /* tCubeVar::pluralname */
322
323// --------------------------------------------------
324
326template <class coordtype>
328 const tCubeVar<coordtype> &c2)
329{
330 return !(c1 == c2);
331} /* operator != */
332
333// --------------------------------------------------
334
336template <class coordtype>
338 const tCubeVar<coordtype> &c2)
339{
340 int dim1 = c1. dim (), dim2 = c2. dim ();
341 if (dim1 + dim2 >= tCubeVar<coordtype>::MaxDim)
342 throw "Dimension too high to concatenate coordinates.";
343 coordtype coord [tCubeVar<coordtype>::MaxDim];
344 c1. coord (coord);
345 c2. coord (coord + dim1);
346 return tCubeVar<coordtype> (coord, dim1 + dim2);
347} /* operator * */
348
349// --------------------------------------------------
350
354template <class coordtype>
355inline std::ostream &operator << (std::ostream &out,
356 const tCubeVar<coordtype> &c)
357{
358 return WriteCube (out, c);
359} /* operator << */
360
364template <class coordtype>
365inline std::istream &operator >> (std::istream &in, tCubeVar<coordtype> &c)
366{
367 return ReadCube (in, c);
368} /* operator >> */
369
371template <class coordtype>
372inline std::istream &operator >> (std::istream &in,
374{
375 return ReadCubes (in, s);
376} /* operator >> */
377
379template <class coordtype>
380inline std::istream &operator >> (std::istream &in,
382{
383 return ReadCubicalMap (in, m);
384} /* operator >> */
385
386
387} // namespace homology
388} // namespace chomp
389
390#endif // _CHOMP_CUBES_CUBEVAR_H_
391
393
This file contains the definition of a bitfield class which works an array of bits.
This is a template for a set of objects of the given type.
Definition: hashsets.h:185
This class defines a multivalued map.
Definition: hashsets.h:945
This class defines cubical cell in R^n with edges parallel to the axes and with size 0 or 1 in each d...
Definition: cellvar.h:68
This class defines a hypercube in R^n with edges parallel to the axes and with size 1 in each directi...
Definition: cubevar.h:72
static const char * pluralname()
Returns the plural name of the objects represented by this class.
Definition: cubevar.h:318
coordtype * tab
The table containing the coordinates of the cube, as well as its dimension (at tab [0]).
Definition: cubevar.h:143
coordtype * coord(coordtype *c) const
Fills out the coordinate table with the cube's coordinates.
Definition: cubevar.h:250
tCellVar< coordtype > CellType
The type of a cell related to this cube type.
Definition: cubevar.h:78
tCubeVar< coordtype > & operator=(const tCubeVar< coordtype > &c)
The assignment operator.
Definition: cubevar.h:205
int_t hashkey1() const
Returns hashing key no. 1 required by the hashing set template.
Definition: cubevar.h:260
int dim() const
Returns the dimension of the cube.
Definition: cubevar.h:233
coordtype * initialize(int dim)
Initializes the data for a cube of a given dimension > 0.
Definition: cubevar.h:154
coordtype CoordType
The type of coordinates of a cube.
Definition: cubevar.h:75
friend int operator==(const tCubeVar< coordtype > &c1, const tCubeVar< coordtype > &c2)
The operator == for cubes.
Definition: cubevar.h:127
tCubeVar()
The default constructor.
Definition: cubevar.h:166
~tCubeVar()
The destructor.
Definition: cubevar.h:225
int_t hashkey2() const
Returns hashing key no. 2 required by the hashing set template.
Definition: cubevar.h:286
tWrapBase< coordtype > PointBase
The point base (for wrapping and tabulating coordinates).
Definition: cubevar.h:84
static const int MaxDim
The maximal dimension of a cube.
Definition: cubevar.h:81
static const char * name()
Returns the name of the objects represented by this class.
Definition: cubevar.h:312
static void wrapcopy(coordtype *dest, const coordtype *src, int dim)
Copies the coordinates and wraps them if necessary.
Definition: pointbas.h:322
This class is a simplified version of the point base class used only for the space wrapping support.
Definition: pointbas.h:471
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
This file contains the definition of some functions that are common for all types of cubes,...
int int_t
Index type for indexing arrays, counting cubes, etc.
Definition: config.h:115
This file contains the definition of the container "hashedset" which can be used to represent a set o...
This file defines a class "integer" which represents the ring of integers or the field of integers mo...
std::ostream & WriteCube(std::ostream &out, const cubetype &c)
Writes a cube to the output stream in the text mode.
Definition: cubemain.h:69
std::ostream & operator<<(std::ostream &out, const bincube< Dim, twoPower > &b)
Definition: bincube.h:907
std::istream & ReadCubes(std::istream &in, cubsettype &s)
Reads a set of cubes and ignores the line at the beginning of the file which starts with the letter '...
Definition: cubemain.h:193
int_t hashkey2(const hashNumber< Number > &n)
The second hashing key.
Definition: bincube.h:1036
tCellBase< coordtype > operator*(const tCellBase< coordtype > &c1, const tCellBase< coordtype > &c2)
Computes the Cartesian product of two cells.
Definition: cellbase.h:438
int thesame(const coordtype *c1, const coordtype *c2, int dim)
Compare two points. Returns true iff they have the same coordinates.
Definition: pointset.h:98
std::istream & ReadCube(std::istream &in, cubetype &c)
Reads a cube from the input text stream.
Definition: cubemain.h:184
bool operator!=(const typename bincube< Dim, twoPower >::neighborhood_iterator &x1, const typename bincube< Dim, twoPower >::neighborhood_iterator &x2)
Definition: bincube.h:794
std::istream & operator>>(std::istream &in, bincube< Dim, twoPower > &b)
Definition: bincube.h:914
int_t hashkey1(const hashNumber< Number > &n)
The first hashing key.
Definition: bincube.h:1029
void copycoord(coordtype *destination, const coordtype *source, int dim)
Copies the coordinates of one point to another.
Definition: pointset.h:108
std::istream & ReadCubicalMap(std::istream &in, mvmap< tCube, tCube > &m)
Reads a combinatorial cubical multivalued map from an input text stream.
Definition: cubemain.h:211
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51
This file contains the definition of a point base class which is used for indexing all the points (n-...
This file contains the definition of a set of n-dimensional points with integer coordinates and sever...
This file contains some useful functions related to the text input/output procedures.