# 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   FORCEINLINE 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   FORCEINLINE SmallMap() { }
    00050   FORCEINLINE ~SmallMap() { }
    00051
    00057   FORCEINLINE 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   FORCEINLINE bool Contains(const T &key)
    00071   {
    00072     return this->Find(key) != this->End();
    00073   }
    00074
    00080   FORCEINLINE void Erase(Pair *pair)
    00081   {
    00082     assert(pair >= this->Begin() && pair < this->End());
    00083     *pair = this->data[--this->items];
    00084   }
    00085
    00092   FORCEINLINE 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   FORCEINLINE 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   FORCEINLINE 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   FORCEINLINE 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 /* SMALLMAP_TYPE_HPP */