hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
array.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_UTIL_ARRAY_HPP_INCLUDED
3 #define HOBBES_UTIL_ARRAY_HPP_INCLUDED
4 
5 #include <map>
6 #include <set>
7 #include <vector>
8 #include <stdexcept>
9 #include <algorithm>
10 #include <functional>
11 #include <sstream>
12 #include <string.h>
13 
14 namespace hobbes {
15 
16 // a unit value, since C++ doesn't handle 'void' correctly
17 typedef unsigned char UnitV;
18 static const UnitV unitv = 0x00;
19 
20 // byte sequences, a common type of array
21 typedef std::vector<uint8_t> bytes;
22 
23 // shorthand for list construction (maybe not necessary with C++ initializer lists)
24 template <typename T>
25  std::vector<T> list() {
26  return std::vector<T>();
27  }
28 
29 template <typename T, typename ... Ts>
30  std::vector<T> list(const T& x, const Ts& ... xs) {
31  std::vector<T> r = {x,xs...};
32  return r;
33  }
34 
35 // [i..e]
36 template <typename T>
37  std::vector<T> range(const T& i, const T& e) {
38  std::vector<T> result;
39  for (T t = i; t < e; ++t) {
40  result.push_back(t);
41  }
42  return result;
43  }
44 
45 // x in (xs :: set T)
46 template <typename T>
47  bool in(T x, const std::set<T>& xs) {
48  return xs.find(x) != xs.end();
49  }
50 
51 // x in (xs :: vector T)
52 template <typename T>
53  bool in(T x, const std::vector<T>& xs) {
54  for (typename std::vector<T>::const_iterator xi = xs.begin(); xi != xs.end(); ++xi) {
55  if (*xi == x) {
56  return true;
57  }
58  }
59  return false;
60  }
61 
62 template <typename T>
63  int index(const std::vector<T>& xs, T x) {
64  for (int i = 0; i < xs.size(); ++i) {
65  if (xs[i] == x) {
66  return i;
67  }
68  }
69 
70  std::ostringstream ss;
71  ss << x << " not in [";
72  if (xs.size() > 0) {
73  ss << xs[0];
74  for (size_t i = 1; i < xs.size(); ++i) {
75  ss << ", " << xs[i];
76  }
77  }
78  ss << "]";
79  throw std::runtime_error(ss.str());
80  }
81 
82 template <typename T>
83  std::vector<int> index(const std::vector<T>& xs, const std::vector<T>& lxs) {
84  std::vector<int> result;
85  for (typename std::vector<T>::const_iterator lx = lxs.begin(); lx != lxs.end(); ++lx) {
86  result.push_back(index<T>(xs, *lx));
87  }
88  return result;
89  }
90 
91 template <typename T, typename I>
92  T select(const std::vector<T>& xs, I i) {
93  return xs[i];
94  }
95 
96 template <typename T, typename I>
97  std::vector<T> select(const std::vector<T>& xs, I b, I e) {
98  std::vector<T> r;
99  for (I j = b; j < e; ++j) {
100  r.push_back(select(xs, j));
101  }
102  return r;
103  }
104 
105 template <typename T, typename I>
106  std::vector<T> select(const std::vector<T>& xs, const std::vector<I>& is) {
107  std::vector<T> r;
108  for (typename std::vector<I>::const_iterator i = is.begin(); i != is.end(); ++i) {
109  r.push_back(select(xs, *i));
110  }
111  return r;
112  }
113 
114 template <typename K, typename V>
115  std::pair<K, V> select(const std::map<K, V>& m, K k) {
116  typename std::map<K,V>::const_iterator mi = m.find(k);
117 
118  if (mi == m.end()) {
119  throw std::runtime_error("domain out of range error in map lookup");
120  } else {
121  return *mi;
122  }
123  }
124 
125 template <typename K, typename V>
126  std::vector< std::pair<K, V> > select(const std::map<K, V>& m, const std::vector<K>& ks) {
127  std::vector< std::pair<K, V> > result;
128  for (typename std::vector<K>::const_iterator k = ks.begin(); k != ks.end(); ++k) {
129  result.push_back(select(m, *k));
130  }
131  return result;
132  }
133 
134 template <typename K, typename V>
135  std::map<K, V> drop(const std::map<K, V>& m, const std::set<K>& ks) {
136  std::map<K, V> result;
137  for (typename std::map<K,V>::const_iterator p = m.begin(); p != m.end(); ++p) {
138  if (ks.find(p->first) == ks.end()) {
139  result[p->first] = p->second;
140  }
141  }
142  return result;
143  }
144 
145 template <typename T>
146  std::vector<T> toVector(const std::set<T>& xs) {
147  return std::vector<T>(xs.begin(), xs.end());
148  }
149 
150 template <typename CT>
151  std::set<typename CT::value_type> toSet(const CT& xs) {
152  std::set<typename CT::value_type> r;
153  for (auto x : xs) {
154  r.insert(x);
155  }
156  return r;
157  }
158 
159 template <typename CT>
160  inline CT fromSet(const std::set<typename CT::value_type>& xs) {
161  CT r;
162  for (auto x : xs) {
163  r.insert(r.end(), x);
164  }
165  return r;
166  }
167 
168 template <typename T>
169  std::set<T> setUnion(const std::set<T>& lhs, const std::set<T>& rhs) {
170  std::set<T> r;
171  std::set_union(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), std::inserter(r, r.begin()));
172  return r;
173  }
174 
175 template <typename T>
176  std::set<T> setUnion(const std::vector< std::set<T> >& ss) {
177  std::set<T> r;
178  for (typename std::vector< std::set<T> >::const_iterator s = ss.begin(); s != ss.end(); ++s) {
179  r.insert(s->begin(), s->end());
180  }
181  return r;
182  }
183 
184 template <typename T>
185  std::set<T> setDifference(const std::set<T>& lhs, const std::set<T>& rhs) {
186  std::set<T> r;
187  std::set_difference(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), std::inserter(r, r.begin()));
188  return r;
189  }
190 
191 template <typename T>
192  std::set<T> setDifference(const std::set<T>& lhs, const T& x) {
193  std::set<T> sx;
194  sx.insert(x);
195  return setDifference(lhs, sx);
196  }
197 
198 template <typename K, typename V>
199  std::set<K> keys(const std::map<K, V>& m) {
200  std::set<K> r;
201  for (typename std::map<K, V>::const_iterator kv = m.begin(); kv != m.end(); ++kv) {
202  r.insert(kv->first);
203  }
204  return r;
205  }
206 
207 template <typename L, typename R>
208  std::vector<L> first(const std::vector< std::pair<L, R> >& xs) {
209  std::vector<L> result;
210  result.reserve(xs.size());
211  for (typename std::vector< std::pair<L, R> >::const_iterator x = xs.begin(); x != xs.end(); ++x) {
212  result.push_back(x->first);
213  }
214  return result;
215  }
216 
217 template <typename K, typename V>
218  std::vector<V> values(const std::map<K, V>& m) {
219  std::vector<V> r;
220  r.reserve(m.size());
221  for (typename std::map<K, V>::const_iterator kv = m.begin(); kv != m.end(); ++kv) {
222  r.push_back(kv->second);
223  }
224  return r;
225  }
226 
227 template <typename L, typename R>
228  std::vector<R> second(const std::vector< std::pair<L, R> >& xs) {
229  std::vector<R> result;
230  result.reserve(xs.size());
231  for (typename std::vector< std::pair<L, R> >::const_iterator x = xs.begin(); x != xs.end(); ++x) {
232  result.push_back(x->second);
233  }
234  return result;
235  }
236 
237 template <typename L, typename R>
238  std::pair< std::vector<L>, std::vector<R> > unzip(const std::vector< std::pair<L, R> >& ps) {
239  return std::pair< std::vector<L>, std::vector<R> >(first(ps), second(ps));
240  }
241 
242 template <typename L, typename R>
243  std::vector< std::pair<L, R> > zip(const std::vector<L>& left, const std::vector<R>& right) {
244  std::vector< std::pair<L, R> > r;
245  size_t n = std::min<size_t>(left.size(), right.size());
246  r.reserve(n);
247  for (size_t i = 0; i < n; ++i) {
248  r.push_back(std::pair<L, R>(left[i], right[i]));
249  }
250  return r;
251  }
252 
253 template <typename T>
254  std::vector<T> take(const std::vector<T>& xs, size_t n) {
255  return std::vector<T>(xs.begin(), xs.begin() + std::min(xs.size(), n));
256  }
257 
258 template <typename T>
259  std::vector<T> drop(const std::vector<T>& xs, size_t n) {
260  if (n >= xs.size()) {
261  return std::vector<T>();
262  } else {
263  return std::vector<T>(xs.begin() + n, xs.end());
264  }
265  }
266 
267 template <typename T>
268  std::vector<std::string> show(const std::vector<T>& xs) {
269  std::vector<std::string> r;
270  for (auto x = xs.begin(); x != xs.end(); ++x) {
271  r.push_back(show(*x));
272  }
273  return r;
274  }
275 
276 template <typename CT, typename CCT>
277  CT concat(const CCT& cs) {
278  CT r;
279  for (const auto& c : cs) {
280  r.insert(r.end(), c.begin(), c.end());
281  }
282  return r;
283  }
284 
285 template <typename T>
286  std::vector<T> cons(T h, std::vector<T> t) {
287  t.insert(t.begin(), h);
288  return t;
289  }
290 
291 template <typename T>
292  void append(std::vector<T>* xs, const std::vector<T>& ys) {
293  xs->insert(xs->end(), ys.begin(), ys.end());
294  }
295 
296 template <typename T>
297  std::vector<T> append(const std::vector<T>& xs, T x) {
298  std::vector<T> r(xs);
299  r.push_back(x);
300  return r;
301  }
302 
303 template <typename T>
304  std::vector<T> append(const std::vector<T>& xs, const std::vector<T>& ys) {
305  std::vector<T> r;
306  r.reserve(xs.size() + ys.size());
307  append(&r, xs);
308  append(&r, ys);
309  return r;
310  }
311 
312 
313 // basic bit-packed 2D bool array (maybe there's already something that does this job?)
314 class bit_table {
315 public:
316  inline bit_table() : rowc(0), colc(0), data(0) {
317  }
318  inline bit_table(size_t rowc, size_t colc, bool s) : rowc(rowc), colc(colc) {
319  size_t msz = 1+((rowc*colc)/8);
320  this->data = new uint8_t[msz];
321  memset(this->data, s ? 0xFF : 0, msz);
322  }
323  inline bit_table(const bit_table& rhs) : rowc(rhs.rowc), colc(rhs.colc) {
324  size_t msz = 1+((this->rowc*this->colc)/8);
325  this->data = new uint8_t[msz];
326  memcpy(this->data, rhs.data, msz);
327  }
328  inline bit_table& operator=(const bit_table& rhs) {
329  if (this != &rhs) {
330  delete[] this->data;
331  this->rowc = rhs.rowc;
332  this->colc = rhs.colc;
333  size_t msz = 1+((this->rowc*this->colc)/8);
334  this->data = new uint8_t[msz];
335  memcpy(this->data, rhs.data, msz);
336  }
337  return *this;
338  }
339  inline ~bit_table() {
340  delete[] this->data;
341  }
342  inline bool operator()(size_t r, size_t c) const {
343  size_t i = (r*this->colc)+c;
344  size_t k = i / 8;
345  uint8_t b = i % 8;
346  return (this->data[k] & (1 << b)) != 0;
347  }
348  inline void set(size_t r, size_t c, bool f) {
349  size_t i = (r*this->colc)+c;
350  size_t k = i / 8;
351  uint8_t b = i % 8;
352 
353  if (f) {
354  this->data[k] |= 1 << b;
355  } else {
356  this->data[k] &= ~(1 << b);
357  }
358  }
359  inline size_t rows() const { return this->rowc; }
360  inline size_t cols() const { return this->colc; }
361 private:
362  uint8_t* data;
363  size_t rowc, colc;
364 };
365 inline std::ostream& operator<<(std::ostream& out, const bit_table& bt) {
366  for (size_t r = 0; r != bt.rows(); ++r) {
367  for (size_t c = 0; c != bt.cols(); ++c) {
368  out << (bt(r,c) ? "1 " : "0 ");
369  }
370  out << "\n";
371  }
372  return out;
373 }
374 
375 }
376 
377 #endif
uint8_t * data
Definition: array.H:362
bool operator()(size_t r, size_t c) const
Definition: array.H:342
size_t cols() const
Definition: array.H:360
const T * is(const S *s)
Definition: ptr.H:107
Definition: terminal.H:71
Definition: array.H:314
std::vector< T > take(const std::vector< T > &xs, size_t n)
Definition: array.H:254
std::vector< R > second(const std::vector< std::pair< L, R > > &xs)
Definition: array.H:228
std::vector< uint8_t > bytes
Definition: array.H:21
bit_table(size_t rowc, size_t colc, bool s)
Definition: array.H:318
Definition: boot.H:7
void append(std::vector< T > *xs, const std::vector< T > &ys)
Definition: array.H:292
std::pair< std::vector< L >, std::vector< R > > unzip(const std::vector< std::pair< L, R > > &ps)
Definition: array.H:238
int index(const std::vector< T > &xs, T x)
Definition: array.H:63
std::ostream & operator<<(std::ostream &out, const array< char > *x)
Definition: hobbes.H:38
bit_table(const bit_table &rhs)
Definition: array.H:323
std::set< T > setDifference(const std::set< T > &lhs, const std::set< T > &rhs)
Definition: array.H:185
T select(const std::vector< T > &xs, I i)
Definition: array.H:92
size_t rows() const
Definition: array.H:359
std::vector< T > cons(T h, std::vector< T > t)
Definition: array.H:286
std::map< K, V > drop(const std::map< K, V > &m, const std::set< K > &ks)
Definition: array.H:135
~bit_table()
Definition: array.H:339
std::string show(const Expr &e)
Definition: expr.C:19
std::vector< std::pair< L, R > > zip(const std::vector< L > &left, const std::vector< R > &right)
Definition: array.H:243
std::vector< T > list()
Definition: array.H:25
static const UnitV unitv
Definition: array.H:18
size_t r(const reader::MetaData &md, size_t o, T *t)
Definition: storage.H:1730
size_t colc
Definition: array.H:363
bit_table & operator=(const bit_table &rhs)
Definition: array.H:328
#define out
Definition: netio.H:19
std::set< K > keys(const std::map< K, V > &m)
Definition: array.H:199
Definition: terminal.H:71
std::set< T > setUnion(const std::set< T > &lhs, const std::set< T > &rhs)
Definition: array.H:169
uint32_t result
Definition: regex.C:376
size_t rowc
Definition: array.H:363
std::vector< T > range(const T &i, const T &e)
Definition: array.H:37
std::vector< V > values(const std::map< K, V > &m)
Definition: array.H:218
std::set< typename CT::value_type > toSet(const CT &xs)
Definition: array.H:151
std::vector< L > first(const std::vector< std::pair< L, R > > &xs)
Definition: array.H:208
std::vector< T > toVector(const std::set< T > &xs)
Definition: array.H:146
CT concat(const CCT &cs)
Definition: array.H:277
bit_table()
Definition: array.H:316
LexicalAnnotation m(const YYLTYPE &p)
Definition: hexpr.parse.C:127
bool in(T x, const std::set< T > &xs)
Definition: array.H:47
CT fromSet(const std::set< typename CT::value_type > &xs)
Definition: array.H:160
unsigned char UnitV
Definition: array.H:17