The ChainCon Software (Release 0.03)
filtcomplex.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A filtered cell complex.
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: January 11, 2013.
26 
27 
28 #ifndef _CHAINCON_FILTCOMPLEX_H_
29 #define _CHAINCON_FILTCOMPLEX_H_
30 
31 
32 // include some standard C++ header files
33 #include <istream>
34 #include <ostream>
35 
36 // include selected header files from the CHomP library
37 #include "chomp/system/config.h"
38 #include "chomp/system/textfile.h"
39 #include "chomp/struct/hashsets.h"
40 
41 
42 // --------------------------------------------------
43 // ------------- filtered cell complex --------------
44 // --------------------------------------------------
45 
46 /// A filtered complex. Cells must be added to this complex
47 /// in an order that ensures that any cell that appears in the boundary
48 /// of another cell is added AFTER that cell.
49 /// Contains a method for generating the boundaries of all the cells
50 /// that were previously added to the complex.
51 /// Then the cells can be accessed in the reversed order using operator [].
52 template <class CellT>
54 {
55 public:
56  /// The type of cells in the filtered complex.
57  typedef CellT CellType;
58 
59  /// The type of elements in the filtered complex
60  /// perceived as a standard-type aggregate.
61  typedef CellT value_type;
62 
63  /// The default constructor.
65 
66  /// Adds a cell to the complex. Does not add its boundary cells.
67  /// Returns the number of the added cell in the complex.
68  int_t add (const CellT &c);
69 
70  /// Finds a cell in the complex.
71  /// Returns its number or -1 if not found.
72  int_t getNumber (const CellT &c) const;
73 
74  /// Checks if a cell is in the complex.
75  bool check (const CellT &c) const;
76 
77  /// Returns the size of the complex.
78  int_t size () const;
79 
80  /// Returns the information on whether the complex is empty or not.
81  bool empty () const;
82 
83  /// Returns the given cell in the complex in the reversed order.
84  const CellT &operator [] (int_t n) const;
85 
86  /// Swaps the data with another filtered complex.
87  void swap (tFilteredComplex<CellT> &fc);
88 
89 private:
90  /// The set of all the cells in the complex in the right order.
91  chomp::homology::hashedset<CellT> cells;
92 
93 }; /* class tFilteredComplex */
94 
95 // --------------------------------------------------
96 
97 template <class CellT>
99  cells (1024)
100 {
101  return;
102 } /* tFilteredComplex::tFilteredComplex */
103 
104 template <class CellT>
105 inline int_t tFilteredComplex<CellT>::add (const CellT &c)
106 {
107  return cells. add (c);
108 } /* tFilteredComplex::add */
109 
110 template <class CellT>
111 inline int_t tFilteredComplex<CellT>::getNumber (const CellT &c) const
112 {
113  int_t n = cells. getnumber (c);
114  return (n >= 0) ? (cells. size () - n - 1) : -1;
115 } /* tFilteredComplex::getNumber */
116 
117 template <class CellT>
118 inline bool tFilteredComplex<CellT>::check (const CellT &c) const
119 {
120  return cells. check (c);
121 } /* tFilteredComplex::check */
122 
123 template <class CellT>
124 inline int_t tFilteredComplex<CellT>::size () const
125 {
126  return cells. size ();
127 } /* tFilteredComplex::size */
128 
129 template <class CellT>
130 inline bool tFilteredComplex<CellT>::empty () const
131 {
132  return cells. empty ();
133 } /* tFilteredComplex::empty */
134 
135 template <class CellT>
136 inline const CellT &tFilteredComplex<CellT>::operator [] (int_t n) const
137 {
138  int_t size = cells. size ();
139  if ((n < 0) || (n >= size))
140  throw "Wrong number of a cell in a filtered complex.";
141  return cells [size - n - 1];
142 } /* tFilteredComplex::operator [] */
143 
144 template <class CellT>
146 {
147  cells. swap (fc. cells);
148 } /* tFilteredComplex::swap */
149 
150 
151 // --------------------------------------------------
152 
153 /// Writes a filtered cell complex to an output stream in the text format.
154 template <class CellT>
155 std::ostream &operator << (std::ostream &out,
156  const tFilteredComplex<CellT> &C)
157 {
158  int_t size = C. size ();
159  for (int_t i = 0; i < size; ++ i)
160  {
161  out << C [i] << "\n";
162  }
163  return out;
164 } /* operator << */
165 
166 /// Reads a filtered cell complex from an input stream.
167 /// The cells are supposed to be listed in the input stream
168 /// in the reverse order, that is, each cell should be followed
169 /// by its boundary cells, which is not verified in this procedure.
170 /// Reading the cells is interrupted in case of the empty cell
171 /// (which is added to the complex if the empty cell is in use)
172 /// or when the end of the stream is encountered.
173 template <class CellT>
174 std::istream &operator >> (std::istream &in, tFilteredComplex<CellT> &K)
175 {
176  chomp::homology::ignorecomments (in);
177  while (!in. eof ())
178  {
179  CellT s;
180  in >> s;
181  chomp::homology::ignorecomments (in);
182  if (s. dim () < 0)
183  {
184 #ifndef NO_EMPTY_CELL
185  K. add (s);
186 #endif
187  break;
188  }
189  K. add (s);
190  }
191  return in;
192 } /* operator >> */
193 
194 
195 #endif // _CHAINCON_FILTCOMPLEX_H_
196 
void swap(tFilteredComplex< CellT > &fc)
Swaps the data with another filtered complex.
Definition: filtcomplex.h:145
bool check(const CellT &c) const
Checks if a cell is in the complex.
Definition: filtcomplex.h:118
const CellT & operator[](int_t n) const
Returns the given cell in the complex in the reversed order.
Definition: filtcomplex.h:136
chomp::homology::hashedset< CellT > cells
The set of all the cells in the complex in the right order.
Definition: filtcomplex.h:91
std::ostream & operator<<(std::ostream &out, const tFilteredComplex< CellT > &C)
Writes a filtered cell complex to an output stream in the text format.
Definition: filtcomplex.h:155
A filtered complex.
Definition: filtcomplex.h:53
int_t getNumber(const CellT &c) const
Finds a cell in the complex.
Definition: filtcomplex.h:111
CellT CellType
The type of cells in the filtered complex.
Definition: filtcomplex.h:57
bool empty() const
Returns the information on whether the complex is empty or not.
Definition: filtcomplex.h:130
CellT value_type
The type of elements in the filtered complex perceived as a standard-type aggregate.
Definition: filtcomplex.h:61
int_t add(const CellT &c)
Adds a cell to the complex.
Definition: filtcomplex.h:105
tFilteredComplex()
The default constructor.
Definition: filtcomplex.h:98
std::istream & operator>>(std::istream &in, tFilteredComplex< CellT > &K)
Reads a filtered cell complex from an input stream.
Definition: filtcomplex.h:174
int_t size() const
Returns the size of the complex.
Definition: filtcomplex.h:124