Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
MutableDictionary Struct Reference

#include <MutableDictionary.h>

Overview

Mutable key-value stores.

Definition at line 40 of file MutableDictionary.h.

Inheritance diagram for MutableDictionary:
Dictionary Object

Properties

Dictionary dictionary
 The superclass. More...
 
- Properties inherited from Dictionary
size_t count
 The count of elements. More...
 
Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

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

Protected Attributes

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

Property Details

◆ dictionary

MutableDictionary * dictionary ( void  )

The superclass.

Definition at line 45 of file MutableDictionary.h.

◆ interface

MutableDictionaryInterface* MutableDictionary::interface
protected

The interface.

Definition at line 51 of file MutableDictionary.h.

Method Details

◆ _MutableDictionary()

Class * _MutableDictionary ( void  )

The MutableDictionary archetype.

Returns
The MutableDictionary Class.

Definition at line 371 of file MutableDictionary.c.

371 {
372 static Class *clazz;
373 static Once once;
374
375 do_once(&once, {
376 clazz = _initialize(&(const ClassDef) {
377 .name = "MutableDictionary",
378 .superclass = _Dictionary(),
379 .instanceSize = sizeof(MutableDictionary),
380 .interfaceOffset = offsetof(MutableDictionary, interface),
381 .interfaceSize = sizeof(MutableDictionaryInterface),
383 });
384 });
385
386 return clazz;
387}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
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
Class * _Dictionary(void)
The Dictionary archetype.
Definition: Dictionary.c:451
Mutable key-value stores.
MutableDictionaryInterface * interface
The interface.
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51

◆ addEntriesFromDictionary()

void addEntriesFromDictionary ( MutableDictionary self,
const Dictionary dictionary 
)

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

Parameters
selfThe MutableDictionary.
dictionaryA Dictionary.

Definition at line 68 of file MutableDictionary.c.

68 {
69
70 assert(dictionary);
71
73}
static void addEntriesFromDictionary_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for addEntriesFromDictionary.
void enumerateObjectsAndKeys(const Dictionary *self, DictionaryEnumerator enumerator, ident data)
Enumerate the pairs of this Dictionary with the given function.
Definition: Dictionary.c:253
Dictionary dictionary
The superclass.

◆ dictionary()

MutableDictionary * dictionary ( void  )

Returns a new MutableDictionary.

Returns
The new MutableDictionary, or NULL on error.

Definition at line 79 of file MutableDictionary.c.

79 {
80
81 return $(alloc(MutableDictionary), init);
82}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
MutableDictionary * init(MutableDictionary *self)
Initializes this MutableDictionary.

◆ dictionaryWithCapacity()

MutableDictionary * dictionaryWithCapacity ( size_t  capacity)

Returns a new MutableDictionary with the given capacity.

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

Definition at line 88 of file MutableDictionary.c.

88 {
89
90 return $(alloc(MutableDictionary), initWithCapacity, capacity);
91}
MutableDictionary * initWithCapacity(MutableDictionary *self, size_t capacity)
Initializes this MutableDictionary with the specified capacity.

◆ init()

Initializes this MutableDictionary.

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

Definition at line 97 of file MutableDictionary.c.

97 {
98
100}
#define MUTABLEDICTIONARY_DEFAULT_CAPACITY

◆ initWithCapacity()

MutableDictionary * initWithCapacity ( MutableDictionary self,
size_t  capacity 
)

Initializes this MutableDictionary with the specified capacity.

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

Definition at line 106 of file MutableDictionary.c.

106 {
107
108 self = (MutableDictionary *) super(Object, self, init);
109 if (self) {
110
111 self->dictionary.capacity = capacity;
112 if (self->dictionary.capacity) {
113
114 self->dictionary.elements = calloc(self->dictionary.capacity, sizeof(ident));
115 assert(self->dictionary.elements);
116 }
117 }
118
119 return self;
120}
#define super(type, obj, method,...)
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ removeAllObjects()

void removeAllObjects ( MutableDictionary self)

Removes all Objects from this MutableDictionary.

Parameters
selfThe MutableDictionary.

Definition at line 126 of file MutableDictionary.c.

126 {
127
128 for (size_t i = 0; i < self->dictionary.capacity; i++) {
129
130 Array *array = self->dictionary.elements[i];
131 if (array) {
132 self->dictionary.elements[i] = release(array);
133 }
134 }
135
136 self->dictionary.count = 0;
137}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
static MutableArray * array(void)
Definition: MutableArray.c:153
Immutable arrays.
Definition: Array.h:56
size_t count
The count of elements.
Definition: Dictionary.h:82

◆ removeAllObjectsWithEnumerator()

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

Definition at line 143 of file MutableDictionary.c.

