25 using std::vector<T>::vector;
26 using base = std::vector<T>;
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(); }
53class IList :
public std::vector<T> {
55 using std::vector<T>::vector;
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(); }
86class IMap :
public std::map<Key,Value> {
88 using std::map<Key,Value>::map;
89 using base = std::map<Key,Value>;
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(); }
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(); }
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; }
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; }
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; }
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); }
140 using std::multimap<Key,Value>::multimap;
141 using base = std::multimap<Key,Value>;
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(); }
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(); }
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(); }
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; }
179class ISet :
public std::set<T> {
181 using std::set<T>::set;
182 using base = std::set<T>;
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; }
195class IHash :
public std::unordered_map<Key,Value> {
197 using std::unordered_map<Key,Value>::unordered_map;
198 using base = std::unordered_map<Key,Value>;
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(); }
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(); }
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); }
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; }
237class IMultiHash :
public std::unordered_multimap<Key,Value> {
239 using std::unordered_multimap<Key,Value>::unordered_multimap;
240 using base = std::unordered_multimap<Key,Value>;
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(); }
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(); }
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(); }
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; }
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