CUtlMap: Difference between revisions
No edit summary |
No edit summary |
||
Line 11: | Line 11: | ||
; <code>InvalidIndex( )</code> | ; <code>InvalidIndex( )</code> | ||
: Returns an invalid index. | : Returns an invalid index. | ||
; <code>EnsureCapacity( int num )</code> | ; <code>EnsureCapacity( [[int]] num )</code> | ||
: Makes sure we have enough memory allocated to store a requested number of elements. | : Makes sure we have enough memory allocated to store a requested number of elements. | ||
; <code>MaxElement()</code> | ; <code>MaxElement()</code> | ||
Line 17: | Line 17: | ||
== Adding elements == | == Adding elements == | ||
; <code>Insert( const KeyType_t &key, const ElemType_t &insert )</code> | ; <code>Insert( const [[KeyType_t]] &key, const [[ElemType_t]] &insert )</code> | ||
: Inserts the element and key into the tail of the map. | : Inserts the element and key into the tail of the map. | ||
; <code>InsertOrReplace( ( const KeyType_t &key, const ElemType_t &insert )</code> | ; <code>InsertOrReplace( ( const [[KeyType_t]] &key, const [[ElemType_t]] &insert )</code> | ||
: Inserts the element and key into the tail of the map, replacing the element if the key is the same as one that already exists | : Inserts the element and key into the tail of the map, replacing the element if the key is the same as one that already exists | ||
; <code>Reinsert( const KeyType_t &key, IndexType_t i )</code> | ; <code>Reinsert( const [[KeyType_t]] &key, [[IndexType_t]] i )</code> | ||
: If you change the search key, this can be used to reinsert the element into the map. | : If you change the search key, this can be used to reinsert the element into the map. | ||
{{warning|You must specify the LessFunc or else Source will crash on trying to insert more than one element into the map!}} | {{warning|You must specify the LessFunc or else Source will crash on trying to insert more than one element into the map!}} | ||
To specify the LessFunc for your map, use <code>SetDefLessFunc( | To specify the LessFunc for your map, use <code>SetDefLessFunc(exampleCUtlMap)</code>. | ||
== Accessing Elements == | == Accessing Elements == | ||
You can use array-style or use a method for index access: | You can use array-style or use a method for index access: | ||
; <code>Element( IndexType_t i )</code> | ; <code>Element( [[IndexType_t]] i )</code> | ||
; <code>operator[]( IndexType_t i )</code> | ; <code>operator[]( [[IndexType_t]] i )</code> | ||
: Returns the element at index i | : Returns the element at index i | ||
{{warning|Unlike std::map, '''<code>CUtlMap</code>''''s accessor functions do not find a specific element by key, but rather, by index. }} | {{warning|Unlike std::map, '''<code>CUtlMap</code>''''s accessor functions do not find a specific element by key, but rather, by index. }} | ||
; <code>Key( IndexType_t i )</code> | ; <code>Key( [[IndexType_t]] i )</code> | ||
: Returns the key at index i | : Returns the key at index i | ||
; <code>Find( const KeyType_t &key )</code> | ; <code>Find( const [[KeyType_t]] &key )</code> | ||
: Returns the index at the given key | : Returns the index at the given key | ||
Line 43: | Line 43: | ||
Single: | Single: | ||
; <code>RemoveAt( IndexType_t i )</code> | ; <code>RemoveAt( [[IndexType_t]] i )</code> | ||
: Removes the element at index i | : Removes the element at index i | ||
; <code>Remove( const KeyType %key )</code> | ; <code>Remove( const [[KeyType]] %key )</code> | ||
: Removes the element mapped to the given key | : Removes the element mapped to the given key | ||
Line 56: | Line 56: | ||
== Iteration == | == Iteration == | ||
; <code>FirstInorder()</code> | ; <code>FirstInorder()</code> | ||
; <code>NextInorder( IndexType_t i )</code> | ; <code>NextInorder( [[IndexType_t]] i )</code> | ||
; <code>PrevInorder( IndexType_t i )</code> | ; <code>PrevInorder( [[IndexType_t]] i )</code> | ||
; <code>LastInorder()</code> | ; <code>LastInorder()</code> | ||
Line 63: | Line 63: | ||
You can get the tree which the CUtlMap consists of using | You can get the tree which the CUtlMap consists of using | ||
; <code>AccessTree()</code> | ; <code>AccessTree()</code> | ||
: Returns CTree | : Returns [[CTree]] | ||
You can swap one CUtlMap for another with | You can swap one CUtlMap for another with | ||
; <code>Swap( CUtlMap< K, T, I > &that )</code> | ; <code>Swap( CUtlMap< K, T, I > &that )</code> |
Revision as of 04:04, 14 March 2016
CUtlMap
is Source's equivalent of the C++ Map type. A map is essentially a dynamic length array where each element, or member,
is mapped to a specific, unique key value. The code can be found in public/tier1/utlmap.h
.
Count()
- The total number of items in the map.
IsValidIndex( int i )
- Checks if a node is valid and in the map.
InvalidIndex( )
- Returns an invalid index.
EnsureCapacity( int num )
- Makes sure we have enough memory allocated to store a requested number of elements.
MaxElement()
- Max "size" of the vector
Adding elements
Insert( const KeyType_t &key, const ElemType_t &insert )
- Inserts the element and key into the tail of the map.
InsertOrReplace( ( const KeyType_t &key, const ElemType_t &insert )
- Inserts the element and key into the tail of the map, replacing the element if the key is the same as one that already exists
Reinsert( const KeyType_t &key, IndexType_t i )
- If you change the search key, this can be used to reinsert the element into the map.

To specify the LessFunc for your map, use SetDefLessFunc(exampleCUtlMap)
.
Accessing Elements
You can use array-style or use a method for index access:
Element( IndexType_t i )
operator[]( IndexType_t i )
- Returns the element at index i

CUtlMap
's accessor functions do not find a specific element by key, but rather, by index. Key( IndexType_t i )
- Returns the key at index i
Find( const KeyType_t &key )
- Returns the index at the given key
Removing elements
You can remove elements at a given index, a given key, or all at once.
Single:
RemoveAt( IndexType_t i )
- Removes the element at index i
Remove( const KeyType %key )
- Removes the element mapped to the given key
All:
RemoveAll( )
Purge( )
PurgeAndDeleteElements( )
- Purges the list and calls delete on each element in it.
Iteration
FirstInorder()
NextInorder( IndexType_t i )
PrevInorder( IndexType_t i )
LastInorder()
Other functions
You can get the tree which the CUtlMap consists of using
AccessTree()
- Returns CTree
You can swap one CUtlMap for another with
Swap( CUtlMap< K, T, I > &that )
- Self explanatory
Discrepancies
In std::map accessor functions will find an element by key., CUtlMap
's accessor functions do not find a specific element by key, but rather, by index.
This means that the following code
// Initialize map for given type
CUtlMap<int, CBaseEntity*> mapList;
// (Populate map here)
mapList.Insert(CBaseEntity->entindex(), CBaseEntity);
// Access map
mapList[ 3 ]->entindex();
mapList.Element( 3 )->entindex();
will return a pointer to CBaseEntity at index position 3
of the map, instead of the CBaseEntity that has key of 3.
In order to access the element at a specific key, use
//Find the index of our key value
int idx = mapList.Find(pBlock->entindex());
//Ensure that it is a valid index
if (mapList.IsValidIndex(idx))
{
CBaseEntity *pOther = mapList.Element(idx);
}