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 kFx(kStatus) kMap_Add(kMap map, const void* key, const void* value);
393 
406 #define kMap_AddT(kMap_map, KPtr_key, VPtr_value) \
407  xkMap_AddT(kMap_map, KPtr_key, VPtr_value, sizeof(*(KPtr_key)), sizeof(*(VPtr_value)))
408 
422 kFx(kStatus) kMap_Replace(kMap map, const void* key, const void* value);
423 
440 #define kMap_ReplaceT(kMap_map, KPtr_key, VPtr_value) \
441  xkMap_ReplaceT(kMap_map, KPtr_key, VPtr_value, sizeof(*(KPtr_key)), sizeof(*(VPtr_value)))
442 
457 kFx(kStatus) kMap_Remove(kMap map, const void* key, void* oldKey, void* oldValue);
458 
476 #define kMap_RemoveT(kMap_map, KPtr_key, KPtr_oldKey, VPtr_oldValue) \
477  xkMap_RemoveT(kMap_map, KPtr_key, KPtr_oldKey, VPtr_oldValue, sizeof(*(KPtr_key)), sizeof(*(KPtr_oldKey)), sizeof(*(VPtr_oldValue)))
478 
490 kInlineFx(kStatus) kMap_Discard(kMap map, const void* key)
491 {
492  return kMap_Remove(map, key, kNULL, kNULL);
493 }
494 
509 #define kMap_DiscardT(kMap_map, KPtr_key) \
510  xkMap_DiscardT(kMap_map, KPtr_key, sizeof(*(KPtr_key)))
511 
520 kFx(kStatus) kMap_Reserve(kMap map, kSize capacity);
521 
529 kFx(kStatus) kMap_Clear(kMap map);
530 
538 kFx(kStatus) kMap_Purge(kMap map);
539 
547 kFx(kMapItem) kMap_First(kMap map);
548 
557 kFx(kMapItem) kMap_Next(kMap map, kMapItem item);
558 
568 kFx(kStatus) kMap_FindItem(kMap map, const void* key, kMapItem* item);
569 
582 #define kMap_FindItemT(kMap_map, KPtr_key, kMapItemPtr_item) \
583  xkMap_FindItemT(kMap_map, KPtr_key, kMapItemPtr_item, sizeof(*(KPtr_key)))
584 
593 kFx(kStatus) kMap_RemoveItem(kMap map, kMapItem item);
594 
603 kInlineFx(const void*) kMap_Key(kMap map, kMapItem item)
604 {
605  kObj(kMap, map);
606 
607  return kPointer_ByteOffset(item, (kSSize)obj->keyField.offset);
608 }
609 
622 #define kMap_KeyT(kMap_map, kMapItem_item, K) \
623  kCast(K*, xkMap_KeyT(kMap_map, kMapItem_item, sizeof(K)))
624 
637 #define kMap_KeyAsT(kMap_map, kMapItem_item, K) \
638  kPointer_ReadAs(xkMap_KeyAsT(kMap_map, kMapItem_item, sizeof(K)), K)
639 
649 {
650  kObj(kMap, map);
651 
652  return kPointer_ByteOffset(item, (kSSize)obj->valueField.offset);
653 }
654 
667 #define kMap_ValueT(kMap_map, kMapItem_item, V) \
668  kCast(V*, xkMap_ValueT(kMap_map, kMapItem_item, sizeof(V)))
669 
679 kInlineFx(kStatus) kMap_SetValue(kMap map, kMapItem item, const void* value)
680 {
681  kObj(kMap, map);
682  void* valueSlot = kMap_Value(map, item);
683 
684  kValue_Import(obj->valueField.type, valueSlot, value);
685 
686  return kOK;
687 }
688 
701 #define kMap_SetValueT(kMap_map, kMapItem_item, VPtr_value) \
702  xkMap_SetValueT(kMap_map, kMapItem_item, VPtr_value, sizeof(*(VPtr_value)))
703 
717 #define kMap_SetValueAsT(kMap_map, kMapItem_item, V_value, V) \
718  (kPointer_WriteAs(xkMap_ValueAsT(kMap_map, kMapItem_item, sizeof(V)), V_value, V), (void)0)
719 
732 #define kMap_ValueAsT(kMap_map, kMapItem_item, V) \
733  kPointer_ReadAs(xkMap_ValueAsT(kMap_map, kMapItem_item, sizeof(V)), V)
734 
735 #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:548
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:490
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:561
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:26
const void * kMap_Key(kMap map, kMapItem item)
Returns a pointer to the key associated with a map item.
Definition: kMap.h:603
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:1796
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.
kBool(kCall * kEqualsFx)(const void *item1, const void *item2)
Callback signature to determine equality of two items.
Definition: kApiDef.h:1788
#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:3383
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:280
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:679
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:648
#define kOK
Operation successful.
Definition: kApiDef.h:515
Represents an error code.
#define kNULL
Null pointer.
Definition: kApiDef.h:269
Represents a boolean value.
Represents a collection of key-value pairs stored in a hash table.