NDDEM
ternary.h
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <cstdio>
3 #include <iostream>
4 
5 class ternary {
6 public:
7  int operator[] (int i)
8  {
9  if (i==special_quat_bit) // Special bit returns: 00=>0, 10=>1, 11=>-1, like normal bit, and 01=>+2 if bit 0==-1 ; 01=>-2 if bit0==+1
10  {
11  if ((bit1>>i)&1)
12  {
13  if ((bit2>>i)&1)
14  return -1 ;
15  else
16  return 1 ;
17  }
18  else
19  {
20  if ((bit2>>i)&1)
21  return 2 ;
22  else
23  return 0 ;
24  }
25  }
26  else
27  return ((bit2>>i)&1) ? -1: ((bit1>>i) & 1) ; // state bit2=1 and bit1=0 is no legal. 00 => 0, 10=>1, 11=> -1, 01=>illegal.
28  }
29 
30  void operator++ (int a [[maybe_unused]])
31  {
32  int i=0 ;
33  bool carry=false ;
34  do {
35  carry = false ;
36  if ( !((bit1>>i)&1) )
37  {
38  if (i==special_quat_bit && !((bit2>>i)&1))
39  {
40  bit2 |= 1<<i ;
41  }
42  else
43  {
44  bit1 |= (1<<i) ;
45  bit2 &= ~(1<<i) ;
46  }
47  }
48  else
49  {
50  if ( !((bit2>>i)&1) )
51  {
52  bit2 |= 1<<i ;
53  }
54  else
55  {
56  bit1 &= ~(1<<i) ;
57  bit2 &= ~(1<<i) ;
58  carry = true ;
59  }
60  }
61  i++ ;
62  } while (carry && i<32) ;
63  }
64 
65  bool operator< (unsigned int n)
66  {
67  return (to_integer()<n) ;
68  }
69 
70  void set(int i, int v)
71  {
72  if (v==0)
73  {
74  bit1 &= ~(one<<i) ;
75  bit2 &= ~(one<<i) ;
76  }
77  else if (v<0)
78  {
79  bit1 |= (one<<i) ;
80  bit2 |= (one<<i) ;
81  }
82  else if (v>0)
83  {
84  bit1 |= (one<<i) ;
85  bit2 &= ~(one<<i) ;
86  }
87  else
88  printf("ERR a number needs to be positive or negative or zero ... \n") ;
89  }
90 
91  void set_quat_bit (int n) {special_quat_bit = n ; }
92  void reset_quat_bit () {special_quat_bit = 255 ; }
93 
94  void clear ()
95  {
96  bit1=bit2=0 ;
97  }
98 
100  {
101  int base = 1 ;
102  uint64_t sum = 0 ;
103  auto b1 = bit1, b2=bit2 ;
104  while (b1)
105  {
106  if (b1&1)
107  {
108  if (b2&1)
109  sum += base * 2 ;
110  else
111  sum += base ;
112  }
113  b1>>=1 ;
114  b2>>=1 ;
115  base *= 3 ;
116  }
117  return sum ;
118  }
119 
120  friend std::ostream& operator<< (std::ostream& stream, ternary& t) {
121  char buf[33] ; buf[32]=0 ;
122  for (int i=31 ; i>=0 ; i--)
123  {
124  if ((t.bit1>>i)&1)
125  {
126  if ((t.bit2>>i)&1)
127  buf[31-i] = '2' ;
128  else
129  buf[31-i] = '1' ;
130  }
131  else
132  {
133  if ((t.bit2>>i)&1)
134  {
135  if (i==t.special_quat_bit)
136  buf[31-i] = '3' ;
137  else
138  buf[31-i] = '?' ;
139  }
140  else
141  buf[31-i] = '0' ;
142  }
143  }
144  stream << buf ;
145  return stream ;
146  }
147 
148 private:
150  const uint32_t one = 1 ;
152 } ;
Definition: ternary.h:5
void operator++(int a[[maybe_unused]])
Definition: ternary.h:30
uint8_t special_quat_bit
Definition: ternary.h:151
void reset_quat_bit()
Definition: ternary.h:92
void clear()
Definition: ternary.h:94
friend std::ostream & operator<<(std::ostream &stream, ternary &t)
Definition: ternary.h:120
uint32_t bit1
Definition: ternary.h:149
void set_quat_bit(int n)
Definition: ternary.h:91
uint64_t to_integer()
Definition: ternary.h:99
const uint32_t one
Definition: ternary.h:150
uint32_t bit2
Definition: ternary.h:149
void set(int i, int v)
Definition: ternary.h:70
int operator[](int i)
Definition: ternary.h:7
bool operator<(unsigned int n)
Definition: ternary.h:65
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1181
unsigned int uint32_t
Definition: stdint.h:126
unsigned char uint8_t
Definition: stdint.h:124
unsigned __int64 uint64_t
Definition: stdint.h:136