Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Dictionary Struct Reference

#include <Dictionary.h>

Overview

Key-value stores.

Definition at line 60 of file Dictionary.h.

Inheritance diagram for Dictionary:
Object

Properties

size_t count
 The count of elements.
 
Object object
 The superclass.
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class_Dictionary (void)
 The Dictionary archetype.
 
void addEntriesFromDictionary (Dictionary *self, const Dictionary *dictionary)
 Adds the key-value entries from dictionary to this Dictionary.
 
ArrayallKeys (const Dictionary *self)
 
ArrayallObjects (const Dictionary *self)
 
bool containsKey (const Dictionary *self, const ident key)
 
bool containsKeyPath (const Dictionary *self, const char *path)
 
Dictionarydictionary (void)
 Returns a new Dictionary.
 
DictionarydictionaryWithCapacity (size_t capacity)
 Returns a new Dictionary with the given capacity.
 
DictionarydictionaryWithDictionary (const Dictionary *dictionary)
 Returns a new Dictionary containing all pairs from dictionary.
 
DictionarydictionaryWithObjectsAndKeys (ident obj,...)
 Returns a new Dictionary containing pairs from the given arguments.
 
void enumerateObjectsAndKeys (const Dictionary *self, DictionaryEnumerator enumerator, ident data)
 Enumerate the pairs of this Dictionary with the given function.
 
Dicionary * filterObjectsAndKeys (const Dictionary *self, DictionaryPredicate predicate, ident data)
 Creates a new Dictionary with pairs that pass the filter function.
 
Dictionaryinit (Dictionary *self)
 Initializes this Dictionary.
 
DictionaryinitWithCapacity (Dictionary *self, size_t capacity)
 Initializes this Dictionary with the specified capacity.
 
DictionaryinitWithDictionary (Dictionary *self, const Dictionary *dictionary)
 Initializes this Dictionary to contain elements of dictionary.
 
DictionaryinitWithObjectsAndKeys (Dictionary *self,...)
 Initializes this Dictionary with the NULL-terminated list of Objects and keys.
 
ident objectForKey (const Dictionary *self, const ident key)
 
ident objectForKeyPath (const Dictionary *self, const char *path)
 
ident objectForKeyPathWithClass (const Dictionary *self, const char *path, const Class *clazz)
 
void removeAllObjects (Dictionary *self)
 Removes all Objects from this Dictionary.
 
void removeAllObjectsWithEnumerator (Dictionary *self, DictionaryEnumerator enumerator, ident data)
 Removes all Objects from this Dictionary, invoking enumerator for each Object and key pair.
 
void removeObjectForKey (Dictionary *self, const ident key)
 Removes the Object with the specified key from this Dictionary.
 
void removeObjectForKeyPath (Dictionary *self, const char *path)
 Removes the Object with the specified key path from this Dictionary.
 
void setObjectForKey (Dictionary *self, const ident obj, const ident key)
 Sets a pair in this Dictionary.
 
void setObjectForKeyPath (Dictionary *self, const ident obj, const char *path)
 Sets a pair in this Dictionary.
 
void setObjectsForKeyPaths (Dictionary *self,...)
 Sets pairs in this Dictionary from the NULL-terminated list.
 
void setObjectsForKeys (Dictionary *self,...)
 Sets pairs in this Dictionary from the NULL-terminated list.
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

DictionaryInterface * interface
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

Property Details

◆ count

size_t Dictionary::count

The count of elements.

Definition at line 82 of file Dictionary.h.

◆ interface

DictionaryInterface* Dictionary::interface
protected

The interface.

Definition at line 71 of file Dictionary.h.

◆ object

Object Dictionary::object

The superclass.

Definition at line 65 of file Dictionary.h.

Method Details

◆ _Dictionary()

Class * _Dictionary ( void  )

The Dictionary archetype.

Returns
The Dictionary Class.

Definition at line 762 of file Dictionary.c.

762 {
763 static Class *clazz;
764 static Once once;
765
766 do_once(&once, {
767 clazz = _initialize(&(const ClassDef) {
768 .name = "Dictionary",
769 .superclass = _Object(),
770 .instanceSize = sizeof(Dictionary),
771 .interfaceOffset = offsetof(Dictionary, interface),
772 .interfaceSize = sizeof(DictionaryInterface),
774 });
775 });
776
777 return clazz;
778}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:95
Key-value stores.
Definition Dictionary.h:60
DictionaryInterface * interface
The interface.
Definition Dictionary.h:71
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136

◆ addEntriesFromDictionary()

void addEntriesFromDictionary ( Dictionary self,
const Dictionary dictionary 
)

