Fleet  0.0.9
Inference in the LOT
IntegerizedStack.h
Go to the documentation of this file.
1 #pragma once
2 
3 typedef size_t enumerationidx_t; // this is the type we use to store enuemration indices
4 
13  typedef unsigned long long value_t;
14 
15 protected:
16  value_t value;
17 
18 public:
19  IntegerizedStack(value_t v=0) : value(v) {
20  }
21 
22  // A bunch of static operations
23 
24  //std::pair<enumerationidx_t, enumerationidx_t> cantor_decode(const enumerationidx_t z) {
25  // enumerationidx_t w = (enumerationidx_t)std::floor((std::sqrt(8*z+1)-1.0)/2);
26  // enumerationidx_t t = w*(w+1)/2;
27  // return std::make_pair(z-t, w-(z-t));
28  //}
29 
30  static std::pair<enumerationidx_t, enumerationidx_t> rosenberg_strong_decode(const enumerationidx_t z) {
31  // https:arxiv.org/pdf/1706.04129.pdf
32  enumerationidx_t m = (enumerationidx_t)std::floor(std::sqrt(z));
33  if(z-m*m < m) {
34  return std::make_pair(z-m*m,m);
35  }
36  else {
37  return std::make_pair(m, m*(m+2)-z);
38  }
39  }
40 
42  auto m = std::max(x,y);
43  return m*(m+1)+x-y;
44  }
45 
46  static std::pair<enumerationidx_t,enumerationidx_t> mod_decode(const enumerationidx_t z, const enumerationidx_t k) {
47 
48  if(z == 0)
49  return std::make_pair(0,0); // I think?
50 
51  auto x = z % k;
52  return std::make_pair(x, (z-x)/k);
53  }
54 
56  assert(x < k);
57  return x + y*k;
58  }
59 
60  // Stack-like operations on z
62  // https:arxiv.org/pdf/1706.04129.pdf
63  enumerationidx_t m = (enumerationidx_t)std::floor(std::sqrt(z));
64  if(z-m*m < m) {
65  auto ret = z-m*m;
66  z = m;
67  return ret;
68  }
69  else {
70  auto ret = m;
71  z = m*(m+2)-z;
72  return ret;
73  }
74  }
76  auto x = z%k;
77  z = (z-x)/k;
78  return x;
79  }
80 
81 
82  value_t pop() {
83  auto u = rosenberg_strong_decode(value);
84  value = u.second;
85  return u.first;
86  }
87 
88  value_t pop(value_t modulus) { // for mod decoding
89  auto u = mod_decode(value, modulus);
90  value = u.second;
91  return u.first;
92  }
93 
94  void push(value_t x) {
95  auto before = value;
96  value = rosenberg_strong_encode(x, value);
97  assert(before <= value && "*** Overflow in encoding IntegerizedStack::push");
98  }
99 
100  void push(value_t x, value_t modulus) {
101  auto before = value;
102  value = mod_encode(x, value, modulus);
103  assert(before <= value && "*** Overflow in encoding IntegerizedStack::push");
104  }
105 
114  std::vector<value_t> split(size_t n) {
115  std::vector<value_t> out(n);
116  for(size_t i=0;i<n-1;i++) {
117  out[i] = pop();
118  }
119  out[n-1] = get_value();
120  value = 0; // we remove that last value so nobody gets confused, although the stack really shouldn't be accessed after this
121  return out;
122  }
123 
124  value_t get_value() const {
125  return value;
126  }
127 
128  bool empty() const {
129  // not necessarily empty, just will forever return 0
130  return value == 0;
131  }
132 
133  void operator=(value_t z) {
134  // just set my value
135  value = z;
136  }
137  void operator-=(value_t x) {
138  // subtract off my value
139  value -= x;
140  }
141  void operator+=(value_t x) {
142  // add to value
143  value += x;
144  }
145 };
146 std::ostream& operator<<(std::ostream& o, const IntegerizedStack& n) {
147  o << n.get_value();
148  return o;
149 }
value_t pop()
Definition: IntegerizedStack.h:82
value_t pop(value_t modulus)
Definition: IntegerizedStack.h:88
std::ostream & operator<<(std::ostream &o, const IntegerizedStack &n)
Definition: IntegerizedStack.h:146
value_t value
Definition: IntegerizedStack.h:16
static enumerationidx_t mod_pop(enumerationidx_t &z, const enumerationidx_t k)
Definition: IntegerizedStack.h:75
value_t get_value() const
Definition: IntegerizedStack.h:124
std::vector< value_t > split(size_t n)
Split into n children – this is the same as looping and popping, but provides a nicer interface NOTE...
Definition: IntegerizedStack.h:114
static enumerationidx_t mod_encode(const enumerationidx_t x, const enumerationidx_t y, const enumerationidx_t k)
Definition: IntegerizedStack.h:55
void operator+=(value_t x)
Definition: IntegerizedStack.h:141
static std::pair< enumerationidx_t, enumerationidx_t > rosenberg_strong_decode(const enumerationidx_t z)
Definition: IntegerizedStack.h:30
IntegerizedStack(value_t v=0)
Definition: IntegerizedStack.h:19
void push(value_t x, value_t modulus)
Definition: IntegerizedStack.h:100
void push(value_t x)
Definition: IntegerizedStack.h:94
static enumerationidx_t rosenberg_strong_encode(const enumerationidx_t x, const enumerationidx_t y)
Definition: IntegerizedStack.h:41
void operator=(value_t z)
Definition: IntegerizedStack.h:133
void operator-=(value_t x)
Definition: IntegerizedStack.h:137
static enumerationidx_t rosenberg_strong_pop(enumerationidx_t &z)
Definition: IntegerizedStack.h:61
static std::pair< enumerationidx_t, enumerationidx_t > mod_decode(const enumerationidx_t z, const enumerationidx_t k)
Definition: IntegerizedStack.h:46
size_t enumerationidx_t
Definition: IntegerizedStack.h:3
Definition: IntegerizedStack.h:12
bool empty() const
Definition: IntegerizedStack.h:128