Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Dictionary.c File Reference
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include "Dictionary.h"
#include "Hash.h"
#include "String.h"
#include "Null.h"

Go to the source code of this file.

Macros

#define _Class   _Dictionary
 
#define DICTIONARY_DEFAULT_CAPACITY   64
 
#define DICTIONARY_GROW_FACTOR   2.0
 
#define DICTIONARY_MAX_LOAD   0.75f
 

Functions

Class_Dictionary (void)
 
static void addEntriesFromDictionary (Dictionary *self, const Dictionary *dictionary)
 
static void addEntriesFromDictionary_enumerator (const Dictionary *dict, ident obj, ident key, ident data)
 DictionaryEnumerator for addEntriesFromDictionary.
 
static ArrayallKeys (const Dictionary *self)
 
static void allKeys_enumerator (const Dictionary *dict, ident obj, ident key, ident data)
 DictionaryEnumerator for allKeys.
 
static ArrayallObjects (const Dictionary *self)
 
static void allObjects_enumerator (const Dictionary *dict, ident obj, ident key, ident data)
 DictionaryEnumerator for allObjects.
 
static bool containsKey (const Dictionary *self, const ident key)
 
static bool containsKeyPath (const Dictionary *self, const char *path)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static Stringdescription (const Object *self)
 
static void description_enumerator (const Dictionary *dict, ident obj, ident key, ident data)
 A DictionaryEnumerator for description.
 
static Dictionarydictionary (void)
 
static DictionarydictionaryWithCapacity (size_t capacity)
 
static DictionarydictionaryWithDictionary (const Dictionary *dictionary)
 
static DictionarydictionaryWithObjectsAndKeys (ident obj,...)
 
static void enumerateObjectsAndKeys (const Dictionary *self, DictionaryEnumerator enumerator, ident data)
 
static DictionaryfilterObjectsAndKeys (const Dictionary *self, DictionaryPredicate predicate, ident data)
 
static int hash (const Object *self)
 
static Dictionaryinit (Dictionary *self)
 
static void initialize (Class *clazz)
 
static DictionaryinitWithCapacity (Dictionary *self, size_t capacity)
 
static DictionaryinitWithDictionary (Dictionary *self, const Dictionary *dictionary)
 
static DictionaryinitWithObjectsAndKeys (Dictionary *self,...)
 
static bool isEqual (const Object *self, const Object *other)
 
static ident objectForKey (const Dictionary *self, const ident key)
 
static ident objectForKeyPath (const Dictionary *self, const char *path)
 
static ident objectForKeyPathWithClass (const Dictionary *self, const char *path, const Class *clazz)
 
static void removeAllObjects (Dictionary *self)
 
static void removeAllObjectsWithEnumerator (Dictionary *self, DictionaryEnumerator enumerator, ident data)
 
static void removeObjectForKey (Dictionary *self, const ident key)
 
static void removeObjectForKeyPath (Dictionary *self, const char *path)
 
static void setObjectForKey (Dictionary *self, const ident obj, const ident key)
 
static void setObjectForKey_resize (Dictionary *dict)
 A helper for resizing Dictionaries as pairs are added to them.
 
static void setObjectForKeyPath (Dictionary *self, const ident obj, const char *path)
 
static void setObjectsForKeyPaths (Dictionary *self,...)
 
static void setObjectsForKeys (Dictionary *self,...)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Dictionary

Definition at line 33 of file Dictionary.c.

◆ DICTIONARY_DEFAULT_CAPACITY

#define DICTIONARY_DEFAULT_CAPACITY   64

Definition at line 35 of file Dictionary.c.

◆ DICTIONARY_GROW_FACTOR

#define DICTIONARY_GROW_FACTOR   2.0

Definition at line 36 of file Dictionary.c.

◆ DICTIONARY_MAX_LOAD

#define DICTIONARY_MAX_LOAD   0.75f

Definition at line 37 of file Dictionary.c.

Function Documentation

◆ _Dictionary()

Class * _Dictionary ( void  )

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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
static void initialize(Class *clazz)
Definition Dictionary.c:722
Class * _Object(void)
Definition Object.c:136
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

◆ addEntriesFromDictionary()

