Zen API

- Home
- Topics
- Types
- Files

All Classes Files Functions Variables Typedefs Friends Macros Modules Pages
Public Member Functions | Related
kMap Class Reference
kApi/Data

## Description

Represents a collection of key-value pairs stored in a hash table.

The kMap class represents a hash table of key-value pairs. The kMap constructor accepts kType arguments that determine the key type and value type. The map will automatically grow as new items are inserted.

The following example illustrates how to construct a kMap instance, populate it, and access elements.

kStatus MapExample()
{
kMap map = kNULL;
kText32 keys[] = { "one", "two", "three", "four", "five" };
k32s values[] = { 1, 2, 3, 4, 5 };
kSize i;
kMapItem it = kNULL; //map iterator
kTry
{
//create a map that can store small char-array keys and 32-bit integer values
kTest(kMap_Construct(&map, kTypeOf(kText32), kTypeOf(k32s), 0, kNULL));
//add some initial items to the map
for (i = 0; i < kCountOf(keys); ++i)
{
kTest(kMap_Add(map, &keys[i], &values[i]));
}
//print some information about the map and its items
printf("Key type: %s\n", kType_Name(kMap_KeyType(map)));
printf("Value type: %s\n", kType_Name(kMap_ValueType(map)));
printf("Count: %u\n", (k32u) kMap_Count(map));
it = kMap_First(map);
while (!kIsNull(it))
{
const kChar* key = kMap_Key(map, it);
k32s value = *(k32s*) kMap_Value(map, it);
it = kMap_Next(map, it);
}
}
kFinally
{
kObject_Destroy(map);
kEndFinally();
}
return kOK;
}

For maps that contain key or value objects (e.g. kString) as opposed to values (e.g. kText32), the objects are not automatically destroyed when the map is destroyed. To recursively destroy both the map and its keys/values, use kObject_Dispose. Similarly, kMap does not automatically dispose key/value objects when they are replaced or cleared from the map.

kStatus MapOfObjectsExample()
{
kMap map = kNULL;
kMapItem it = kNULL; //map iterator
kString keyIn = kNULL;
kImage valueIn = kNULL;
kTry
{
//create a map that can store string keys and image values
kTest(kMap_Construct(&map, kTypeOf(kString), kTypeOf(kImage), 0, kNULL));
//add some initial items to the map
kTest(kString_Construct(&keyIn, "one", kNULL));
kTest(kImage_Construct(&valueIn, kTypeOf(k8u), 100, 100, kNULL));
kTest(kMap_Add(map, &keyIn, &valueIn));
keyIn = valueIn = kNULL;
kTest(kString_Construct(&keyIn, "two", kNULL));
kTest(kImage_Construct(&valueIn, kTypeOf(k8u), 200, 200, kNULL));
kTest(kMap_Add(map, &keyIn, &valueIn));
keyIn = valueIn = kNULL;
kTest(kString_Construct(&keyIn, "three", kNULL));
kTest(kImage_Construct(&valueIn, kTypeOf(k8u), 300, 300, kNULL));
kTest(kMap_Add(map, &keyIn, &valueIn));
keyIn = valueIn = kNULL;
//print some information about the map and its items
printf("Key type: %s\n", kType_Name(kMap_KeyType(map)));
printf("Value type: %s\n", kType_Name(kMap_ValueType(map)));
printf("Count: %u\n", (k32u) kMap_Count(map));
it = kMap_First(map);
while (!kIsNull(it))
{
kString key = kMap_KeyAs_(map, it, kString);
kImage value = kMap_ValueAs_(map, it, kImage);
it = kMap_Next(map, it);
}
}
kFinally
{
//clean up temp variables, in case of error above
kObject_Destroy(keyIn);
kObject_Destroy(valueIn);
//dispose map; will destroy internal key/value objects
kObject_Dispose(map);
kEndFinally();
}
return kOK;
}

kMap supports the kObject_Clone, kObject_Dispose, and kObject_Size methods.

kMap supports the kdat6 serialization protocol.

Inheritance diagram for kMap:

