Class HashArray

Index Home MAE > MAE Architecture > MAE Data Types > Class Index > Class HashArray

Summary
Using HashArray
Caveats
Public Methods
inline unsigned int size() const
inline bool empty() const
inline bool isHashArray(text)
HashArray()
HashArray(array)
HashArray(text, separator)
HashArray(text, separator)
const string& operator[](key) const
const string& operator[](key) const
void set(key, value)
void set(key, value)
inline void set(key, value)
void set(key, value)
inline void set(key, value)
void setBool(key, value)
void set(key, value)
void set(key, value)
void set(key, value)
void set(key, value)
void setBool(key, value)
void set(key, value)
void unset(key)
void unset(key)
void push(value)
string pop()
void clear()
const string& get(key) const
long getInt(key) const
float getFloat(key) const
double getDouble(key) const
bool getBool(key) const
HashArray getHashArray(key) const
inline HashArray HA(key) const
const string& get(key) const
long getInt(key) const
bool getBool(key) const
const string& getByIndex(index) const
bool operator==(other) const
inline bool sameas(other) const
bool contains(other) const
int find_key(key) const
inline bool keyExists(key) const
int find_value(value) const
inline bool has_value(value) const
int find_value(value) const
inline bool has_value(value) const
const string& find_value_key(value) const
const string& find_value_key(value) const
void change_key_case_lower()
void change_key_case_upper()
const string& key(index) const
const string& value(index) const
inline const vector& getKeys() const
inline const vector& getValues() const
string toString(separator) const
void fromString(text, separator)
void fromString(text, separator)
void sortKeys(numeric)
void sortValues(numeric)
HashArray& add(array2)
static HashArray merge(array1, array2)
inline HashArray operator+(array2) const
inline HashArray& operator+=(array2)
Private Properties
Private Methods
void append(key, value)
void append(key, value)

#include <HashArray.h >

Summary

This class manages key/value pairs, implementing an associated array.

The data structure can be serialized using toString() and loaded from text using fromString().

Using HashArray

The HashArray data type is a key-value container.

It is typeless, but a specific type can be asserted (e.g. setBool()) or retrieved (e.g. getBool(), getInt(), etc).  It is up the the application to store/retrieve by type consistently.

This data type is very convenient for messaging.  Calling the toString() method converts the HashArray into a string.  Using the fromString() method or constructor converts it back.

Caveats

  1. Numerical keys are not handled rigorously; unless numerical keys are set sequentially, you may get inconsistencies.
  2. When used inside XMLData, keys must be identifers with alphanumeric characters; if non-alphanumerics are given, they will be converted to underscores when converted using toString().  XMLData requires keys to be valid XML tag identifiers.

Public Methods

inline unsigned int size() const

Return the number of key-value pairs.

Return value: # keys in hash array

inline bool empty() const

Determine if there are no key-value pairs in the hash array.

Return value: True if there are no key-value pairs, false if there's at least one

inline bool isHashArray(text)

const string& text

some text to chck if in valid format


Determine if a string is a serialized HashArray structure or not.

Return value: True if it's a serialized HashArray

HashArray()

Constructor for an empty HashArray

HashArray(array)

const HashArray& array

the HashArray to copy


Copy constructor for a HashArray

HashArray(text, separator)

const string& text

serialized string created by HashArray::toString()

char separator

the key-value separator string

Default value: '\n'


Constructor for a HashArray from a string (serialized HashArray value).

HashArray(text, separator)

const string& text

serialized string created by HashArray::toString()

const string& separator

the key-value separator string


Constructor for a HashArray from a string (serialized HashArray value).

const string& operator[](key) const

const string& key

the key to use to find the value


Get a hash array value from its key

Return value: the value for the key

const string& operator[](key) const

int key

the key to use to find the value (it is converted to a string)


Get a hash array value from its key

Return value: the value for the key

void set(key, value)

const string& key

the key to use for the value

const string& value

the value to assign to the key


Set a hash array value for a key, overwriting any previous value

void set(key, value)

const string& key

the key to use for the value

long value

the value to assign to the key (it is converted to a string)


Set a string value for a key, overwriting any previous value

inline void set(key, value)

const string& key

the key to use for the value

int value

the value to assign to the key (it is converted to a string)


Set a long value for a key, overwriting any previous value

void set(key, value)

const string& key

the key to use for the value

unsigned long value

