The Original CHomP Software
cubemain.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: June 4, 2007.
33
34
35#ifndef _CHOMP_CUBES_CUBEMAIN_H_
36#define _CHOMP_CUBES_CUBEMAIN_H_
37
38
39#include "chomp/system/config.h"
45
46#include <iostream>
47#include <fstream>
48#include <cstdlib>
49#include <cstring>
50
51
52namespace chomp {
53namespace homology {
54
55
57template <class dest_cube, class src_cube>
58inline dest_cube cube_cast (const src_cube &src)
59{
60 typename dest_cube::CoordType tab [src_cube::MaxDim];
61 src. coord (tab);
62 return dest_cube (tab, src. dim ());
63} /* cube_cast */
64
65// --------------------------------------------------
66
68template <class cubetype>
69inline std::ostream &WriteCube (std::ostream &out, const cubetype &c)
70{
71 typename cubetype::CoordType coord [cubetype::MaxDim];
72 c. coord (coord);
73 int dim = c. dim ();
74 out << "(";
75 for (int i = 0; i < dim; ++ i)
76 {
77 if (i)
78 out << ",";
79 out << coord [i];
80 }
81 out << ")";
82 return out;
83} /* WriteCube */
84
85// --------------------------------------------------
86
89template <class cubetype>
90std::istream &ReadCubeFix (std::istream &in, cubetype &c, int dimfix)
91{
92 // retrieve the type of coordinates of the cube to read
93 typedef typename cubetype::CoordType coordtype;
94
95 // ignore any comments, spaces, tabs and new-line characters
96 ignorecomments (in);
97
98 // if there is a number in the input, then there are apparently
99 // no parentheses used for the coords or the cube is defined
100 // by its number (also indicated with '#')
101 if (((in. peek () >= '0') && (in. peek () <= '9')) ||
102 (in. peek () == '-') || (in. peek () == '+') ||
103 (in. peek () == '#'))
104 {
105 bool cubenumber = false;
106 if (in. peek () == '#')
107 {
108 in. get ();
109 ignorecomments (in);
110 cubenumber = true;
111 }
112 if (in. peek () == '+')
113 in. get ();
114 int n = -1;
115 in >> n;
116 while ((in. peek () == ' ') || (in. peek () == '\t'))
117 in. get ();
118
119 // if the next coordinate of the cube follows,
120 // read the coordinates until end-of-line is encountered
121 if (!cubenumber &&
122 (((in. peek () >= '0') && (in. peek () <= '9')) ||
123 (in. peek () == '-') || (in. peek () == '+')))
124 {
125 // read the coords and determine the space dimension
126 coordtype coord [cubetype::MaxDim];
127 coord [0] = n;
128 int dim = -1;
129 if (in. peek () != '\n')
130 {
131 dim = readcoordinates (in, coord + 1,
132 cubetype::MaxDim - 1, '\n');
133 if (dim < 0)
134 throw "Unable to read a cube: "
135 "Dim too high.";
136 }
137 ++ dim;
138
139 // if could not read the cube, throw an error message
140 if ((dimfix >= 0) && (dim != dimfix))
141 throw "Unable to read a cube: Wrong dim.";
142
143 // create a cube with the given coordinates
144 c = cubetype (coord, dim);
145
146 return in;
147 }
148
149 // if the cube is given by its number, read it this way
150 else
151 {
152 int dim = cubetype::PointBase::defaultdimension ();
153 const typename cubetype::CoordType *coord = 0;
154 if ((n > 0) && (dim > 0))
155 {
156 coord = cubetype::PointBase::coord
157 (n - 1, dim);
158 }
159 if (!coord)
160 throw "Cube no. out of range while reading.";
161 c = cubetype (n - 1, dim);
162 return in;
163 }
164 }
165
166 // read the coordinates and determine the space dimension
167 typename cubetype::CoordType coord [cubetype::MaxDim];
168 int dim = readcoordinates (in, coord, cubetype::MaxDim);
169 if (dim < 0)
170 throw "Unable to read a cube: Dimension too high.";
171
172 // if could not read the cube, throw an error message
173 if ((dimfix >= 0) && (dim != dimfix))
174 throw "Unable to read a cube: Wrong dimension.";
175
176 // create the cube with the given coordinates
177 c = cubetype (coord, dim);
178
179 return in;
180} /* ReadCubeFix */
181
183template <class cubetype>
184inline std::istream &ReadCube (std::istream &in, cubetype &c)
185{
186 return ReadCubeFix (in, c, -1);
187} /* ReadCube */
188
192template <class cubsettype>
193std::istream &ReadCubes (std::istream &in, cubsettype &s)
194{
195 // ignore any comments at the beginning of the file
196 ignorecomments (in);
197
198 // if the word "dimension" found, ignore the entire line
199 if (in. peek () == 'd')
200 {
201 in. ignore (20000, '\n');
202 ignorecomments (in);
203 }
204
205 // read the set of cubes using the standard procedure
206 return read (in, s, LARGE_SIZE);
207} /* ReadCubes */
208
210template <class tCube>
211std::istream &ReadCubicalMap (std::istream &in, mvmap<tCube,tCube> &m)
212{
213 // process the entire input file and read the map line-by-line
214 ignorecomments (in);
215 while (in. peek () != EOF)
216 {
217 // ignore all the lines which do not define a map assignment
218 while ((closingparenthesis (in. peek ()) == EOF) &&
219 ((in. peek () < '0') || (in. peek () > '9')) &&
220 (in. peek () != EOF))
221 {
222 ignoreline (in);
223 ignorecomments (in);
224 }
225
226 // if the end of the file has been reached, exit the loop
227 if (in. peek () == EOF)
228 break;
229
230 // determine the closing parenthesis corresp. to this one
231 int closing = closingparenthesis (in. peek ());
232
233 // if the opening parenthesis is valid, read it and
234 // check the next character to determine the assignment type
235 if (closing != EOF)
236 {
237 in. get ();
238 ignorecomments (in);
239 }
240
241 // if the assignment is in the general form, decode the line
242 if ((closing == EOF) ||
243 (closingparenthesis (in. peek ()) == EOF))
244 {
245 // read the domain element
246 tCube e;
247 // if it is given as a number, read it directly
248 if (closing == EOF)
249 {
250 in >> e;
251 if (!in)
252 throw "Can't read cube's number.";
253 }
254 // otherwise read the coordinates of the cube
255 else
256 {
257 typename tCube::CoordType coord
258 [tCube::MaxDim];
259 int dim = readcoordinates (in, coord,
260 tCube::MaxDim, closing);
261 if (!in || (dim <= 0))
262 throw "Unable to read a cube.";
263 e = tCube (coord, dim);
264 }
265 ignorecomments (in);
266
267 // read the assignment arrow
268 while (in. peek () == '-')
269 in. get ();
270 if (in. peek () == '>')
271 in. get ();
272 ignorecomments (in);
273
274 // read the image of the cube
275 read (in, m [e], SMALL_SIZE);
276 ignorecomments (in);
277 }
278
279 // otherwise read the assignment in the Jacek & Marcin format
280 else
281 {
282 // read the argument cell
283 typename tCube::CoordType argleft [tCube::MaxDim];
284 typename tCube::CoordType argright [tCube::MaxDim];
285 int dim = readcoordinates (in, argleft,
286 tCube::MaxDim);
287 ignorecomments (in);
288 int d1 = readcoordinates (in, argright,
289 tCube::MaxDim);
290 ignorecomments (in);
291
292 // read the closing and opening brackets
293 in. get ();
294 ignorecomments (in);
295 in. get ();
296 ignorecomments (in);
297
298 // read the value cell
299 typename tCube::CoordType vleft [tCube::MaxDim];
300 typename tCube::CoordType vright [tCube::MaxDim];
301 int d2 = readcoordinates (in, vleft, tCube::MaxDim);
302 ignorecomments (in);
303 int d3 = readcoordinates (in, vright, tCube::MaxDim);
304 ignorecomments (in);
305
306 // if there was an I/O error, interrupt reading here
307 if (!in || (in. peek () == EOF))
308 throw "Cannot read a map assignment line.";
309
310 // read the closing bracket
311 in. get ();
312 ignorecomments (in);
313
314 // check that all the dimensions are the same
315 if ((d1 != dim) || (d2 != dim) || (d3 != dim))
316 throw "Wrong dimensions of vertices.";
317
318 // verify that the argument cube is of the right size
319 for (int i = 0; i < dim; ++ i)
320 {
321 if (argright [i] - argleft [i] != 1)
322 throw "Wrong size of an argument.";
323 }
324
325 // add the argument cube to the map's domain
326 hashedset<tCube> &v = m [tCube (argleft, dim)];
327
328 // form a rectangle from this value cell
330 (vleft, vright, dim);
331
332 // add all the value cubes to the image of this element
333 const typename tCube::CoordType *c;
334 while ((c = r. get ()) != NULL)
335 v. add (tCube (c, dim));
336 }
337 }
338
339 return in;
340} /* ReadCubicalMap */
341
342
343} // namespace homology
344} // namespace chomp
345
346#endif // _CHOMP_CUBES_CUBEMAIN_H_
347
349
This file contains the definition of a bitfield class which works an array of bits.
This class can be used for iterating a rectangular set of points, given its left and right bound.
Definition: pointset.h:1627
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
This file contains the definition of the container "hashedset" which can be used to represent a set o...
#define SMALL_SIZE
This constant passed to the function 'write' makes the hashed set be displayed in a way that is appro...
Definition: hashsets.h:813
#define LARGE_SIZE
This constant passed to the function 'write' makes the hashed set be displayed in a way that is appro...
Definition: hashsets.h:820
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
dest_cube cube_cast(const src_cube &src)
Converts one cube into another.
Definition: cubemain.h:58
void ignoreline(std::istream &in)
Ignores the input characters until the end of a line, including this end of the line.
Definition: textfile.h:376
int closingparenthesis(int ch)
Returns the matching closing parenthesis for the given opening one or EOF if none.
Definition: textfile.h:411
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
std::istream & ReadCube(std::istream &in, cubetype &c)
Reads a cube from the input text stream.
Definition: cubemain.h:184
int readcoordinates(std::istream &in, coordtype *coord, int maxdim, int closing)
Reads the coordinates of a point.
Definition: pointset.h:2073
int read(textfile &f, coordtype *c, int maxdim)
Reads a point from a text file and removes a pair of parentheses, braces or brackets if present.
Definition: pointset.h:1994
std::istream & ReadCubeFix(std::istream &in, cubetype &c, int dimfix)
Reads a cube from the input text stream.
Definition: cubemain.h:90
void ignorecomments(std::istream &in)
Ignores white characters (spaces, tabulators, CRs and LFs), as well as comments from the input text f...
Definition: textfile.h:385
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 set of n-dimensional points with integer coordinates and sever...
This file contains some useful functions related to the text input/output procedures.