The ChainCon Software (Release 0.03)
cellnames.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// A class for naming cells for nice text data output.
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 10, 2013.
26 
27 
28 #ifndef _CHAINCON_CELLNAMES_H_
29 #define _CHAINCON_CELLNAMES_H_
30 
31 
32 // include some standard C++ header files
33 //#include <istream>
34 #include <ostream>
35 #include <string>
36 #include <sstream>
37 #include <algorithm>
38 
39 // include selected header files from the CHomP library
40 #include "chomp/system/config.h"
41 #include "chomp/struct/multitab.h"
42 #include "chomp/struct/hashsets.h"
43 
44 // include relevant local header files
45 #include "chaincon/stringhash.h"
46 #include "chaincon/combchain.h"
47 #include "chaincon/chain.h"
48 #include "chaincon/combtensor.h"
49 #include "chaincon/tensor.h"
50 #include "chaincon/comblinmap.h"
51 #include "chaincon/linmap.h"
52 
53 
54 // --------------------------------------------------
55 // ------------------- cell names -------------------
56 // --------------------------------------------------
57 
58 /// A class whose instances can be used to generate names of cells
59 /// with subsequent numbers, in each dimension separately.
60 template <class CellT>
62 {
63 public:
64  /// The type of a cell identifier.
65  typedef CellT CellType;
66 
67  /// The type of the name of a cell.
68  typedef std::string NameType;
69 
70  /// The default constructor of an empty object.
71  tCellNames ();
72 
73  /// The copy constructor.
74  tCellNames (const tCellNames<CellT> &c);
75 
76  /// The assignment operator.
78 
79  /// The destructor.
80  ~tCellNames ();
81 
82  /// Sets the prefix for cell names. If the prefix is nonempty
83  /// then the cell names are formed from this perfix
84  /// with dimension indicator and a subsequent number.
85  void setPrefix (const std::string &s);
86 
87  /// Sets the postfix for cell names. This is a string
88  /// that is appended at the end of every cell name.
89  void setPostfix (const std::string &s);
90 
91  /// Returns the name of the given cell.
92  std::string operator () (const CellT &c);
93 
94  /// Returns the smallest strict upper bound for the dimensions
95  /// of named cells.
96  int maxDim () const;
97 
98  /// The equality operator.
99  bool operator == (const tCellNames<CellT> &s) const;
100 
101  /// Swaps the data with another object.
102  void swap (tCellNames<CellT> &s);
103 
104 private:
105  /// An array of sets of cells whose names have been requested.
106  chomp::homology::multitable<chomp::homology::hashedset<CellT> >
108 
109  /// A prefix for cell names (use none for alphabetic names).
110  std::string prefix;
111 
112  /// A postfix for cell names.
113  std::string postfix;
114 
115  /// The number of sets of encountered cells,
116  /// indexed by their dimension.
117  /// In other words, this is the minimal strict upper bound
118  /// for the dimensions of cells.
119  int nSets;
120 
121 }; /* class tCellNames */
122 
123 // --------------------------------------------------
124 
125 template <class CellT>
127 {
128  return;
129 } /* tCellNames::tCellNames */
130 
131 template <class CellT>
133  cells (c. cells), prefix (c. prefix), postfix (c. postfix),
134  nSets (c. nSets)
135 {
136  return;
137 } /* tCellNames::tCellNames */
138 
139 template <class CellT>
141  (const tCellNames<CellT> &c)
142 {
143  cells = c. cells;
144  prefix = c. prefix;
145  postfix = c. postfix;
146  nSets = c. nSets;
147  return *this;
148 } /* tCellNames::operator = */
149 
150 template <class CellT>
152 {
153  return;
154 } /* tCellNames::~tCellNames */
155 
156 template <class CellT>
157 inline void tCellNames<CellT>::setPrefix (const std::string &s)
158 {
159  prefix = s;
160  return;
161 } /* tCellNames::setPrefix */
162 
163 template <class CellT>
164 inline void tCellNames<CellT>::setPostfix (const std::string &s)
165 {
166  postfix = s;
167  return;
168 } /* tCellNames::setPostfix */
169 
170 template <class CellT>
171 inline int tCellNames<CellT>::maxDim () const
172 {
173  return nSets;
174 } /* tCellNames::maxDim */
175 
176 template <class CellT>
177 inline std::string tCellNames<CellT>::operator () (const CellT &c)
178 {
179  // determine the dimension of the cell and update the number of sets
180  int dim = c. dim ();
181  if (dim >= nSets)
182  nSets = dim + 1;
183 
184  // add the cell to the hashed set if necessary and get its number
185  int_t n = cells [dim]. add (c) + 1;
186 
187  // generate the cell name and return it
188  std::ostringstream s;
189  if (prefix. empty ())
190  {
191  if (dim < 26)
192  {
193  s << static_cast<char> ('a' + dim);
194  }
195  else if (dim < 26 * 26)
196  {
197  s << static_cast<char> ('a' + dim / 26 - 1);
198  s << static_cast<char> ('a' + dim % 26);
199  }
200  else
201  {
202  throw "Dimension too high for a cell name.";
203  }
204  s << n;
205  }
206  else
207  {
208  s << prefix << "^" << dim << "_" << n;
209  }
210  s << postfix;
211  return s. str ();
212 } /* tCellNames::operator () */
213 
214 template <class CellT>
216 {
217  return ((cells == cells) && (prefix == prefix) &&
218  (postfix == postfix));
219 } /* tCellNames::operator == */
220 
221 template <class CellT>
223 {
224  cells. swap (c. cells);
225  prefix. swap (c. prefix);
226  postfix. swap (c. postfix);
227  std::swap (nSets, c. nSets);
228  return;
229 } /* tCellNames::swap */
230 
231 
232 // --------------------------------------------------
233 // ------------ pass cells through names ------------
234 // --------------------------------------------------
235 
236 /// A class for providing the cells as their names.
237 template <class CellT>
239 {
240 public:
241  /// The type of a cell identifier.
242  typedef CellT CellType;
243 
244  /// The type of the name of a cell.
245  typedef CellT NameType;
246 
247  /// The default constructor of an empty object.
248  tCellOwnNames ();
249 
250  // The default copy constructor is OK.
251  //tCellOwnNames (const tCellOwnNames<CellT> &c);
252 
253  // The default assignment operator is OK.
254  //tCellOwnNames<CellT> &operator = (const tCellOwnNames<CellT> &s);
255 
256  // The default destructor is OK.
257  //~tCellOwnNames ();
258 
259  /// Dummy setPrefix procedure.
260  void setPrefix (const std::string &s);
261 
262  /// Dummy setPostfix procedure.
263  void setPostfix (const std::string &s);
264 
265  /// Returns the name of the given cell.
266  CellT operator () (const CellT &c);
267 
268  /// Returns the smallest strict upper bound for the dimensions
269  /// of named cells.
270  int maxDim () const;
271 
272  /// The equality operator.
273  bool operator == (const tCellOwnNames<CellT> &s) const;
274 
275  /// Swaps the data with another object.
276  void swap (tCellOwnNames<CellT> &s);
277 
278 private:
279  /// The maximal dimension of the encountered cells plus 1.
281 
282 }; /* class tCellOwnNames */
283 
284 // --------------------------------------------------
285 
286 template <class CellT>
287 inline tCellOwnNames<CellT>::tCellOwnNames (): maxCellDim1 (0)
288 {
289  return;
290 } /* tCellOwnNames::tCellOwnNames */
291 
292 template <class CellT>
293 inline void tCellOwnNames<CellT>::setPrefix (const std::string &)
294 {
295  return;
296 } /* tCellOwnNames::setPrefix */
297 
298 template <class CellT>
299 inline void tCellOwnNames<CellT>::setPostfix (const std::string &)
300 {
301  return;
302 } /* tCellOwnNames::setPostfix */
303 
304 template <class CellT>
305 inline int tCellOwnNames<CellT>::maxDim () const
306 {
307  return maxCellDim1;
308 } /* tCellOwnNames::maxDim */
309 
310 template <class CellT>
311 inline CellT tCellOwnNames<CellT>::operator () (const CellT &c)
312 {
313  // determine the dimension of the cell and update the stored max dim
314  int dim = c. dim ();
315  if (dim >= maxCellDim1)
316  maxCellDim1 = dim + 1;
317 
318  // return the cell as its own name
319  return c;
320 } /* tCellOwnNames::operator () */
321 
322 template <class CellT>
323 inline bool tCellOwnNames<CellT>::operator ==
324  (const tCellOwnNames<CellT> &s) const
325 {
326  return maxCellDim1 == s. maxCellDim1;
327 } /* tCellOwnNames::operator == */
328 
329 template <class CellT>
331 {
332  std::swap (maxCellDim1, c. maxCellDim1);
333  return;
334 } /* tCellOwnNames::swap */
335 
336 
337 // --------------------------------------------------
338 // ----------- cells to names converters ------------
339 // --------------------------------------------------
340 
341 /// Returns the name of a cell according to the given naming object.
342 template <class CellT, class NamesT>
343 inline typename NamesT::NameType cells2names (const CellT &in, NamesT &names)
344 {
345  return names (in);
346 } /* cells2names */
347 
348 /// Returns a combinatorial chain in which the cells have been replaced
349 /// with their names, given by the provided naming object.
350 template <class CellT, class NamesT>
352  (const tCombChain<CellT> &in, NamesT &names)
353 {
355  int_t size = in. size ();
356  for (int_t i = 0; i < size; ++ i)
357  out. add (names (in. getCell (i)));
358  return out;
359 } /* cells2names */
360 
361 /// Returns a chain in which the cells have been replaced
362 /// with their names, given by the provided naming object.
363 template <class CellT, class CoefT, class NamesT>
365  (const tChain<CellT,CoefT> &in, NamesT &names)
366 {
368  int_t size = in. size ();
369  for (int_t i = 0; i < size; ++ i)
370  out. add (names (in. getCell (i)), in. getCoef (i));
371  return out;
372 } /* cells2names */
373 
374 /// Returns a combinatorial tensor in which the cells have been replaced
375 /// with their names, given by the provided naming object.
376 template <class Cell1T, class Cell2T, class Names1T, class Names2T>
379  Names1T &names1, Names2T &names2)
380 {
382  out;
383  int_t size = in. size ();
384  for (int_t i = 0; i < size; ++ i)
385  out. add (names1 (in. left (i)), names2 (in. right (i)));
386  return out;
387 } /* cells2names */
388 
389 /// Returns a tensor in which the cells have been replaced
390 /// with their names, given by the provided naming object.
391 template <class Cell1T, class Cell2T, class CoefT,
392  class Names1T, class Names2T>
395  Names1T &names1, Names2T &names2)
396 {
398  out;
399  int_t size = in. size ();
400  for (int_t i = 0; i < size; ++ i)
401  {
402  out. add (names1 (in. left (i)), names2 (in. right (i)),
403  in. coef (i));
404  }
405  return out;
406 } /* cells2names */
407 
408 /// Returns a combinatorial linear map in which the cells have been replaced
409 /// with their names, given by the provided naming object.
410 template <class CellDomT, class CellImgT, class NamesDomT, class NamesImgT>
413  NamesDomT &names1, NamesImgT &names2)
414 {
415  tCombLinMap<typename NamesDomT::NameType,
416  typename NamesImgT::NameType> out;
417  int_t size = in. size ();
418  for (int_t i = 0; i < size; ++ i)
419  out. add (names1 (in [i]), cells2names (in (i), names2));
420  return out;
421 } /* cells2names */
422 
423 /// Returns a linear map in which the cells have been replaced
424 /// with their names, given by the provided naming object.
425 template <class CellDomT, class CellImgT, class CoefT,
426  class NamesDomT, class NamesImgT>
427 inline tLinMap<typename NamesDomT::NameType,typename NamesImgT::NameType,
429  NamesDomT &names1, NamesImgT &names2)
430 {
431  tLinMap<typename NamesDomT::NameType,typename NamesImgT::NameType,
432  CoefT> out;
433  int_t size = in. size ();
434  for (int_t i = 0; i < size; ++ i)
435  out. add (names1 (in [i]), cells2names (in (i), names2));
436  return out;
437 } /* cells2names */
438 
439 
440 #endif // _CHAINCON_CELLNAMES_H_
441 
CellT CellType
The type of a cell identifier.
Definition: cellnames.h:242
std::string operator()(const CellT &c)
Returns the name of the given cell.
Definition: cellnames.h:177
A linear map for coefficients in an arbitrary commutative ring.
int maxDim() const
Returns the smallest strict upper bound for the dimensions of named cells.
Definition: cellnames.h:305
void swap(tCellOwnNames< CellT > &s)
Swaps the data with another object.
Definition: cellnames.h:330
void setPrefix(const std::string &s)
Sets the prefix for cell names.
Definition: cellnames.h:157
void setPrefix(const std::string &s)
Dummy setPrefix procedure.
Definition: cellnames.h:293
void swap(tCellNames< CellT > &s)
Swaps the data with another object.
Definition: cellnames.h:222
int maxDim() const
Returns the smallest strict upper bound for the dimensions of named cells.
Definition: cellnames.h:171
A combinatorial chain, that is, a chain with Z_2 coefficients.
tCellNames()
The default constructor of an empty object.
Definition: cellnames.h:126
A chain with coefficients in an arbitrary ring.
Definition: chain.h:50
std::string NameType
The type of the name of a cell.
Definition: cellnames.h:68
int nSets
The number of sets of encountered cells, indexed by their dimension.
Definition: cellnames.h:119
std::string prefix
A prefix for cell names (use none for alphabetic names).
Definition: cellnames.h:110
A class whose instances can be used to generate names of cells with subsequent numbers, in each dimension separately.
Definition: cellnames.h:61
A combinatorial linear map (for coefficients in Z_2).
A tensor of chains with coefficients in an arbitrary commutative ring.
A linear map.
Definition: linmap.h:55
A class for providing the cells as their names.
Definition: cellnames.h:238
Tensor of chains.
Definition: tensor.h:52
tCellNames< CellT > & operator=(const tCellNames< CellT > &s)
The assignment operator.
Definition: cellnames.h:141
Combinatorial tensor of cells.
Definition: combtensor.h:53
A combinatorial chain.
Definition: combchain.h:49
A combinatorial tensor (for coefficients in Z_2).
tCellOwnNames()
The default constructor of an empty object.
Definition: cellnames.h:287
Hashing keys for std::string.
void setPostfix(const std::string &s)
Sets the postfix for cell names.
Definition: cellnames.h:164
NamesT::NameType cells2names(const CellT &in, NamesT &names)
Returns the name of a cell according to the given naming object.
Definition: cellnames.h:343
std::string postfix
A postfix for cell names.
Definition: cellnames.h:113
A chain with coefficients in an arbitrary commutative ring.
int maxCellDim1
The maximal dimension of the encountered cells plus 1.
Definition: cellnames.h:280
bool operator==(const tCellNames< CellT > &s) const
The equality operator.
Definition: cellnames.h:215
~tCellNames()
The destructor.
Definition: cellnames.h:151
CellT operator()(const CellT &c)
Returns the name of the given cell.
Definition: cellnames.h:311
void setPostfix(const std::string &s)
Dummy setPostfix procedure.
Definition: cellnames.h:299
CellT NameType
The type of the name of a cell.
Definition: cellnames.h:245
A combinatorial linear map.
Definition: comblinmap.h:56
chomp::homology::multitable< chomp::homology::hashedset< CellT > > cells
An array of sets of cells whose names have been requested.
Definition: cellnames.h:107
CellT CellType
The type of a cell identifier.
Definition: cellnames.h:65