Dictionaries
This page contains a definition for the MeasuredDictionary functionality within blib. This entity uses measuredbuffers for all members, including keys and values.
Measured Dictionaries uses the halloc family to allocate on the heap. Keys are freed using hfree so values manually entered should be done using the halloc family.
Measured Dictionaries will automatically resize at capacity. This requires calling hrealloc three times. At a future version this will be optimised to a single halloc call by stacking the keys, values, and tombstones arrays.
Measured Dictionary
typedef struct _MeasuredDict {
DWORD count;
DWORD capacity;
WORD* tombstones;
DWORD dwTombstones;
MeasuredBuffer* keys;
MeasuredBuffer* values;
} MeasuredDictionary, MD, Dictionary, Dict;
Initialisers
MeasuredDictionary* MDnew(DWORD initialCapcity)
Allocates the keys, values, and tombstones immediately on the heap up to initialCapacity.
void MDfree(MD* dict);
Frees the dictionary and its allocations.
Methods
MDget and MDremove contain PBYTE* cstring variants MDgetS and MDremoveS for querying with an intermediate cstring that is freed after use.
BOOL MDadd(MD* dict,MeasuredBuffer* key, MeasuredBuffer* value)
Adds a Measured Buffer
'value' with a Measured Buffer
'key' to the dictionary dict. Returns TRUE
on success.
MeasuredBuffer* MDget(MD* dict,MeasuredBuffer* key);
Returns a pointer to the Measured Buffer
value that corrosponds to the Measured Buffer
'key'. Returns NULL
on failure and sets the last error to BLIB_ERROR_KEY_NOT_FOUND
.
BOOL MDset(MD* dict,MeasuredBuffer* key, MeasuredBuffer* value);
Sets the Measured Buffer
'value' to the Measured Buffer
'key' within dict. Returns TRUE
on success, FALSE
on fail. If this function fails, the error can be queried with bGetLastError. This should fail with BLIB_ERROR_KEY_NOT_FOUND
.
BOOL MDremove(MD* dict,MeasuredBuffer* key)
This function result in a double free at this point in time if the values are freed elsewhere in the code.
Unsets the key and frees the data fields of the key and value for the buffers pointed to by this element. Returns TRUE
on success and FALSE
on failure.
unsigned int MDcontainsKey(MD* dict, MeasuredBuffer* key);
Returns the index of the Measured Buffer
key within the dict. Returns -1
upon failure and sets the error code BLIB_ERROR_KEY_NOT_FOUND
.