143 {
144
145 assert(enumerator);
146
147 for (size_t i = 0; i < self->dictionary.capacity; i++) {
148
149 Array *array = self->dictionary.elements[i];
150 if (array) {
151 for (size_t j = array->count; j > 0; j -= 2) {
152
153 ident obj = array->elements[j - 1];
154 ident key = array->elements[j - 2];
155
156 enumerator((Dictionary *) self, obj, key, data);
157
160 }
161
162 self->dictionary.elements[i] = release(array);
163 }
164 }
165
166 self->dictionary.count = 0;
167}
#define obj
static void removeObjectAtIndex(MutableArray *self, size_t index)
Definition: MutableArray.c:282
static MutableData * data(void)
Definition: MutableData.c:75
size_t count
The count of elements.
Definition: Array.h:72
Immutable key-value stores.
Definition: Dictionary.h:60
Mutable arrays.
Definition: MutableArray.h:40

◆ removeObjectForKey()

void removeObjectForKey ( MutableDictionary self,
const ident  key 
)

Removes the Object with the specified key from this MutableDictionary.

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

Definition at line 173 of file MutableDictionary.c.

173 {
174
175 if (self->dictionary.capacity == 0) {
176 return;
177 }
178
179 const size_t bin = HashForObject(HASH_SEED, key) % self->dictionary.capacity;
180
181 MutableArray *array = self->dictionary.elements[bin];
182 if (array) {
183
184 const ssize_t index = $((Array *) array, indexOfObject, key);
185 if (index > -1) {
186
187 $(array, removeObjectAtIndex, index);
188 $(array, removeObjectAtIndex, index);
189
190 if (((Array *) array)->count == 0) {
191 self->dictionary.elements[bin] = release(array);
192 }
193
194 self->dictionary.count--;
195 }
196 }
197}
static ssize_t indexOfObject(const Array *self, const ident obj)
Definition: Array.c:271
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Definition: Hash.c:66
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37

◆ removeObjectForKeyPath()

void removeObjectForKeyPath ( MutableDictionary self,
const char *  path 
)

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

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

Definition at line 203 of file MutableDictionary.c.

203 {
204
205 String *key = $$(String, stringWithCharacters, path);
206
207 $(self, removeObjectForKey, key);
208
209 release(key);
210}
static String * stringWithCharacters(const char *chars)
Definition: String.c:487
void removeObjectForKey(MutableDictionary *self, const ident key)
Removes the Object with the specified key from this MutableDictionary.
Immutable UTF-8 strings.
Definition: String.h:69

◆ setObjectForKey()

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

Sets a pair in this MutableDictionary.

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

Definition at line 260 of file MutableDictionary.c.

260 {
261
262 Dictionary *dict = (Dictionary *) self;
263
265
266 const size_t bin = HashForObject(HASH_SEED, key) % dict->capacity;
267
268 MutableArray *array = dict->elements[bin];
269 if (array == NULL) {
270 array = dict->elements[bin] = $$(MutableArray, arrayWithCapacity, (dict->capacity >> 2) + 1);
271 }
272
273 const ssize_t index = $((Array *) array, indexOfObject, key);
274 if (index > -1) {
275 $(array, setObjectAtIndex, obj, index + 1);
276 } else {
277 $(array, addObject, key);
278 $(array, addObject, obj);
279
280 dict->count++;
281 }
282}
static MutableArray * arrayWithCapacity(size_t capacity)
Definition: MutableArray.c:162
static void setObjectAtIndex(MutableArray *self, const ident obj, size_t index)
Definition: MutableArray.c:299
static void addObject(MutableArray *self, const ident obj)
Definition: MutableArray.c:99
static void setObjectForKey_resize(Dictionary *dict)
A helper for resizing Dictionaries as pairs are added to them.

◆ setObjectForKeyPath()

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

Sets a pair in this MutableDictionary.

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

Definition at line 288 of file MutableDictionary.c.

288 {
289
290 String *key = $$(String, stringWithCharacters, path);
291
292 $(self, setObjectForKey, obj, key);
293
294 release(key);
295}
void setObjectForKey(MutableDictionary *self, const ident obj, const ident key)
Sets a pair in this MutableDictionary.

◆ setObjectsForKeyPaths()

void setObjectsForKeyPaths ( MutableDictionary self,
  ... 
)

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

Parameters
selfThe MutableDictionary.

Definition at line 301 of file MutableDictionary.c.

301 {
302
303 va_list args;
304 va_start(args, self);
305
306 while (true) {
307
308 ident obj = va_arg(args, ident);
309 if (obj) {
310 const char *path = va_arg(args, const char *);
311 $(self, setObjectForKeyPath, obj, path);
312 } else {
313 break;
314 }
315 }
316
317 va_end(args);
318}
void setObjectForKeyPath(MutableDictionary *self, const ident obj, const char *path)
Sets a pair in this MutableDictionary.

◆ setObjectsForKeys()

void setObjectsForKeys ( MutableDictionary self,
  ... 
)

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

Parameters
selfThe MutableDictionary.

Definition at line 324 of file MutableDictionary.c.

324 {
325
326 va_list args;
327 va_start(args, self);
328
329 while (true) {
330
331 ident obj = va_arg(args, ident);
332 if (obj) {
333 ident key = va_arg(args, ident);
334 $(self, setObjectForKey, obj, key);
335 } else {
336 break;
337 }
338 }
339
340 va_end(args);
341}

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