#include <evo/substring.h>
References a list of sorted substrings for fast lookup.
- Lookups are done with binary search
- Caution: String list must be sorted for
find
methods to work correctly
- See also: Enum Conversion
- Example
int main() {
"bar",
"foo",
"stuff"
};
int i1 = LISTMAP.find("foo");
int i2 = LISTMAP.find("baz");
return 0;
}
|
| SubStringMapList () |
| Constructor for null and empty SubString list. More...
|
|
| SubStringMapList (const SubString *data, SizeT size, bool verify_order=false) |
| Constructor for referencing an existing SubString list. More...
|
|
const SubString * | data () const |
| Get pointer to map string values. More...
|
|
bool | empty () const |
| Get whether empty. More...
|
|
SizeT | find (const SubString &key) const |
| Find key string in list. More...
|
|
template<class T > |
T | find_enum (const SubString &key, T first_enum, T last_enum, T unknown) const |
| Find key string in list and convert to enum value. More...
|
|
template<class T > |
T | find_enum_class (const SubString &key) const |
| Find key string in list and convert to enum class value (C++11). More...
|
|
template<class T > |
T | find_enum_remap (const T *remap_array, const SubString &key, T first_enum, T last_enum, T unknown) const |
| Find key string in list and convert to enum value, with unsorted enum remapped to sorted values. More...
|
|
template<class T > |
SubString | get_enum_class_string (T enum_value) const |
| Convert enum class value to key string from list (C++11). More...
|
|
template<class T > |
SubString | get_enum_string (T enum_value, T first_enum, T last_enum) const |
| Convert enum value to key string from list. More...
|
|
template<class T > |
SubString | get_enum_string_remap (const SizeT *reverse_remap_array, T enum_value, T first_enum, T last_enum) const |
| Convert enum value to key string from list, with unsorted enum remapped to sorted values. More...
|
|
bool | null () const |
| Get whether null. More...
|
|
SizeT | size () const |
| Get number of items in map. More...
|
|
bool | verify () const |
| Verify strings are in correct order. More...
|
|
◆ SubStringMapList() [1/2]
Constructor for null and empty SubString list.
◆ SubStringMapList() [2/2]
Constructor for referencing an existing SubString list.
- This references list data and does not allocate any memory
- If
verify_order
is true and verify() fails, this throws ExceptionMapVerifyOrder
- Parameters
-
data | List data, NULL to set as null |
size | Size as number of strings in list, 0 to set as empty |
verify_order | Whether to verify string order with verify() and call abort() if this fails |
◆ data()
Get pointer to map string values.
- Returns
- Pointer to string values
◆ empty()
Get whether empty.
- Returns
- Whether empty
◆ find()
Find key string in list.
- This uses a binary search so list must be sorted, otherwise results are accurate
- If there are duplicate strings in list, which one is found first is undefined
- Caution: String values must have been sorted
- Parameters
-
key | Key string to look for |
- Returns
- Found key index (0 for first), NONE if not found
◆ find_enum()
T find_enum |
( |
const SubString & |
key, |
|
|
T |
first_enum, |
|
|
T |
last_enum, |
|
|
T |
unknown |
|
) |
| const |
|
inline |
Find key string in list and convert to enum value.
- This uses a binary search so list must be sorted, otherwise results are inaccurate
- This calls assert() to check the result and that the number of enum values matches the string list size
- Caution: String values must have been sorted
- See also: Enum Conversion
- Template Parameters
-
T | Enum type to convert to, inferred from arguments |
- Parameters
-
key | Key string to look for |
first_enum | First enum value to map to, maps to first string |
last_enum | Last enum value to map to, maps to last string – must be >= first_enum |
unknown | Unknown enum value to use if key not found or result out of range |
- Returns
- Found enum value,
unknown
if not found or out of range
- Example
enum Color {
cUNKNOWN = 0,
cBLUE,
cGREEN,
cRED
};
int main() {
"blue",
"green",
"red"
};
Color color = COLOR_MAP.find_enum("green", cBLUE, cRED, cUNKNOWN);
return 0;
}
◆ find_enum_class()
T find_enum_class |
( |
const SubString & |
key | ) |
const |
|
inline |
Find key string in list and convert to enum class value (C++11).
- This uses a binary search so list must be sorted, otherwise results are inaccurate
- This calls assert() to check the number of enum values matches the string list size
- This assumes the enum class has guard values: starts with
T::UNKNOWN
, ends with T::ENUM_END
, with at least 1 mapped value in between
- See also: Enum Conversion
- Template Parameters
-
- Parameters
-
key | Key string to look for |
- Returns
- Found enum value for key,
T::UNKNOWN
if not found
- Example
enum class Color {
UNKNOWN = 0,
BLUE,
GREEN,
RED,
ENUM_END
};
int main() {
};
Color color = COLOR_MAP.find_enum_class<Color>("green");
return 0;
}
◆ find_enum_remap()
T find_enum_remap |
( |
const T * |
remap_array, |
|
|
const SubString & |
key, |
|
|
T |
first_enum, |
|
|
T |
last_enum, |
|
|
T |
unknown |
|
) |
| const |
|
inline |
Find key string in list and convert to enum value, with unsorted enum remapped to sorted values.
- This is a variant of find_enum() for an unsorted enum
- This uses
remap_array
to map an unsorted enum to sorted values – the data should be constant and static
- See also: Enum Conversion
- Template Parameters
-
T | Enum type to convert to, inferred from arguments |
- Parameters
-
remap_array | Pointer to array of enum values sorted so they match the mapped sorted strings |
key | Key string to look for |
first_enum | First enum value to map to – must be first unsorted value |
last_enum | Last enum value to map to – must be last unsorted value, and must be >= first_enum |
unknown | Unknown enum value to use if key not found or result out of range |
- Returns
- Found enum value,
unknown
if not found or out of range
- Example
enum Color {
cUNKNOWN = 0,
cRED,
cGREEN,
cBLUE
};
int main() {
static const Color COLORS_REMAP[] = {
cBLUE,
cGREEN,
cRED
};
"blue",
"green",
"red"
};
Color color = COLOR_MAP.find_enum_remap(COLORS_REMAP, "green", cRED, cBLUE, cUNKNOWN);
return 0;
}
◆ get_enum_class_string()
SubString get_enum_class_string |
( |
T |
enum_value | ) |
const |
|
inline |
Convert enum class value to key string from list (C++11).
- This is the reverse of find_enum_class()
- This assumes the enum class has guard values: starts with
T::UNKNOWN
, ends with T::ENUM_END
, with at least 1 mapped value in between
- See also: Enum Conversion
- Template Parameters
-
T | Enum class type to use, inferred from argument |
- Parameters
-
enum_value | Enum value to convert from |
- Returns
- String for enum value,
T::UNKNOWN
if unknown
◆ get_enum_string()
SubString get_enum_string |
( |
T |
enum_value, |
|
|
T |
first_enum, |
|
|
T |
last_enum |
|
) |
| const |
|
inline |
Convert enum value to key string from list.
- Template Parameters
-
T | Enum type to convert from, inferred from arguments |
- Parameters
-
enum_value | Enum value to convert from |
first_enum | First enum value to map to, maps to first string |
last_enum | Last enum value to map to, maps to last string – must be >= first_enum |
- Returns
- String for enum value, null if unknown
◆ get_enum_string_remap()
SubString get_enum_string_remap |
( |
const SizeT * |
reverse_remap_array, |
|
|
T |
enum_value, |
|
|
T |
first_enum, |
|
|
T |
last_enum |
|
) |
| const |
|
inline |
Convert enum value to key string from list, with unsorted enum remapped to sorted values.
- Template Parameters
-
T | Enum type to convert from, inferred from arguments |
- Parameters
-
reverse_remap_array | Pointer to index array for mapping sorted strings to unsorted enum values – see ReverseRemap |
enum_value | Enum value to convert from |
first_enum | First enum value to map to, maps to first string |
last_enum | Last enum value to map to, maps to last string – must be >= first_enum |
- Returns
- String for enum value, null if unknown
◆ null()
Get whether null.
- Returns
- Whether null
◆ size()
Get number of items in map.
- Returns
- Number of items, 0 if empty
◆ verify()
Verify strings are in correct order.
- This loops through and compares all strings to verify ordering
- Duplicates are not considered valid
- Call during initialization or from unit tests
- Returns
- Whether strings are correctly ordered
The documentation for this class was generated from the following file: