File: PermutationPartialComparator.hh

package info (click to toggle)
topcom 1.2.0~beta%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,596 kB
  • sloc: cpp: 40,956; sh: 4,663; makefile: 679; ansic: 55
file content (193 lines) | stat: -rw-r--r-- 6,279 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
////////////////////////////////////////////////////////////////////////////////
// 
// PermutationPartialComparator.hh 
//
//    produced: 15/03/2020 jr
//
////////////////////////////////////////////////////////////////////////////////
#ifndef PERMUTATIONPARTIALCOMPARATOR_HH
#define PERMUTATIONPARTIALCOMPARATOR_HH

#include "Permutation.hh"

namespace topcom {

  class PermutationPartialComparator {
  protected:
    const Permutation* _permptr;
    parameter_type     _min;
    parameter_type     _max;
  public:
    // constructors:
    inline PermutationPartialComparator();
    inline PermutationPartialComparator(const PermutationPartialComparator&);
    inline PermutationPartialComparator(const Permutation&, const parameter_type, const parameter_type);
    // destructor:
    inline ~PermutationPartialComparator();
    // assignment:
    inline PermutationPartialComparator& operator=(const PermutationPartialComparator&);
    // accessors:
    inline const parameter_type min() const;
    inline const parameter_type max() const;
    // comparison:
    inline const bool operator==(const PermutationPartialComparator&) const;
    inline const bool operator!=(const PermutationPartialComparator&) const;
    // keys for containers:
    inline const size_type keysize() const;
    inline const size_type key(const size_type n) const;
    // stream output/input:
    inline std::ostream& write(std::ostream& ost) const;
    inline std::istream& read(std::istream& ist);
    inline friend std::ostream& operator<<(std::ostream& ost, const PermutationPartialComparator& p);
    inline friend std::istream& operator>>(std::istream& ist, PermutationPartialComparator& p);
  };

  // // specialization of Hash (for debugging):
  // template<>
  // class Hash<PermutationPartialComparator> {
  // public:
  //   inline size_type operator()(const PermutationPartialComparator& key) const {
  //     size_type result(0UL);
  //     size_type factor(1UL);
  //     const size_type keysize = key.keysize();
  //     for (size_type i = 0; i < keysize; ++i) {
  //       result = (result << 5UL) - result;
  // #ifdef HASH_DEBUG
  //       if (key.min() > 63) {
  // 	MessageStreams::debug() << "result after shift = " << result << std::endl;
  //       }
  // #endif
  //       result += key.key(i);
  // #ifdef HASH_DEBUG
  //       if (key.min() > 63) {
  // 	MessageStreams::debug() << "result after add   = " << result << std::endl;
  //       }
  // #endif
  //     }
  //     return result;
  //   }
  // };

  // constructors:
  inline PermutationPartialComparator::PermutationPartialComparator() : _permptr(0), _min(0), _max(-1) {}
  inline PermutationPartialComparator::PermutationPartialComparator(const PermutationPartialComparator& ppc) : 
    _permptr(ppc._permptr), _min(ppc._min), _max(ppc._max) {}
  inline PermutationPartialComparator::PermutationPartialComparator(const Permutation& perm, const parameter_type min, const parameter_type max) : 
    _permptr(&perm), _min(min), _max(max) {}

  // destructor:
  inline PermutationPartialComparator::~PermutationPartialComparator() {}

  // assignment:
  inline PermutationPartialComparator& PermutationPartialComparator::operator=(const PermutationPartialComparator& ppc) {
    if (&ppc == this) {
      return *this;
    }
    _permptr = ppc._permptr;
    _min     = ppc._min;
    _max     = ppc._max;
    return *this;
  }

  // accessors:
  inline const parameter_type PermutationPartialComparator::min() const {
    return _min;
  }

  inline const parameter_type PermutationPartialComparator::max() const {
    return _max;
  }

  // comparison:
  inline const bool PermutationPartialComparator::operator==(const PermutationPartialComparator& ppc) const {
    if ((_max != ppc._max) || (_min != ppc._min)) {
      return false;
    }
    for (parameter_type i = _min; i <= _max; ++i) {
      const parameter_type p1 = (*_permptr)[i];
      const parameter_type p2 = (*(ppc._permptr))[i];
      if (p1 == p2) {
	continue;
      }
      else if ((p1 < _min) && (p2 < _min)) {
	continue;
      }
      else if ((p1 > _max) && (p2 > _max)) {
	continue;
      }
      else {
	return false;
      }
    }
    return true;
  }

  inline const bool PermutationPartialComparator::operator!=(const PermutationPartialComparator& ppc) const {
    return !operator==(ppc);
  }

  // keys for containers:
  inline const size_type PermutationPartialComparator::keysize() const {
#ifdef HASH_DEBUG
    if (_min > 63) {
      MessageStreams::debug() << "Permutation:keysize() was called - return value = " << _max - _min + 1 << std::endl;
    }
#endif
    return (size_type)(_max - _min + 1);
  }

  inline const size_type PermutationPartialComparator::key(const size_type n) const {
    const parameter_type value = (*_permptr)[(parameter_type)n + _min];
    if (value < _min) {
#ifdef HASH_DEBUG
      if (_min > 63) {
	MessageStreams::debug() << "Permutation:key(" << n << ") was called - return value = "
					    << 0 << std::endl;
      }
#endif
      return (size_type)0;
    }
    else if (value > _max) {
#ifdef HASH_DEBUG
      if (_min > 63) {
	MessageStreams::debug() << "Permutation:key(" << n << ") was called - return value = "
					    << _permptr->n() - 1 << std::endl;
      }
#endif
      return (size_type)(_permptr->n() - 1);
    }
    else {
#ifdef HASH_DEBUG
      if (_min > 63) {
	MessageStreams::debug() << "Permutation:key(" << n << ") was called - return value = "
					    << value << std::endl;
      }
#endif
      return (size_type)value;
    }
  }

  // stream output/input:
  inline std::ostream& PermutationPartialComparator::write(std::ostream& ost) const {
    ost << "[" << _min << "=>" << *_permptr << "<=" << _max << "]";
    return ost;
  }

  inline std::istream& PermutationPartialComparator::read(std::istream& ist) {
    MessageStreams::forced() << "PermutationPartialComparator::read(std::istream& ist): reading unsupported - exiting" << std::endl;
    exit(1);
  }

  inline std::ostream& operator<<(std::ostream& ost, const PermutationPartialComparator& p) {
    return p.write(ost);
  }

  inline std::istream& operator>>(std::istream& ist, PermutationPartialComparator& p) {
    return p.read(ist);
  }

}; // namespace topcom

#endif

// eof PermutationPartialComparator.hh