Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef SMALLMAP_TYPE_HPP
00013 #define SMALLMAP_TYPE_HPP
00014
00015 #include "smallvec_type.hpp"
00016 #include "sort_func.hpp"
00017
00023 template <typename T, typename U>
00024 struct SmallPair {
00025 T first;
00026 U second;
00027
00029 inline SmallPair(const T &first, const U &second) : first(first), second(second) { }
00030 };
00031
00041 template <typename T, typename U, uint S = 16>
00042 struct SmallMap : SmallVector<SmallPair<T, U>, S> {
00043 typedef ::SmallPair<T, U> Pair;
00044 typedef Pair *iterator;
00045 typedef const Pair *const_iterator;
00046
00048 inline SmallMap() { }
00050 inline ~SmallMap() { }
00051
00057 inline Pair *Find(const T &key)
00058 {
00059 for (uint i = 0; i < this->items; i++) {
00060 if (key == this->data[i].first) return &this->data[i];
00061 }
00062 return this->End();
00063 }
00064
00070 inline bool Contains(const T &key)
00071 {
00072 return this->Find(key) != this->End();
00073 }
00074
00080 inline void Erase(Pair *pair)
00081 {
00082 assert(pair >= this->Begin() && pair < this->End());
00083 *pair = this->data[--this->items];
00084 }
00085
00092 inline bool Erase(const T &key)
00093 {
00094 for (uint i = 0; i < this->items; i++) {
00095 if (key == this->data[i].first) {
00096 this->data[i] = this->data[--this->items];
00097 return true;
00098 }
00099 }
00100 return false;
00101 }
00102
00109 inline bool Insert(const T &key, const U &data)
00110 {
00111 if (this->Contains(key)) return false;
00112 Pair *n = this->Append();
00113 n->first = key;
00114 n->second = data;
00115 return true;
00116 }
00117
00124 inline U &operator[](const T &key)
00125 {
00126 for (uint i = 0; i < this->items; i++) {
00127 if (key == this->data[i].first) return this->data[i].second;
00128 }
00129 Pair *n = this->Append();
00130 n->first = key;
00131 return n->second;
00132 }
00133
00134 inline void SortByKey()
00135 {
00136 QSortT(this->Begin(), this->items, KeySorter);
00137 }
00138
00139 static int CDECL KeySorter(const Pair *a, const Pair *b)
00140 {
00141 return a->first - b->first;
00142 }
00143 };
00144
00145 #endif