static void addEntriesFromDictionary ( Dictionary self,
const Dictionary dictionary 
)
static

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
static void enumerateObjectsAndKeys(const Dictionary *self, DictionaryEnumerator enumerator, ident data)
Definition Dictionary.c:295
static Dictionary * dictionary(void)
Definition Dictionary.c:242

◆ addEntriesFromDictionary_enumerator()

static void addEntriesFromDictionary_enumerator ( const Dictionary dict,
ident  obj,
ident  key,
ident  data 
)
static

DictionaryEnumerator for addEntriesFromDictionary.

Definition at line 167 of file Dictionary.c.

167 {
168 $((Dictionary *) data, setObjectForKey, obj, key);
169}
#define obj
static Data * data(void)
Definition Data.c:286
static void setObjectForKey(Dictionary *self, const ident obj, const ident key)
Definition Dictionary.c:634

◆ allKeys()

static Array * allKeys ( const Dictionary self)
static

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 Dictionary * initWithCapacity(Dictionary *self, size_t capacity)
Definition Dictionary.c:359
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
size_t count
The count of elements.
Definition Dictionary.h:82

◆ allKeys_enumerator()

static void allKeys_enumerator ( const Dictionary dict,
ident  obj,
ident  key,
ident  data 
)
static

DictionaryEnumerator for allKeys.

Definition at line 185 of file Dictionary.c.

185 {
186 $((Array *) data, addObject, key);
187}
static void addObject(Array *self, const ident obj)
Definition Array.c:181

◆ allObjects()

static Array * allObjects ( const Dictionary self)
static

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

◆ allObjects_enumerator()

static void allObjects_enumerator ( const Dictionary dict,
ident  obj,
ident  key,
ident  data 
)
static

DictionaryEnumerator for allObjects.

Definition at line 205 of file Dictionary.c.

205 {
206 $((Array *) data, addObject, obj);
207}

◆ containsKey()

static bool containsKey ( const Dictionary self,
const ident  key 
)
static

Definition at line 226 of file Dictionary.c.

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

◆ containsKeyPath()

static bool containsKeyPath ( const Dictionary self,
const char *  path 
)
static

Definition at line 234 of file Dictionary.c.

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

◆ copy()

static Object * copy ( const Object self)
static
See also
Object::copy(const Object *)

Definition at line 44 of file Dictionary.c.

44 {
45
46 const Dictionary *this = (const Dictionary *) self;
47
48 Dictionary *that = $(alloc(Dictionary), initWithCapacity, this->capacity);
49 if (that) {
50 $(that, addEntriesFromDictionary, this);
51 }
52
53 return (Object *) that;
54}
static void addEntriesFromDictionary(Dictionary *self, const Dictionary *dictionary)
Definition Dictionary.c:175
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 59 of file Dictionary.c.

59 {
60
61 Dictionary *this = (Dictionary *) self;
62
63 for (size_t i = 0; i < this->capacity; i++) {
64 release(this->elements[i]);
65 }
66
67 free(this->elements);
68
69 super(Object, self, dealloc);
70}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
#define super(type, obj, method,...)
static void dealloc(Object *self)
Definition Dictionary.c:59

◆ description()

static String * description ( const Object self)
static
See also
Object::description(const Object *)

Definition at line 91 of file Dictionary.c.

91 {
92
93 const Dictionary *this = (Dictionary *) self;
94
95 String *desc = $(alloc(String), init);
96
97 $(desc, appendCharacters, "{");
98
100
101 $(desc, appendCharacters, "}");
102
103 return (String *) desc;
104}
static Dictionary * init(Dictionary *self)
Definition Dictionary.c:350
static void description_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
A DictionaryEnumerator for description.
Definition Dictionary.c:75
static void appendCharacters(String *self, const char *chars)
Definition String.c:506
UTF-8 strings.
Definition String.h:69

◆ description_enumerator()

static void description_enumerator ( const Dictionary dict,
ident  obj,
ident  key,
ident  data 
)
static

A DictionaryEnumerator for description.

Definition at line 75 of file Dictionary.c.

75 {
76
77 String *desc = (String *) data;
78
79 String *objDesc = $((Object *) obj, description);
80 String *keyDesc = $((Object *) key, description);
81
82 $(desc, appendFormat, "%s: %s, ", keyDesc->chars, objDesc->chars);
83
84 release(objDesc);
85 release(keyDesc);
86}
static String * description(const Object *self)
Definition Dictionary.c:91
static void appendFormat(String *self, const char *fmt,...)
Definition String.c:541
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition String.h:85

◆ dictionary()

static Dictionary * dictionary ( void  )
static

Definition at line 242 of file Dictionary.c.

242 {
243
244 return $(alloc(Dictionary), init);
245}

◆ dictionaryWithCapacity()

static Dictionary * dictionaryWithCapacity ( size_t  capacity)
static

Definition at line 251 of file Dictionary.c.

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

◆ dictionaryWithDictionary()

static Dictionary * dictionaryWithDictionary ( const Dictionary dictionary)
static

Definition at line 260 of file Dictionary.c.

260 {
261
263}
static Dictionary * initWithDictionary(Dictionary *self, const Dictionary *dictionary)
Definition Dictionary.c:379

◆ dictionaryWithObjectsAndKeys()

static Dictionary * dictionaryWithObjectsAndKeys ( ident  obj,
  ... 
)
static

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}
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49

◆ enumerateObjectsAndKeys()

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

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
size_t count
The count of elements.
Definition Array.h:72

◆ filterObjectsAndKeys()

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

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}

◆ hash()

static int hash ( const Object self)
static
See also
Object::hash(const Object *)

Definition at line 109 of file Dictionary.c.

109 {
110
111 const Dictionary *this = (Dictionary *) self;
112
113 int hash = HashForInteger(HASH_SEED, this->count);
114
115 for (size_t i = 0; i < this->capacity; i++) {
116 if (this->elements[i]) {
117 hash = HashForObject(hash, this->elements[i]);
118 }
119 }
120
121 return hash;
122}
static int hash(const Object *self)
Definition Dictionary.c:109
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition Hash.c:66
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

◆ init()

static Dictionary * init ( Dictionary self)
static

Definition at line 350 of file Dictionary.c.

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

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 722 of file Dictionary.c.