the value to assign to the key (it is converted to a string)


Set a unsingned long value for a key, overwriting any previous value

inline void set(key, value)

const string& key

the key to use for the value

unsigned int value

the value to assign to the key (it is converted to a string)


Set a unsigned int value for a key, overwriting any previous value

void setBool(key, value)

const string& key

the key to use for the value

bool value

the value to assign to the key (it is converted to a T or F)


Set a boolean value for a key, overwriting any previous value

void set(key, value)

const string& key

the key to use for the value

float value

the value to assign to the key (it is converted to a string)


Set a float value for a key, overwriting any previous value

void set(key, value)

const string& key

the key to use for the value

double value

the value to assign to the key (it is converted to a string)


Set a double value for a key, overwriting any previous value

void set(key, value)

int key

the key to use for the value (it is converted to a string)

const string& value

the value to assign to the key


Set a string value for an intgeger key, overwriting any previous value

void set(key, value)

int key

the key to use for the value (it is converted to a string)

long value

the value to assign to the key (it is converted to a string)


Set a long value for an intgeger key, overwriting any previous value

void setBool(key, value)

int key

the key to use for the value (it is converted to a string)

bool value

the value to assign to the key (it is converted to a string)


Set a boolean value for an integer key, overwriting any previous value

void set(key, value)

int key

the key to use for the value (it is converted to a string)

float value

the value to assign to the key (it is converted to a string)


Set a float value for an integer key, overwriting any previous value

void unset(key)

const string& key

the key to use for the value


Clear/Unset a hash array value for a key

void unset(key)

int key

the key to use for the value (it is converted to a string)


Clear/Unset a hash array value for a key

void push(value)

const string& value

the value to append


Add a new string value into the HashArray, using a key as its index (the new 5th pair will have a key value of 5).

string pop()

Regardless of its key, return the last value in the HashArray

Return value: value of last key-value pair in this HashArray

void clear()

Remove all key-value pairs, creating an empty HashArray.

const string& get(key) const

const string& key

the key for the value lookup


Given a string key, return the matching value as string.

If the key does not exit, the default is null string.

Return value: the value (as string), null string if key not found

long getInt(key) const

const string& key

the key for the value lookup


Given a string key, return the matching value as integer.

If the key does not exit, the default is 0.

Return value: the value (as long), 0 if key not found

float getFloat(key) const

const string& key

the key for the value lookup


Given a string key, return the matching value as float.

If the key does not exit, the default is 0.

Return value: the value (as float)

double getDouble(key) const

const string& key

the key for the value lookup


Given a string key, return the matching value as double.

If the key does not exit, the default is 0.

Return value: the value (as double)

bool getBool(key) const

const string& key

the key for the value lookup


Given a string key, return the matching value as boolean.

Note that T, Y, and 1 as the first character make the value true.

If the key does not exit, the default is False.

Return value: the value (as bool)

HashArray getHashArray(key) const

const string& key

the key for the value lookup


Given a string key, return the matching value as a HashArray.

Presumably the value is a serialized HashArray.

If the key does not exit, the default is en empty HashArray.

Return value: the value (as HashArray)

inline HashArray HA(key) const

const string& key

the key for the value lookup


Given a string key, return the matching value as a HashArray.

Presumably the value is a serialized HashArray.

If the key does not exit, the default is en empty HashArray.

Return value: the value (as HashArray)

const string& get(key) const

int key

the key for the value lookup (it is converted to a string)


Given an integer key, return the matching value as string.

If the key does not exit, the default is null string.

Return value: the value (as string)

long getInt(key) const

int key

the key for the value lookup (it is converted to a string)


Given an integer key, return the matching value as integer.

If the key does not exit, the default is 0.

Return value: the value (as long)

bool getBool(key) const

int key

the key for the value lookup (it is converted to a string)


Given an integer key, return the matching value as boolean.

Note that T, Y, and 1 as the first character make the value true.

If the key does not exit, the default is false.

Return value: the value (as bool)

const string& getByIndex(index) const

int index

the index/offset into the HashArray


Given an index/offset into the HashArray, return the value there.

If the index it out of range, the default is null string.

Return value: the value (as string)

bool operator==(other) const

const HashArray& other

the other HashArray for comparison


Compare to HashArrays for equality. Both must have the same number of keys with appropriately matching values.

Note that the HashArrays do not need to have their keys at the same index.