Adds the key-value entries from dictionary to this Dictionary.

Parameters
selfThe Dictionary.
dictionaryA Dictionary.

Definition at line 175 of file Dictionary.c.

175 {
176
177 assert(dictionary);
178
180}
static void addEntriesFromDictionary_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for addEntriesFromDictionary.
Definition Dictionary.c:167
Dictionary * dictionary(void)
Returns a new Dictionary.
Definition Dictionary.c:242
void enumerateObjectsAndKeys(const Dictionary *self, DictionaryEnumerator enumerator, ident data)
Enumerate the pairs of this Dictionary with the given function.
Definition Dictionary.c:295

◆ allKeys()

Array * allKeys ( const Dictionary self)
Parameters
selfThe Dictionary.
Returns
An Array containing all keys in this Dictionary.

Definition at line 193 of file Dictionary.c.

193 {
194
195 Array *keys = $(alloc(Array), initWithCapacity, self->count);
196
198
199 return (Array *) keys;
200}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
static void allKeys_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for allKeys.
Definition Dictionary.c:185
Arrays.
Definition Array.h:56
Dictionary * initWithCapacity(Dictionary *self, size_t capacity)
Initializes this Dictionary with the specified capacity.
Definition Dictionary.c:359
size_t count
The count of elements.
Definition Dictionary.h:82

◆ allObjects()

Array * allObjects ( const Dictionary self)
Parameters
selfThe Dictionary.
Returns
An Array containing all Objects in this Dictionary.

Definition at line 213 of file Dictionary.c.

213 {
214
215 Array *objects = $(alloc(Array), initWithCapacity, self->count);
216
218
219 return (Array *) objects;
220}
static void allObjects_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for allObjects.
Definition Dictionary.c:205

◆ containsKey()

bool containsKey ( const Dictionary self,
const ident  key 
)
Parameters
selfThe Dictionary.
keyThe key to test.
Returns
True if this Dictionary contains the specified key, false otherwise.

Definition at line 226 of file Dictionary.c.

226 {
227 return $(self, objectForKey, key) != NULL;
228}
ident objectForKey(const Dictionary *self, const ident key)
Definition Dictionary.c:439

◆ containsKeyPath()

bool containsKeyPath ( const Dictionary self,
const char *  path 
)
Parameters
selfThe Dictionary.
pathThe key path to test.
Returns
True if this Dictionary contains the specified key path, false otherwise.

Definition at line 234 of file Dictionary.c.

234 {
235 return $(self, objectForKeyPath, path) != NULL;
236}
ident objectForKeyPath(const Dictionary *self, const char *path)
Definition Dictionary.c:463

◆ dictionary()

Dictionary * dictionary ( void  )

Returns a new Dictionary.

Returns
The new Dictionary, or NULL on error.

Definition at line 242 of file Dictionary.c.

242 {
243
244 return $(alloc(Dictionary), init);
245}
Dictionary * init(Dictionary *self)
Initializes this Dictionary.
Definition Dictionary.c:350

◆ dictionaryWithCapacity()

Dictionary * dictionaryWithCapacity ( size_t  capacity)

Returns a new Dictionary with the given capacity.

Parameters
capacityThe desired initial capacity.
Returns
The new Dictionary, or NULL on error.

Definition at line 251 of file Dictionary.c.

251 {
252
253 return $(alloc(Dictionary), initWithCapacity, capacity);
254}

◆ dictionaryWithDictionary()

Dictionary * dictionaryWithDictionary ( const Dictionary dictionary)

Returns a new Dictionary containing all pairs from dictionary.

Parameters
dictionaryA Dictionary.
Returns
The new Dictionary, or NULL on error.

Definition at line 260 of file Dictionary.c.

260 {
261
263}
Dictionary * initWithDictionary(Dictionary *self, const Dictionary *dictionary)
Initializes this Dictionary to contain elements of dictionary.
Definition Dictionary.c:379

◆ dictionaryWithObjectsAndKeys()

Dictionary * dictionaryWithObjectsAndKeys ( ident  obj,
  ... 
)

Returns a new Dictionary containing pairs from the given arguments.

Parameters
objThe first in a NULL-terminated list of Objects and keys.
Returns
The new Dictionary, or NULL on error.

Definition at line 269 of file Dictionary.c.