722 {
723
724 ((ObjectInterface *) clazz->interface)->copy = copy;
725 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
726 ((ObjectInterface *) clazz->interface)->description = description;
727 ((ObjectInterface *) clazz->interface)->hash = hash;
728 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
729
730 ((DictionaryInterface *) clazz->interface)->addEntriesFromDictionary = addEntriesFromDictionary;
731 ((DictionaryInterface *) clazz->interface)->allKeys = allKeys;
732 ((DictionaryInterface *) clazz->interface)->allObjects = allObjects;
733 ((DictionaryInterface *) clazz->interface)->containsKey = containsKey;
734 ((DictionaryInterface *) clazz->interface)->containsKeyPath = containsKeyPath;
735 ((DictionaryInterface *) clazz->interface)->dictionary = dictionary;
736 ((DictionaryInterface *) clazz->interface)->dictionaryWithCapacity = dictionaryWithCapacity;
737 ((DictionaryInterface *) clazz->interface)->dictionaryWithDictionary = dictionaryWithDictionary;
738 ((DictionaryInterface *) clazz->interface)->dictionaryWithObjectsAndKeys = dictionaryWithObjectsAndKeys;
739 ((DictionaryInterface *) clazz->interface)->enumerateObjectsAndKeys = enumerateObjectsAndKeys;
740 ((DictionaryInterface *) clazz->interface)->filterObjectsAndKeys = filterObjectsAndKeys;
741 ((DictionaryInterface *) clazz->interface)->init = init;
742 ((DictionaryInterface *) clazz->interface)->initWithCapacity = initWithCapacity;
743 ((DictionaryInterface *) clazz->interface)->initWithDictionary = initWithDictionary;
744 ((DictionaryInterface *) clazz->interface)->initWithObjectsAndKeys = initWithObjectsAndKeys;
745 ((DictionaryInterface *) clazz->interface)->objectForKey = objectForKey;
746 ((DictionaryInterface *) clazz->interface)->objectForKeyPath = objectForKeyPath;
747 ((DictionaryInterface *) clazz->interface)->objectForKeyPathWithClass = objectForKeyPathWithClass;
748 ((DictionaryInterface *) clazz->interface)->removeAllObjects = removeAllObjects;
749 ((DictionaryInterface *) clazz->interface)->removeAllObjectsWithEnumerator = removeAllObjectsWithEnumerator;
750 ((DictionaryInterface *) clazz->interface)->removeObjectForKey = removeObjectForKey;
751 ((DictionaryInterface *) clazz->interface)->removeObjectForKeyPath = removeObjectForKeyPath;
752 ((DictionaryInterface *) clazz->interface)->setObjectForKey = setObjectForKey;
753 ((DictionaryInterface *) clazz->interface)->setObjectForKeyPath = setObjectForKeyPath;
754 ((DictionaryInterface *) clazz->interface)->setObjectsForKeyPaths = setObjectsForKeyPaths;
755 ((DictionaryInterface *) clazz->interface)->setObjectsForKeys = setObjectsForKeys;
756}
static void removeAllObjects(Dictionary *self)
Definition Dictionary.c:500
static void setObjectsForKeys(Dictionary *self,...)
Definition Dictionary.c:698
static Dictionary * dictionaryWithDictionary(const Dictionary *dictionary)
Definition Dictionary.c:260
static void setObjectsForKeyPaths(Dictionary *self,...)
Definition Dictionary.c:675
static bool containsKeyPath(const Dictionary *self, const char *path)
Definition Dictionary.c:234
static Dictionary * dictionaryWithCapacity(size_t capacity)
Definition Dictionary.c:251
static bool isEqual(const Object *self, const Object *other)
Definition Dictionary.c:127
static void removeObjectForKey(Dictionary *self, const ident key)
Definition Dictionary.c:547
static Array * allObjects(const Dictionary *self)
Definition Dictionary.c:213
static Dictionary * initWithObjectsAndKeys(Dictionary *self,...)
Definition Dictionary.c:409
static Array * allKeys(const Dictionary *self)
Definition Dictionary.c:193
static Dictionary * dictionaryWithObjectsAndKeys(ident obj,...)
Definition Dictionary.c:269
static ident objectForKeyPathWithClass(const Dictionary *self, const char *path, const Class *clazz)
Definition Dictionary.c:480
static void removeObjectForKeyPath(Dictionary *self, const char *path)
Definition Dictionary.c:577
static void setObjectForKeyPath(Dictionary *self, const ident obj, const char *path)
Definition Dictionary.c:662
static Dictionary * filterObjectsAndKeys(const Dictionary *self, DictionaryPredicate predicate, ident data)
Definition Dictionary.c:320
static Object * copy(const Object *self)
Definition Dictionary.c:44
static void removeAllObjectsWithEnumerator(Dictionary *self, DictionaryEnumerator enumerator, ident data)
Definition Dictionary.c:517
static bool containsKey(const Dictionary *self, const ident key)
Definition Dictionary.c:226
ident interface
The interface of the Class.
Definition Class.h:105
Dictionary * initWithCapacity(Dictionary *self, size_t capacity)
Initializes this Dictionary with the specified capacity.
Definition Dictionary.c:359
Dictionary * dictionaryWithObjectsAndKeys(ident obj,...)
Returns a new Dictionary containing pairs from the given arguments.
Definition Dictionary.c:269
Dictionary * init(Dictionary *self)
Initializes this Dictionary.
Definition Dictionary.c:350
Dictionary * initWithDictionary(Dictionary *self, const Dictionary *dictionary)
Initializes this Dictionary to contain elements of dictionary.
Definition Dictionary.c:379
Dictionary * dictionaryWithCapacity(size_t capacity)
Returns a new Dictionary with the given capacity.
Definition Dictionary.c:251
Dictionary * dictionaryWithDictionary(const Dictionary *dictionary)
Returns a new Dictionary containing all pairs from dictionary.
Definition Dictionary.c:260
ident objectForKey(const Dictionary *self, const ident key)
Definition Dictionary.c:439
Dictionary * initWithObjectsAndKeys(Dictionary *self,...)
Initializes this Dictionary with the NULL-terminated list of Objects and keys.
Definition Dictionary.c:409
void enumerateObjectsAndKeys(const Dictionary *self, DictionaryEnumerator enumerator, ident data)
Enumerate the pairs of this Dictionary with the given function.
Definition Dictionary.c:295
int hash(const Object *self)
Definition Array.c:129
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99

