2 #ifndef HOBBES_UTIL_ARRAY_HPP_INCLUDED 3 #define HOBBES_UTIL_ARRAY_HPP_INCLUDED 18 static const UnitV
unitv = 0x00;
21 typedef std::vector<uint8_t>
bytes;
26 return std::vector<T>();
29 template <
typename T,
typename ... Ts>
30 std::vector<T>
list(
const T& x,
const Ts& ... xs) {
31 std::vector<T>
r = {x,xs...};
37 std::vector<T>
range(
const T& i,
const T& e) {
39 for (T t = i; t < e; ++t) {
47 bool in(T x,
const std::set<T>& xs) {
48 return xs.find(x) != xs.end();
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) {
63 int index(
const std::vector<T>& xs, T x) {
64 for (
int i = 0; i < xs.size(); ++i) {
70 std::ostringstream ss;
71 ss << x <<
" not in [";
74 for (
size_t i = 1; i < xs.size(); ++i) {
79 throw std::runtime_error(ss.str());
83 std::vector<int>
index(
const std::vector<T>& xs,
const std::vector<T>& lxs) {
85 for (
typename std::vector<T>::const_iterator lx = lxs.begin(); lx != lxs.end(); ++lx) {
86 result.push_back(index<T>(xs, *lx));
91 template <
typename T,
typename I>
92 T
select(
const std::vector<T>& xs, I i) {
96 template <
typename T,
typename I>
97 std::vector<T>
select(
const std::vector<T>& xs, I b, I e) {
99 for (I j = b; j < e; ++j) {
100 r.push_back(
select(xs, j));
105 template <
typename T,
typename I>
106 std::vector<T>
select(
const std::vector<T>& xs,
const std::vector<I>&
is) {
108 for (
typename std::vector<I>::const_iterator i = is.begin(); i != is.end(); ++i) {
109 r.push_back(
select(xs, *i));
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);
119 throw std::runtime_error(
"domain out of range error in map lookup");
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));
134 template <
typename K,
typename V>
135 std::map<K, V>
drop(
const std::map<K, V>&
m,
const std::set<K>& ks) {
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;
145 template <
typename T>
147 return std::vector<T>(xs.begin(), xs.end());
150 template <
typename CT>
151 std::set<typename CT::value_type>
toSet(
const CT& xs) {
152 std::set<typename CT::value_type>
r;
159 template <
typename CT>
160 inline CT
fromSet(
const std::set<typename CT::value_type>& xs) {
163 r.insert(r.end(), x);
168 template <
typename T>
169 std::set<T>
setUnion(
const std::set<T>& lhs,
const std::set<T>& rhs) {
171 std::set_union(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), std::inserter(r, r.begin()));
175 template <
typename T>
176 std::set<T>
setUnion(
const std::vector< std::set<T> >& ss) {
178 for (
typename std::vector< std::set<T> >::const_iterator s = ss.begin(); s != ss.end(); ++s) {
179 r.insert(s->begin(), s->end());
184 template <
typename T>
187 std::set_difference(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), std::inserter(r, r.begin()));
191 template <
typename T>
198 template <
typename K,
typename V>
199 std::set<K>
keys(
const std::map<K, V>&
m) {
201 for (
typename std::map<K, V>::const_iterator kv = m.begin(); kv != m.end(); ++kv) {
207 template <
typename L,
typename R>
208 std::vector<L>
first(
const std::vector< std::pair<L, R> >& xs) {
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);
217 template <
typename K,
typename V>
218 std::vector<V>
values(
const std::map<K, V>&
m) {
221 for (
typename std::map<K, V>::const_iterator kv = m.begin(); kv != m.end(); ++kv) {
222 r.push_back(kv->second);
227 template <
typename L,
typename R>
228 std::vector<R>
second(
const std::vector< std::pair<L, R> >& xs) {
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);
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));
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());
247 for (
size_t i = 0; i < n; ++i) {
248 r.push_back(std::pair<L, R>(left[i], right[i]));
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));
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>();
263 return std::vector<T>(xs.begin() + n, xs.end());
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));
276 template <
typename CT,
typename CCT>
279 for (
const auto& c : cs) {
280 r.insert(r.end(), c.begin(), c.end());
285 template <
typename T>
286 std::vector<T>
cons(T h, std::vector<T> t) {
287 t.insert(t.begin(), h);
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());
296 template <
typename T>
297 std::vector<T>
append(
const std::vector<T>& xs, T x) {
298 std::vector<T>
r(xs);
303 template <
typename T>
304 std::vector<T>
append(
const std::vector<T>& xs,
const std::vector<T>& ys) {
306 r.reserve(xs.size() + ys.size());
319 size_t msz = 1+((rowc*
colc)/8);
320 this->
data =
new uint8_t[msz];
321 memset(this->
data, s ? 0xFF : 0, msz);
324 size_t msz = 1+((this->
rowc*this->
colc)/8);
325 this->
data =
new uint8_t[msz];
333 size_t msz = 1+((this->
rowc*this->
colc)/8);
334 this->
data =
new uint8_t[msz];
343 size_t i = (r*this->
colc)+c;
346 return (this->
data[k] & (1 << b)) != 0;
348 inline void set(
size_t r,
size_t c,
bool f) {
349 size_t i = (r*this->
colc)+c;
354 this->
data[k] |= 1 << b;
356 this->
data[k] &= ~(1 << b);
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 ");
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
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
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