The Original CHomP Software
pointbas.h
Go to the documentation of this file.
1
3
18
19// Copyright (C) 1997-2020 by Pawel Pilarczyk.
20//
21// This file is part of my research software package. This is free software:
22// you can redistribute it and/or modify it under the terms of the GNU
23// General Public License as published by the Free Software Foundation,
24// either version 3 of the License, or (at your option) any later version.
25//
26// This software is distributed in the hope that it will be useful,
27// but WITHOUT ANY WARRANTY; without even the implied warranty of
28// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29// GNU General Public License for more details.
30//
31// You should have received a copy of the GNU General Public License
32// along with this software; see the file "license.txt". If not,
33// please, see <https://www.gnu.org/licenses/>.
34
35// Started in January 2002. Last revision: January 23, 2010.
36
37
38#ifndef _CHOMP_CUBES_POINTBAS_H_
39#define _CHOMP_CUBES_POINTBAS_H_
40
41#include "chomp/system/config.h"
44
45#include <iostream>
46#include <fstream>
47#include <cstdlib>
48
49
50namespace chomp {
51namespace homology {
52
53
54// classes defined within this header file (in this order):
55template <class coordtype>
56class tPointBase;
57
58template <class coordtype>
59class tPointBaseInitializer;
60
63
67const int DimBits = (sizeof (int_t) > 4) ? 7 : 6;
68
71const int NumBits = (sizeof (int_t) << 3) - DimBits;
72
74const int_t SignBit = static_cast<int_t> (1) << ((sizeof (int_t) << 3) - 1);
75
77const int_t NumMask = (~(static_cast<int_t> (0) ^ SignBit)) >> (DimBits - 1);
78
80const int MaxBasDim1 = static_cast<int> (1u << (DimBits - 1));
81
84const int MaxBasDim2 = static_cast<int> ((sizeof (int_t) << 3) - DimBits);
85
90
91
92// --------------------------------------------------
93// ------------------- Point Base -------------------
94// --------------------------------------------------
95
100template <class coordtype>
102{
103public:
106 static int_t number (const coordtype *c, int d);
107
111 static bool check (const coordtype *c, int d);
112
114 static const coordtype *coord (int_t number, int d);
115
117 static int defaultdimension (void);
118
121 static void quiet (bool what = true);
122
126 static void setwrapping (const coordtype *c,
127 int mindim = 1, int maxdim = MaxBasDim);
128
132 static void setwrapping (coordtype c,
133 int mindim = 1, int maxdim = MaxBasDim);
134
136 static const coordtype *getwrapping (int d);
137
139 static void wrapcoord (coordtype *c, int dim);
140
142 static void wrapcopy (coordtype *dest, const coordtype *src,
143 int dim);
144
147 static void reset (void);
148
151 static void forget (void);
152
154 static outputstream &showused (outputstream &out);
155
157 static std::ostream &showused (std::ostream &out);
158
160 friend class tPointBaseInitializer<coordtype>;
161
162protected:
165
167 static int n;
168
170 static bool show;
171
173 static bool forgotten;
174
177
178}; /* class tPointBase */
179
180// --------------------------------------------------
181
182template <class coordtype>
183inline const coordtype *tPointBase<coordtype>::coord (int_t nr, int d)
184{
185 if (forgotten)
186 throw "Trying to retrieve forgotten coordinates.";
187 if ((d > n) || (d <= 0) || (p [d - 1] == NULL))
188 return NULL;
189 else
190 return (*(p [d - 1])) [nr];
191} /* tPointBase::coord */
192
193template <class coordtype>
194inline bool tPointBase<coordtype>::check (const coordtype *c, int d)
195{
196 if (forgotten)
197 throw "Trying to check forgotten coordinates.";
198 if ((d > n) || (d <= 0) || (p [d - 1] == NULL))
199 return 0;
200 return p [d - 1] -> check (c);
201} /* tPointBase::check */
202
203template <class coordtype>
204inline void tPointBase<coordtype>::quiet (bool what)
205{
206 show = !what;
207 return;
208} /* tPointBase::quiet */
209
210template <class coordtype>
211inline int_t tPointBase<coordtype>::number (const coordtype *c, int d)
212{
213 if (forgotten)
214 throw "Trying to find the number of forgotten coordinates.";
215
216 if (d < 0)
217 return -1;
218 if (d >= MaxBasDim)
219 throw "Dimension too high.";
220
221 // enhance the table of sets of points if necessary
222 if (d > n)
223 {
224 tPointset<coordtype> **newtable =
225 new tPointset<coordtype> * [d];
226 for (int i = 0; i < d; ++ i)
227 newtable [i] = (i < n) ? p [i] : NULL;
228 if (p)
229 delete [] p;
230 p = newtable;
231 n = d;
232 }
233
234 if (!p [d - 1])
235 {
236 p [d - 1] = new tPointset<coordtype> (1024);
237 p [d - 1] -> dimension (d);
238 }
239
240 int_t number = p [d - 1] -> add (c);
241 if (number > NumMask)
242 throw "Too many points.";
243
244 return number;
245} /* tPointBase::number */
246
247template <class coordtype>
248inline void tPointBase<coordtype>::setwrapping (const coordtype *c,
249 int mindim, int maxdim)
250{
251 if (forgotten)
252 throw "Trying to wrap forgotten coordinates.";
253
254 // correct the left and right bound for dimensions
255 if (mindim < 1)
256 mindim = 1;
257 if (maxdim > MaxBasDim)
258 maxdim = MaxBasDim;
259
260 // enhance the table of sets of points if necessary
261 if (maxdim > n)
262 {
263 tPointset<coordtype> **newtable =
264 new tPointset<coordtype> * [maxdim];
265 for (int i = 0; i < maxdim; ++ i)
266 newtable [i] = (i < n) ? p [i] : NULL;
267 if (p)
268 delete [] p;
269 p = newtable;
270 n = maxdim;
271 }
272
273 // set wrapping coordinates and allocate sets of points if needed
274 for (int d = mindim; d < maxdim; ++ d)
275 {
276 if (!p [d - 1])
277 {
278 p [d - 1] = new tPointset<coordtype>;
279 p [d - 1] -> dimension (d);
280 }
281 p [d - 1] -> wrapspace (c);
282 }
283
284 return;
285} /* tPointBase::setwrapping */
286
287template <class coordtype>
288inline void tPointBase<coordtype>::setwrapping (coordtype c,
289 int mindim, int maxdim)
290{
291 coordtype wraptable [MaxBasDim];
292 for (int i = 0; i < MaxBasDim; ++ i)
293 wraptable [i] = c;
294 setwrapping (wraptable, mindim, maxdim);
295 return;
296} /* tPointBase::setwrapping */
297
298template <class coordtype>
299inline const coordtype *tPointBase<coordtype>::getwrapping (int d)
300{
301 if (forgotten)
302 throw "Trying to get wrapping of forgotten coordinates.";
303
304 if ((d <= 0) || (d - 1 >= n) || !p [d - 1])
305 return NULL;
306 else
307 return p [d - 1] -> wrapspace ();
308} /* tPointBase::getwrapping */
309
310template <class coordtype>
311inline void tPointBase<coordtype>::wrapcoord (coordtype *c, int dim)
312{
313 if ((dim > n) || (dim <= 0) || (p [dim - 1] == NULL))
314 return;
315 const coordtype *cw = p [dim - 1] -> wrapspace ();
316 if (cw)
317 chomp::homology::wrapcoord (c, c, cw, dim);
318 return;
319} /* tPointBase::wrapcoord */
320
321template <class coordtype>
322inline void tPointBase<coordtype>::wrapcopy (coordtype *dest,
323 const coordtype *src, int dim)
324{
325 const coordtype *cw;
326 if ((dim > n) || (dim <= 0) || (p [dim - 1] == 0))
327 cw = 0;
328 else
329 cw = p [dim - 1] -> wrapspace ();
330 if (cw)
331 chomp::homology::wrapcoord (dest, src, cw, dim);
332 else
333 copycoord (dest, src, dim);
334 return;
335} /* tPointBase::wrapcopy */
336
337template <class coordtype>
339{
340 for (int i = 0; i < n; ++ i)
341 {
342 if (p [i])
343 {
344 delete p [i];
345 p [i] = NULL;
346 }
347 }
348 delete [] p;
349 p = NULL;
350 n = 0;
351 forgotten = false;
352 return;
353} /* tPointBase::reset */
354
355template <class coordtype>
357{
358 if (forgotten)
359 throw "Trying to forget already forgotten coordinates.";
360
361 reset ();
362 forgotten = true;
363 return;
364} /* tPointBase::forget */
365
366// --------------------------------------------------
367
368template <class coordtype>
370
371template <class coordtype>
373
374template <class coordtype>
376
377template <class coordtype>
379
380template <class coordtype>
382{
383 static int dim = 0;
384 if (dim)
385 return dim;
386 for (int d = 1; d <= n; ++ d)
387 {
388 if (!p [d - 1])
389 continue;
390 if (p [d - 1] -> empty ())
391 continue;
392 dim = d;
393 return d;
394 }
395 return 0;
396} /* tPointBase::defaultdimension */
397
398template <class coordtype>
400{
401 if (forgotten)
402 throw "Trying to show forgotten coordinates.";
403
404 bool shown = false;
405 for (int i = 0; i < n; ++ i)
406 {
407 if (!p [i])
408 continue;
409 const tPointset<coordtype> &pset = *(p [i]);
410 if (pset. empty ())
411 continue;
412 out << (shown ? ", " : "Vertices used: ") <<
413 pset. size () << " of dim " << (i + 1);
414 shown = true;
415 }
416 if (shown)
417 out << ".\n";
418 return out;
419} /* tPointBase::showused */
420
421template <class coordtype>
422std::ostream &tPointBase<coordtype>::showused (std::ostream &out)
423{
424 outputstream tout (out);
425 showused (tout);
426 return out;
427} /* tPointBase::showused */
428
429
430// --------------------------------------------------
431// ------------- Point Base Initializer -------------
432// --------------------------------------------------
433
437template <class coordtype>
439{
440public:
445
446}; /* PointBaseInitializer */
447
448template <class coordtype>
450
451template <class coordtype>
453{
455 return;
459 return;
460} /* PointBaseInitializer::~PointBaseInitializer */
461
462
463// --------------------------------------------------
464// ------------------- Wrap Base --------------------
465// --------------------------------------------------
466
469template <class coordtype>
470class tWrapBase: public tPointBase<coordtype>
471{
472public:
474 static int_t number (const coordtype *, int)
475 {throw "Trying to get a point number.";}
476
478 static bool check (const coordtype *, int)
479 {return true;}
480
482 static const coordtype *coord (int_t, int)
483 {throw "Trying to get the coordinates of a point.";}
484
485 static void quiet (bool = true) {return;}
486
487 static void reset (void) {return;}
488
489 static void forget (void) {return;}
490
491 static outputstream &showused (outputstream &out) {return out;}
492
493 static std::ostream &showused (std::ostream &out) {return out;}
494
495}; /* class tWrapBase */
496
497// --------------------------------------------------
498
499
500} // namespace homology
501} // namespace chomp
502
503#endif // _CHOMP_CUBES_POINTBAS_H_
504
506
This class defines an output stream for replacing the standard 'cout'.
Definition: textfile.h:64
This class keeps a common set of points which are indexed for the use of other classes,...
Definition: pointbas.h:102
static void wrapcoord(coordtype *c, int dim)
Wraps the given coordinates if necessary.
Definition: pointbas.h:311
static const coordtype * coord(int_t number, int d)
Retrieves the coordinates of the given point.
Definition: pointbas.h:183
static void quiet(bool what=true)
Sets the point base to be quiet or not.
Definition: pointbas.h:204
static bool forgotten
Were the base points forgotten?
Definition: pointbas.h:173
static void forget(void)
Forgets the base points, frees memory and makes this data structure ususable: An exception is thrown ...
Definition: pointbas.h:356
static tPointset< coordtype > ** p
The sets of points from which numbers are taken.
Definition: pointbas.h:164
static int defaultdimension(void)
Determines the default dimension of points.
Definition: pointbas.h:381
static tPointBaseInitializer< coordtype > pointBaseInitializer
An object responsible for deleting the pointsets at exit.
Definition: pointbas.h:176
static void reset(void)
Resets the internal data.
Definition: pointbas.h:338
static bool check(const coordtype *c, int d)
Checks whether this point is already included in the list.
Definition: pointbas.h:194
static bool show
Should the summary of points' usage be displayed?
Definition: pointbas.h:170
static const coordtype * getwrapping(int d)
Returns the space wrapping for the given dimension.
Definition: pointbas.h:299
static outputstream & showused(outputstream &out)
Shows the number of vertices stored for all the dimensions.
Definition: pointbas.h:399
static int n
The number of allocated sets of points.
Definition: pointbas.h:167
static int_t number(const coordtype *c, int d)
Returns the number of the point with given coordinates.
Definition: pointbas.h:211
static void wrapcopy(coordtype *dest, const coordtype *src, int dim)
Copies the coordinates and wraps them if necessary.
Definition: pointbas.h:322
static void setwrapping(const coordtype *c, int mindim=1, int maxdim=MaxBasDim)
Sets space wrapping for all the dimensions in the range starting with 'mindim' and strictly smaller t...
Definition: pointbas.h:248
This class is used to deallocate memory kept in the static variables of the corresponding class "tPoi...
Definition: pointbas.h:439
~tPointBaseInitializer()
Deallocates memory kept in the static variables of the corresponding class "tPointBase" and shows rel...
Definition: pointbas.h:452
This class represents a set of points in R^n with integer coordinates.
Definition: pointset.h:482
This class is a simplified version of the point base class used only for the space wrapping support.
Definition: pointbas.h:471
static int_t number(const coordtype *, int)
No coord to number translation is available.
Definition: pointbas.h:474
static bool check(const coordtype *, int)
Always says that the point exists.
Definition: pointbas.h:478
static void reset(void)
Definition: pointbas.h:487
static void forget(void)
Definition: pointbas.h:489
static outputstream & showused(outputstream &out)
Definition: pointbas.h:491
static void quiet(bool=true)
Definition: pointbas.h:485
static const coordtype * coord(int_t, int)
No number to coord translation is available.
Definition: pointbas.h:482
static std::ostream & showused(std::ostream &out)
Definition: pointbas.h:493
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
int int_t
Index type for indexing arrays, counting cubes, etc.
Definition: config.h:115
outputstream sout
A replacement for standard output stream, with optional logging and other features provided by the cl...
const int MaxBasDim
The maximal dimension that can be used if the high bits of an integer store the value of the dimensio...
Definition: pointbas.h:89
void wrapcoord(coordtype *destination, const coordtype *source, const coordtype *wrap, int dim)
Wraps coordinates stored in 'c' accordint to the wrap table 'wrap' and store the result in the table ...
Definition: pointset.h:119
const int DimBits
The number of signed bits to store the dimension (i.e., 6: max 31).
Definition: pointbas.h:67
const int NumBits
The number of bits in an integer number that remain to be used for other purposes,...
Definition: pointbas.h:71
const int_t SignBit
The sign bit of the int_t number.
Definition: pointbas.h:74
tPointBase< coordinate > PointBase
The default type of the point base class.
Definition: pointbas.h:62
const int MaxBasDim2
The maximal dimension which still leaves enough bits in the integer to have one bit for each directio...
Definition: pointbas.h:84
const int MaxBasDim1
The maximal dimension that can be represented using 'DimBits' bits.
Definition: pointbas.h:80
void copycoord(coordtype *destination, const coordtype *source, int dim)
Copies the coordinates of one point to another.
Definition: pointset.h:108
const int_t NumMask
The mask of the bits remaining after the dimension bits are excluded.
Definition: pointbas.h:77
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51
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.