OpenTTD
Public Types | Public Member Functions | Static Public Member Functions
SmallMap< T, U, S > Struct Template Reference

Implementation of simple mapping class. More...

#include <smallmap_type.hpp>

Inheritance diagram for SmallMap< T, U, S >:
SmallVector< SmallPair< T, U >, S >

Public Types

typedef ::SmallPair< T, U > Pair
 
typedef Pairiterator
 
typedef const Pairconst_iterator
 

Public Member Functions

 SmallMap ()
 Creates new SmallMap. More...
 
 ~SmallMap ()
 Data are freed in SmallVector destructor.
 
const PairFind (const T &key) const
 Finds given key in this map. More...
 
PairFind (const T &key)
 Finds given key in this map. More...
 
bool Contains (const T &key) const
 Tests whether a key is assigned in this map. More...
 
void Erase (Pair *pair)
 Removes given pair from this map. More...
 
bool Erase (const T &key)
 Removes given key from this map. More...
 
bool Insert (const T &key, const U &data)
 Adds new item to this map. More...
 
U & operator[] (const T &key)
 Returns data belonging to this key. More...
 
void SortByKey ()
 
- Public Member Functions inherited from SmallVector< SmallPair< T, U >, S >
 SmallVector (const SmallVector &other)
 Copy constructor. More...
 
 SmallVector (const SmallVector< SmallPair< T, U >, X > &other)
 Generic copy constructor. More...
 
SmallVectoroperator= (const SmallVector &other)
 Assignment. More...
 
SmallVectoroperator= (const SmallVector< SmallPair< T, U >, X > &other)
 Generic assignment. More...
 
void Assign (const SmallVector< SmallPair< T, U >, X > &other)
 Assign items from other vector.
 
void Clear ()
 Remove all items from the list.
 
void Reset ()
 Remove all items from the list and free allocated memory.
 
void Compact ()
 Compact the list down to the smallest block size boundary.
 
SmallPair< T, U > * Append (uint to_add=1)
 Append an item and return it. More...
 
void Resize (uint num_items)
 Set the size of the vector, effectively truncating items from the end or appending uninitialised ones. More...
 
const SmallPair< T, U > * Find (const SmallPair< T, U > &item) const
 Search for the first occurrence of an item. More...
 
SmallPair< T, U > * Find (const SmallPair< T, U > &item)
 Search for the first occurrence of an item. More...
 
int FindIndex (const SmallPair< T, U > &item) const
 Search for the first occurrence of an item. More...
 
bool Contains (const SmallPair< T, U > &item) const
 Tests whether a item is present in the vector. More...
 
void Erase (SmallPair< T, U > *item)
 Removes given item from this vector. More...
 
void ErasePreservingOrder (uint pos, uint count=1)
 Remove items from the vector while preserving the order of other items. More...
 
bool Include (const SmallPair< T, U > &item)
 Tests whether a item is present in the vector, and appends it to the end if not. More...
 
uint Length () const
 Get the number of items in the list. More...
 
const SmallPair< T, U > * Begin () const
 Get the pointer to the first item (const) More...
 
SmallPair< T, U > * Begin ()
 Get the pointer to the first item. More...
 
const SmallPair< T, U > * End () const
 Get the pointer behind the last valid item (const) More...
 
SmallPair< T, U > * End ()
 Get the pointer behind the last valid item. More...
 
const SmallPair< T, U > * Get (uint index) const
 Get the pointer to item "number" (const) More...
 
SmallPair< T, U > * Get (uint index)
 Get the pointer to item "number". More...
 
const SmallPair< T, U > & operator[] (uint index) const
 Get item "number" (const) More...
 
SmallPair< T, U > & operator[] (uint index)
 Get item "number". More...
 

Static Public Member Functions

static int CDECL KeySorter (const Pair *a, const Pair *b)
 

Additional Inherited Members

- Protected Attributes inherited from SmallVector< SmallPair< T, U >, S >
SmallPair< T, U > * data
 The pointer to the first item.
 
uint items
 The number of items stored.
 
uint capacity
 The available space for storing items.
 

Detailed Description

template<typename T, typename U, uint S = 16>
struct SmallMap< T, U, S >

Implementation of simple mapping class.

Both types have to be POD ("Plain Old Data")! It has inherited accessors from SmallVector().

Template Parameters
TKey type.
UValue type.
SUnit of allocation.
See also
SmallVector

Definition at line 42 of file smallmap_type.hpp.

Constructor & Destructor Documentation

◆ SmallMap()

template<typename T, typename U, uint S = 16>
SmallMap< T, U, S >::SmallMap ( )
inline

Creates new SmallMap.

Data are initialized in SmallVector constructor

Definition at line 48 of file smallmap_type.hpp.

Member Function Documentation

◆ Contains()

template<typename T, typename U, uint S = 16>
bool SmallMap< T, U, S >::Contains ( const T &  key) const
inline

Tests whether a key is assigned in this map.

Parameters
keykey to test
Returns
true iff the item is present

Definition at line 83 of file smallmap_type.hpp.

Referenced by ScriptInfo::AddLabels(), GRFParameterInfo::Finalize(), UnmappedChoiceList::Flush(), and NetworkAddress::Resolve().

◆ Erase() [1/2]

template<typename T, typename U, uint S = 16>
void SmallMap< T, U, S >::Erase ( Pair pair)
inline

Removes given pair from this map.

Parameters
pairpair to remove
Note
it has to be pointer to pair in this map. It is overwritten by the last item.

Definition at line 93 of file smallmap_type.hpp.

◆ Erase() [2/2]

template<typename T, typename U, uint S = 16>
bool SmallMap< T, U, S >::Erase ( const T &  key)
inline

Removes given key from this map.

Parameters
keykey to remove
Returns
true iff the key was found
Note
last item is moved to its place, so don't increase your iterator if true is returned!

Definition at line 105 of file smallmap_type.hpp.

◆ Find() [1/2]

template<typename T, typename U, uint S = 16>
const Pair* SmallMap< T, U, S >::Find ( const T &  key) const
inline

Finds given key in this map.

Parameters
keykey to find
Returns
&Pair(key, data) if found, this->End() if not

Definition at line 57 of file smallmap_type.hpp.

Referenced by ChangeGRFParamValueNames(), Vehicle::GetConsistFreeCapacities(), Layouter::GetFont(), FreeTypeFontCache::GetFontTable(), and GetRating().

◆ Find() [2/2]

template<typename T, typename U, uint S = 16>
Pair* SmallMap< T, U, S >::Find ( const T &  key)
inline

Finds given key in this map.

Parameters
keykey to find
Returns
&Pair(key, data) if found, this->End() if not

Definition at line 70 of file smallmap_type.hpp.

◆ Insert()

template<typename T, typename U, uint S = 16>
bool SmallMap< T, U, S >::Insert ( const T &  key,
const U &  data 
)
inline

Adds new item to this map.

Parameters
keykey
datadata
Returns
true iff the key wasn't already present

Definition at line 122 of file smallmap_type.hpp.

Referenced by ScriptInfo::AddLabels(), ChangeGRFParamValueNames(), FreeTypeFontCache::GetFontTable(), and GRFParameterInfo::GRFParameterInfo().

◆ operator[]()

template<typename T, typename U, uint S = 16>
U& SmallMap< T, U, S >::operator[] ( const T &  key)
inline

Returns data belonging to this key.

Parameters
keykey
Returns
data belonging to this key
Note
if this key wasn't present, new entry is created

Definition at line 137 of file smallmap_type.hpp.


The documentation for this struct was generated from the following file: