Loading...
Searching...
No Matches
C++ STL Utilities

Helper functions/classes for STL. More...

+ Collaboration diagram for C++ STL Utilities:

Files

file  stl.h
 

Classes

class  TfGet< N >
 Function object for retrieving the N'th element of a std::pair or std::tuple. More...
 
class  TfIterator< T, Reverse >
 A simple iterator adapter for STL containers. More...
 
struct  TfDeleter
 Function object for deleting any pointer. More...
 

Functions

template<class Container , class Key , class Result >
bool TfMapLookup (Container const &map, Key const &key, Result *valuePtr)
 Checks if an item exists in a map or a TfHashMap.
 
template<class Container , class Key , class Result >
const Result TfMapLookupByValue (Container const &map, Key const &key, const Result &defaultValue)
 Checks if an item exists in a map or a TfHashMap.
 
template<class Container , class Key >
Container::mapped_type * TfMapLookupPtr (Container &map, Key const &key)
 Checks if an item exists in a map or TfHashMap, without copying it.
 
template<typename T >
std::pair< T, T > TfOrderedPair (T a, T b)
 Return an std::pair in sorted order.
 

Detailed Description

Helper functions/classes for STL.

Function Documentation

◆ TfMapLookup()

bool TfMapLookup ( Container const &  map,
Key const &  key,
Result *  valuePtr 
)

Checks if an item exists in a map or a TfHashMap.

If key exists in map, then this function returns true and the value indexed by key is copied into *valuePtr. Otherwise, *valuePtr is not modified, and false is returned.

Example:

TfHashMap<string, int, TfHash> m = ...;
int value;
if (TfMapLookup(m, "someKey", &value))
printf("Value found: %d\n", value);
else
printf("Value not found\n");
...
bool TfMapLookup(Container const &map, Key const &key, Result *valuePtr)
Checks if an item exists in a map or a TfHashMap.
Definition: stl.h:86

Definition at line 86 of file stl.h.

◆ TfMapLookupByValue()

const Result TfMapLookupByValue ( Container const &  map,
Key const &  key,
const Result &  defaultValue 
)

Checks if an item exists in a map or a TfHashMap.

If key exists in map, then this function returns the value index by key. Otherwise, defaultValue is returned. Note that the result is returned by value, so this is best used for types that are quick to copy.

Example:

TfHashMap<string, int, TfHash> m;
m["foo"] = 1;
int value = TfMapLookupByValue(m, "someKey", -1);
TF_AXIOM(value == -1);
int value = TfMapLookupByValue(m, "foo", -1);
TF_AXIOM(value == 1);
#define TF_AXIOM(cond)
Aborts if the condition cond is not met.
Definition: diagnostic.h:210
const Result TfMapLookupByValue(Container const &map, Key const &key, const Result &defaultValue)
Checks if an item exists in a map or a TfHashMap.
Definition: stl.h:112

Definition at line 112 of file stl.h.

◆ TfMapLookupPtr()

Container::mapped_type * TfMapLookupPtr ( Container &  map,
Key const &  key 
)

Checks if an item exists in a map or TfHashMap, without copying it.

If key exists in the map, then this function returns a pointer to the value indexed by key. Otherwise, NULL is returned.

Example:

TfHashMap<string, BigData, TfHash> m = ...;
if (BigData* bigPtr = TfMapLookupPtr(m, "someKey"))
bigPtr->ModifyStuff();
else
printf("Value not found\n");
Container::mapped_type * TfMapLookupPtr(Container &map, Key const &key)
Checks if an item exists in a map or TfHashMap, without copying it.
Definition: stl.h:141

Definition at line 141 of file stl.h.

◆ TfOrderedPair()

std::pair< T, T > TfOrderedPair ( a,
b 
)
inline

Return an std::pair in sorted order.

This call is a useful helper for maps whose key is an unordered pair of elements. One can either define a new data type such that (a,b) is deemed equivalent to (b,a), or more simply, adopt the convention that a key is always written (a,b) with a < b.

Definition at line 165 of file stl.h.