269 {
270
271 Dictionary *dict = (Dictionary *) $((Object *) alloc(Dictionary), init);
272 if (dict) {
273
274 va_list args;
275 va_start(args, obj);
276
277 while (obj) {
278 ident key = va_arg(args, ident);
279
280 $(dict, setObjectForKey, obj, key);
281
282 obj = va_arg(args, ident);
283 }
284
285 va_end(args);
286 }
287
288 return dict;
289}
#define obj
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
void setObjectForKey(Dictionary *self, const ident obj, const ident key)
Sets a pair in this Dictionary.
Definition Dictionary.c:634
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ enumerateObjectsAndKeys()

void enumerateObjectsAndKeys ( const Dictionary self,
DictionaryEnumerator  enumerator,
ident  data 
)

Enumerate the pairs of this Dictionary with the given function.

Parameters
selfThe Dictionary.
enumeratorThe enumerator function.
dataUser data.
Remarks
The enumerator should return true to break the iteration.

Definition at line 295 of file Dictionary.c.

296 {
297
298 assert(enumerator);
299
300 for (size_t i = 0; i < self->capacity; i++) {
301
302 Array *array = self->elements[i];
303 if (array) {
304
305 for (size_t j = 0; j < array->count; j += 2) {
306
307 ident key = $(array, objectAtIndex, j);
308 ident obj = $(array, objectAtIndex, j + 1);
309
310 enumerator(self, obj, key, data);
311 }
312 }
313 }
314}
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
static Array * array(void)
Definition Array.c:234
static Data * data(void)
Definition Data.c:286
size_t count
The count of elements.
Definition Array.h:72

◆ filterObjectsAndKeys()

Dictionary * filterObjectsAndKeys ( const Dictionary self,
DictionaryPredicate  predicate,
ident  data 
)

Creates a new Dictionary with pairs that pass the filter function.

Parameters
selfThe Dictionary.
predicateThe predicate function.
dataUser data.
Returns
The new, filtered Dictionary.

Definition at line 320 of file Dictionary.c.

320 {
321
322 assert(predicate);
323
325
326 for (size_t i = 0; i < self->capacity; i++) {
327
328 Array *array = self->elements[i];
329 if (array) {
330
331 for (size_t j = 0; j < array->count; j += 2) {
332
333 ident key = $(array, objectAtIndex, j);
334 ident obj = $(array, objectAtIndex, j + 1);
335
336 if (predicate(obj, key, data)) {
338 }
339 }
340 }
341 }
342
343 return dictionary;
344}

◆ init()

Dictionary * init ( Dictionary self)

Initializes this Dictionary.

Parameters
selfThe Dictionary.
Returns
The initialized Dictionary, or NULL on error.

Definition at line 350 of file Dictionary.c.

350 {
351
353}
#define DICTIONARY_DEFAULT_CAPACITY
Definition Dictionary.c:35

◆ initWithCapacity()

Dictionary * initWithCapacity ( Dictionary self,
size_t  capacity 
)

Initializes this Dictionary with the specified capacity.

Parameters
selfThe Dictionary.
capacityThe initial capacity.
Returns
The initialized Dictionary, or NULL on error.

Definition at line 359 of file Dictionary.c.

359 {
360
361 self = (Dictionary *) super(Object, self, init);
362 if (self) {
363
364 self->capacity = capacity;
365 if (self->capacity) {
366
367 self->elements = calloc(self->capacity, sizeof(ident));
368 assert(self->elements);
369 }
370 }
371
372 return self;
373}
#define super(type, obj, method,...)

◆ initWithDictionary()

Dictionary * initWithDictionary ( Dictionary self,
const Dictionary dictionary 
)

Initializes this Dictionary to contain elements of dictionary.

Parameters
selfThe Dictionary.
dictionaryA Dictionary.
Returns
The initialized Dictionary, or NULL on error.

Definition at line 379 of file Dictionary.c.

379 {
380
381 self = (Dictionary *) super(Object, self, init);
382 if (self) {
383 if (dictionary) {
384
385 self->capacity = dictionary->capacity;
386
387 self->elements = calloc(self->capacity, sizeof(ident));
388 assert(self->elements);
389
390 for (size_t i = 0; i < dictionary->capacity; i++) {
391
392 Array *array = dictionary->elements[i];
393 if (array) {
394 self->elements[i] = $((Object *) array, copy);
395 }
396 }
397
398 self->count = dictionary->count;
399 }
400 }
401
402 return self;
403}
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition Array.c:84

◆ initWithObjectsAndKeys()

Dictionary * initWithObjectsAndKeys ( Dictionary self,
  ... 
)

Initializes this Dictionary with the NULL-terminated list of Objects and keys.

Parameters
selfThe Dictionary.
Returns
The initialized Dictionary, or NULL on error.

Definition at line 409 of file Dictionary.c.

