Evo C++ Library v0.5.1
Classes | Public Member Functions | List of all members
SubStringMapList Class Reference

#include <evo/substring.h>

Detailed Description

References a list of sorted substrings for fast lookup.

Example
#include <evo/enum.h>
using namespace evo;
int main() {
// Pre-sorted string list (no mem allocs)
static const SubString LIST[] = {
"bar",
"foo",
"stuff"
};
// Efficient map from string list (no mem allocs, binary search)
static const SubStringMapList LISTMAP(LIST, fixed_array_size(LIST));
int i1 = LISTMAP.find("foo"); // set to 1
int i2 = LISTMAP.find("baz"); // set to NONE (not found)
return 0;
}

Classes

struct  ReverseRemap
 Builds a reversed enum value remap array for fast reverse lookups. More...
 

Public Member Functions

 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 SubStringdata () 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 >
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 >
find_enum_class (const SubString &key) const
 Find key string in list and convert to enum class value (C++11). More...
 
template<class 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...
 

Constructor & Destructor Documentation

◆ SubStringMapList() [1/2]

SubStringMapList ( )
inline

Constructor for null and empty SubString list.

◆ SubStringMapList() [2/2]

SubStringMapList ( const SubString data,
SizeT  size,
bool  verify_order = false 
)
inline

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
dataList data, NULL to set as null
sizeSize as number of strings in list, 0 to set as empty
verify_orderWhether to verify string order with verify() and call abort() if this fails

Member Function Documentation

◆ data()

const SubString* data ( ) const
inline

Get pointer to map string values.

Returns
Pointer to string values

◆ empty()

bool empty ( ) const
inline

Get whether empty.

Returns
Whether empty

◆ find()

SizeT find ( const SubString key) const
inline

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
keyKey string to look for
Returns
Found key index (0 for first), NONE if not found

◆ find_enum()

T find_enum ( const SubString key,
first_enum,
last_enum,
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
TEnum type to convert to, inferred from arguments
Parameters
keyKey string to look for
first_enumFirst enum value to map to, maps to first string
last_enumLast enum value to map to, maps to last string – must be >= first_enum
unknownUnknown 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
#include <evo/enum.h>
using namespace evo;
enum Color {
cUNKNOWN = 0,
cBLUE,
cGREEN,
cRED
};
int main() {
static const SubString COLORS[] = {
"blue",
"green",
"red"
};
static const SubStringMapList COLOR_MAP(COLORS, fixed_array_size(COLORS));
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
TEnum class type to use
Parameters
keyKey string to look for
Returns
Found enum value for key, T::UNKNOWN if not found
Example
#include <evo/enum.h>
using namespace evo;
enum class Color {
UNKNOWN = 0,
BLUE,
GREEN,
RED,
ENUM_END
};
int main() {
static const SubString COLORS[] = {
SubString("blue"),
SubString("green"),
SubString("red")
};
static const SubStringMapList COLOR_MAP(COLORS, fixed_array_size(COLORS));
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,
first_enum,
last_enum,
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
TEnum type to convert to, inferred from arguments
Parameters
remap_arrayPointer to array of enum values sorted so they match the mapped sorted strings
keyKey string to look for
first_enumFirst enum value to map to – must be first unsorted value
last_enumLast enum value to map to – must be last unsorted value, and must be >= first_enum
unknownUnknown 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
#include <evo/enum.h>
using namespace evo;
// Unsorted values
enum Color {
cUNKNOWN = 0,
cRED,
cGREEN,
cBLUE
};
int main() {
// Values in sorted order
static const Color COLORS_REMAP[] = {
cBLUE,
cGREEN,
cRED
};
static const SubString COLORS[] = {
"blue",
"green",
"red"
};
static const SubStringMapList COLOR_MAP(COLORS, fixed_array_size(COLORS));
Color color = COLOR_MAP.find_enum_remap(COLORS_REMAP, "green", cRED, cBLUE, cUNKNOWN);
return 0;
}

◆ get_enum_class_string()

SubString get_enum_class_string ( 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
TEnum class type to use, inferred from argument
Parameters
enum_valueEnum value to convert from
Returns
String for enum value, T::UNKNOWN if unknown

◆ get_enum_string()

SubString get_enum_string ( enum_value,
first_enum,
last_enum 
) const
inline

Convert enum value to key string from list.

Template Parameters
TEnum type to convert from, inferred from arguments
Parameters
enum_valueEnum value to convert from
first_enumFirst enum value to map to, maps to first string
last_enumLast 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,
enum_value,
first_enum,
last_enum 
) const
inline

Convert enum value to key string from list, with unsorted enum remapped to sorted values.

Template Parameters
TEnum type to convert from, inferred from arguments
Parameters
reverse_remap_arrayPointer to index array for mapping sorted strings to unsorted enum values – see ReverseRemap
enum_valueEnum value to convert from
first_enumFirst enum value to map to, maps to first string
last_enumLast enum value to map to, maps to last string – must be >= first_enum
Returns
String for enum value, null if unknown

◆ null()

bool null ( ) const
inline

Get whether null.

Returns
Whether null

◆ size()

SizeT size ( ) const
inline

Get number of items in map.

Returns
Number of items, 0 if empty

◆ verify()

bool verify ( ) const
inline

Verify strings are in correct order.

  • This loops through and compares all strings to verify ordering
    • This takes linear time
  • 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: