CUtlVector: Difference between revisions
| TomEdwards (talk | contribs) mNo edit summary |  (iterator macros) | ||
| (4 intermediate revisions by 3 users not shown) | |||
| Line 5: | Line 5: | ||
| For detailed discussion of the pros and cons of vectors, [[W:Vector (C++)|see Wikipedia]]. | For detailed discussion of the pros and cons of vectors, [[W:Vector (C++)|see Wikipedia]]. | ||
| {{warning|Vector elements are moved around in memory, so never  | {{warning|Vector elements are moved around in memory, so never store pointers to them. Always use <code>Element(int)</code> or the <code>[]</code> notation.}} | ||
| == Accessing elements == | == Accessing elements == | ||
| Line 60: | Line 60: | ||
| ; <code>AddVectorToTail( CUtlVector const &src )</code> | ; <code>AddVectorToTail( CUtlVector const &src )</code> | ||
| : Adds another CUtlVector to the tail. | : Adds another CUtlVector to the tail. | ||
| == Iterating over elements == | |||
| To loop through each element, you can write a for loop the same way you would for an <code>std::vector</code>, or you can use the <code>FOR_EACH_VEC</code> macro: | |||
| <source lang=cpp> | |||
| // start at first element | |||
| FOR_EACH_VEC(vectorList, i) | |||
| { | |||
| 	CBaseEntity* ent = vectorList[i]; | |||
| 	... | |||
| } | |||
| // start at last element and work backwards | |||
| FOR_EACH_VEC_BACK(vectorList, i) | |||
| { | |||
| 	CBaseEntity* ent = vectorList[i]; | |||
| 	... | |||
| } | |||
| </source> | |||
| == Searching elements == | == Searching elements == | ||
| Line 67: | Line 85: | ||
| To search for an element, your type must have implemented the <code>==</code> operator. | To search for an element, your type must have implemented the <code>==</code> operator. | ||
| ; <code>Find()</code> | ; <code>Find( const T& src )</code> | ||
| : Searches the vector  | : Searches the vector for the given element, returning the index if found, and -1 if not. | ||
| ; <code>HasElement(T&)</code> | ; <code>HasElement( const T& src )</code> | ||
| : Check if the vector contains the given element. | : Check if the vector contains the given element. | ||
| ; <code>FindAndRemove()</code> | ; <code>FindAndRemove()</code> | ||
| Line 104: | Line 122: | ||
| : Calls <code>RemoveAll()</code> and purges the allocated memory. | : Calls <code>RemoveAll()</code> and purges the allocated memory. | ||
| ; <code>PurgeAndDeleteElements()</code> | ; <code>PurgeAndDeleteElements()</code> | ||
| : First <code | : First <code>delete</code>s all elements and then calls <code>Purge()</code>. | ||
| == Sorting == | == Sorting == | ||
| Line 133: | Line 151: | ||
| [[Category:Tier1]] | [[Category:Tier1]] | ||
| [[Category:Classes]] | [[Category:Classes|U]] | ||
Latest revision as of 23:22, 25 October 2025
CUtlVector is Source's equivalent of the C++ Vector type. It can be thought of as an array of dynamic length. Code can be found in public/tier1/utlvector.h.
For detailed discussion of the pros and cons of vectors, see Wikipedia.
 Warning:Vector elements are moved around in memory, so never store pointers to them. Always use
Warning:Vector elements are moved around in memory, so never store pointers to them. Always use Element(int) or the [] notation.Accessing elements
You can use array-style or use a method for index access:
// Initialize vector for given type
CUtlVector<CBaseEntity*> vectorList;
// (Populate vector here)
// Access vector 
vectorList[ 0 ]->entindex();
vectorList.Element( 0 )->entindex();
You can also access the head (first element) and tail (last element):
vectorList.Head();
vectorList.Tail();
Finding bounds
- Count()
- The total number of items in the vector. (There is also a Size()method, but it is deprecated.)
- IsValidIndex( int i )
- Self-explanatory.
- EnsureCapacity( int num )
- Makes sure we have enough memory allocated to store a requested number of elements.
- EnsureCount( int num )
- Makes sure we have at least this many elements.
Adding elements
There are several methods for adding individual elements, multiple elements as well as other CUtlVectors:
Singly:
- AddToHead( const T& src )
- AddToTail( const T& src )
- Adds the element to the start or end of the list.
- InsertBefore( int elem, const T& src )
- InsertAfter( int elem, const T& src )
- Puts the element before or after a certain index.
In batches:
- AddMultipleToTail( int num, const T *pToCopy )
- Adds several elements to the end of the list.
- InsertMultipleBefore( int elem, int num, const T *pToInsert )
- Adds several elements before a valid index position except the head (which is always 0).
- AddVectorToTail( CUtlVector const &src )
- Adds another CUtlVector to the tail.
Iterating over elements
To loop through each element, you can write a for loop the same way you would for an std::vector, or you can use the FOR_EACH_VEC macro:
// start at first element
FOR_EACH_VEC(vectorList, i)
{
	CBaseEntity* ent = vectorList[i];
	...
}
// start at last element and work backwards
FOR_EACH_VEC_BACK(vectorList, i)
{
	CBaseEntity* ent = vectorList[i];
	...
}
Searching elements
 Tip:If you want an automatically sorting list, have a look at CUtlSortVector.
Tip:If you want an automatically sorting list, have a look at CUtlSortVector. To search for an element, your type must have implemented the == operator.
- Find( const T& src )
- Searches the vector for the given element, returning the index if found, and -1 if not.
- HasElement( const T& src )
- Check if the vector contains the given element.
- FindAndRemove()
- Self-explanatory.
Removing elements
When you remove an element, its destructor is called and the vector is reordered.
Singly:
- FindAndRemove()
- Self-explanatory.
- Remove( int elem )
- Calls the destructor of the element at the given position and shifts all elements left.
- FastRemove( int elem )
- Similar to Remove(), but just does a memcpy to accomodate for the removed element.
In batches:
- RemoveMultiple( int elem, int num )
- Removes numelements starting at positionelem. Actual removal of elements is done in a right-to-left manner.
- RemoveAll()
- Clears the entire vector.
Copying
- CopyArray( const T *pArray, int size )
- Copies sizeelements of the given array into the vector, possibly overwriting elements. Always starts from index 0.
- Swap( CUtlVector< T, A > &vec )
- Simply swaps the vector with the given one.
Deallocating
- Purge()
- Calls RemoveAll()and purges the allocated memory.
- PurgeAndDeleteElements()
- First deletes all elements and then callsPurge().
Sorting
The class has a Sort method that takes a predicate, like so:
// Sort vector by predicate
vectorList.Sort( predicateFunction );
In this case, the predicate must be of the type int (__cdecl *)(const CBaseEntity *, const CBaseEntity *):
static int __cdecl predicateFunction( const CBaseEntity *a, const CBaseEntity *b )
{
	int valueA = a->entindex();
	int valueB = b->entindex();
	if( valueA < valueB )
		return -1;
	else if( valueA == valueB )
		return 0;
	else return 1;
 }
This function needs to be defined before the method it is called in.