12 #ifndef SMALLMAP_TYPE_HPP 13 #define SMALLMAP_TYPE_HPP 23 template <
typename T,
typename U>
29 inline SmallPair(
const T &first,
const U &second) : first(first), second(second) { }
41 template <
typename T,
typename U, u
int S = 16>
43 typedef ::SmallPair<T, U>
Pair;
57 inline const Pair *
Find(
const T &key)
const 59 for (uint i = 0; i < this->items; i++) {
60 if (key == this->data[i].first)
return &this->data[i];
70 inline Pair *
Find(
const T &key)
72 for (uint i = 0; i < this->items; i++) {
73 if (key == this->data[i].first)
return &this->data[i];
85 return this->Find(key) != this->End();
95 assert(pair >= this->Begin() && pair < this->End());
96 *pair = this->data[--this->items];
107 for (uint i = 0; i < this->items; i++) {
108 if (key == this->data[i].first) {
109 this->data[i] = this->data[--this->items];
122 inline bool Insert(
const T &key,
const U &data)
124 if (this->Contains(key))
return false;
125 Pair *n = this->Append();
139 for (uint i = 0; i < this->items; i++) {
140 if (key == this->data[i].first)
return this->data[i].second;
142 Pair *n = this->Append();
147 inline void SortByKey()
149 QSortT(this->Begin(), this->items, KeySorter);
152 static int CDECL KeySorter(
const Pair *a,
const Pair *b)
154 return a->first - b->first;
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
const Pair * Find(const T &key) const
Finds given key in this map.
SmallPair(const T &first, const U &second)
Initializes this Pair with data.
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
Implementation of simple mapping class.
Pair * Find(const T &key)
Finds given key in this map.
bool Insert(const T &key, const U &data)
Adds new item to this map.
~SmallMap()
Data are freed in SmallVector destructor.
Simple vector template class.
SmallMap()
Creates new SmallMap.
void Erase(Pair *pair)
Removes given pair from this map.
U & operator[](const T &key)
Returns data belonging to this key.
Functions related to sorting operations.
static void QSortT(T *base, uint num, int(CDECL *comparator)(const T *, const T *), bool desc=false)
Type safe qsort()
bool Erase(const T &key)
Removes given key from this map.