The Original CHomP Software
bincube.h
Go to the documentation of this file.
1
3
17
18// Copyright (C) 1997-2020 by Pawel Pilarczyk.
19//
20// This file is part of my research software package. This is free software:
21// you can redistribute it and/or modify it under the terms of the GNU
22// General Public License as published by the Free Software Foundation,
23// either version 3 of the License, or (at your option) any later version.
24//
25// This software is distributed in the hope that it will be useful,
26// but WITHOUT ANY WARRANTY; without even the implied warranty of
27// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28// GNU General Public License for more details.
29//
30// You should have received a copy of the GNU General Public License
31// along with this software; see the file "license.txt". If not,
32// please, see <https://www.gnu.org/licenses/>.
33
34// Started on November 17, 2005. Last revision: February 2, 2018.
35
36
37#ifndef _CHOMP_CUBES_BINCUBE_H_
38#define _CHOMP_CUBES_BINCUBE_H_
39
40#include "chomp/system/config.h"
43
44#include <iostream>
45#include <fstream>
46#include <ctime>
47#include <cstdlib>
48#include <cstring>
49#include <queue>
50
51namespace chomp {
52namespace homology {
53
54
55// --------------------------------------------------
56// ---------------------- n^k -----------------------
57// --------------------------------------------------
58
61template <int number, int power>
62class Power
63{
64public:
65 static const int value = Power<number,power-1>::value * number;
66}; /* Power */
67
70template <int number>
71class Power<number,0>
72{
73public:
74 static const int value = 1;
75}; /* Power */
76
77
78// --------------------------------------------------
79// --------------- SET OF FULL CUBES ----------------
80// --------------------------------------------------
81
85{
86public:
91 {
92 };
93};
94
95
96// --------------------------------------------------
97// --------------------- BITMAP ---------------------
98// --------------------------------------------------
99
104template <int Dim, int twoPower>
106{
107public:
108protected:
109}; /* class FixDimBitmap */
110
111
112// --------------------------------------------------
113// -------------------- BINCUBE ---------------------
114// --------------------------------------------------
115
119template <int Dim, int twoPower>
120class bincube: public FixDimBitmap<Dim,twoPower>, public SetOfFullCubes
121{
122public:
124 bincube ();
125
128 bincube (char *buffer);
129
132
135
137 ~bincube ();
138
140 static int dimension ();
141
143 static const int MaxDim;
144
148 int findcube (int start = 0) const;
149
152 {
153 friend class bincube<Dim,twoPower>;
154 public:
156 typedef int CoordType;
157
159 iterator (bincube<Dim,twoPower> *bcub = 0, int num = -1);
160
164
167
169 operator int () const;
170
172 static const int MaxDim;
173
175 static int dim ();
176
178 const int *coord () const;
179
181 template <class intType>
182 intType *coord (intType *tab) const;
183
184// protected:
187
189 int n;
190
191 }; /* class iterator */
192
194 iterator begin ();
195
197 iterator end ();
198
200 static const int max_neighbors;
201
204 {
205 public:
208 int n = -1, int inicur = -1);
209
213
216
218 operator int () const;
219
221 // operator iterator () const;
222
224// friend bool operator ==
225// (const bincube<Dim,twoPower>::neighborhood_iterator &x1,
226// const bincube<Dim,twoPower>::neighborhood_iterator &x2);
227
228 protected:
231
233 int coord [Dim];
234
236 int ncoord [Dim];
237
240
242 int cur;
243
244 }; /* class neighborhood_iterator */
245
248 neighborhood_iterator neighborhood_begin (int number) const;
249
251 neighborhood_iterator neighborhood_end (int number) const;
252
255 void add (int number);
256
259 template <class intType>
260 void add (const intType *coord);
261
264 bool check (int number) const;
265
268 template <class intType>
269 bool check (const intType *coord) const;
270
273 void remove (int number);
274
277 template <class intType>
278 void remove (const intType *coord);
279
281 const char *getbuffer () const;
282
284 operator const char * () const;
285
287 static int getbufsize ();
288
290 std::istream &read (std::istream &in);
291
293 static bool wrapped (int dir);
294
296 static void wrap (int dir);
297
299 static void dontwrap (int dir);
300
302 template <class intType>
303 static intType wrap (intType coord, int dir);
304
307 template <class intType>
308 static int coord2num (const intType *coord);
309
311 template <class intType>
312 static intType *num2coord (int number, intType *coord);
313
317 template <class intType>
318 static const intType *num2coord (int number);
319
321 int count () const;
322 operator int () const;
323
325 bool empty () const;
326
328 void clear ();
329
331 static const int maxcount;
332
333protected:
335 char *buf;
336
339
341 mutable int cardinality;
342
344 static const int bufsize;
345
347 static const int twoMask;
348
350 static const int width;
351
353 static int wrapping;
354
355}; /* class bincube */
356
357template <int Dim, int twoPower>
358const int bincube<Dim,twoPower>::bufsize = 1 << (Dim * twoPower - 3);
359
360template <int Dim, int twoPower>
361const int bincube<Dim,twoPower>::maxcount = 1 << (Dim * twoPower);
362
363template <int Dim, int twoPower>
364const int bincube<Dim,twoPower>::twoMask = ~0u >> (32 - twoPower);
365
366template <int Dim, int twoPower>
367const int bincube<Dim,twoPower>::width = 1 << twoPower;
368
369template <int Dim, int twoPower>
371
372template <int Dim, int twoPower>
373const int bincube<Dim,twoPower>::MaxDim = Dim;
374
375template <int Dim, int twoPower>
377
378template <int Dim, int twoPower>
380
381
382// --------------------------------------------------
383
384template <int Dim, int twoPower>
386{
387 buf = new char [bufsize];
388 allocated = true;
389 memset (buf, 0, bufsize);
390 cardinality = 0;
391 return;
392} /* bincube::bincube */
393
394template <int Dim, int twoPower>
396{
397 buf = buffer;
398 allocated = false;
399 cardinality = -1;
400 return;
401} /* bincube::bincube */
402
403template <int Dim, int twoPower>
405{
406 buf = new char [bufsize];
407 allocated = true;
408 memcpy (buf, b. buf, bufsize);
409 cardinality = b. cardinality;
410 return;
411} /* bincube::bincube */
412
413template <int Dim, int twoPower>
416{
417 memcpy (buf, b. buf, bufsize);
418 cardinality = b. cardinality;
419 return *this;
420} /* bincube::bincube */
421
422template <int Dim, int twoPower>
424{
425 if (allocated)
426 delete [] buf;
427 return;
428} /* bincube::~bincube */
429
430// --------------------------------------------------
431
432template <int Dim, int twoPower>
434{
435 return Dim;
436} /* bincube::dimension */
437
438template <int Dim, int twoPower>
440{
441 return bincube<Dim,twoPower>::wrapping & (1 << dir);
442} /* bincube::wrapped */
443
444template <int Dim, int twoPower>
445inline void bincube<Dim,twoPower>::wrap (int dir)
446{
448 return;
449} /* bincube::wrap */
450
451template <int Dim, int twoPower>
453{
454 bincube<Dim,twoPower>::wrapping &= ~(1 << dir);
455 return;
456} /* bincube::dontwrap */
457
458template <int Dim, int twoPower>
459template <class intType>
460inline intType bincube<Dim,twoPower>::wrap (intType coord, int dir)
461{
462 if (wrapped (dir))
463 {
464 // this is fast but can be very slow if the
465 // coordinates are far from the actual binary cube
466 while (coord < 0)
467 coord += width;
468 while (coord >= width)
469 coord -= width;
470 }
471 return coord;
472} /* bincube::wrap */
473
474// --------------------------------------------------
475
476template <int Dim, int twoPower>
477template <class intType>
478inline int bincube<Dim,twoPower>::coord2num (const intType *coord)
479{
480 int number = 0;
481 for (int i = Dim - 1; i >= 0; -- i)
482 {
483 int c = coord [i];
484 if (wrapped (i))
485 {
486 // this is fast but can be very slow if the
487 // coordinates are far from the actual binary cube
488 while (c < 0)
489 c += width;
490 while (c >= width)
491 c -= width;
492 }
493 else if ((c < 0) || (c >= width))
495 number <<= twoPower;
496 number |= c;
497 }
498 return number;
499} /* bincube::coord2num */
500
501template <int Dim, int twoPower>
502template <class intType>
503inline intType *bincube<Dim,twoPower>::num2coord (int number, intType *coord)
504{
505 for (int i = 0; i < Dim; ++ i)
506 {
507 coord [i] = number & bincube<Dim,twoPower>::twoMask;
508 number >>= twoPower;
509 }
510 return coord;
511} /* bincube::num2coord */
512
513template <int Dim, int twoPower>
514template <class intType>
515inline const intType *bincube<Dim,twoPower>::num2coord (int /*number*/)
516{
517 return 0;
518} /* bincube::num2coord */
519
520// --------------------------------------------------
521
522template <int Dim, int twoPower>
523inline int bincube<Dim,twoPower>::findcube (int start) const
524{
525 // determine the offset of the byte containing the cube
526 int offset = start >> 3;
527
528 // don't look for cubes outside the valid range
529 if (offset >= bufsize)
530 return maxcount;
531
532 // look for a cube within this byte
533 if (buf [offset])
534 {
535 int bitnumber = start & 7;
536 while (bitnumber < 8)
537 {
538 if (buf [offset] & (1 << bitnumber))
539 return (offset << 3) + bitnumber;
540 ++ bitnumber;
541 }
542 }
543
544 // search for a non-zero byte
545 while (1)
546 {
547 ++ offset;
548 if (offset >= bufsize)
549 return maxcount;
550 if (buf [offset])
551 break;
552 }
553
554 // retrieve the cube with the non-zero bit within this byte
555 int bitnumber = 0;
556 while (1)
557 {
558 if (buf [offset] & (1 << bitnumber))
559 return (offset << 3) + bitnumber;
560 ++ bitnumber;
561 }
562} /* bincube::findcube */
563
564// --------------------------------------------------
565
566template <int Dim, int twoPower>
567inline bool bincube<Dim,twoPower>::check (int number) const
568{
569 return buf [number >> 3] & (1 << (number & 7));
570} /* bincube::check */
571
572template <int Dim, int twoPower>
573template <class intType>
574inline bool bincube<Dim,twoPower>::check (const intType *coord) const
575{
576 return check (coord2num (coord));
577} /* bincube::check */
578
579template <int Dim, int twoPower>
580inline void bincube<Dim,twoPower>::add (int number)
581{
582 if ((cardinality >= 0) && !check (number))
583 ++ cardinality;
584 buf [number >> 3] |= (char) (1 << (number & 7));
585 return;
586} /* bincube::add */
587
588template <int Dim, int twoPower>
589template <class intType>
590inline void bincube<Dim,twoPower>::add (const intType *coord)
591{
592 return add (coord2num (coord));
593} /* bincube::add */
594
595template <int Dim, int twoPower>
596inline void bincube<Dim,twoPower>::remove (int number)
597{
598 if ((cardinality > 0) && check (number))
599 -- cardinality;
600 buf [number >> 3] &= (char) (~(1 << (number & 7)));
601 return;
602} /* bincube::remove */
603
604template <int Dim, int twoPower>
605template <class intType>
606inline void bincube<Dim,twoPower>::remove (const intType *coord)
607{
608 return remove (coord2num (coord));
609} /* bincube::remove */
610
611// --------------------------------------------------
612
613template <int Dim, int twoPower>
615 (bincube<Dim,twoPower> *bcub, int num): b (bcub), n (num)
616{
617 return;
618} /* bincube::iterator::iterator */
619
620template <int Dim, int twoPower>
621inline typename bincube<Dim,twoPower>::iterator &
623{
624 if (!b || (n >= bincube<Dim,twoPower>::maxcount))
625 return *this;
626 n = b -> findcube (n + 1);
627 return *this;
628} /* bincube::iterator::operator ++ */
629
630template <int Dim, int twoPower>
631inline typename bincube<Dim,twoPower>::iterator &
633{
634 iterator prev = *this;
635 ++ *this;
636 return prev;
637} /* bincube::iterator::operator ++ */
638
639template <int Dim, int twoPower>
641{
642 return n;
643} /* bincube::iterator::operator int */
644
645template <int Dim, int twoPower>
647{
648 return Dim;
649} /* bincube::iterator::dim */
650
651template <int Dim, int twoPower>
653{
654 return 0;
655} /* bincube::coord */
656
657template <int Dim, int twoPower>
658template <class intType>
659inline intType *bincube<Dim,twoPower>::iterator::coord (intType *tab) const
660{
661 return bincube<Dim,twoPower>::num2coord (n, tab);
662} /* bincube::coord */
663
664// --------------------------------------------------
665
666template <class coordinate>
667inline void bit2neighborAlg (int number, const coordinate *src,
668 coordinate *dest, int Dim)
669{
670 ++ number;
671 for (int i = Dim - 1; i >= 0; -- i)
672 {
673 switch (number % 3)
674 {
675 case 2:
676 dest [i] = src [i] - 1;
677 break;
678 case 1:
679 dest [i] = src [i] + 1;
680 break;
681 case 0:
682 dest [i] = src [i];
683 break;
684 default:
685 throw "Erratic neighbor.";
686 }
687 number /= 3;
688 }
689
690 return;
691} /* bit2neighborAlg */
692
693template <class settype>
694typename settype::iterator bit2neighborAlg
695 (const typename settype::iterator &q, int n)
696{
697 const int Dim = settype::MaxDim;
698 int coord [Dim];
699 q. b -> num2coord (q, coord);
700 bit2neighborAlg (n, coord, coord, Dim);
701 try
702 {
703 return typename settype::iterator (q. b,
704 q. b -> coord2num (coord));
705 }
706 catch (...)
707 {
708 }
709 return q;
710} /* bit2neighborAlg */
711
712// --------------------------------------------------
713
714template <int Dim, int twoPower>
716 (bincube<Dim,twoPower> *bcub, int num, int inicur):
717 b (bcub), curnum (-1), cur (inicur)
718{
719 if (b && (num >= 0))
720 b -> num2coord (num, coord);
721 else
722 {
723 // this zeroing operation is to avoid a compilation warning,
724 // probably it is not necessary
725 for (int i = 0; i < Dim; ++ i)
726 coord [i] = 0;
727 }
729 {
730 for (int i = 0; i < Dim; ++ i)
731 ncoord [i] = 0;
732 }
733 return;
734} /* bincube::neighborhood_iterator::neighborhood_iterator */
735
736template <int Dim, int twoPower>
739{
740 if (!b || (cur >= bincube<Dim,twoPower>::max_neighbors))
741 return *this;
742 while (1)
743 {
744 ++ cur;
746 {
747 for (int i = 0; i < Dim; ++ i)
748 ncoord [i] = 0;
749 curnum = -1;
750 return *this;
751 }
752 bit2neighborAlg (cur, coord, ncoord, Dim);
753 try
754 {
755 curnum = b -> coord2num (ncoord);
756 if (b -> check (curnum))
757 return *this;
758 }
759 catch (...)
760 {
761 }
762 }
763 return *this;
764} /* bincube::iterator::operator ++ */
765
766template <int Dim, int twoPower>
769{
770 if (!b || (cur >= bincube<Dim,twoPower>::maxcount))
771 return *this;
772 neighborhood_iterator prev = *this;
773 ++ (*this);
774 return prev;
775} /* bincube::neighborhood_iterator::operator ++ */
776
777template <int Dim, int twoPower>
779{
780 return curnum;
781} /* bincube::neighborhood_iterator::operator int */
782
783template <int Dim, int twoPower>
784inline bool operator ==
787{
788// sout << "DEBUG ==\n";
789 return ((x1. b == x2. b) && (x1. cur == x2. cur));
790} /* bincube::neighborhood_iterator::operator == */
791
792template <int Dim, int twoPower>
793inline bool operator !=
796{
797// sout << "DEBUG !=\n";
798 return !(x1 == x2);
799} /* bincube::neighborhood_iterator::operator != */
800
801// --------------------------------------------------
802
803template <int Dim, int twoPower>
804inline typename bincube<Dim,twoPower>::neighborhood_iterator
806{
808 (const_cast<bincube<Dim,twoPower> *> (this), n);
809 return ++ iter;
810} /* bincube::neighborhood_begin */
811
812template <int Dim, int twoPower>
815{
817 (const_cast<bincube<Dim,twoPower> *> (this), n,
819} /* bincube::neighborhood_end */
820
821// --------------------------------------------------
822
823template <int Dim, int twoPower>
826{
827 return iterator (this, findcube ());
828} /* bincube::begin */
829
830template <int Dim, int twoPower>
833{
834 return iterator (this, maxcount);
835} /* bincube::end */
836
837// --------------------------------------------------
838
839template <int Dim, int twoPower>
840inline const char *bincube<Dim,twoPower>::getbuffer () const
841{
842 return buf;
843} /* bincube::getbuffer */
844
845template <int Dim, int twoPower>
846inline bincube<Dim,twoPower>::operator const char * () const
847{
848 return buf;
849} /* bincube::operator const char * */
850
851template <int Dim, int twoPower>
853{
854 return bufsize;
855} /* bincube::getbufsize */
856
857template <int Dim, int twoPower>
858inline std::istream &bincube<Dim,twoPower>::read (std::istream &in)
859{
860 in. read (buf, bufsize);
861 cardinality = -1;
862 return in;
863} /* bincube::read */
864
865// --------------------------------------------------
866
867template <int Dim, int twoPower>
869{
870 if (cardinality >= 0)
871 return cardinality;
872 char *byte = buf;
873 char *end = byte + bufsize;
874 int c = 0;
875 while (byte != end)
876 {
877 c += bitcountbyte (*byte);
878 ++ byte;
879 }
880 cardinality = c;
881 return cardinality;
882} /* bincube::count */
883
884template <int Dim, int twoPower>
886{
887 return count ();
888} /* bincube::operator int */
889
890template <int Dim, int twoPower>
892{
893 return !count ();
894} /* bincube::empty */
895
896template <int Dim, int twoPower>
898{
899 memset (buf, 0, bufsize);
900 cardinality = 0;
901 return;
902} /* bincube::clear */
903
904// --------------------------------------------------
905
906template <int Dim, int twoPower>
907inline std::ostream &operator << (std::ostream &out,
908 const bincube<Dim,twoPower> & b)
909{
910 return out. write (static_cast<const char *> (b), b. getbufsize ());
911} /* operator << */
912
913template <int Dim, int twoPower>
914inline std::istream &operator >> (std::istream &in,
916{
917 return b. read (in);
918} /* operator >> */
919
920// --------------------------------------------------
921
925template <class cubetype, class settype>
927{
928public:
929 // the default constructor
930 NeighborsBdd (const cubetype &middle, const settype &cset):
931 q (middle), s (cset) {};
932
933 // the procedure for checking whether the given neighbor exists;
934 // the number of the neighbor is consistent with the "bit2neighbor"
935 // and "neighbor2bit" procedures
936 bool check (int n) const
937 {
938 cubetype neighbor = bit2neighborAlg<settype> (q, n);
939 if (neighbor == q)
940 return false;
941 return (s. check (neighbor));
942 }
943
944private:
945 // the cube whose neighbors are verified
946 const cubetype &q;
947
948 // the set of cubes in which the neighbors of the cube are sought
949 const settype &s;
950
951}; /* class NeighborsBdd */
952
955template <class SetT>
957{
958public:
959 static bool check (typename SetT::iterator q, const SetT &s)
960 {
962 return acyclic1d (n);
963 }
964 static bool check (int n, const SetT &s)
965 {
966 // FIX const cast!
967 typename SetT::iterator q (const_cast<SetT *> (&s), n);
968 return check (q, s);
969 }
970}; /* Acyclic1d */
971
974template <class SetT>
976{
977public:
978 static bool check (typename SetT::iterator q, const SetT &s)
979 {
981 return acyclic2d (n);
982 }
983 static bool check (int n, const SetT &s)
984 {
985 // FIX const cast!
986 typename SetT::iterator q (const_cast<SetT *> (&s), n);
987 return check (q, s);
988 }
989}; /* Acyclic2d */
990
993template <class SetT>
995{
996public:
997 static bool check (typename SetT::iterator q, const SetT &s)
998 {
1000 return acyclic3d (n);
1001 }
1002 static bool check (int n, const SetT &s)
1003 {
1004 // FIX const cast!
1005 typename SetT::iterator q (const_cast<SetT *> (&s), n);
1006 return check (q, s);
1007 }
1008}; /* Acyclic3d */
1009
1010// --------------------------------------------------
1011
1013template <class Number>
1015{
1016public:
1018 hashNumber (Number n = 0): number (n) {return;}
1019
1021 operator Number () const {return number;}
1022
1023protected:
1025}; /* hashNumber */
1026
1028template <class Number>
1030{
1031 return static_cast<int_t> (static_cast<Number> (n));
1032} /* hashkey1 */
1033
1035template <class Number>
1037{
1038 return static_cast<int_t> (static_cast<Number> (n) ^
1039 0xFA5A75A7ul) << 5;
1040} /* hashkey2 */
1041
1043
1044// --------------------------------------------------
1045
1047template <class SetT, class QueueT>
1048void addneighbors (const int &c, const SetT &s, QueueT &q)
1049{
1050 typename SetT::neighborhood_iterator cur = s. neighborhood_begin (c),
1051 end = s. neighborhood_end (c);
1052 while (cur != end)
1053 {
1054 q. add (hashNumber<int> (cur));
1055 ++ cur;
1056 }
1057 return;
1058} /* addneighbors */
1059
1064template <typename SetT, typename Acyclic, typename QueueT>
1065int reduceFullCubesAlg (SetT &X, bool quiet)
1066{
1067 // prepare the set of cubes to consider next time
1068 QueueT Q;
1069
1070 // scan the entire set until very few cubes are removed
1071 int count = 0;
1072 bool exitloop = false;
1073 bool lastrun = false;
1074 while (!exitloop)
1075 {
1076 // remember to exit the loop after the last run
1077 if (lastrun)
1078 exitloop = true;
1079
1080 int countremoved = 0, countleft = 0;
1081 typename SetT::iterator cur = X. begin (), end = X. end ();
1082 while (cur != end)
1083 {
1084 if (Acyclic::check (cur, X))
1085 {
1086 X. remove (cur);
1087 ++ countremoved;
1088 if (lastrun)
1089 addneighbors (cur, X, Q);
1090 }
1091 else
1092 ++ countleft;
1093 ++ cur;
1094
1095 // show progress indicator
1096 if (!quiet && !(count % 5273))
1097 scon << std::setw (10) << count <<
1098 "\b\b\b\b\b\b\b\b\b\b";
1099 ++ count;
1100 }
1101 if (!quiet)
1102 sout << ".";
1103
1104 if (!lastrun && (countremoved - 10 < (countleft >> 2)))
1105 lastrun = true;
1106 }
1107
1108 if (!quiet)
1109 sout << " ";
1110 count = 0;
1111 while (!Q. empty ())
1112 {
1113 typename QueueT::value_type elem = Q. front ();
1114 Q. pop ();
1115 if (Acyclic::check (elem, X))
1116 {
1117 X. remove (elem);
1118 addneighbors (elem, X, Q);
1119 }
1120
1121 // show progress indicator
1122 if (!quiet && !(count % 5273))
1123 scon << std::setw (10) << count <<
1124 "\b\b\b\b\b\b\b\b\b\b";
1125 count ++;
1126 }
1127
1128 return 0;
1129} /* reduceFullCubesAlg */
1130
1131/*
1133template <int Dim, int twoPower>
1134inline int reduceFullCubes (bincube<Dim,twoPower> &X, bool quiet = false)
1135{
1136 throw "Binary cube reduction not implemented "
1137 "for dimension > 3.";
1138}
1139
1141template <int twoPower>
1142inline int reduceFullCubes<3,twoPower> (bincube<3,twoPower> &X, bool quiet = false)
1143{
1144 return reduceFullCubesAlg<bincube<3,twoPower>,
1145 Acyclic3d<bincube<3,twoPower> >,hashIntQueue> (X, quiet);
1146}
1147
1149template <int twoPower>
1150inline int reduceFullCubes<2,twoPower> (bincube<2,twoPower> &X, bool quiet = false)
1151{
1152 return reduceFullCubesAlg<bincube<2,twoPower>,
1153 Acyclic2d<bincube<2,twoPower> >,hashIntQueue> (X, quiet);
1154}
1155
1157template <int twoPower>
1158inline int reduceFullCubes<1,twoPower> (bincube<1,twoPower> &X, bool quiet = false)
1159{
1160 return reduceFullCubesAlg<bincube<1,twoPower>,
1161 Acyclic1d<bincube<1,twoPower> >,hashIntQueue> (X, quiet);
1162}
1163*/
1164
1166template <class FullCubSet>
1167inline int reduceFullCubes (FullCubSet &X, bool quiet = false)
1168{
1169 switch (X. dimension ())
1170 {
1171 case 3:
1172 return reduceFullCubesAlg<FullCubSet,
1174 case 2:
1175 return reduceFullCubesAlg<FullCubSet,
1177 case 1:
1178 return reduceFullCubesAlg<FullCubSet,
1180 default:
1181 throw "Binary cube reduction not implemented "
1182 "for dimension > 3.";
1183 }
1184} /* reduceFullCubes */
1185
1186
1187} // namespace homology
1188} // namespace chomp
1189
1190#endif // _CHOMP_CUBES_BINCUBE_H_
1191
1193
This file defines a very simple function for counting the number of bits in a byte or a multi-byte in...
This class defines a procedure for verifying if a full-cubical neighborhood in a given set of a full ...
Definition: bincube.h:957
static bool check(typename SetT::iterator q, const SetT &s)
Definition: bincube.h:959
static bool check(int n, const SetT &s)
Definition: bincube.h:964
This class defines a procedure for verifying if a full-cubical neighborhood in a given set of a full ...
Definition: bincube.h:976
static bool check(typename SetT::iterator q, const SetT &s)
Definition: bincube.h:978
static bool check(int n, const SetT &s)
Definition: bincube.h:983
This class defines a procedure for verifying if a full-cubical neighborhood in a given set of a full ...
Definition: bincube.h:995
static bool check(typename SetT::iterator q, const SetT &s)
Definition: bincube.h:997
static bool check(int n, const SetT &s)
Definition: bincube.h:1002
A fixed-dimensional bitmap of the size 2^n in each direction.
Definition: bincube.h:106
This is a class used by the classes "Acyclic1d", "Acyclic2d", and "Acyclic3d" to use binary decision ...
Definition: bincube.h:927
bool check(int n) const
Definition: bincube.h:936
const cubetype & q
Definition: bincube.h:946
NeighborsBdd(const cubetype &middle, const settype &cset)
Definition: bincube.h:930
This is a helper class which makes the compiler compute n^k during the compilation of the program.
Definition: bincube.h:63
static const int value
Definition: bincube.h:65
This class defines an error type which is caused by using coordinates of a cube out of range with res...
Definition: bincube.h:91
This is an abstract class which defines a set of full cubes represented as a bitmap for the purpose o...
Definition: bincube.h:85
The iterator of the set of cubes within a bitmap.
Definition: bincube.h:152
int CoordType
The type of coordinates.
Definition: bincube.h:156
int n
The number of the current bit in the set.
Definition: bincube.h:189
iterator & operator++()
The preincrement operator.
Definition: bincube.h:622
static const int MaxDim
The maximal possible dimension of the cube.
Definition: bincube.h:172
const int * coord() const
The coordinates of the cube.
Definition: bincube.h:652
static int dim()
The dimension of the cube.
Definition: bincube.h:646
iterator(bincube< Dim, twoPower > *bcub=0, int num=-1)
The default constructor.
Definition: bincube.h:615
bincube< Dim, twoPower > * b
The binary cube in which the cube is contained.
Definition: bincube.h:186
neighborhood_iterator & operator++()
The preincrement operator.
Definition: bincube.h:738
int curnum
The number of the current neighbor in the binary cube.
Definition: bincube.h:239
int coord[Dim]
The coordinates of the middle cube in the neighborhood.
Definition: bincube.h:233
bincube< Dim, twoPower > * b
Conversion to a cube iterator.
Definition: bincube.h:230
int ncoord[Dim]
The coordinates of the current neighbor.
Definition: bincube.h:236
neighborhood_iterator(bincube< Dim, twoPower > *bcub=0, int n=-1, int inicur=-1)
The default constructor.
Definition: bincube.h:716
int cur
The neighbor counter (up to max_neighbors).
Definition: bincube.h:242
A binary n-dimensional hypercube for storing cubes as bits.
Definition: bincube.h:121
static intType * num2coord(int number, intType *coord)
Determines the coordinates of the cube with given number.
Definition: bincube.h:503
bool allocated
Was the memory for the buffer allocated?
Definition: bincube.h:338
static int coord2num(const intType *coord)
Determines the number of the cube with given coordinates.
Definition: bincube.h:478
static const int bufsize
The size of the buffer in bytes.
Definition: bincube.h:344
static const int max_neighbors
The maximal possible number of neighbors of a cube.
Definition: bincube.h:200
static int wrapping
Wrapping in each direction.
Definition: bincube.h:353
static void wrap(int dir)
Turns on wrapping in the given direction.
Definition: bincube.h:445
neighborhood_iterator neighborhood_begin(int number) const
Creates a neighborhood iterator for the specified cube and sets it at the first cube.
Definition: bincube.h:805
neighborhood_iterator neighborhood_end(int number) const
Creates a one-behind-the-end iterator for the given cube.
Definition: bincube.h:814
std::istream & read(std::istream &in)
Reads the binary buffer from an input stream.
Definition: bincube.h:858
void clear()
Makes the set empty.
Definition: bincube.h:897
void remove(int number)
Clears the bit corresponding to the given cube (by number).
Definition: bincube.h:596
static const int maxcount
The maximal number of cubes that can be stored in the set.
Definition: bincube.h:331
static const int MaxDim
The maximal possible dimension of a cube.
Definition: bincube.h:143
static bool wrapped(int dir)
Verifies whether the space is wrapped in the given direction.
Definition: bincube.h:439
static int dimension()
Retrieves the dimension of the cube.
Definition: bincube.h:433
int count() const
Get the number of cubes in the set.
Definition: bincube.h:868
iterator end()
Returns the iterator that points beyond the end of the set.
Definition: bincube.h:832
bool check(int number) const
Checks if the given cube belongs to the set or not.
Definition: bincube.h:567
bincube()
The default constructor.
Definition: bincube.h:385
static void dontwrap(int dir)
Turns off wrapping in the given direction.
Definition: bincube.h:452
char * buf
The memory for storing the hypercubes.
Definition: bincube.h:335
static const int twoMask
The mask for extracting one coordinate from the number.
Definition: bincube.h:347
bool empty() const
Verifies whether the set is empty or not.
Definition: bincube.h:891
int findcube(int start=0) const
Finds the first existing cube beginning at the given number.
Definition: bincube.h:523
int cardinality
The number of cubes in the set (or -1 if unknown)
Definition: bincube.h:341
static const int width
The width of the set in each direction (in cubes).
Definition: bincube.h:350
iterator begin()
Returns the iterator that points at the first cube in the set.
Definition: bincube.h:825
static int getbufsize()
Gets the buffer size.
Definition: bincube.h:852
~bincube()
The destructor.
Definition: bincube.h:423
void add(int number)
Sets the bit corresponding to the given cube (by number).
Definition: bincube.h:580
bincube< Dim, twoPower > & operator=(const bincube< Dim, twoPower > &b)
The assignment operator.
Definition: bincube.h:415
const char * getbuffer() const
Gets the binary buffer for reading only.
Definition: bincube.h:840
A class of numbers that can be used in a hashed set.
Definition: bincube.h:1015
hashNumber(Number n=0)
The default constructor.
Definition: bincube.h:1018
This is a template for a set of objects of the given type.
Definition: hashsets.h:185
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
int int_t
Index type for indexing arrays, counting cubes, etc.
Definition: config.h:115
This file contains the definition of the container "hashedset" which can be used to represent a set o...
hashedset< hashNumber< int > > hashIntQueue
Definition: bincube.h:1042
outputstream sout
A replacement for standard output stream, with optional logging and other features provided by the cl...
bool acyclic3d(NeighborCheck &n)
Verifies whether the neighborhood of a 3-dimensional cube is acyclic.
Definition: bddacycl.h:613
std::ostream & operator<<(std::ostream &out, const bincube< Dim, twoPower > &b)
Definition: bincube.h:907
int write(std::ostream &out, const coordtype *c, int dim, char parenthesis=40, char closing=0)
Definition: pointset.h:2148
int_t hashkey2(const hashNumber< Number > &n)
The second hashing key.
Definition: bincube.h:1036
void addneighbors(const int &c, const SetT &s, QueueT &q)
Adds the neighbors of the cube 'c' in the set 's' to the set 'q'.
Definition: bincube.h:1048
bool acyclic1d(NeighborCheck &n)
Verifies whether the neighborhood of a 1-dimensional "cube" is acyclic.
Definition: bddacycl.h:49
void bit2neighborAlg(int number, const coordinate *src, coordinate *dest, int Dim)
Definition: bincube.h:667
short int coordinate
The default type of coordinates.
Definition: pointset.h:63
std::istream & operator>>(std::istream &in, bincube< Dim, twoPower > &b)
Definition: bincube.h:914
int_t hashkey1(const hashNumber< Number > &n)
The first hashing key.
Definition: bincube.h:1029
outputstream scon
The console output stream to which one should put all the junk that spoils the log file,...
int reduceFullCubes(FullCubSet &X, bool quiet=false)
Reduces the set of full cubes.
Definition: bincube.h:1167
int reduceFullCubesAlg(SetT &X, bool quiet)
Reduces the set of full cubes.
Definition: bincube.h:1065
int bitcountbyte(char n)
Definition: bitcount.h:43
bool acyclic2d(NeighborCheck &n)
Verifies whether the neighborhood of a 2-dimensional "cube" is acyclic.
Definition: bddacycl.h:63
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51