Mantid
Loading...
Searching...
No Matches
BnId.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include <algorithm>
8#include <cmath>
9#include <fstream>
10#include <functional>
11#include <iterator>
12#include <map>
13#include <set>
14#include <sstream>
15#include <stack>
16#include <vector>
17
19
20namespace Mantid::Geometry {
21
27std::ostream &operator<<(std::ostream &of, const BnId &A) {
28 of << A.display();
29 return of;
30}
31
33 : size(0), PI(1), Tnum(0), Znum(0)
37{}
38
39BnId::BnId(const size_t A, const unsigned int X)
40 : size(A), PI(1), Tnum(0), Znum(0), Tval(A)
47{
48 unsigned int cnt = 1;
49 int sum(0);
50 for (size_t i = 0; i < size; cnt *= 2, i++) {
51 Tval[i] = (X & cnt) ? 1 : -1;
52 sum += Tval[i];
53 }
54 Tnum = (sum + static_cast<int>(size)) / 2;
55}
56
57bool BnId::operator==(const BnId &A) const
64{
65 if (A.size != size || A.Tnum != Tnum || A.Znum != Znum)
66 return false;
67 auto ac = A.Tval.cbegin();
68 for (auto vc = Tval.cbegin(); vc != Tval.cend(); ++vc, ++ac) {
69 if (ac == A.Tval.cend()) // This should never happen
70 return false;
71 if (*vc != *ac)
72 return false;
73 }
74 return true;
75}
76
77int BnId::equivalent(const BnId &A) const
84{
85 if (A.size != size)
86 return 0;
87 int retval = 1;
88 for (size_t i = 0; i < size; i++) {
89 if (Tval[i] * A.Tval[i] < 0) // true * false == -1.
90 return 0;
91 if (retval == 1 && Tval[i] != A.Tval[i])
92 retval = 2;
93 }
94 return retval;
95}
96
97bool BnId::operator>(const BnId &A) const
103{
104 return (&A != this) ? !(*this < A) : false;
105}
106
107bool BnId::operator<(const BnId &A) const
114{
115 if (A.size != size)
116 return size < A.size;
117 if (Znum != A.Znum)
118 return (Znum < A.Znum);
119
120 if (Tnum != A.Tnum)
121 return (Tnum < A.Tnum);
122
123 auto tvc = Tval.crbegin();
124 auto avc = A.Tval.crbegin();
125 while (tvc != Tval.crend()) {
126 if (*tvc != *avc)
127 return *tvc < *avc;
128 ++tvc;
129 ++avc;
130 }
131 return false;
132}
133
134int BnId::operator[](const int A) const
140{
141 if (A < 0 && A >= static_cast<int>(size))
142 return -99;
143 return Tval[A];
144}
145
153{
154 return this->operator++();
155}
156
165{
166 std::vector<int>::iterator vc;
167 for (vc = Tval.begin(); vc != Tval.end() && (*vc) != -1; ++vc) {
168 if (*vc == 1) {
169 Tnum--;
170 *vc = -1;
171 }
172 }
173 if (vc == Tval.end())
174 return 0;
175 // Normal exit
176 *vc = 1;
177 Tnum++;
178 return 1;
179}
180
181int BnId::operator--(const int)
188{
189 return this->operator--();
190}
191
200{
201 std::vector<int>::iterator vc;
202 for (vc = Tval.begin(); vc != Tval.end() && (*vc) != 1; ++vc)
203 if (*vc == -1) {
204 *vc = 1;
205 Tnum++;
206 }
207 if (vc == Tval.end()) // Loop took place
208 return 0;
209
210 *vc = -1;
211 Tnum--;
212 return 1;
213}
214
219{
220 std::vector<int>::const_iterator vc;
221 Tnum = 0;
222 Znum = 0;
223 for (vc = Tval.begin(); vc != Tval.end(); ++vc) {
224 if (*vc == 1)
225 Tnum++;
226 else if (*vc == 0)
227 Znum++;
228 }
229}
230
231int BnId::intValue() const
237{
238 unsigned out(0);
239 std::vector<int>::const_reverse_iterator vc;
240 for (vc = Tval.rbegin(); vc != Tval.rend(); ++vc) {
241 out <<= 1;
242 out += ((*vc) == 1) ? 1 : 0;
243 }
244 return out;
245}
246
247void BnId::mapState(const std::vector<int> &Index, std::map<int, int> &Base) const
253{
254 std::vector<int>::const_iterator vc;
255 int i(0);
256 for (vc = Index.begin(); vc != Index.end(); ++vc, ++i)
257 Base[*vc] = (Tval[i] == 1) ? 1 : 0;
258}
259
260std::pair<int, BnId> BnId::makeCombination(const BnId &A) const
272{
273
274 if (size != A.size) // sizes different
275 return std::pair<int, BnId>(-1, BnId());
276
277 // Zero unequal or 1 value to far apart
278 if (Znum != A.Znum || (Tnum - A.Tnum) * (Tnum - A.Tnum) > 1)
279 return std::pair<int, BnId>(-1, BnId());
280
281 // no difference
282 if (Tnum == A.Tnum)
283 return std::pair<int, BnId>(0, BnId());
284
285 int flag(0); // numb of diff
286 auto avc = A.Tval.cbegin();
287 std::vector<int>::const_iterator chpt; // change point
288 for (auto tvc = Tval.cbegin(); tvc != Tval.cend(); ++tvc, ++avc) {
289 if ((*avc * *tvc) < 0) // false/true
290 {
291 if (flag) // failed
292 return std::pair<int, BnId>(0, BnId());
293 ;
294 flag = 1; // inc change counter
295 chpt = tvc;
296 } else if (*avc != *tvc) // failed on a 0 : value
297 return std::pair<int, BnId>(0, BnId());
298 ;
299 }
300 // Good value
301 if (flag) {
302 BnId PIout(*this);
303 PIout.Tval[(chpt - Tval.begin())] = 0;
304 PIout.setCounters();
305 return std::pair<int, BnId>(1, PIout);
306 }
307 // Nothing to do.
308 return std::pair<int, BnId>(0, BnId());
309 ;
310}
311
317{
318 using std::placeholders::_1;
319 transform(Tval.begin(), Tval.end(), Tval.begin(), std::bind(std::multiplies<int>(), _1, -1));
320 setCounters();
321}
322
323std::string BnId::display() const
329{
330 std::string Out;
331 std::vector<int>::const_reverse_iterator vc;
332 std::ostringstream cx;
333 for (vc = Tval.rbegin(); vc != Tval.rend(); ++vc) {
334 if (*vc == 0)
335 Out += "-";
336 else if (*vc == 1)
337 Out += "1";
338 else
339 Out += "0";
340 }
341 cx << "(" << Tnum << ":" << Znum << ")";
342 return Out + cx.str();
343}
344
345void BnId::write(std::ostream &os) const { os << display(); }
346
347} // namespace Mantid::Geometry
Tri-state variable.
Definition: BnId.h:37
int intValue() const
Integer from binary expression.
Definition: BnId.cpp:231
void write(std::ostream &) const
writes the value to a stream
Definition: BnId.cpp:345
size_t size
number of variables
Definition: BnId.h:39
bool operator>(const BnId &) const
operator> for tri-state object
Definition: BnId.cpp:97
void mapState(const std::vector< int > &, std::map< int, int > &) const
Sets the components within base with true/false.
Definition: BnId.cpp:247
std::string display() const
Displays the value as a string.
Definition: BnId.cpp:323
void setCounters()
Calculates Tnum and Znum.
Definition: BnId.cpp:215
bool operator==(const BnId &) const
Equals operator for tri-state object.
Definition: BnId.cpp:57
bool operator<(const BnId &) const
operator> for tri-state object
Definition: BnId.cpp:107
std::pair< int, BnId > makeCombination(const BnId &) const
Find if A and this can be differ by one 1/-1 bit and make a 0 value for that bit.
Definition: BnId.cpp:260
void reverse()
Swap -1 to 1 adn leaver the zeros.
Definition: BnId.cpp:312
int Znum
Zero number (0 in Tval)
Definition: BnId.h:42
int operator++()
addition operator (returns !carry flag)
Definition: BnId.cpp:157
BnId()
Standard Constructor.
Definition: BnId.cpp:32
int equivalent(const BnId &) const
Equal but - is assume to be ok.
Definition: BnId.cpp:77
int operator--()
subtraction operator (returns !carry flag)
Definition: BnId.cpp:192
int operator[](int const) const
Access operator.
Definition: BnId.cpp:134
int Tnum
True number (1 in Tval)
Definition: BnId.h:41
std::vector< int > Tval
Truth values.
Definition: BnId.h:43
MANTID_GEOMETRY_DLL std::ostream & operator<<(std::ostream &stream, const PointGroup &self)
Returns a streamed representation of the PointGroup object.
Definition: PointGroup.cpp:312