◆ initWithCapacity()

static Dictionary * initWithCapacity ( Dictionary self,
size_t  capacity 
)
static

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}

◆ initWithDictionary()

static Dictionary * initWithDictionary ( Dictionary self,
const Dictionary dictionary 
)
static

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}

◆ initWithObjectsAndKeys()

static Dictionary * initWithObjectsAndKeys ( Dictionary self,
  ... 
)
static

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}

◆ isEqual()

static bool isEqual ( const Object self,
const Object other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 127 of file Dictionary.c.

127 {
128
129 if (super(Object, self, isEqual, other)) {
130 return true;
131 }
132
133 if (other && $(other, isKindOfClass, _Dictionary())) {
134
135 const Dictionary *this = (Dictionary *) self;
136 const Dictionary *that = (Dictionary *) other;
137
138 if (this->count == that->count) {
139
140 Array *keys = $(this, allKeys);
141
142 for (size_t i = 0; i < keys->count; i++) {
143 const ident key = $(keys, objectAtIndex, i);
144
145 const Object *thisObject = $(this, objectForKey, key);
146 const Object *thatObject = $(that, objectForKey, key);
147
148 if ($(thisObject, isEqual, thatObject) == false) {
149 release(keys);
150 return false;
151 }
152 }
153
154 release(keys);
155 return true;
156 }
157 }
158
159 return false;
160}
Class * _Dictionary(void)
Definition Dictionary.c:762
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101

◆ objectForKey()

static ident objectForKey ( const Dictionary self,
const ident  key 
)
static

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

◆ objectForKeyPath()

static ident objectForKeyPath ( const Dictionary self,
const char *  path 
)
static

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}
String * str(const char *fmt,...)
Definition String.c:1084

◆ objectForKeyPathWithClass()

static ident objectForKeyPathWithClass ( const Dictionary self,
const char *  path,
const Class clazz 
)
static

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()

static void removeAllObjects ( Dictionary self)
static

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()

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

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()

static void removeObjectForKey ( Dictionary self,
const ident  key 
)
static

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()

static void removeObjectForKeyPath ( Dictionary self,
const char *  path 
)
static

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

◆ setObjectForKey()

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

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 setObjectForKey_resize(Dictionary *dict)
A helper for resizing Dictionaries as pairs are added to them.
Definition Dictionary.c:590

◆ setObjectForKey_resize()

static void setObjectForKey_resize ( Dictionary dict)
static

A helper for resizing Dictionaries as pairs are added to them.

Remarks
Static method invocations are used for all operations.

Definition at line 590 of file Dictionary.c.

590 {
591
592 if (dict->capacity) {
593
594 const float load = dict->count / (float) dict->capacity;
595 if (load >= DICTIONARY_MAX_LOAD) {
596
597 size_t capacity = dict->capacity;
598 ident *elements = dict->elements;
599
600 dict->capacity = dict->capacity * DICTIONARY_GROW_FACTOR;
601 dict->count = 0;
602
603 dict->elements = calloc(dict->capacity, sizeof(ident));
604 assert(dict->elements);
605
606 for (size_t i = 0; i < capacity; i++) {
607
608 Array *array = elements[i];
609 if (array) {
610
611 for (size_t j = 0; j < array->count; j += 2) {
612
613 ident key = $(array, objectAtIndex, j);
614 ident obj = $(array, objectAtIndex, j + 1);
615
616 $$(Dictionary, setObjectForKey, dict, obj, key);
617 }
618
619 release(array);
620 }
621 }
622
623 free(elements);
624 }
625 } else {
627 }
628}
#define DICTIONARY_GROW_FACTOR
Definition Dictionary.c:36
#define DICTIONARY_MAX_LOAD
Definition Dictionary.c:37

◆ setObjectForKeyPath()

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

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()

static void setObjectsForKeyPaths ( Dictionary self,
  ... 
)
static

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}

◆ setObjectsForKeys()

static void setObjectsForKeys ( Dictionary self,
  ... 
)
static

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}