smallmap_type.hpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
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 const Pair *Find(const T &key) const
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 Pair *Find(const T &key)
00071   {
00072     for (uint i = 0; i < this->items; i++) {
00073       if (key == this->data[i].first) return &this->data[i];
00074     }
00075     return this->End();
00076   }
00077 
00083   inline bool Contains(const T &key) const
00084   {
00085     return this->Find(key) != this->End();
00086   }
00087 
00093   inline void Erase(Pair *pair)
00094   {
00095     assert(pair >= this->Begin() && pair < this->End());
00096     *pair = this->data[--this->items];
00097   }
00098 
00105   inline bool Erase(const T &key)
00106   {
00107     for (uint i = 0; i < this->items; i++) {
00108       if (key == this->data[i].first) {
00109         this->data[i] = this->data[--this->items];
00110         return true;
00111       }
00112     }
00113     return false;
00114   }
00115 
00122   inline bool Insert(const T &key, const U &data)
00123   {
00124     if (this->Contains(key)) return false;
00125     Pair *n = this->Append();
00126     n->first = key;
00127     n->second = data;
00128     return true;
00129   }
00130 
00137   inline U &operator[](const T &key)
00138   {
00139     for (uint i = 0; i < this->items; i++) {
00140       if (key == this->data[i].first) return this->data[i].second;
00141     }
00142     Pair *n = this->Append();
00143     n->first = key;
00144     return n->second;
00145   }
00146 
00147   inline void SortByKey()
00148   {
00149     QSortT(this->Begin(), this->items, KeySorter);
00150   }
00151 
00152   static int CDECL KeySorter(const Pair *a, const Pair *b)
00153   {
00154     return a->first - b->first;
00155   }
00156 };
00157 
00158 #endif /* SMALLMAP_TYPE_HPP */