00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _CHAINCON_COMBLINMAP_H_
00029 #define _CHAINCON_COMBLINMAP_H_
00030
00031
00032
00033 #include <istream>
00034 #include <ostream>
00035
00036
00037 #include "chomp/system/config.h"
00038 #include "chomp/struct/hashsets.h"
00039 #include "chomp/struct/multitab.h"
00040
00041
00042 #include "chaincon/combchain.h"
00043 #include "chaincon/combtensor.h"
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 template <class CellDomT, class CellImgT>
00055 class tCombLinMap
00056 {
00057 public:
00058
00059 typedef CellDomT CellDomType;
00060
00061
00062 typedef CellImgT CellImgType;
00063
00064
00065 tCombLinMap ();
00066
00067
00068 void add (const CellDomT &c, const CellImgT &d);
00069
00070
00071 void add (const CellDomT &c, const tCombChain<CellImgT> &ch);
00072
00073
00074 void add (const tCombLinMap<CellDomT,CellImgT> &f);
00075
00076
00077
00078 void removenum (int_t n);
00079
00080
00081
00082 void remove (const CellDomT &c);
00083
00084
00085 const tCombChain<CellImgT> &operator () (int_t n) const;
00086
00087
00088 tCombChain<CellImgT> operator () (const CellDomT &c) const;
00089
00090
00091 tCombChain<CellImgT> operator ()
00092 (const tCombChain<CellDomT> &ch) const;
00093
00094
00095 tCombTensor<CellImgT> operator ()
00096 (const tCombTensor<CellDomT> &ch) const;
00097
00098
00099
00100
00101 int_t size () const;
00102
00103
00104 const CellDomT &operator [] (int_t n) const;
00105
00106
00107 tCombChain<CellImgT> &getImage (int_t n);
00108
00109
00110 tCombChain<CellImgT> &getImage (const CellDomT &c);
00111
00112
00113 bool operator == (const tCombLinMap<CellDomT,CellImgT> &f) const;
00114
00115
00116 void swap (tCombLinMap<CellDomT,CellImgT> &f);
00117
00118 private:
00119
00120 chomp::homology::hashedset<CellDomT> domain;
00121
00122
00123 chomp::homology::multitable<tCombChain<CellImgT> > images;
00124
00125 };
00126
00127
00128
00129 template <class CellDomT, class CellImgT>
00130 inline tCombLinMap<CellDomT,CellImgT>::tCombLinMap ():
00131 domain (1000), images (256)
00132 {
00133 return;
00134 }
00135
00136 template <class CellDomT, class CellImgT>
00137 inline void tCombLinMap<CellDomT,CellImgT>::add
00138 (const CellDomT &c, const CellImgT &d)
00139 {
00140 int_t n = domain. getnumber (c);
00141 if (n < 0)
00142 {
00143 n = domain. size ();
00144 domain. add (c);
00145 }
00146 images [n]. add (d);
00147 return;
00148 }
00149
00150 template <class CellDomT, class CellImgT>
00151 inline void tCombLinMap<CellDomT,CellImgT>::add
00152 (const CellDomT &c, const tCombChain<CellImgT> &ch)
00153 {
00154 int_t n = domain. getnumber (c);
00155 if (n < 0)
00156 {
00157 n = domain. size ();
00158 domain. add (c);
00159 }
00160 images [n]. add (ch);
00161 return;
00162 }
00163
00164 template <class CellDomT, class CellImgT>
00165 inline void tCombLinMap<CellDomT,CellImgT>::add
00166 (const tCombLinMap<CellDomT,CellImgT> &f)
00167 {
00168 int_t size = f. domain. size ();
00169 for (int_t i = 0; i < size; ++ i)
00170 {
00171 if (f. images [i]. empty ())
00172 continue;
00173 this -> add (f. domain [i], f. images [i]);
00174 }
00175 return;
00176 }
00177
00178 template <class CellDomT, class CellImgT>
00179 inline void tCombLinMap<CellDomT,CellImgT>::removenum (int_t n)
00180 {
00181 domain. removenum (n);
00182 tCombChain<CellImgT> empty;
00183 images [n] = empty;
00184 if (n != domain. size ())
00185 images [n]. swap (images [domain. size ()]);
00186 return;
00187 }
00188
00189 template <class CellDomT, class CellImgT>
00190 inline void tCombLinMap<CellDomT,CellImgT>::remove (const CellDomT &c)
00191 {
00192 int_t n = domain. getnumber (c);
00193 if (n >= 0)
00194 removenum (n);
00195 return;
00196 }
00197
00198 template <class CellDomT, class CellImgT>
00199 inline const tCombChain<CellImgT> &
00200 tCombLinMap<CellDomT,CellImgT>::operator () (int_t n) const
00201 {
00202 return images [n];
00203 }
00204
00205 template <class CellDomT, class CellImgT>
00206 inline tCombChain<CellImgT> tCombLinMap<CellDomT,CellImgT>::operator ()
00207 (const CellDomT &c) const
00208 {
00209 int_t n = domain. getnumber (c);
00210 if (n >= 0)
00211 return images [n];
00212 else
00213 return tCombChain<CellImgT> ();
00214 }
00215
00216 template <class CellDomT, class CellImgT>
00217 inline tCombChain<CellImgT> tCombLinMap<CellDomT,CellImgT>::operator ()
00218 (const tCombChain<CellDomT> &ch) const
00219 {
00220 tCombChain<CellImgT> image;
00221 int_t size = ch. size ();
00222 for (int_t i = 0; i < size; ++ i)
00223 {
00224 int_t n = domain. getnumber (ch [i]);
00225 if (n >= 0)
00226 image. add (images [n]);
00227 }
00228 return image;
00229 }
00230
00231 template <class CellDomT, class CellImgT>
00232 inline tCombTensor<CellImgT> tCombLinMap<CellDomT,CellImgT>::operator ()
00233 (const tCombTensor<CellDomT> &ch) const
00234 {
00235 tCombTensor<CellImgT> image;
00236 int_t size = ch. size ();
00237 for (int_t i = 0; i < size; ++ i)
00238 {
00239 int_t n1 = domain. getnumber (ch. left (i));
00240 int_t n2 = domain. getnumber (ch. right (i));
00241 if ((n1 >= 0) && (n2 >= 0))
00242 image. add (images [n1], images [n2]);
00243 }
00244 return image;
00245 }
00246
00247 template <class CellDomT, class CellImgT>
00248 inline int_t tCombLinMap<CellDomT,CellImgT>::size () const
00249 {
00250 return domain. size ();
00251 }
00252
00253 template <class CellDomT, class CellImgT>
00254 inline const CellDomT &tCombLinMap<CellDomT,CellImgT>::operator []
00255 (int_t n) const
00256 {
00257 return domain [n];
00258 }
00259
00260 template <class CellDomT, class CellImgT>
00261 inline tCombChain<CellImgT> &tCombLinMap<CellDomT,CellImgT>::getImage
00262 (int_t n)
00263 {
00264 return images [n];
00265 }
00266
00267 template <class CellDomT, class CellImgT>
00268 inline tCombChain<CellImgT> &tCombLinMap<CellDomT,CellImgT>::getImage
00269 (const CellDomT &c)
00270 {
00271 int_t n = domain. getnumber (c);
00272 if (n < 0)
00273 {
00274 n = domain. size ();
00275 domain. add (c);
00276 }
00277 return images [n];
00278 }
00279
00280 template <class CellDomT, class CellImgT>
00281 inline bool tCombLinMap<CellDomT,CellImgT>::operator ==
00282 (const tCombLinMap<CellDomT,CellImgT> &f) const
00283 {
00284 int_t thisSize = domain. size ();
00285 for (int_t i = 0; i < thisSize; ++ i)
00286 {
00287 const CellDomT &x = domain [i];
00288 const tCombChain<CellImgT> &y = images [i];
00289 if (y. empty ())
00290 continue;
00291 int_t n = f. domain. getnumber (x);
00292 if (n < 0)
00293 return false;
00294 if (!(y == f. images [n]))
00295 return false;
00296 }
00297 int_t fSize = f. domain. size ();
00298 for (int_t i = 0; i < fSize; ++ i)
00299 {
00300 const CellDomT &x = f. domain [i];
00301 const tCombChain<CellImgT> &y = f. images [i];
00302 if (y. empty ())
00303 continue;
00304 int_t n = domain. getnumber (x);
00305 if (n < 0)
00306 return false;
00307 if (!(y == images [n]))
00308 return false;
00309 }
00310 return true;
00311 }
00312
00313 template <class CellDomT, class CellImgT>
00314 inline void tCombLinMap<CellDomT,CellImgT>::swap
00315 (tCombLinMap<CellDomT,CellImgT> &f)
00316 {
00317 domain. swap (f. domain);
00318 images. swap (f. images);
00319 return;
00320 }
00321
00322
00323
00324
00325 template <class CellDomT, class CellImgT>
00326 inline std::ostream &operator << (std::ostream &out,
00327 const tCombLinMap<CellDomT,CellImgT> &f)
00328 {
00329 int_t size = f. size ();
00330 for (int_t n = 0; n < size; ++ n)
00331 {
00332 const tCombChain<CellImgT> &ch = f (n);
00333 if (ch. empty ())
00334 continue;
00335 const CellDomT &c = f [n];
00336 out << c << " -> " << ch << "\n";
00337 }
00338 return out;
00339 }
00340
00341
00342 template <class CellDomT, class CellImgT>
00343 inline std::istream &operator >> (std::istream &in,
00344 tCombLinMap<CellDomT,CellImgT> &f)
00345 {
00346 throw "Operator >> not implemented for tCombLinMap.";
00347 return in;
00348 }
00349
00350
00351
00352
00353 template <class CellArray, class CellT>
00354 inline void addIdentity (const CellArray &domain,
00355 tCombLinMap<CellT,CellT> &f)
00356 {
00357 int_t size = domain. size ();
00358 for (int_t i = 0; i < size; ++ i)
00359 {
00360 const CellT &c = domain [i];
00361 f. add (c, c);
00362 }
00363 return;
00364 }
00365
00366
00367 template <class CellArray, class CellT>
00368 inline tCombLinMap<CellT,CellT> identity (const CellArray &domain)
00369 {
00370 tCombLinMap<CellT,CellT> f;
00371 f. addIdentity (domain);
00372 return f;
00373 }
00374
00375
00376
00377
00378 template <class CellArray, class CellT>
00379 inline void computeBoundaryMap (const CellArray &cells,
00380 tCombLinMap<CellT,CellT> &f)
00381 {
00382
00383 chomp::homology::hashedset<CellT> waiting;
00384
00385
00386
00387 int_t inputCellNumber = 0;
00388 const int_t inputCellCount = cells. size ();
00389 while (1)
00390 {
00391
00392 bool useInput = (inputCellNumber < inputCellCount);
00393
00394
00395
00396 if (!useInput && waiting. empty ())
00397 break;
00398
00399
00400 int_t waitingPosition = waiting. size () - 1;
00401 const CellT &c = useInput ? cells [inputCellNumber] :
00402 waiting [waitingPosition];
00403
00404
00405 int length = c. boundaryLength ();
00406
00407
00408 for (int b = 0; b < length; ++ b)
00409 {
00410
00411 CellT bc (c, b);
00412
00413
00414 if (bc. boundaryLength () &&
00415 f. getImage (bc). empty ())
00416 {
00417 waiting. add (bc);
00418 }
00419
00420
00421 f. add (c, bc);
00422 }
00423
00424
00425 if (useInput)
00426 ++ inputCellNumber;
00427
00428
00429 else
00430 waiting. removenum (waitingPosition);
00431 }
00432 return;
00433 }
00434
00435
00436
00437
00438 template <class CellXT, class CellYT, class CellZT>
00439 inline tCombLinMap<CellXT,CellZT> operator *
00440 (const tCombLinMap<CellYT,CellZT> &f,
00441 const tCombLinMap<CellXT,CellYT> &g)
00442 {
00443 tCombLinMap<CellXT,CellZT> result;
00444 int_t size = g. size ();
00445 for (int_t i = 0; i < size; ++ i)
00446 {
00447 const CellXT &x = g [i];
00448 const tCombChain<CellYT> &y = g (i);
00449 const tCombChain<CellZT> z = f (y);
00450 if (z. empty ())
00451 continue;
00452 result. add (x, z);
00453 }
00454 return result;
00455 }
00456
00457
00458 #endif // _CHAINCON_COMBLINMAP_H_
00459