@retuen True if the two arrays are identifical

inline bool sameas(other) const

const HashArray& other

the other HashArray for comparison


Compare to HashArrays for equality. Both must have the same number of keys with appropriately matching values.

Note that the HashArrays do not need to have their keys at the same index.

@retuen True if the two arrays are identifical

bool contains(other) const

const HashArray& other

the HashArray subset candidate


Check if a provided HashArray is a subset of this HashArray.

Return value: True if other is a subset of this HashArray

int find_key(key) const

const string& key

the key to find


Return the index of a key in the HashArray

Return value: the index (0+) or -1 if key not found

inline bool keyExists(key) const

const string& key

the key to find


Determine if the provided key is inside the HashArray.

Return value: True if the key was found

int find_value(value) const

const string& value

the value to find


Return the index of the first key-value pair found in the HashArray with the value provided.

Return value: the index (0+) or -1 if value not found

inline bool has_value(value) const

const string& value

the value to find


Determine if the value provided is contained in one of the key-value pairs.

Return value: True if value found

int find_value(value) const

long value

the value to find (converted to string)


Return the index of the first key-value pair found in the HashArray with the value provided.

Return value: the index (0+) or -1 if value not found

inline bool has_value(value) const

long value

the value to find (converted to string)


Determine if the value provided is contained in one of the key-value pairs.

Return value: True if value found

const string& find_value_key(value) const

const string& value

the value to find


Given a value, return the first matching key

Return value: key as string for that value, or null string if not found

const string& find_value_key(value) const

long value

the value to find (converted to string)


Given a value, return the first matching key

Return value: key as string for that value, or null string if not found

void change_key_case_lower()

Convert all the keys to lower case.

void change_key_case_upper()

Convert all the keys to upper case.

const string& key(index) const

int index

the key to find


Return the key for the provided index.

Note that this index is not guaranteed to be correct after a call to set().

Return value: the offset into the HashArray where the key was found

const string& value(index) const

int index

the key to find


Return the value for the provided index.

Note that this index is not guaranteed to be correct after a call to set().

Return value: the offset into the HashArray where the key was found

inline const vector <string >& getKeys() const

Get the list of keys in the HashArray

Return value: a vector of strings of the keys

inline const vector <string >& getValues() const

Get the list of values in the HashArray.

If more than one key has a same value, then the value is repeated.

Return value: a vector of strings of the keys

string toString(separator) const

const string& separator

Specify a key-pair separator other than newline.

Default value: "\n"


Serialize the HashArray into a string.

Return value: a string representing the HashArray

void fromString(text, separator)

string text

input string which is a serialized HashArray

char separator

specify an alternate key-pair separator than newline

Default value: '\n'


Deserialize a HashArray - take a string and convert it to a HashArray.

Any previous key-pair values in the HashArray are cleared.

void fromString(text, separator)

string text

input string which is a serialized HashArray

string separator

specify the key-pair separator


Deserialize a HashArray - take a string and convert it to a HashArray.

Any previous key-pair values in the HashArray are cleared.

void sortKeys(numeric)

bool numeric

if true, a numeric sort is used instead of alphanumeric

Default value: false


Sort the key-value pairs by the keys.  This effects the getKeys() output.

void sortValues(numeric)

bool numeric

if true, a numeric sort is used instead of alphanumeric

Default value: false


Sort the key-value pairs by the values.  This effects the getValues() output.

HashArray& add(array2)

const HashArray& array2

the other HashArray to combine into this HashArray


Add the provided HashArray to the existing HashArray.  For any duplicate keys, the value from array2 will override.

Return value: this HashArray

static HashArray merge(array1, array2)

const HashArray& array1

the first array

const HashArray& array2

the second array


Combine two HashArray structures. For any duplicate keys, the value from array2 will override.

Return value: a new HashArray

inline HashArray operator+(array2) const

const HashArray& array2

the second array


Combine two HashArray structures. For any duplicate keys, the value from array2 will override.

Return value: a new HashArray

inline HashArray& operator+=(array2)

const HashArray& array2

the other HashArray to combine into this HashArray


Add the provided HashArray to the existing HashArray.  For any duplicate keys, the value from array2 will override.

Return value: this HashArray

Private Properties

unsigned int ha_size

vector <string > keys

vector <string > values


Private Methods

void append(key, value)

string key

const string& value


void append(key, value)

int key

const string& value