The ChainCon Software (Release 0.03)
simplset.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A simplicial set.
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: August 21, 2013.
26 
27 
28 #ifndef _CHAINCON_SIMPLSET_H_
29 #define _CHAINCON_SIMPLSET_H_
30 
31 
32 // include some standard C++ header files
33 #include <istream>
34 #include <ostream>
35 #include <algorithm>
36 #include <vector>
37 
38 // include selected header files from the CHomP library
39 #include "chomp/system/config.h"
40 
41 // include relevant local header files
42 //#include "chaincon/emptycell.h"
43 
44 
45 // --------------------------------------------------
46 // ----------------- simplicial set -----------------
47 // --------------------------------------------------
48 
49 /// A simplicial set.
50 template <class CellT>
51 class tSimplSet
52 {
53 public:
54  /// The type of the vertex.
55  typedef CellT CellType;
56 
57  /// The type of elements in the filtered complex
58  /// perceived as a standard-type aggregate.
59  typedef CellT value_type;
60 
61  /// The default constructor of an empty set.
62  tSimplSet ();
63 
64  /// The copy constructor.
65  tSimplSet (const tSimplSet<CellT> &s);
66 
67  /// The assignment operator.
69 
70  /// The destructor.
71  ~tSimplSet ();
72 
73  /// The equality operator.
74  bool operator == (const tSimplSet<CellT> &s) const;
75 
76  /// Swaps the data between two simplicial sets.
77  void swap (tSimplSet<CellT> &s);
78 
79  /// Returns the dimension of the highest-dimensional
80  /// non-degenerate cell in the simplicial set, or -1 if none.
81  int dim () const;
82 
83  /// Adds a cell to the complex. Does not add its boundary cells.
84  /// Returns the number of the added cell in the complex.
85  int_t add (const CellT &c);
86 
87  /// Finds a cell in the complex.
88  /// Returns its number or -1 if not found.
89  int_t getNumber (const CellT &c) const;
90 
91  /// Checks if a cell is in the complex.
92  bool check (const CellT &c) const;
93 
94  /// Returns the number of cells in the simplicial set.
95  int_t size () const;
96 
97  /// Returns the information on whether the complex is empty or not.
98  bool empty () const;
99 
100  /// Returns the given cell in the simplicial set.
101  const CellT &operator [] (int_t n) const;
102 
103  /// Adds faces of all the cells of the given dimension.
104  /// Returns the number of faces added.
105  int_t addFaces (int dim);
106 
107  /// Adds faces of all the cells in the set.
108  /// Returns the number of faces added.
109  int_t addFaces ();
110 
111  /// Adds degenerate cells for all cells whose dimension falls
112  /// within the given range, including minDim,
113  /// up to but excluding maxDim.
114  /// Also adds degenerate cells for all the added cells,
115  /// taking the dimension criterion into consideration.
116  /// Returns the number of degenerate cells added.
117  int_t addDegenerate (int minDim, int maxDim);
118 
119  /// Normalizes the set of cells, that is, removes degenerate cells.
120  /// Note that the order and the numbers of cells may change.
121  /// Returns the number of degenerate cells removed.
122  int_t normalize ();
123 
124  /// Adds 1 to the reference counter. If in use, returns the number
125  /// that indicates how many references to the object exist.
126  int_t addRef ();
127 
128  /// Subtracts 1 from the reference counter. If in use, returns the
129  /// number that indicates how many references of the object remain.
130  int_t delRef ();
131 
132 private:
133  /// The highest dimension of a non-degenerate cell.
135 
136  /// The set of all the cells in the complex.
137  chomp::homology::hashedset<CellT> cells;
138 
139  /// Reference counter. If in use, keeps track on how many
140  /// references to the object exist in the program.
141  /// Note that if this value is > 0 then the "normalize"
142  /// operation is not permitted, because it may change
143  /// the order of cells in the array.
144  int_t refCount;
145 
146 }; /* class tSimplSet */
147 
148 // --------------------------------------------------
149 
150 template <class CellT>
152  dimension (-1), cells (0), refCount (0)
153 {
154  return;
155 } /* tSimplSet::tSimplSet */
156 
157 template <class CellT>
159  dimension (s. dimension), cells (s. cells), refCount (0)
160 {
161  return;
162 } /* tSimplSet::tSimplSet */
163 
164 template <class CellT>
166  (const tSimplSet<CellT> &s)
167 {
168  if (this == &s)
169  return *this;
170  if (refCount > 0)
171  {
172  chomp::homology::sbug << "WARNING: Assigning a simplicial "
173  "set to one with " << refCount << " references.\n";
174  }
175  dimension = s. dimension ();
176  cells = s. cells;
177  refCount = 0;
178  return *this;
179 } /* tSimplSet::operator = */
180 
181 template <class CellT>
183 {
184  if (refCount > 0)
185  {
186  chomp::homology::sbug << "WARNING: A simplicial set "
187  "with " << refCount << " references deleted.\n";
188  }
189  return;
190 } /* tSimplSet::~tSimplSet */
191 
192 template <class CellT>
193 inline bool tSimplSet<CellT>::operator ==
194  (const tSimplSet<CellT> &s) const
195 {
196  return ((this == &s) || (cells == s. cells));
197 } /* tSimplSet::operator == */
198 
199 template <class CellT>
201 {
202  std::swap (dimension, s. dimension);
203  if ((refCount > 0) || (s. refCount > 0))
204  {
205  chomp::homology::sbug << "WARNING: Swapping "
206  "simplicial sets with " << refCount << " + " <<
207  s. refCount << " references.\n";
208  }
209  refCount = 0;
210  s. refCount = 0;
211  cells. swap (s. cells);
212  return;
213 } /* tSimplSet::swap */
214 
215 template <class CellT>
216 inline int tSimplSet<CellT>::dim () const
217 {
218  return dimension;
219 } /* tSimplSet::dim */
220 
221 template <class CellT>
222 inline int_t tSimplSet<CellT>::add (const CellT &cell)
223 {
224  int dim = cell. dim ();
225  int degenerate = cell. degenerate ();
226  if (!degenerate && (dimension < dim))
227  dimension = dim;
228  return cells. add (cell);
229 } /* tSimplSet::add */
230 
231 template <class CellT>
232 inline int_t tSimplSet<CellT>::getNumber (const CellT &cell) const
233 {
234  return cells. getnumber (cell);
235 } /* tSimplSet::getNumber */
236 
237 template <class CellT>
238 inline bool tSimplSet<CellT>::check (const CellT &cell) const
239 {
240  return (cells. getnumber (cell) >= 0);
241 } /* tSimplSet::check */
242 
243 template <class CellT>
244 inline int_t tSimplSet<CellT>::size () const
245 {
246  return cells. size ();
247 } /* tSimplSet::size */
248 
249 template <class CellT>
250 inline bool tSimplSet<CellT>::empty () const
251 {
252  return cells. empty ();
253 } /* tSimplSet::empty */
254 
255 template <class CellT>
256 inline const CellT &tSimplSet<CellT>::operator [] (int_t n) const
257 {
258  int_t size = cells. size ();
259  if ((n < 0) || (n >= size))
260  throw "Wrong number of a cell in a simplicial set.";
261  return cells [n];
262 } /* tSimplSet::operator [] */
263 
264 template <class CellT>
265 inline int_t tSimplSet<CellT>::addFaces (int dim)
266 {
267  int_t prevSize (cells. size ());
268  for (int_t i = 0; i < prevSize; ++ i)
269  {
270  const CellT &cell (cells [i]);
271  if (cell. dim () != dim)
272  continue;
273  // int bLen (cell. boundaryLength ());
274  for (int j = 0; j <= dim; ++ j)
275  cells. add (CellT (cell, j));
276  }
277  return (cells. size () - prevSize);
278 } /* tSimplSet::addFaces */
279 
280 template <class CellT>
282 {
283  int_t added (0);
284  for (int_t dim = dimension; dim > 0; -- dim)
285  added += addFaces (dim);
286  return added;
287 } /* tSimplSet::addFaces */
288 
289 template <class CellT>
290 inline int_t tSimplSet<CellT>::addDegenerate (int minDim, int maxDim)
291 {
292  int_t prevSize = cells. size ();
293  for (int_t cur = 0; cur < cells. size (); ++ cur)
294  {
295  const CellT &cell (cells [cur]);
296  int dim (cell. dim ());
297  if ((dim < minDim) || (dim >= maxDim))
298  continue;
299  for (int j = 0; j <= dim; ++ j)
300  cells. add (CellT (cell, -1 - j));
301  }
302  return (cells. size () - prevSize);
303 } /* tSimplSet::addDegenerate */
304 
305 template <class CellT>
307 {
308  // NOTE: This procedure uses the fact that in a hashedset
309  // an element from the end of the array is put at the location
310  // of a removed one.
311  int_t prevSize = cells. size ();
312  for (int_t cur = prevSize - 1; cur >= 0; -- cur)
313  {
314  if (cells [cur]. degenerate ())
315  cells. removenum (cur);
316  }
317  return (prevSize - cells. size ());
318 } /* tSimplSet::normalize */
319 
320 template <class CellT>
322 {
323  ++ refCount;
324  return refCount;
325 } /* tSimplSet::addRef */
326 
327 template <class CellT>
329 {
330  -- refCount;
331  if (refCount < 0)
332  {
333  chomp::homology::sbug << "WARNING: Negative number "
334  "of references (" << refCount << ") to a "
335  "simplicial set encountered.\n";
336  }
337  return refCount;
338 } /* tSimplSet::delRef */
339 
340 // --------------------------------------------------
341 
342 /// Writes a simplicial set to an output stream using operator << for cells.
343 template <class CellT>
344 std::ostream &operator << (std::ostream &out, const tSimplSet<CellT> &s)
345 {
346  int_t size = s. size ();
347  for (int_t i = 0; i < size; ++ i)
348  {
349  out << s [i] << "\n";
350  }
351  return out;
352 } /* operator << */
353 
354 /// Reads a simplicial set from an input stream using oeprator >> for cells.
355 template <class CellT>
356 std::istream &operator >> (std::istream &in, tSimplSet<CellT> &s)
357 {
358  chomp::homology::ignorecomments (in);
359  while (!in. eof ())
360  {
361  CellT c;
362  in >> c;
363  chomp::homology::ignorecomments (in);
364  if (c. dim () < 0)
365  break;
366  s. add (c);
367  }
368  return in;
369 } /* operator >> */
370 
371 
372 #endif // _CHAINCON_SIMPLSET_H_
373 
chomp::homology::hashedset< CellT > cells
The set of all the cells in the complex.
Definition: simplset.h:137
int_t refCount
Reference counter.
Definition: simplset.h:144
int_t size() const
Returns the number of cells in the simplicial set.
Definition: simplset.h:244
tSimplSet< CellT > & operator=(const tSimplSet< CellT > &s)
The assignment operator.
Definition: simplset.h:166
int dim() const
Returns the dimension of the highest-dimensional non-degenerate cell in the simplicial set...
Definition: simplset.h:216
const CellT & operator[](int_t n) const
Returns the given cell in the simplicial set.
Definition: simplset.h:256
int_t addDegenerate(int minDim, int maxDim)
Adds degenerate cells for all cells whose dimension falls within the given range, including minDim...
Definition: simplset.h:290
CellT value_type
The type of elements in the filtered complex perceived as a standard-type aggregate.
Definition: simplset.h:59
bool operator==(const tSimplSet< CellT > &s) const
The equality operator.
Definition: simplset.h:194
int_t getNumber(const CellT &c) const
Finds a cell in the complex.
Definition: simplset.h:232
int_t addFaces()
Adds faces of all the cells in the set.
Definition: simplset.h:281
bool empty() const
Returns the information on whether the complex is empty or not.
Definition: simplset.h:250
int_t delRef()
Subtracts 1 from the reference counter.
Definition: simplset.h:328
void swap(tSimplSet< CellT > &s)
Swaps the data between two simplicial sets.
Definition: simplset.h:200
~tSimplSet()
The destructor.
Definition: simplset.h:182
int_t normalize()
Normalizes the set of cells, that is, removes degenerate cells.
Definition: simplset.h:306
int dimension
The highest dimension of a non-degenerate cell.
Definition: simplset.h:134
tSimplSet()
The default constructor of an empty set.
Definition: simplset.h:151
CellT CellType
The type of the vertex.
Definition: simplset.h:55
A simplicial set.
Definition: simplset.h:51
int_t add(const CellT &c)
Adds a cell to the complex.
Definition: simplset.h:222
std::istream & operator>>(std::istream &in, tSimplSet< CellT > &s)
Reads a simplicial set from an input stream using oeprator >> for cells.
Definition: simplset.h:356
int_t addRef()
Adds 1 to the reference counter.
Definition: simplset.h:321
bool check(const CellT &c) const
Checks if a cell is in the complex.
Definition: simplset.h:238