409 {
410
411 self = (Dictionary *) super(Object, self, init);
412 if (self) {
413
414 va_list args;
415 va_start(args, self);
416
417 while (true) {
418
419 ident obj = va_arg(args, ident);
420 if (obj) {
421
422 ident key = va_arg(args, ident);
423 $(self, setObjectForKey, obj, key);
424 } else {
425 break;
426 }
427 }
428
429 va_end(args);
430 }
431
432 return self;
433}

◆ objectForKey()

ident objectForKey ( const Dictionary self,
const ident  key 
)
Parameters
selfThe Dictionary.
keyThe key.
Returns
The Object stored at the specified key in this Dictionary.

Definition at line 439 of file Dictionary.c.

439 {
440
441 if (self->capacity == 0) {
442 return NULL;
443 }
444
445 const size_t bin = HashForObject(HASH_SEED, key) % self->capacity;
446
447 Array *array = self->elements[bin];
448 if (array) {
449
450 const ssize_t index = $(array, indexOfObject, key);
451 if (index > -1 && (index & 1) == 0) {
452 return $(array, objectAtIndex, index + 1);
453 }
454 }
455
456 return NULL;
457}
static ssize_t indexOfObject(const Array *self, const ident obj)
Definition Array.c:403
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Definition Hash.c:72
#define HASH_SEED
The hash seed value.
Definition Hash.h:37

◆ objectForKeyPath()

ident objectForKeyPath ( const Dictionary self,
const char *  path 
)
Parameters
selfThe Dictionary.
pathThe key path.
Returns
The Object stored at the specified key path in this Dictionary.

Definition at line 463 of file Dictionary.c.

463 {
464
465 assert(path);
466
467 String *key = str(path);
468
469 ident obj = $(self, objectForKey, key);
470
471 release(key);
472
473 return obj;
474}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
String * str(const char *fmt,...)
Definition String.c:1084
UTF-8 strings.
Definition String.h:69

◆ objectForKeyPathWithClass()

ident objectForKeyPathWithClass ( const Dictionary self,
const char *  path,
const Class clazz 
)
Parameters
selfThe Dictionary.
pathThe key path.
clazzThe Class.
Returns
The Object stored at the specified key path in this Dictionary, or NULL.
Remarks
This method performs unboxing of Null Objects, returning NULL in such cases.
The returned Object reference will either be of the specified type, or NULL.

Definition at line 480 of file Dictionary.c.

480 {
481
482 assert(path);
483
484 const ident value = $(self, objectForKeyPath, path);
485 if (value) {
486 if (instanceof(Null, value)) {
487 return NULL;
488 } else {
489 return _cast(clazz, value);
490 }
491 }
492
493 return NULL;
494}
ident _cast(const Class *clazz, const ident obj)
Perform a type-checking cast.
Definition Class.c:142
#define instanceof(type, obj)
Test if the given pointer is an instance of the specified type.
Definition Class.h:170
The Null sentinel.
Definition Null.h:42

◆ removeAllObjects()

void removeAllObjects ( Dictionary self)

Removes all Objects from this Dictionary.

Parameters
selfThe Dictionary.

Definition at line 500 of file Dictionary.c.

500 {
501
502 for (size_t i = 0; i < self->capacity; i++) {
503
504 Array *array = self->elements[i];
505 if (array) {
506 self->elements[i] = release(array);
507 }
508 }
509
510 self->count = 0;
511}

◆ removeAllObjectsWithEnumerator()

void removeAllObjectsWithEnumerator ( Dictionary self,
DictionaryEnumerator  enumerator,
ident  data 
)

Removes all Objects from this Dictionary, invoking enumerator for each Object and key pair.

Parameters
selfThe Dictionary.
enumeratorThe enumerator.
dataThe data.

Definition at line 517 of file Dictionary.c.

517 {
518
519 assert(enumerator);
520
521 for (size_t i = 0; i < self->capacity; i++) {
522
523 Array *array = self->elements[i];
524 if (array) {
525 for (size_t j = array->count; j > 0; j -= 2) {
526
527 ident obj = array->elements[j - 1];
528 ident key = array->elements[j - 2];
529
530 enumerator(self, obj, key, data);
531
532 $((Array *) array, removeObjectAtIndex, j - 1);
533 $((Array *) array, removeObjectAtIndex, j - 2);
534 }
535
536 self->elements[i] = release(array);
537 }
538 }
539
540 self->count = 0;
541}
static void removeObjectAtIndex(Array *self, size_t index)
Definition Array.c:654

◆ removeObjectForKey()

void removeObjectForKey ( Dictionary self,
const ident  key 
)

Removes the Object with the specified key from this Dictionary.