## Public Member Functions
kStatus | kMap_Add (kMap map, const void *key, const void *value)
| Adds a new key-value pair. More...
kStatus | kMap_Allocate (kMap map, kType keyType, kType valueType, kSize initialCapacity)
| Reallocates the map. More...
kStatus | kMap_Assign (kMap map, kMap source)
| Performs a shallow copy of the source map. More...
kSize | kMap_Capacity (kMap map)
| Returns the number of elements for which space has been allocated. More...
kStatus | kMap_Clear (kMap map)
| Sets the count of map items to zero. More...
kStatus | kMap_Construct (kMap *map, kType keyType, kType valueType, kSize initialCapacity, kAlloc allocator)
| Constructs a kMap object. More...
kSize | kMap_Count (kMap map)
| Returns the count of map elements. More...
kStatus | kMap_Find (kMap map, const void *key, void *value)
| Finds the value associated with the given key. More...
kStatus | kMap_FindItem (kMap map, const void *key, kMapItem *item)
| Finds the map item associated with the given key. More...
kMapItem | kMap_First (kMap map)
| Gets a reference to the first map item (key-value pair). More...
const void * | kMap_Key (kMap map, kMapItem item)
| Returns a pointer to the key associated with a map item. More...
kType | kMap_KeyType (kMap map)
| Returns the key type. More...
kMapItem | kMap_Next (kMap map, kMapItem item)
| Given a map item, gets a reference to the next map item. More...
kStatus | kMap_Purge (kMap map)
| Disposes any elements in the map and sets the count of map items to zero. More...
kStatus | kMap_Remove (kMap map, const void *key, void *oldKey, void *oldValue)
| Removes a key-value pair from the map. More...
kStatus | kMap_RemoveItem (kMap map, kMapItem item)
| Removes an item from the map. More...
kStatus | kMap_Replace (kMap map, const void *key, const void *value)
| Adds or replaces a key-value pair. More...
kStatus | kMap_Reserve (kMap map, kSize capacity)
| Ensures that capacity is reserved for at least the specified number of map items. More...
kStatus | kMap_SetEqualsFx (kMap map, kEqualsFx function)
| Sets a custom key equality comparator. More...
kSize | kMap_SetHashFx (kMap map, kHashFx function)
| Sets a custom hash code generator. More...
kStatus | kMap_SetValue (kMap map, kMapItem item, const void *value)
| Sets the value associated with a map item. More...
void * | kMap_Value (kMap map, kMapItem item)
| Returns a pointer to the value associated with a map item. More...
kType | kMap_ValueType (kMap map)
| Returns the value type. More...
Public Member Functions inherited from kObject
kAlloc | kObject_Alloc (kObject object)
| Gets the memory allocator associated with this object. More...
kStatus | kObject_Clone (kObject *object, kObject source, kAlloc allocator)
| Constructs a new object by copying an existing object, including any aggregated child elements. More...
kStatus | kObject_Destroy (kObject object)
| Destroys the object. More...
kStatus | kObject_Dispose (kObject object)
| Destroys the object and any aggregated child elements. More...
kBool | kObject_Equals (kObject object, kObject other)
| Determines whether the object is equal to another object. More...
kSize | kObject_HashCode (kObject object)
| Gets a hash code representing the state of this object. More...
kBool | kObject_Is (kObject object, kType type)
| Determines whether this object is an instance of the specified type. More...
kBool | kObject_IsShared (kObject object)
| Reports whether the object is currently shared (reference count greater than one). More...
kStatus | kObject_SetPool (kObject object, kObjectPool pool)
| Sets the object pool associated with this object. More...
kStatus | kObject_Share (kObject object)
| Increments the reference count associated with this object. More...
kSize | kObject_Size (kObject object)
| Estimates the memory consumed by this object, including any aggregated child elements. More...
kType | kObject_Type (kObject object)
| Returns the type of the object. More...

## Related
kPointer | kMapItem
| Represents a key-value pair within a map.
#define | kMap_KeyType_(MAP) kxMap_KeyType_(MAP)
| Macro version of kMap_KeyType.
#define | kMap_ValueType_(MAP) kxMap_ValueType_(MAP)
| Macro version of kMap_ValueType.
#define | kMap_Count_(MAP) kxMap_Count_(MAP)
| Macro version of kMap_Count.
#define | kMap_KeyAs_(MAP, ITEM, TYPE) kxMap_KeyAs_(MAP, ITEM, TYPE)
| Accesses a map item key and casts the key to the specified type. More...
#define | kMap_ValueAs_(MAP, ITEM, TYPE) kxMap_ValueAs_(MAP, ITEM, TYPE)
| Accesses a map item value and casts the value to the specified type. More...
Related inherited from kObject
#define | kObject_Type_(OBJ) kxObject_Type_(OBJ)
| Macro version of kObject_Type.
#define | kObject_Is_(OBJ, TYPE) kxObject_Is_(OBJ, TYPE)
| Macro version of kObject_Is.
#define | kObject_Alloc_(OBJ) kxObject_Allocator_(OBJ)
| Macro version of kObject_Alloc.

The documentation for this class was generated from the following file:

- Data/kMap.h