The Original CHomP Software
cubiset.h
Go to the documentation of this file.
1
3
14
15// This file copyright (C) 1997-2016 by Pawel Pilarczyk.
16//
17// This file is part of the "chomp" program. It is free software;
18// you can redistribute it and/or modify it under the terms of the GNU
19// General Public License as published by the Free Software Foundation,
20// either version 3 of the License, or (at your option) any later version.
21//
22// This software is distributed in the hope that it will be useful,
23// but WITHOUT ANY WARRANTY; without even the implied warranty of
24// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25// GNU General Public License for more details.
26//
27// You should have received a copy of the GNU General Public License
28// along with this software; see the file "license.txt". If not,
29// please, see <https://www.gnu.org/licenses/>.
30
31// Started on March 19, 2006. Last revision: July 27, 2007.
32
33
34#ifndef _CAPD_HOMENGIN_CUBISET_H_
35#define _CAPD_HOMENGIN_CUBISET_H_
36
37#include <string>
38#include <cstring>
40
41
42// --------------------------------------------------
43// ------------------ CUBICAL SET -------------------
44// --------------------------------------------------
45
50{
51public:
54 CubicalSet (const int *left_coords, const int *right_coords,
55 int dim = EMBEDDING_DIM, const int *space_wrapping = 0);
56
58 ~CubicalSet ();
59
61 CubicalSet (const CubicalSet &c);
62
65
68 void ComputeBettiNumbers (int *result, const char *engine = 0,
69 bool quiet = false) const;
70
73 int Add (const int *coords);
74
77 int Delete (const int *coords);
78
81 bool Contains (const int *coords) const;
82
84 void Clear ();
85
86private:
88 int ByteOffset (const int *coords) const;
89
91 int BitMask (const int *coords) const;
92
94 bool Inside (const int *coords);
95
97 unsigned char *buffer;
98
101
103 int dim;
104
106 int *sizes;
107
110
113
114}; /* class CubicalComples */
115
116// --------------------------------------------------
117
118inline CubicalSet::CubicalSet (const int *left_coords,
119 const int *right_coords, int dim, const int *space_wrapping)
120{
121 if (dim <= 0)
122 throw "Non-positive dimension of a cubical complex.";
123 this -> dim = dim;
124 this -> sizes = new int [dim];
125 this -> minimal = new int [dim];
126 this -> wrapping = space_wrapping ? new int [dim] : 0;
127 this -> bufsize = 0;
128 for (int i = 0; i < dim; ++ i)
129 {
130 minimal [i] = left_coords [i];
131 sizes [i] = right_coords [i] - left_coords [i];
132 if (sizes [i] <= 0)
133 throw "Non-positive size of a cubical complex.";
134 if (!i)
135 {
136 sizes [0] = (sizes [0] + 63) & ~63;
137 bufsize = sizes [0] >> 3;
138 }
139 else
140 bufsize *= sizes [i];
141 }
142 if (space_wrapping)
143 {
144 for (int i = 0; i < dim; ++ i)
145 {
146 wrapping [i] = space_wrapping [i];
147 if (wrapping [i] < 0)
148 wrapping [i] = -space_wrapping [i];
149 }
150 }
151 if (bufsize <= 0)
152 throw "Wrong buffer size in a cubical complex.";
153 buffer = new unsigned char [bufsize];
154 std::memset (buffer, 0, bufsize);
155 return;
156} /* CubicalSet::CubicalSet */
157
159{
160 delete [] sizes;
161 delete [] minimal;
162 delete [] buffer;
163 if (wrapping)
164 delete [] wrapping;
165 return;
166} /* CubicalSet::~CubicalSet */
167
169{
170 dim = c. dim;
171 bufsize = c. bufsize;
172 sizes = new int [dim];
173 minimal = new int [dim];
174 for (int i = 0; i < dim; ++ i)
175 {
176 minimal [i] = c. minimal [i];
177 sizes [i] = c. sizes [i];
178 }
179 buffer = new unsigned char [bufsize];
180 std::memcpy (buffer, c. buffer, bufsize);
181 return;
182} /* CubicalSet::CubicalSet */
183
185{
186 delete [] sizes;
187 delete [] minimal;
188 delete [] buffer;
189
190 dim = c. dim;
191 bufsize = c. bufsize;
192 sizes = new int [dim];
193 minimal = new int [dim];
194 for (int i = 0; i < dim; ++ i)
195 {
196 minimal [i] = c. minimal [i];
197 sizes [i] = c. sizes [i];
198 if (i > 0)
199 bufsize *= sizes [i];
200 }
201 buffer = new unsigned char [bufsize];
202 std::memcpy (buffer, c. buffer, bufsize);
203 return *this;
204} /* CubicalSet::operator = */
205
206inline int CubicalSet::ByteOffset (const int *coords) const
207{
208 int offset = (coords [0] - minimal [0]) >> 3;
209 int multiply = ((sizes [0] + 31) >> 5) << 2;
210 for (int i = 1; i < dim; ++ i)
211 {
212 offset += multiply * (coords [i] - minimal [i]);
213 multiply *= sizes [i];
214 }
215 return offset;
216} /* CubicalSet::ByteOffset */
217
218inline int CubicalSet::BitMask (const int *coords) const
219{
220 return 1 << ((coords [0] - minimal [0]) & 0x07);
221} /* CubicalSet::BitMask */
222
223inline bool CubicalSet::Inside (const int *coords)
224{
225 for (int i = 0; i < dim; ++ i)
226 {
227 if (coords [i] < minimal [i])
228 return false;
229 if (coords [i] >= minimal [i] + sizes [i])
230 return false;
231 }
232 return true;
233} /* CubicalSet::Inside */
234
235inline int CubicalSet::Add (const int *coords)
236{
237 if (!Inside (coords))
238 return -1;
239 buffer [ByteOffset (coords)] |= BitMask (coords);
240 return 0;
241} /* CubicalSet::Add */
242
243inline int CubicalSet::Delete (const int *coords)
244{
245 if (!Inside (coords))
246 return -1;
247 buffer [ByteOffset (coords)] &= ~(BitMask (coords));
248 return 0;
249} /* CubicalSet::Delete */
250
251inline bool CubicalSet::Contains (const int *coords) const
252{
253 return buffer [ByteOffset (coords)] & BitMask (coords);
254} /* CubicalSet::Contains */
255
256inline void CubicalSet::Clear ()
257{
258 std::memset (buffer, 0, bufsize);
259 return;
260} /* CubicalSet::Clear */
261
262inline void CubicalSet::ComputeBettiNumbers (int *result,
263 const char *engine, bool quiet) const
264{
265 ::ComputeBettiNumbers (buffer, sizes, dim, result, engine,
266 wrapping, quiet);
267 return;
268} /* CubicalSet::ComputeBettiNumbers */
269
272inline void ComputeBettiNumbers (const CubicalSet &s, int *result,
273 const char *engine = 0, bool quiet = false)
274{
275 s. ComputeBettiNumbers (result, engine, quiet);
276 return;
277} /* ComputeBettiNumbers */
278
279
280#endif // _CAPD_HOMENGIN_CUBISET_H_
281
283
This file defines a top-level interface to a homology computation procedure of full cubical sets repr...
This class stores a full cubical set and implements some basic operations on such a set,...
Definition: cubiset.h:50
int * sizes
The sizes of the binary data in the buffer.
Definition: cubiset.h:106
int * wrapping
The space wrapping in each direction, if defined.
Definition: cubiset.h:112
unsigned char * buffer
The binary buffer for storing the cubes.
Definition: cubiset.h:97
int * minimal
The coordinates of the minimal vertex of the buffer box.
Definition: cubiset.h:109
int bufsize
The actual size of the buffer.
Definition: cubiset.h:100
int dim
The dimension of the embedding space.
Definition: cubiset.h:103
~CubicalSet()
The destructor.
Definition: cubiset.h:158
int Add(const int *coords)
Adds a cube to the set unless the cube is outside the box.
Definition: cubiset.h:235
bool Inside(const int *coords)
Verifies whether the cube is within the bounding box.
Definition: cubiset.h:223
CubicalSet(const int *left_coords, const int *right_coords, int dim=EMBEDDING_DIM, const int *space_wrapping=0)
The only constructor allowed: Creates an empty cubical bitmap of the given size.
Definition: cubiset.h:118
void ComputeBettiNumbers(const CubicalSet &s, int *result, const char *engine=0, bool quiet=false)
Computes the Betti numbers of a full cubical set represented by an object of the class "CubicalSet".
Definition: cubiset.h:272
CubicalSet & operator=(const CubicalSet &c)
The assignment operator.
Definition: cubiset.h:184
void ComputeBettiNumbers(int *result, const char *engine=0, bool quiet=false) const
Computes the Betti numbers of the cubical set.
Definition: cubiset.h:262
bool Contains(const int *coords) const
Verifies whether the given cube is contained in the cubical set.
Definition: cubiset.h:251
void Clear()
Clears the bitmap and makes the cubical set empty.
Definition: cubiset.h:256
int Delete(const int *coords)
Deletes a cube from the set.
Definition: cubiset.h:243
int BitMask(const int *coords) const
Computes the mask for the bit in the right word.
Definition: cubiset.h:218
int ByteOffset(const int *coords) const
Computes the right word offset in the buffer.
Definition: cubiset.h:206