File: MarkedFlips.hh

package info (click to toggle)
topcom 0.17.8%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,572 kB
  • sloc: cpp: 16,640; sh: 975; makefile: 345; ansic: 40
file content (168 lines) | stat: -rw-r--r-- 3,847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
////////////////////////////////////////////////////////////////////////////////
// 
// MarkedFlips.hh 
//
//    produced: 18/06/98 jr
// last change: 18/06/98 jr
//
////////////////////////////////////////////////////////////////////////////////
#ifndef MARKEDFLIPS_HH
#define MARKEDFLIPS_HH

#include <map>

#include "Pair.hh"
#include "HashSet.hh"
#include "IntegerSet.hh"
#include "SimplicialComplex.hh"

#include "CommandlineOptions.hh"

#include "Circuits.hh"
#include "Flip.hh"

#ifndef STL_FLIPS
typedef PlainHashMap<FlipRep, bool> flips_type; // fliprep->[marked/unmarked]
#else
typedef std::map<FlipRep, bool> flips_type; // fliprep->[marked/unmarked]
#endif

class MarkedFlips : public flips_type {
private:
  size_type _no_of_marked;
public:
  // constructors:
  inline MarkedFlips() : flips_type(), _no_of_marked(0) {}
  inline MarkedFlips(const MarkedFlips& f) : flips_type(f), _no_of_marked(f._no_of_marked) {}
  // destructor:
  inline ~MarkedFlips() {}
  // assignment:
  inline MarkedFlips& operator=(const MarkedFlips& f) {
    if (this == &f) {
      return *this;
    }
    flips_type::operator=(f);
    _no_of_marked = f._no_of_marked;
    return *this;
  }

  // accessors:
  inline const size_type no_of_marked() const { return _no_of_marked; }
#ifndef STL_FLIPS
  inline const bool      all_marked()   const { return (_no_of_marked == load()); }
#else
  inline const bool      all_marked()   const { return (_no_of_marked == size()); }
#endif

  // mark/unmark:
  inline size_type mark(const FlipRep& fliprep) {
    iterator finder(find(fliprep));
#ifdef CHECK_MARK
    std::cerr << "marking " << fliprep << " in " << *this << std::endl;
#endif
    if (finder == end()) {
#ifdef CHECK_MARK
      std::cerr << "WARNING: " << fliprep << " not in " << *this << std::endl;
#endif
      return _no_of_marked;
    }
#ifdef CHECK_MARK
    if (!member(fliprep)) {
      std::cerr << "Error in HashMap:  flip " << fliprep << " is not in " << *this << std::endl;
    }
#endif
#ifndef STL_FLIPS
    if (!finder->data()) {
#else
   if (!(*finder).second) {
#endif
#ifndef STL_FLIPS
      finder->data() = true;
#else
      (*finder).second = true;
#endif
      ++_no_of_marked;
    }
#ifdef CHECK_MARK
    else {
      std::cerr << "WARNING: " << fliprep << " already marked in " << *this << std::endl;
    }
#endif
    return _no_of_marked;
  }
  inline void mark_all() {
    for (iterator iter = begin(); iter != end(); ++iter) {
#ifndef STL_FLIPS
      iter->data() = true;
#else
      (*iter).second = true;
#endif
    }
#ifndef STL_FLIPS
    _no_of_marked = load();
#else
    _no_of_marked = size();
#endif
  }
  inline size_type unmark(const FlipRep& fliprep) {
    iterator finder(find(fliprep));
#ifndef STL_FLIPS
    if (finder->data()) {
#else
    if ((*finder).second) {
#endif
#ifndef STL_FLIPS
      finder->data() = false;
#else
      (*finder).second = false;
#endif
      --_no_of_marked;
    }
    return _no_of_marked;
  }
  inline void unmark_all() {
    for (iterator iter = begin(); iter != end(); ++iter) {
#ifndef STL_FLIPS
      iter->data() = false;
#else
      (*iter).second = false;
#endif
    }
    _no_of_marked = 0;
  }

  // stream output/input:
  std::ostream& write(std::ostream& ost) const {
    flips_type::write(ost);
    return ost;
  }
  std::istream& read(std::istream& ist) {
    flips_type::read(ist);
    _no_of_marked = 0;
    
    for (iterator iter = begin(); iter != end(); ++iter) {
#ifndef STL_FLIPS
      if (iter->data()) {
#else
      if ((*iter).second) {;
#endif
	++_no_of_marked;
      }
    }

    return ist;
  }

  friend inline std::ostream& operator<<(std::ostream& ost, const MarkedFlips& mf) {
    return mf.write(ost);
  }
  
  friend inline std::istream& operator>>(std::istream& ist, MarkedFlips& mf) {
    return mf.read(ist);
  }
  
};

#endif

// eof MarkedFlips.hh