Parameters
selfThe Dictionary.
keyThe key of the Object to remove.

Definition at line 547 of file Dictionary.c.

547 {
548
549 if (self->capacity == 0) {
550 return;
551 }
552
553 const size_t bin = HashForObject(HASH_SEED, key) % self->capacity;
554
555 Array *array = self->elements[bin];
556 if (array) {
557
558 const ssize_t index = $((Array *) array, indexOfObject, key);
559 if (index > -1) {
560
561 $(array, removeObjectAtIndex, index);
562 $(array, removeObjectAtIndex, index);
563
564 if (((Array *) array)->count == 0) {
565 self->elements[bin] = release(array);
566 }
567
568 self->count--;
569 }
570 }
571}

◆ removeObjectForKeyPath()

void removeObjectForKeyPath ( Dictionary self,
const char *  path 
)

Removes the Object with the specified key path from this Dictionary.

Parameters
selfThe Dictionary.
pathThe key path of the Object to remove.

Definition at line 577 of file Dictionary.c.

577 {
578
579 String *key = $$(String, stringWithCharacters, path);
580
581 $(self, removeObjectForKey, key);
582
583 release(key);
584}
static String * stringWithCharacters(const char *chars)
Definition String.c:349
void removeObjectForKey(Dictionary *self, const ident key)
Removes the Object with the specified key from this Dictionary.
Definition Dictionary.c:547

◆ setObjectForKey()

void setObjectForKey ( Dictionary self,
const ident  obj,
const ident  key 
)

Sets a pair in this Dictionary.

Parameters
selfThe Dictionary.
objThe Object to set.
keyThe key of the Object to set.

Definition at line 634 of file Dictionary.c.

634 {
635
636 Dictionary *dict = self;
637
639
640 const size_t bin = HashForObject(HASH_SEED, key) % dict->capacity;
641
642 Array *array = dict->elements[bin];
643 if (array == NULL) {
644 array = dict->elements[bin] = $$(Array, arrayWithCapacity, (dict->capacity >> 2) + 1);
645 }
646
647 const ssize_t index = $((Array *) array, indexOfObject, key);
648 if (index > -1 && (index & 1) == 0) {
649 $(array, setObjectAtIndex, obj, index + 1);
650 } else {
651 $(array, addObject, key);
652 $(array, addObject, obj);
653
654 dict->count++;
655 }
656}
static void setObjectAtIndex(Array *self, const ident obj, size_t index)
Definition Array.c:671
static Array * arrayWithCapacity(size_t capacity)
Definition Array.c:252
static void addObject(Array *self, const ident obj)
Definition Array.c:181
static void setObjectForKey_resize(Dictionary *dict)
A helper for resizing Dictionaries as pairs are added to them.
Definition Dictionary.c:590

◆ setObjectForKeyPath()

void setObjectForKeyPath ( Dictionary self,
const ident  obj,
const char *  path 
)

Sets a pair in this Dictionary.

Parameters
selfThe Dictionary.
objThe Object to set.
pathThe key path of the Object to set.

Definition at line 662 of file Dictionary.c.

662 {
663
664 String *key = $$(String, stringWithCharacters, path);
665
666 $(self, setObjectForKey, obj, key);
667
668 release(key);
669}

◆ setObjectsForKeyPaths()

void setObjectsForKeyPaths ( Dictionary self,
  ... 
)

Sets pairs in this Dictionary from the NULL-terminated list.

Parameters
selfThe Dictionary.

Definition at line 675 of file Dictionary.c.

675 {
676
677 va_list args;
678 va_start(args, self);
679
680 while (true) {
681
682 ident obj = va_arg(args, ident);
683 if (obj) {
684 const char *path = va_arg(args, const char *);
685 $(self, setObjectForKeyPath, obj, path);
686 } else {
687 break;
688 }
689 }
690
691 va_end(args);
692}
void setObjectForKeyPath(Dictionary *self, const ident obj, const char *path)
Sets a pair in this Dictionary.
Definition Dictionary.c:662

◆ setObjectsForKeys()

void setObjectsForKeys ( Dictionary self,
  ... 
)

Sets pairs in this Dictionary from the NULL-terminated list.

Parameters
selfThe Dictionary.

Definition at line 698 of file Dictionary.c.

698 {
699
700 va_list args;
701 va_start(args, self);
702
703 while (true) {
704
705 ident obj = va_arg(args, ident);
706 if (obj) {
707 ident key = va_arg(args, ident);
708 $(self, setObjectForKey, obj, key);
709 } else {
710 break;
711 }
712 }
713
714 va_end(args);
715}

The documentation for this struct was generated from the following files: