Itasca C++ Interface
Loading...
Searching...
No Matches
idef.h
1#pragma once
2
3#include <algorithm>
4#include <cassert>
5#include <cmath>
6#include <map>
7#include <queue>
8#include <set>
9#include <vector>
10
11template <typename T>
12const T &ibound(const T &min,const T &val,const T &max) {
13 if (val<min) return min;
14 if (val>max) return max;
15 return val;
16}
17
18inline int iround(const double &in) { return static_cast<int>(std::round(in)); }
19
20//template <typename T> class QVector;
21
22template <typename T>
23class IVector : public std::vector<T> {
24 public:
25 using std::vector<T>::vector;
26 using base = std::vector<T>;
27
28 operator IVector<T>();
29
30 int size() const { return to<int>(base::size()); }
31 void replace(int i,const T &value) { assert(i<size()); this->operator[](i) = value; }
32 void remove(int i) { this->erase(this->begin()+i); }
33 T value(int index,T def={}) const { if (index<0 or index>=size()) return def; return this->at(index); }
34 void insert(int index,const T &val) { this->base::insert(this->begin()+index,val); }
35 bool contains(const T &val) const { auto i = std::find(this->begin(),this->end(),val); return i!=this->end(); }
36 bool isEmpty() const { return size()==0; }
37 int indexOf(const T &val) const { auto i = std::find(this->begin(),this->end(),val); if (i==this->end()) return -1; return to<int>(i-this->begin()); }
38 bool removeOne(const T &val) { auto i = std::find(this->begin(),this->end(),val); if (i==this->end()) return false; this->erase(i); return true; }
39 void removeAt(int i) { auto it = this->begin() + i; this->erase(it); }
40 void removeAll(const T &t) { auto iend = std::remove(this->begin(),this->end(),t); this->erase(iend,this->end()); }
41 void pop_front() { this->erase(this->begin()); }
42 void append(const T &t) { this->push_back(t); }
43 void append(const IVector<T> &v) { for (auto &v2 : v) append(v2); }
44 const T &last() const { return this->back(); }
45 void push_front(const T &t) { this->base::insert(this->begin(),t); }
46 T &first() { return this->front(); }
47 const T &first() const{ return this->front(); }
48};
49
50template <typename T> class QList;
51
52template <typename T>
53class IList : public std::vector<T> {
54 public:
55 using std::vector<T>::vector;
56 //using iterator = typename std::vector<T>::iterator;
57
58 operator QList<T>();
59
60 //iterator begin() { return std::vector<T>::begin(); }
61 int size() const { return to<int>(std::vector<T>::size()); }
62 void removeAt(int i) { assert(i>=0); assert(i<size()); this->erase(this->begin()+i); }
63 void removeAll(const T &t) { auto iend = std::remove(this->begin(),this->end(),t); this->erase(iend,this->end()); }
64 bool removeOne(const T &t) { auto i = std::find(this->begin(),this->end(),t); if (i==this->end()) return false; this->erase(i); return true; }
65 void operator+=(const T &t) { this->push_back(t); }
66 void operator+=(const IList<T> &t) { for (auto &v : t) this->push_back(v); }
67 T value(int index,const T &def={}) const { if (index<0 or index>=this->size()) return def; return this->at(index); }
68 bool contains(const T &val) const { return std::find(this->begin(),this->end(),val)!=this->end(); }
69 void append(const T &val) { this->push_back(val); }
70 void prepend(const T &val) { push_front(val); }
71 bool isEmpty() const { return size()==0; }
72 void pop_front() { this->erase(this->begin()); }
73 void push_front(const T &t) { this->insert(this->begin(),t); }
74 const T &last() const { return this->back(); }
75 T &last() { return this->back(); }
76 int indexOf(const T &t,int from=0) const { auto s = this->begin()+from; auto f = std::find(s,this->end(),t); if (f==this->end()) return -1; return to<int>(f-this->begin()); }
77 IVector<T> toVector() const { IVector<T> out; for (auto &v : *this) out.push_back(v); return out; }
78 T takeFirst() { auto i=this->begin(); T val = *i; this->erase(i); return val; }
79 IList<T> &operator<<(const T &t) { append(t); return *this; }
80 void replace(int i,const T &val) { if (i<0 or i>=size()) return; this->operator[](i) = val; }
81 T &first() { return this->front(); }
82 const T &first() const { return this->front(); }
83};
84
85template <typename Key,typename Value>
86class IMap : public std::map<Key,Value> {
87public:
88 using std::map<Key,Value>::map;
89 using base = std::map<Key,Value>;
90 class iterator : public base::iterator {
91 public:
92 //using base::iterator::iterator;
93 iterator(const typename base::iterator &in) : base::iterator(in) { }
94 const Key &key() const { return this->base::iterator::operator*().first; }
95 Value &value() { return this->base::iterator::operator*().second; }
96 Value &operator*() { return value(); }
97 Value *operator->() { return &value(); }
98 };
99 class const_iterator : public base::const_iterator {
100 public:
101 //using base::const_iterator::const_iterator;
102 const_iterator(const typename base::const_iterator &in) : base::const_iterator(in) { }
103 const Key &key() const { return this->base::const_iterator::operator*().first; }
104 const Value &value() const { return this->base::const_iterator::operator*().second; }
105 const Value &operator*() const { return value(); }
106 const Value *operator->() { return &value(); }
107 };
108
109 iterator begin() { return base::begin(); }
110 iterator end() { return base::end(); }
111 const_iterator begin() const { return base::begin(); }
112 const_iterator end() const { return base::end(); }
113 const_iterator constBegin() const { return base::begin(); }
114 const_iterator constEnd() const { return base::end(); }
115 iterator lowerBound(const Key &key) { return this->lower_bound(key); }
116 iterator upperBound(const Key &key) { return this->upper_bound(key); }
117 Value value(const Key &k,Value def={}) const { auto it=this->find(k); if (it==this->end()) return def; return it.value(); }
118 Value &operator[](const Key &key) { return this->base::operator[](key); }
119 Value operator[](const Key &key) const { auto i = this->base::find(key); if (i==this->end()) return Value{}; return i->second; }
120
121 bool contains(const Key &k) const { return base::count(k)>0; }
122 bool isEmpty() const { return this->size()==0; }
123 IList<Key> keys() const { IList<Key> out; for (auto &[k,v] : (base &)*this) out.push_back(k); return out; }
124 IList<Value> values() const { IList<Value> out; for (auto &[k,v] : (base &)*this) out.push_back(v); return out; }
125 Key key(const Value &value,Key def={}) const;
126 Key firstKey() const { return this->base::begin()->first; }
127 Key lastKey() const { auto i = base::end(); --i; return i->first; }
128
129 iterator find(const Key &k) { return base::find(k); }
130 const_iterator find(const Key &k) const { return base::find(k); }
131 Value take(const Key &k) { auto it = find(k); if (it==end()) return {}; Value v = *it; this->erase(it); return v; }
132 iterator insert(const Key &k,const Value &v) { return base::insert({k,v}).first; }
133 iterator insertMulti(const Key &k,const Value &v) { return insert(k,v); }
134 void remove(const Key &k) { this->erase(k); }
135};
136
137template <typename Key,typename Value>
138class IMultiMap : public std::multimap<Key,Value> {
139public:
140 using std::multimap<Key,Value>::multimap;
141 using base = std::multimap<Key,Value>;
142 class iterator : public base::iterator {
143 public:
144 //using base::iterator::iterator;
145 iterator(const typename base::iterator &in) : base::iterator(in) { }
146 const Key &key() const { return this->base::iterator::operator*().first; }
147 Value &value() { return this->base::iterator::operator*().second; }
148 Value &operator*() { return value(); }
149 Value *operator->() { return &value(); }
150 };
151 class const_iterator : public base::const_iterator {
152 public:
153 //using base::const_iterator::const_iterator;
154 const_iterator(const typename base::const_iterator &in) : base::const_iterator(in) { }
155 const Key &key() const { return this->base::const_iterator::operator*().first; }
156 const Value &value() const { return this->base::const_iterator::operator*().second; }
157 const Value &operator*() const { return value(); }
158 Value *operator->() { return &value(); }
159 };
160
161 iterator begin() { return base::begin(); }
162 iterator end() { return base::end(); }
163 const_iterator begin() const { return base::begin(); }
164 const_iterator end() const { return base::end(); }
165 const_iterator constBegin() const { return base::begin(); }
166 const_iterator constEnd() const { return base::end(); }
167
168 iterator find(const Key &v) { return this->base::find(v); }
169 iterator insert(const Key &k,const Value &v) { return this->base::insert({k,v}); }
170 iterator insertMulti(const Key &k,const Value &v) { return this->base::insert({k,v}); }
171 iterator lowerBound(const Key &key) { return this->lower_bound(key); }
172 iterator upperBound(const Key &key) { return this->upper_bound(key); }
173 void remove(const Key &k) { this->erase(k); }
174 bool isEmpty() const { return this->size()==0; }
175 IList<Key> uniqueKeys() const { std::set<Key> s; for (auto &[k,v] : (base &)*this) s.insert(k); IList<Key> ret; for (auto &k : s) ret.push_back(k); return ret; }
176};
177
178template <typename T>
179class ISet : public std::set<T> {
180public:
181 using std::set<T>::set;
182 using base = std::set<T>;
183
184 void operator+=(const T &t) { this->insert(t); }
185 bool remove(const T &t) { return this->erase(t)>0; }
186 bool contains(const T &t) const { return this->find(t)!=this->end(); }
187 IList<T> values() const { IList<T> out; for (auto &v : (base &)*this) out.push_back(v); return out; }
188 int size() const { return to<int>(base::size()); }
189 ISet<T> &operator<<(const T &t) { this->base::insert(t); return *this; }
190 ISet<T> intersect(const ISet<T> &s) { ISet<T> out; std::set_intersection(this->begin(),this->end(),s.begin(),s.end(),std::inserter(out,out.begin())); return out; }
191 ISet<T> &subtract(const ISet<T> &s) { for (auto &t : s) this->erase(t); return *this; }
192};
193
194template <typename Key,typename Value>
195class IHash : public std::unordered_map<Key,Value> {
196public:
197 using std::unordered_map<Key,Value>::unordered_map;
198 using base = std::unordered_map<Key,Value>;
199 class iterator : public base::iterator {
200 public:
201 //using base::iterator::iterator;
202 iterator(const typename base::iterator &in) : base::iterator(in) { }
203 const Key &key() const { return this->base::iterator::operator*().first; }
204 Value &value() const { return this->base::iterator::operator*().second; }
205 Value &operator*() { return value(); }
206 Value *operator->() { return &value(); }
207 };
208 class const_iterator : public base::const_iterator {
209 public:
210 //using base::const_iterator::const_iterator;
211 const_iterator(const typename base::const_iterator &in) : base::const_iterator(in) { }
212 const Key &key() const { return this->base::const_iterator::operator*().first; }
213 const Value &value() const { return this->base::const_iterator::operator*().second; }
214 const Value &operator*() const { return value(); }
215 const Value *operator->() const { return &value(); }
216 };
217 iterator begin() { return base::begin(); }
218 iterator end() { return base::end(); }
219 const_iterator begin() const { return base::begin(); }
220 const_iterator end() const { return base::end(); }
221 const_iterator constBegin() const { return base::begin(); }
222 const_iterator constEnd() const { return base::end(); }
223 const_iterator find(const Key &k) const { return base::find(k); }
224 iterator find(const Key &k) { return base::find(k); }
225 IList<Value> values() const { IList<Value> out; for (auto &[k,v] : (base &)*this) out.push_back(v); return out; }
226 iterator insert(const Key &k,const Value &v) { return this->base::insert({k,v}).first; }
227 iterator insertMulti(const Key &k,const Value &v) { return insert(k,v); }
228 bool contains(const Key &k) const { auto i = this->find(k); return i!=this->end(); }
229 void remove(const Key &k) { this->erase(k); }
230 Value take(const Key &k) { auto i=this->find(k); if (i==this->end()) return Value{}; Value v = i->second; this->erase(i); return v; }
231 Value value(const Key &k,Value v={}) const { auto i=this->find(k); if (i==this->end()) return v; return i->second; }
232 Value &operator[](const Key &key) { return this->base::operator[](key); }
233 Value operator[](const Key &key) const { auto i = this->base::find(key); if (i==this->end()) return Value{}; return i->second; }
234};
235
236template <typename Key,typename Value>
237class IMultiHash : public std::unordered_multimap<Key,Value> {
238 public:
239 using std::unordered_multimap<Key,Value>::unordered_multimap;
240 using base = std::unordered_multimap<Key,Value>;
241 class iterator : public base::iterator {
242 public:
243 //using base::iterator::iterator;
244 iterator(const typename base::iterator &in) : base::iterator(in) { }
245 const Key &key() const { return this->base::iterator::operator*().first; }
246 Value &value() { return this->base::iterator::operator*().second; }
247 Value &operator*() { return value(); }
248 Value *operator->() { return &value(); }
249 };
250 class const_iterator : public base::const_iterator {
251 public:
252 //using base::const_iterator::const_iterator;
253 const_iterator(const typename base::const_iterator &in) : base::const_iterator(in) { }
254 const Key &key() const { return this->base::const_iterator::operator*().first; }
255 const Value &value() const { return this->base::const_iterator::operator*().second; }
256 const Value &operator*() const { return value(); }
257 };
258 iterator begin() { return base::begin(); }
259 iterator end() { return base::end(); }
260 const_iterator begin() const { return base::begin(); }
261 const_iterator end() const { return base::end(); }
262 const_iterator constBegin() const { return base::begin(); }
263 const_iterator constEnd() const { return base::end(); }
264 IList<Value> values() const { IList<Value> out; for (auto &[k,v] : (base &)*this) out.push_back(v); return out; }
265 void insert(const Key &k,const Value &v) { this->base::insert({k,v}); }
266 void insertMulti(const Key &k,const Value &v) { this->base::insert({k,v}); }
267 bool contains(const Key &k) const { auto i = this->find(k); return i!=this->end(); }
268 Value take(const Key &k) { auto i=this->find(k); if (i==this->end()) return Value{}; Value v = i->second; this->erase(i); return v; }
269 Value value(const Key &k,Value v={}) const { auto i=this->find(k); if (i==this->end()) return v; return i->second; }
270 Value &operator[](const Key &key) { return this->base::operator[](key); }
271 Value operator[](const Key &key) const { auto i = this->base::find(key); if (i==this->end()) return Value{}; return i->second; }
272};
273
274template <typename T>
275class IQueue : public std::queue<T> {
276 public:
277 using std::queue<T>::queue;
278 T dequeue() { T val = this->front(); this->pop(); return val; }
279 void enqueue(const T &t) { this->emplace(t); }
280 T &head() { return this->front(); }
281 const T &head() const { return this->front(); }
282 bool isEmpty() const { return this->size()==0; }
283};
284
285//---------------------------------------------------
286// Implementations
287//---------------------------------------------------
288template <typename Key,typename Value>
289Key IMap<Key,Value>::key(const Value &value,Key def) const {
290 for (auto &[k,v] : (base &)*this) {
291 if (v==value) return k;
292 }
293 return def;
294}
295
296// EoF
297
Definition idef.h:208
Definition idef.h:199
Definition idef.h:195
Definition idef.h:53
Definition idef.h:99
Definition idef.h:90
Definition idef.h:86
Definition idef.h:250
Definition idef.h:241
Definition idef.h:237
Definition idef.h:151
Definition idef.h:142
Definition idef.h:138
Definition idef.h:275
Definition idef.h:179
Definition idef.h:23
Definition idef.h:50
constexpr Vector2< T > max(const Vector2< T > &v1, const Vector2< T > &v2)
Template specialization for max, min.
Definition vect.h:433
constexpr Vector2< T > min(const Vector2< T > &v1, const Vector2< T > &v2)
Template specialization for max, min.
Definition vect.h:435