The ChainCon Software (Release 0.03)
combchain.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A combinatorial chain, that is, a chain with Z_2 coefficients.
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 19, 2012.
26 
27 
28 #ifndef _CHAINCON_COMBCHAIN_H_
29 #define _CHAINCON_COMBCHAIN_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/struct/hashsets.h"
39 #include "chaincon/ringz2.h"
40 
41 
42 // --------------------------------------------------
43 // -------------- combinatorial chain ---------------
44 // --------------------------------------------------
45 
46 /// A combinatorial chain. This is in fact a finite set of cells.
47 /// It corresponds to chains with coefficients in F_2 (a.k.a. Z_2).
48 template <class CellT>
50 {
51 public:
52  /// The type of cells in the combinatorial chain.
53  typedef CellT CellType;
54 
55  /// The type of coefficients in the chain.
56  typedef tZ2 CoefType;
57 
58  /// The default constructor of an empty combinatorial chain.
59  tCombChain ();
60 
61  /// Returns the number of elements in the combinatorial chain.
62  int_t size () const;
63 
64  /// Returns true if and only if the chain is zero (empty set).
65  bool empty () const;
66 
67  /// Returns the n-th element of the combinatorial chain.
68  const CellT &getCell (int_t n) const;
69 
70  /// Returns the position of the given cell in the chain
71  /// or -1 if the cell is not there.
72  int_t position (const CellT &c) const;
73 
74  /// Adds a given cell to the combinatorial chain.
75  void add (const CellT &c);
76 
77  /// Adds a given chain to the combinatorial chain.
79 
80  /// Negates all the coefficients in the chain.
82 
83  /// Compares if the two chains are equal.
84  bool operator == (const tCombChain<CellT> &ch) const;
85 
86  /// Swaps the data with another chain.
87  void swap (tCombChain<CellT> &ch);
88 
89 private:
90  /// The set of elements in the combinatorial chain.
91  chomp::homology::hashedset<CellT> cells;
92 
93 }; /* class tCombChain */
94 
95 // --------------------------------------------------
96 
97 template <class CellT>
99  cells (1)
100 {
101  return;
102 } /* tCombChain::tCombChain */
103 
104 template <class CellT>
105 inline int_t tCombChain<CellT>::size () const
106 {
107  return cells. size ();
108 } /* tCombChain::size */
109 
110 template <class CellT>
111 inline bool tCombChain<CellT>::empty () const
112 {
113  return cells. empty ();
114 } /* tCombChain::empty */
115 
116 template <class CellT>
117 inline const CellT &tCombChain<CellT>::getCell (int_t n) const
118 {
119  return cells [n];
120 } /* tCombChain::getCell */
121 
122 template <class CellT>
123 inline int_t tCombChain<CellT>::position (const CellT &c) const
124 {
125  return cells. getnumber (c);
126 } /* tCombChain::position */
127 
128 template <class CellT>
129 inline void tCombChain<CellT>::add (const CellT &c)
130 {
131  int_t n = cells. getnumber (c);
132  if (n < 0)
133  cells. add (c);
134  else
135  cells. removenum (n);
136  return;
137 } /* tCombChain::add */
138 
139 template <class CellT>
141  (const tCombChain<CellT> &ch)
142 {
143  if (empty ())
144  {
145  tCombChain<CellT> chainCopy (ch);
146  swap (chainCopy);
147  }
148  else
149  {
150  int_t size = ch. size ();
151  for (int_t i = 0; i < size; ++ i)
152  this -> add (ch. getCell (i));
153  }
154  return *this;
155 } /* tCombChain::operator += */
156 
157 template <class CellT>
159 {
160  return *this;
161 } /* tChain::negate */
162 
163 template <class CellT>
165  const
166 {
167  int_t size = cells. size ();
168  int_t chSize = ch. cells. size ();
169  if (size != chSize)
170  return false;
171  for (int_t i = 0; i < size; ++ i)
172  {
173  if (!ch. cells. check (cells [i]))
174  return false;
175  }
176  return true;
177 } /* tCombChain::operator == */
178 
179 template <class CellT>
181 {
182  cells. swap (ch. cells);
183  return;
184 } /* tCombChain::swap */
185 
186 // --------------------------------------------------
187 
188 /// Writes a combinatorial chain to an output stream.
189 template <class CellT>
190 std::ostream &operator << (std::ostream &out, const tCombChain<CellT> &c)
191 {
192  int_t size = c. size ();
193  if (!size)
194  out << '0';
195  else
196  {
197  for (int_t i = 0; i < size; ++ i)
198  {
199  if (i)
200  out << "+";
201  out << c. getCell (i);
202  }
203  }
204  return out;
205 } /* operator << */
206 
207 /// Reads a combinatorial chain from an input stream.
208 template <class CellT>
209 std::istream &operator >> (std::istream &in, tCombChain<CellT> &c)
210 {
211  throw "Operator >> not implemented for tCombChain.";
212  return in;
213 } /* operator >> */
214 
215 // --------------------------------------------------
216 
217 template <class CellT>
219  const tCombChain<CellT> &b)
220 {
221  tCombChain<CellT> c (a);
222  c += b;
223  return c;
224 } /* operator + */
225 
226 
227 #endif // _CHAINCON_COMBCHAIN_H_
228 
tCombChain< CellT > & negate()
Negates all the coefficients in the chain.
Definition: combchain.h:158
An element of the ring Z_2.
Definition: ringz2.h:47
tCombChain< CellT > operator+(const tCombChain< CellT > &a, const tCombChain< CellT > &b)
Definition: combchain.h:218
void swap(tCombChain< CellT > &ch)
Swaps the data with another chain.
Definition: combchain.h:180
bool operator==(const tCombChain< CellT > &ch) const
Compares if the two chains are equal.
Definition: combchain.h:164
const CellT & getCell(int_t n) const
Returns the n-th element of the combinatorial chain.
Definition: combchain.h:117
tZ2 CoefType
The type of coefficients in the chain.
Definition: combchain.h:56
int_t size() const
Returns the number of elements in the combinatorial chain.
Definition: combchain.h:105
A combinatorial chain.
Definition: combchain.h:49
int_t position(const CellT &c) const
Returns the position of the given cell in the chain or -1 if the cell is not there.
Definition: combchain.h:123
void add(const CellT &c)
Adds a given cell to the combinatorial chain.
Definition: combchain.h:129
Elements of the ring Z_2.
std::istream & operator>>(std::istream &in, tCombChain< CellT > &c)
Reads a combinatorial chain from an input stream.
Definition: combchain.h:209
chomp::homology::hashedset< CellT > cells
The set of elements in the combinatorial chain.
Definition: combchain.h:91
tCombChain()
The default constructor of an empty combinatorial chain.
Definition: combchain.h:98
tCombChain< CellT > & operator+=(const tCombChain< CellT > &ch)
Adds a given chain to the combinatorial chain.
Definition: combchain.h:141
CellT CellType
The type of cells in the combinatorial chain.
Definition: combchain.h:53
bool empty() const
Returns true if and only if the chain is zero (empty set).
Definition: combchain.h:111