The ChainCon Software (Release 0.03)
algcell.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// An abstract algebraic cell.
6 ///
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
10 //
11 // This file is part of my research software package. This is free software:
12 // you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation,
14 // either version 3 of the License, or (at your option) any later version.
15 //
16 // This software is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this software; see the file "license.txt". If not,
23 // please, see <http://www.gnu.org/licenses/>.
24 
25 // Started on March 24, 2009. Last revision: June 20, 2012.
26 
27 
28 #ifndef _CHAINCON_ALGCELL_H_
29 #define _CHAINCON_ALGCELL_H_
30 
31 
32 // include some standard C++ header files
33 #include <istream>
34 #include <ostream>
35 #include <algorithm>
36 
37 // include selected header files from the CHomP library
38 #include "chomp/system/config.h"
39 #include "chomp/struct/multitab.h"
40 
41 // include relevant local header files
42 #include "chaincon/chain.h"
43 
44 
45 // --------------------------------------------------
46 // ------------ abstract algebraic cell -------------
47 // --------------------------------------------------
48 
49 /// An abstract algebraic cell, characterized by a unique identifier,
50 /// dimension, and a formula for its boundary.
51 /// The identifier type must be convertible to an integer, since it is used
52 /// for indexing arrays of certain types.
53 template <class IdT, class DimT, class CoefT>
54 class tAlgCell
55 {
56 public:
57  /// The type of a cell identifier.
58  typedef IdT IdType;
59 
60  /// The type of integer used to store the dimension of the cell.
61  typedef DimT DimType;
62 
63  /// The type of coefficients in the chain.
64  typedef CoefT CoefType;
65 
66  /// The default constructor of an empty cell (id 0).
67  tAlgCell ();
68 
69  /// The constructor of a cell with a given identifier.
70  /// Note that its dimension (if >= 0) and boundary (if nonzero)
71  /// must be already set earlier (for this id)
72  /// or must be set later (using methods available for this cell).
73  explicit tAlgCell (const IdT &identifier);
74 
75  /// The copy constructor.
77 
78  /// The constructor of the n-th boundary cell.
79  tAlgCell (const tAlgCell<IdT,DimT,CoefT> &c, int_t n);
80 
81  /// The assignment operator.
82  tAlgCell<IdT,DimT,CoefT> &operator =
83  (const tAlgCell<IdT,DimT,CoefT> &s);
84 
85  /// The destructor.
86  ~tAlgCell ();
87 
88  /// Returns the dimension of the cell.
89  const DimT &dim () const;
90 
91  /// Returns the identifier of the cell.
92  const IdT &getId () const;
93 
94  /// Returns the length of the boundary of the elementary cube.
95  int_t boundaryLength () const;
96 
97  /// Returns the n-th coefficient in the boundary of the cube.
98  const CoefT &boundaryCoef (int_t n) const;
99 
100  /// The equality operator.
101  bool operator == (const tAlgCell<IdT,DimT,CoefT> &s) const;
102 
103  /// Swaps the data with another cell.
104  void swap (tAlgCell<IdT,DimT,CoefT> &s);
105 
106  /// Returns the next available identifier to be used for cells.
107  static IdT newId ();
108 
109  /// Sets the dimension of the cell with the given identifier.
110  static void setDimension (const IdT &identifier,
111  const DimT &dimension);
112 
113  /// Sets the boundary of the cell with the given identifier.
114  static void setBoundary (const IdT &identifier,
116 
117  /// Forgets all the boundaries and dimensions of cells and resets
118  /// the identifier counter. Note that after this 'reset' all the
119  /// cells that exist are not valid anymore.
120  static void forget ();
121 
122 private:
123  /// The identifier of the cell.
124  IdT id;
125 
126  /// The next free identifier for a new cell.
127  static IdT maxUsedId;
128 
129  /// A global array of dimensions of the cells.
130  static chomp::homology::multitable<DimT> dimensions;
131 
132  /// A global variable remembering if the dimension of cell 0
133  /// was already set to -1.
134  static bool dim0initialized;
135 
136  /// A global array of boundaries of the cells.
137  static chomp::homology::multitable
139 
140 }; /* class tAlgCell */
141 
142 // --------------------------------------------------
143 
144 template <class IdT, class DimT, class CoefT>
146 
147 template <class IdT, class DimT, class CoefT>
148 chomp::homology::multitable<DimT> tAlgCell<IdT,DimT,CoefT>::dimensions (256);
149 
150 template <class IdT, class DimT, class CoefT>
152 
153 template <class IdT, class DimT, class CoefT>
154 chomp::homology::multitable<tChain<tAlgCell<IdT,DimT,CoefT>,CoefT> >
156 
157 // --------------------------------------------------
158 
159 template <class IdT, class DimT, class CoefT>
161 {
162  return;
163 } /* tAlgCell::tAlgCell */
164 
165 template <class IdT, class DimT, class CoefT>
166 inline tAlgCell<IdT,DimT,CoefT>::tAlgCell (const IdT &identifier):
167  id (identifier)
168 {
169  return;
170 } /* tAlgCell::tAlgCell */
171 
172 template <class IdT, class DimT, class CoefT>
175  id (c. id)
176 {
177  return;
178 } /* tAlgCell::tAlgCell */
179 
180 template <class IdT, class DimT, class CoefT>
182  (const tAlgCell<IdT,DimT,CoefT> &c, int_t n):
183  id (boundaries [c. id]. getCell (n). id)
184 {
185  return;
186 } /* tAlgCell::tAlgCell */
187 
188 template <class IdT, class DimT, class CoefT>
191 {
192  id = c. id;
193  return *this;
194 } /* tAlgCell::operator = */
195 
196 template <class IdT, class DimT, class CoefT>
198 {
199  return;
200 } /* tAlgCell::~tAlgCell */
201 
202 template <class IdT, class DimT, class CoefT>
203 inline const DimT &tAlgCell<IdT,DimT,CoefT>::dim () const
204 {
205  if (!id && !dim0initialized)
206  {
207  dimensions [0] = static_cast<DimT> (-1);
208  dim0initialized = true;
209  }
210  return dimensions [id];
211 } /* tAlgCell::dim */
212 
213 template <class IdT, class DimT, class CoefT>
214 inline const IdT &tAlgCell<IdT,DimT,CoefT>::getId () const
215 {
216  return id;
217 } /* tAlgCell::getId */
218 
219 template <class IdT, class DimT, class CoefT>
221 {
222  return boundaries [id]. size ();
223 } /* tAlgCell::boundaryLength */
224 
225 template <class IdT, class DimT, class CoefT>
226 inline const CoefT &tAlgCell<IdT,DimT,CoefT>::boundaryCoef (int_t n) const
227 {
228  return boundaries [id]. getCoef (n);
229 } /* tAlgCell::boundaryCoef */
230 
231 template <class IdT, class DimT, class CoefT>
232 inline bool tAlgCell<IdT,DimT,CoefT>::operator ==
233  (const tAlgCell<IdT,DimT,CoefT> &c) const
234 {
235  return (id == c. id);
236 } /* tAlgCell::operator == */
237 
238 template <class IdT, class DimT, class CoefT>
240 {
241  std::swap (id, c. id);
242  return;
243 } /* tAlgCell::swap */
244 
245 template <class IdT, class DimT, class CoefT>
247 {
248  return (++ maxUsedId);
249 } /* tAlgCell::newId */
250 
251 template <class IdT, class DimT, class CoefT>
252 inline void tAlgCell<IdT,DimT,CoefT>::setDimension (const IdT &identifier,
253  const DimT &dimension)
254 {
255  dimensions [identifier] = dimension;
256  return;
257 } /* tAlgCell::setDimension */
258 
259 template <class IdT, class DimT, class CoefT>
260 inline void tAlgCell<IdT,DimT,CoefT>::setBoundary (const IdT &identifier,
262 {
263  boundaries [identifier] = boundary;
264  return;
265 } /* tAlgCell::setBoundary */
266 
267 template <class IdT, class DimT, class CoefT>
269 {
270  // reset the identifier numbers
271  maxUsedId = 0;
272 
273  // forget the dimensions of the cells
274  chomp::homology::multitable<DimT> emptyDimensions;
275  dimensions = emptyDimensions;
276 
277  // forget the information if dimension of cell 0 was initialized
278  dim0initialized = false;
279 
280  // forget the boundaries of the cells
281  chomp::homology::multitable
282  <tChain<tAlgCell<IdT,DimT,CoefT>,CoefT> > emptyBoundaries;
283  boundaries = emptyBoundaries;
284 
285  return;
286 } /* tAlgCell::setBoundary */
287 
288 // --------------------------------------------------
289 
290 /// Generates a hashing key no. 1 for an abstract algebraic cell.
291 /// This key is to be used in a hashed set.
292 template <class IdT, class DimT, class CoefT>
293 inline int_t hashkey1 (const tAlgCell<IdT,DimT,CoefT> &c)
294 {
295  return (static_cast<int_t> (c. getId ()) << 3) ^ 0xA5A5A5;
296 } /* hashkey1 */
297 
298 /// Generates a hashing key no. 1 for an abstract algebraic cell.
299 /// This key is to be used in a hashed set.
300 template <class IdT, class DimT, class CoefT>
301 inline int_t hashkey2 (const tAlgCell<IdT,DimT,CoefT> &c)
302 {
303  return (static_cast<int_t> (c. getId ()) << 5) ^ 0x5A5A5A;
304 } /* hashkey2 */
305 
306 // --------------------------------------------------
307 
308 /// Writes an abstract algebraic cell in the text mode to an output stream.
309 template <class IdT, class DimT, class CoefT>
310 std::ostream &operator << (std::ostream &out,
311  const tAlgCell<IdT,DimT,CoefT> &c)
312 {
313  out << c. getId ();
314 // out << '_' << c. dim ();
315  return out;
316 } /* operator << */
317 
318 /// Reads an elementary cube from the input stream in the text format.
319 /// Skips all the characters in the input stream until
320 /// an opening parenthesis is found.
321 /// Then reads the cartesian product that defines the cube.
322 /// If no elementary cube is found in the input then the cube
323 /// is not modified.
324 template <class IdT, class DimT, class CoefT>
325 std::istream &operator >> (std::istream &in, tAlgCell<IdT,DimT,CoefT> &c)
326 {
327  // ignore any comments, spaces, tabs and new-line characters
328  chomp::homology::ignorecomments (in);
329 
330  // get the identifier of the cell
331  IdT id (0);
332  in >> id;
333 
334  // define the cell
336 
337  // check for the underscore and read it if present
338  if (in. peek () != '_')
339  return in;
340  in. get ();
341 
342  // get the dimension of the cell
343  DimT dim (-1);
344  in >> dim;
345 
346  // set the dimensioin of the cell
348 
349  return in;
350 } /* operator >> */
351 
352 
353 #endif // _CHAINCON_ALGCELL_H_
354 
static IdT newId()
Returns the next available identifier to be used for cells.
Definition: algcell.h:246
std::ostream & operator<<(std::ostream &out, const tAlgCell< IdT, DimT, CoefT > &c)
Writes an abstract algebraic cell in the text mode to an output stream.
Definition: algcell.h:310
CoefT CoefType
The type of coefficients in the chain.
Definition: algcell.h:64
static void forget()
Forgets all the boundaries and dimensions of cells and resets the identifier counter.
Definition: algcell.h:268
int_t boundaryLength() const
Returns the length of the boundary of the elementary cube.
Definition: algcell.h:220
~tAlgCell()
The destructor.
Definition: algcell.h:197
DimT DimType
The type of integer used to store the dimension of the cell.
Definition: algcell.h:61
static chomp::homology::multitable< DimT > dimensions
A global array of dimensions of the cells.
Definition: algcell.h:130
const DimT & dim() const
Returns the dimension of the cell.
Definition: algcell.h:203
tCombChain< CellT > boundary(const tCombChain< CellT > &c, const CellRestrT &restr)
Returns the boundary of a given chain (takes boundary cells restricted by the given object...
Definition: boundary.h:156
A chain with coefficients in an arbitrary ring.
Definition: chain.h:50
static chomp::homology::multitable< tChain< tAlgCell< IdT, DimT, CoefT >, CoefT > > boundaries
A global array of boundaries of the cells.
Definition: algcell.h:138
tAlgCell()
The default constructor of an empty cell (id 0).
Definition: algcell.h:160
bool operator==(const tAlgCell< IdT, DimT, CoefT > &s) const
The equality operator.
Definition: algcell.h:233
static IdT maxUsedId
The next free identifier for a new cell.
Definition: algcell.h:127
int_t hashkey2(const tAlgCell< IdT, DimT, CoefT > &c)
Generates a hashing key no.
Definition: algcell.h:301
int_t hashkey1(const tAlgCell< IdT, DimT, CoefT > &c)
Generates a hashing key no.
Definition: algcell.h:293
static bool dim0initialized
A global variable remembering if the dimension of cell 0 was already set to -1.
Definition: algcell.h:134
IdT id
The identifier of the cell.
Definition: algcell.h:124
A chain with coefficients in an arbitrary commutative ring.
std::istream & operator>>(std::istream &in, tAlgCell< IdT, DimT, CoefT > &c)
Reads an elementary cube from the input stream in the text format.
Definition: algcell.h:325
const IdT & getId() const
Returns the identifier of the cell.
Definition: algcell.h:214
IdT IdType
The type of a cell identifier.
Definition: algcell.h:58
An abstract algebraic cell, characterized by a unique identifier, dimension, and a formula for its bo...
Definition: algcell.h:54
static void setDimension(const IdT &identifier, const DimT &dimension)
Sets the dimension of the cell with the given identifier.
Definition: algcell.h:252
const CoefT & boundaryCoef(int_t n) const
Returns the n-th coefficient in the boundary of the cube.
Definition: algcell.h:226
void swap(tAlgCell< IdT, DimT, CoefT > &s)
Swaps the data with another cell.
Definition: algcell.h:239
static void setBoundary(const IdT &identifier, const tChain< tAlgCell< IdT, DimT, CoefT >, CoefT > &boundary)
Sets the boundary of the cell with the given identifier.
Definition: algcell.h:260