• Main Page
  • Classes
  • Files
  • File List
  • File Members

chaincon/combchain.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////
00002 ///
00003 /// \file
00004 ///
00005 /// A combinatorial chain, that is, a chain with Z_2 coefficients.
00006 ///
00007 /////////////////////////////////////////////////////////////////////////////
00008 
00009 // Copyright (C) 2009-2011 by Pawel Pilarczyk.
00010 //
00011 // This file is part of my research software package. This is free software:
00012 // you can redistribute it and/or modify it under the terms of the GNU
00013 // General Public License as published by the Free Software Foundation,
00014 // either version 3 of the License, or (at your option) any later version.
00015 //
00016 // This software is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU General Public License
00022 // along with this software; see the file "license.txt". If not,
00023 // please, see <http://www.gnu.org/licenses/>.
00024 
00025 // Started on March 24, 2009. Last revision: March 6, 2011.
00026 
00027 
00028 #ifndef _CHAINCON_COMBCHAIN_H_
00029 #define _CHAINCON_COMBCHAIN_H_
00030 
00031 
00032 // include some standard C++ header files
00033 #include <istream>
00034 #include <ostream>
00035 
00036 // include selected header files from the CHomP library
00037 #include "chomp/system/config.h"
00038 #include "chomp/struct/hashsets.h"
00039 
00040 
00041 // --------------------------------------------------
00042 // -------------- combinatorial chain ---------------
00043 // --------------------------------------------------
00044 
00045 /// A combinatorial chain. This is in fact a finite set of cells.
00046 /// It corresponds to chains with coefficients in F_2 (a.k.a. Z_2).
00047 template <class CellT>
00048 class tCombChain
00049 {
00050 public:
00051         /// The type of cells in the combinatorial chain.
00052         typedef CellT CellType;
00053 
00054         /// The default constructor of an empty combinatorial chain.
00055         tCombChain ();
00056 
00057         /// The constructor of a combinatorial chain containing one cell.
00058         explicit tCombChain (const CellT &c);
00059 
00060         /// Returns the number of elements in the combinatorial chain.
00061         int_t size () const;
00062 
00063         /// Returns true if and only if the chain is zero (empty set).
00064         bool empty () const;
00065 
00066         /// Returns the n-th element of the combinatorial chain.
00067         const CellT &operator [] (int_t n) const;
00068 
00069         /// Returns the position of the given cell in the chain
00070         /// or -1 if the cell is not there.
00071         int_t position (const CellT &c) const;
00072 
00073         /// Checks if the given cell appears in the chain.
00074         bool contains (const CellT &c) const;
00075 
00076         /// Adds a given cell to the combinatorial chain.
00077         void add (const CellT &c);
00078 
00079         /// Adds a given chain to the combinatorial chain.
00080         void add (const tCombChain<CellT> &ch);
00081 
00082         /// Compares if the two chains are equal.
00083         bool operator == (const tCombChain<CellT> &ch) const;
00084 
00085         /// Swaps the data with another chain.
00086         void swap (tCombChain<CellT> &ch);
00087 
00088 private:
00089         /// The set of elements in the combinatorial chain.
00090         chomp::homology::hashedset<CellT> cells;
00091 
00092 }; /* class tCombChain */
00093 
00094 // --------------------------------------------------
00095 
00096 template <class CellT>
00097 inline tCombChain<CellT>::tCombChain ():
00098         cells (1)
00099 {
00100         return;
00101 } /* tCombChain::tCombChain */
00102 
00103 template <class CellT>
00104 inline tCombChain<CellT>::tCombChain (const CellT &c):
00105         cells (1)
00106 {
00107         cells. add (c);
00108         return;
00109 } /* tCombChain::tCombChain */
00110 
00111 template <class CellT>
00112 inline int_t tCombChain<CellT>::size () const
00113 {
00114         return cells. size ();
00115 } /* tCombChain::size */
00116 
00117 template <class CellT>
00118 inline bool tCombChain<CellT>::empty () const
00119 {
00120         return cells. empty ();
00121 } /* tCombChain::empty */
00122 
00123 template <class CellT>
00124 inline const CellT &tCombChain<CellT>::operator [] (int_t n) const
00125 {
00126         return cells [n];
00127 } /* tCombChain::operator [] */
00128 
00129 template <class CellT>
00130 inline int_t tCombChain<CellT>::position (const CellT &c) const
00131 {
00132         return cells. getnumber (c);
00133 } /* tCombChain::position */
00134 
00135 template <class CellT>
00136 inline bool tCombChain<CellT>::contains (const CellT &c) const
00137 {
00138         return (cells. getnumber (c) >= 0);
00139 } /* tCombChain::contains */
00140 
00141 template <class CellT>
00142 inline void tCombChain<CellT>::add (const CellT &c)
00143 {
00144         int_t n = cells. getnumber (c);
00145         if (n < 0)
00146                 cells. add (c);
00147         else
00148                 cells. removenum (n);
00149         return;
00150 } /* tCombChain::add */
00151 
00152 template <class CellT>
00153 inline void tCombChain<CellT>::add (const tCombChain<CellT> &ch)
00154 {
00155         int_t size = ch. size ();
00156         for (int_t i = 0; i < size; ++ i)
00157                 this -> add (ch [i]);
00158         return;
00159 } /* tCombChain::add */
00160 
00161 template <class CellT>
00162 inline bool tCombChain<CellT>::operator == (const tCombChain<CellT> &ch)
00163         const
00164 {
00165         int_t size = cells. size ();
00166         int_t chSize = ch. cells. size ();
00167         if (size != chSize)
00168                 return false;
00169         for (int_t i = 0; i < size; ++ i)
00170         {
00171                 if (!ch. cells. check (cells [i]))
00172                         return false;
00173         }
00174         return true;
00175 } /* tCombChain::operator == */
00176 
00177 template <class CellT>
00178 inline void tCombChain<CellT>::swap (tCombChain<CellT> &ch)
00179 {
00180         cells. swap (ch. cells);
00181         return;
00182 } /* tCombChain::swap */
00183 
00184 // --------------------------------------------------
00185 
00186 /// Writes a combinatorial chain to an output stream.
00187 template <class CellT>
00188 std::ostream &operator << (std::ostream &out, const tCombChain<CellT> &c)
00189 {
00190         int_t size = c. size ();
00191         for (int_t i = 0; i < size; ++ i)
00192         {
00193                 if (i)
00194                         out << "+";
00195                 out << c [i];
00196         }
00197         return out;
00198 } /* operator << */
00199 
00200 /// Reads a combinatorial chain from an input stream.
00201 template <class CellT>
00202 std::istream &operator >> (std::istream &in, tCombChain<CellT> &c)
00203 {
00204         throw "Operator >> not implemented for tCombChain.";
00205         return in;
00206 } /* operator >> */
00207 
00208 // --------------------------------------------------
00209 
00210 template <class CellT>
00211 tCombChain<CellT> operator + (const tCombChain<CellT> &a,
00212         const tCombChain<CellT> &b)
00213 {
00214         tCombChain<CellT> c (a);
00215         c. add (b);
00216         return c;
00217 } /* operator + */
00218 
00219 // --------------------------------------------------
00220 
00221 /// Adds the boundary of a given cell to the provided chain.
00222 template <class CellT>
00223 inline void computeBoundary (const CellT &c, tCombChain<CellT> &b)
00224 {
00225         // determine the length of the boundary of this cell
00226         int length = c. boundaryLength ();
00227 
00228         // process all the cells in its boundary
00229         for (int i = 0; i < length; ++ i)
00230         {
00231                 // create the subsequent boundary cell
00232                 CellT bc (c, i);
00233 
00234                 // add the boundary cell to the given chain
00235                 b. add (bc);
00236         }
00237         return;
00238 } /* computeBoundary */
00239 
00240 /// Returns the boundary of a given cell.
00241 template <class CellT>
00242 inline tCombChain<CellT> boundary (const CellT &c)
00243 {
00244         // prepare a chain that is going to contain the boundary of c
00245         tCombChain<CellT> b;
00246 
00247         // add the boundary of the cell to b
00248         computeBoundary (c, b);
00249 
00250         // return the computed boundary chain
00251         return b;
00252 } /* boundary */
00253 
00254 /// Adds the boundary of a given chain to the provided chain.
00255 template <class CellT>
00256 inline void computeBoundary (const tCombChain<CellT> &c,
00257         tCombChain<CellT> &b)
00258 {
00259         int_t size = c. size ();
00260         for (int_t i = 0; i < size; ++ i)
00261                 computeBoundary (c [i], b);
00262         return;
00263 } /* computeBoundary */
00264 
00265 /// Returns the boundary of a given chain.
00266 template <class CellT>
00267 inline tCombChain<CellT> boundary (const tCombChain<CellT> &c)
00268 {
00269         // prepare a chain that is going to contain the boundary of c
00270         tCombChain<CellT> b;
00271 
00272         // add the boundary of each cell in the chain to b
00273         computeBoundary (c, b);
00274 
00275         // return the computed boundary chain
00276         return b;
00277 } /* boundary */
00278 
00279 
00280 #endif // _CHAINCON_COMBCHAIN_H_
00281 

Generated on Tue Apr 5 2011 00:06:32 for Chain Contraction Software by  doxygen 1.7.2