Zen API
 All Classes Files Functions Variables Typedefs Friends Macros Modules Pages
kMap.h
Go to the documentation of this file.
1 
10 #ifndef K_API_MAP_H
11 #define K_API_MAP_H
12 
13 #include <kApi/kApiDef.h>
14 
184 //typedef kObject kMap; --forward-declared in kApiDef.x.h
185 
191 typedef kPointer kMapItem;
192 
193 #include <kApi/Data/kMap.x.h>
194 
206 kFx(kStatus) kMap_Construct(kMap* map, kType keyType, kType valueType, kSize initialCapacity, kAlloc allocator);
207 
220 kFx(kStatus) kMap_Allocate(kMap map, kType keyType, kType valueType, kSize initialCapacity);
221 
233 kFx(kStatus) kMap_Assign(kMap map, kMap source);
234 
244 {
245  kObj(kMap, map);
246 
247  obj->equalsFx = function;
248 
249  return kOK;
250 }
264 {
265  kObj(kMap, map);
266 
267  obj->hashFx = function;
268 
269  kCheck(xkMap_Rehash(map));
270 
271  return kOK;
272 }
273 
282 {
283  kObj(kMap, map);
284 
285  return obj->keyField.type;
286 }
287 
296 {
297  kObj(kMap, map);
298 
299  return obj->valueField.type;
300 }
301 
310 {
311  kObj(kMap, map);
312 
313  return obj->count;
314 }
315 
324 {
325  kObj(kMap, map);
326 
327  return obj->capacity;
328 }
329 
339 kFx(kStatus) kMap_Find(kMap map, const void* key, void* value);
340 
353 #define kMap_FindT(kMap_map, KPtr_key, VPtr_value) \
354  xkMap_FindT(kMap_map, KPtr_key, VPtr_value, sizeof(*(KPtr_key)), sizeof(*(VPtr_value)))
355 
364 kInlineFx(kBool) kMap_Has(kMap map, const void* key)
365 {
366  return kSuccess(kMap_Find(map, key, kNULL));
367 }
368 
380 #define kMap_HasT(kMap_map, KPtr_key) \
381  xkMap_HasT(kMap_map, KPtr_key, sizeof(*(KPtr_key)))
382 
392 kInlineFx(kStatus) kMap_Add(kMap map, const void* key, const void* value)
393 {
394  return kMap_AddItem(map, key, value, kNULL);
395 }
396 
409 #define kMap_AddT(kMap_map, KPtr_key, VPtr_value) \
410  xkMap_AddT(kMap_map, KPtr_key, VPtr_value, sizeof(*(KPtr_key)), sizeof(*(VPtr_value)))
411 
425 kFx(kStatus) kMap_Replace(kMap map, const void* key, const void* value);
426 
443 #define kMap_ReplaceT(kMap_map, KPtr_key, VPtr_value) \
444  xkMap_ReplaceT(kMap_map, KPtr_key, VPtr_value, sizeof(*(KPtr_key)), sizeof(*(VPtr_value)))
445 
460 kFx(kStatus) kMap_Remove(kMap map, const void* key, void* oldKey, void* oldValue);
461 
479 #define kMap_RemoveT(kMap_map, KPtr_key, KPtr_oldKey, VPtr_oldValue) \
480  xkMap_RemoveT(kMap_map, KPtr_key, KPtr_oldKey, VPtr_oldValue, sizeof(*(KPtr_key)), sizeof(*(KPtr_oldKey)), sizeof(*(VPtr_oldValue)))
481 
493 kInlineFx(kStatus) kMap_Discard(kMap map, const void* key)
494 {
495  return kMap_Remove(map, key, kNULL, kNULL);
496 }
497 
512 #define kMap_DiscardT(kMap_map, KPtr_key) \
513  xkMap_DiscardT(kMap_map, KPtr_key, sizeof(*(KPtr_key)))
514 
523 kFx(kStatus) kMap_Reserve(kMap map, kSize capacity);
524 
532 kFx(kStatus) kMap_Clear(kMap map);
533 
541 kFx(kStatus) kMap_Purge(kMap map);
542 
550 kFx(kMapItem) kMap_First(kMap map);
551 
560 kFx(kMapItem) kMap_Next(kMap map, kMapItem item);
561 
572 kFx(kStatus) kMap_AddItem(kMap map, const void* key, const void* value, kMapItem* item);
573 
587 #define kMap_AddItemT(kMap_map, KPtr_key, VPtr_value, kMapItemPtr_item) \
588  xkMap_AddItemT(kMap_map, KPtr_key, VPtr_value, kMapItemPtr_item, sizeof(*(KPtr_key)), sizeof(*(VPtr_value)))
589 
599 kFx(kStatus) kMap_FindItem(kMap map, const void* key, kMapItem* item);
600 
613 #define kMap_FindItemT(kMap_map, KPtr_key, kMapItemPtr_item) \
614  xkMap_FindItemT(kMap_map, KPtr_key, kMapItemPtr_item, sizeof(*(KPtr_key)))
615 
624 kFx(kStatus) kMap_RemoveItem(kMap map, kMapItem item);
625 
634 kInlineFx(const void*) kMap_Key(kMap map, kMapItem item)
635 {
636  kObj(kMap, map);
637 
638  return kPointer_ByteOffset(item, (kSSize)obj->keyField.offset);
639 }
640 
653 #define kMap_KeyT(kMap_map, kMapItem_item, K) \
654  kCast(K*, xkMap_KeyT(kMap_map, kMapItem_item, sizeof(K)))
655 
668 #define kMap_KeyAsT(kMap_map, kMapItem_item, K) \
669  kPointer_ReadAs(xkMap_KeyAsT(kMap_map, kMapItem_item, sizeof(K)), K)
670 
680 {
681  kObj(kMap, map);
682 
683  return kPointer_ByteOffset(item, (kSSize)obj->valueField.offset);
684 }
685 
698 #define kMap_ValueT(kMap_map, kMapItem_item, V) \
699  kCast(V*, xkMap_ValueT(kMap_map, kMapItem_item, sizeof(V)))
700 
710 kInlineFx(kStatus) kMap_SetValue(kMap map, kMapItem item, const void* value)
711 {
712  kObj(kMap, map);
713  void* valueSlot = kMap_Value(map, item);
714 
715  kValue_Import(obj->valueField.type, valueSlot, value);
716 
717  return kOK;
718 }
719 
732 #define kMap_SetValueT(kMap_map, kMapItem_item, VPtr_value) \
733  xkMap_SetValueT(kMap_map, kMapItem_item, VPtr_value, sizeof(*(VPtr_value)))
734 
748 #define kMap_SetValueAsT(kMap_map, kMapItem_item, V_value, V) \
749  (kPointer_WriteAs(xkMap_ValueAsT(kMap_map, kMapItem_item, sizeof(V)), V_value, V), (void)0)
750 
763 #define kMap_ValueAsT(kMap_map, kMapItem_item, V) \
764  kPointer_ReadAs(xkMap_ValueAsT(kMap_map, kMapItem_item, sizeof(V)), V)
765 
766 #endif
kStatus kMap_Purge(kMap map)
Disposes any elements in the map and sets the count of map items to zero.
kStatus kMap_Find(kMap map, const void *key, void *value)
Finds the value associated with the given key.
kStatus kMap_Clear(kMap map)
Sets the count of map items to zero.
kBool kSuccess(kStatus status)
Returns kTRUE if the given expression value is kOK.
Definition: kApiDef.h:576
kStatus kMap_SetEqualsFx(kMap map, kEqualsFx function)
Sets a custom key equality comparator.
Definition: kMap.h:243
kMapItem kMap_First(kMap map)
Gets a reference to the first map item (key-value pair).
Represents a void pointer.
kStatus kMap_Discard(kMap map, const void *key)
Removes a key-value pair from the map.
Definition: kMap.h:493
kSize kMap_Count(kMap map)
Returns the count of map elements.
Definition: kMap.h:309
kStatus kMap_Allocate(kMap map, kType keyType, kType valueType, kSize initialCapacity)
Reallocates the map.
kStatus kMap_SetHashFx(kMap map, kHashFx function)
Sets a custom hash code generator.
Definition: kMap.h:263
Represents an unsigned integer that can store a pointer address.
Abstract base class for memory allocator types.
void kValue_Import(kType type, void *value, const void *source)
Imports the content of another value into this value.
Definition: kValue.h:108
#define kCheck(EXPRESSION)
Executes a return statement if the given expression is not kOK.
Definition: kApiDef.h:589
kStatus kMap_Construct(kMap *map, kType keyType, kType valueType, kSize initialCapacity, kAlloc allocator)
Constructs a kMap object.
kStatus kMap_Reserve(kMap map, kSize capacity)
Ensures that capacity is reserved for at least the specified number of map items. ...
#define kInlineFx(TYPE)
Inline method declaration helper.
Definition: kApiDef.h:29
const void * kMap_Key(kMap map, kMapItem item)
Returns a pointer to the key associated with a map item.
Definition: kMap.h:634
kType kMap_ValueType(kMap map)
Returns the value type.
Definition: kMap.h:295
kSize(kCall * kHashFx)(const void *item)
Callback signature to determine hash code of an item.
Definition: kApiDef.h:1861
Represents a signed integer that can store a pointer address.
kSize kMap_Capacity(kMap map)
Returns the number of elements for which space has been allocated.
Definition: kMap.h:323
kStatus kMap_Add(kMap map, const void *key, const void *value)
Adds a new key-value pair.
Definition: kMap.h:392
kBool(kCall * kEqualsFx)(const void *item1, const void *item2)
Callback signature to determine equality of two items.
Definition: kApiDef.h:1853
#define kObj(TypeName_T, T_object)
Declares a local "obj" (this-pointer) variable and initializes it from a type-checked object handle...
Definition: kApiDef.h:3450
kPointer kMapItem
Represents a key-value pair within a map.
kBool kMap_Has(kMap map, const void *key)
Reports whether the specified key is present in the map.
Definition: kMap.h:364
kStatus kMap_FindItem(kMap map, const void *key, kMapItem *item)
Finds the map item associated with the given key.
kStatus kMap_Assign(kMap map, kMap source)
Performs a shallow copy of the source map.
Core Zen type declarations.
kType kMap_KeyType(kMap map)
Returns the key type.
Definition: kMap.h:281
void * kPointer_ByteOffset(const void *pointer, kSSize offset)
Calculates a pointer address from a base address and a byte offset.
Definition: kApiDef.h:322
kMapItem kMap_Next(kMap map, kMapItem item)
Given a map item, gets a reference to the next map item.
kStatus kMap_RemoveItem(kMap map, kMapItem item)
Removes an item from the map.
kStatus kMap_Replace(kMap map, const void *key, const void *value)
Adds or replaces a key-value pair.
kStatus kMap_Remove(kMap map, const void *key, void *oldKey, void *oldValue)
Removes a key-value pair from the map, optionally returning the old key and/or value.
kStatus kMap_SetValue(kMap map, kMapItem item, const void *value)
Sets the value associated with a map item.
Definition: kMap.h:710
Represents metadata about a type (class, interface, or value).
void * kMap_Value(kMap map, kMapItem item)
Returns a pointer to the value associated with a map item.
Definition: kMap.h:679
#define kOK
Operation successful.
Definition: kApiDef.h:543
Represents an error code.
#define kNULL
Null pointer.
Definition: kApiDef.h:311
Represents a boolean value.
Represents a collection of key-value pairs stored in a hash table.
kStatus kMap_AddItem(kMap map, const void *key, const void *value, kMapItem *item)
Adds a new key-value pair.