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

#include <Dictionary.h>

Overview

Immutable key-value stores.

Definition at line 60 of file Dictionary.h.

Inheritance diagram for Dictionary:
Object MutableDictionary

Properties

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_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

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

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 451 of file Dictionary.c.

451 {
452 static Class *clazz;
453 static Once once;
454
455 do_once(&once, {
456 clazz = _initialize(&(const ClassDef) {
457 .name = "Dictionary",
458 .superclass = _Object(),
459 .instanceSize = sizeof(Dictionary),
460 .interfaceOffset = offsetof(Dictionary, interface),
461 .interfaceSize = sizeof(DictionaryInterface),
463 });
464 });
465
466 return clazz;
467}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Dictionary.c:424
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
Immutable 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:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ allKeys()

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

Definition at line 169 of file Dictionary.c.

169 {
170
172
174
175 return (Array *) keys;
176}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
static void allKeys_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for allKeys.
Definition: Dictionary.c:161
static MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Definition: MutableArray.c:195
Immutable arrays.
Definition: Array.h:56
size_t count
The count of elements.
Definition: Dictionary.h:82
void enumerateObjectsAndKeys(const Dictionary *self, DictionaryEnumerator enumerator, ident data)
Enumerate the pairs of this Dictionary with the given function.
Definition: Dictionary.c:253
Mutable arrays.
Definition: MutableArray.h:40

◆ allObjects()

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

Definition at line 189 of file Dictionary.c.

189 {
190
192
194
195 return (Array *) objects;
196}
static void allObjects_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for allObjects.
Definition: Dictionary.c:181

◆ 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 202 of file Dictionary.c.

202 {
203 return $(self, objectForKey, key) != NULL;
204}
ident objectForKey(const Dictionary *self, const ident key)
Definition: Dictionary.c:382

◆ 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 210 of file Dictionary.c.

210 {
211 return $(self, objectForKeyPath, path) != NULL;
212}
ident objectForKeyPath(const Dictionary *self, const char *path)
Definition: Dictionary.c:406

◆ 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 218 of file Dictionary.c.

218 {
219
221}
static MutableDictionary * dictionary(void)
Dictionary * initWithDictionary(Dictionary *self, const Dictionary *dictionary)
Initializes this Dictionary to contain elements of dictionary.
Definition: Dictionary.c:308

◆ 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 227 of file Dictionary.c.

227 {
228
229 Dictionary *dict = (Dictionary *) $((Object *) alloc(Dictionary), init);
230 if (dict) {
231
232 va_list args;
233 va_start(args, obj);
234
235 while (obj) {
236 ident key = va_arg(args, ident);
237
239
240 obj = va_arg(args, ident);
241 }
242
243 va_end(args);
244 }
245
246 return dict;
247}
#define obj
static void setObjectForKey(MutableDictionary *self, const ident obj, const ident key)
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Mutable key-value stores.
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Object * init(Object *self)
Initializes this Object.
Definition: Object.c:83

◆ 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 253 of file Dictionary.c.

254 {
255
256 assert(enumerator);
257
258 for (size_t i = 0; i < self->capacity; i++) {
259
260 Array *array = self->elements[i];
261 if (array) {
262
263 for (size_t j = 0; j < array->count; j += 2) {
264
265 ident key = $(array, objectAtIndex, j);
266 ident obj = $(array, objectAtIndex, j + 1);
267
268 enumerator(self, obj, key, data);
269 }
270 }
271 }
272}
static ident objectAtIndex(const Array *self, size_t index)
Definition: Array.c:394
static MutableArray * array(void)
Definition: MutableArray.c:153
static MutableData * data(void)
Definition: MutableData.c:75
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 278 of file Dictionary.c.

278 {
279
280 assert(predicate);
281
283
284 for (size_t i = 0; i < self->capacity; i++) {
285
286 Array *array = self->elements[i];
287 if (array) {
288
289 for (size_t j = 0; j < array->count; j += 2) {
290
291 ident key = $(array, objectAtIndex, j);
292 ident obj = $(array, objectAtIndex, j + 1);
293
294 if (predicate(obj, key, data)) {
296 }
297 }
298 }
299 }
300
301 return (Dictionary *) dictionary;
302}

◆ 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 308 of file Dictionary.c.

308 {
309
310 self = (Dictionary *) super(Object, self, init);
311 if (self) {
312 if (dictionary) {
313
314 self->capacity = dictionary->capacity;
315
316 self->elements = calloc(self->capacity, sizeof(ident));
317 assert(self->elements);
318
319 for (size_t i = 0; i < dictionary->capacity; i++) {
320
321 Array *array = dictionary->elements[i];
322 if (array) {
323 self->elements[i] = $((Object *) array, copy);
324 }
325 }
326
327 self->count = dictionary->count;
328 }
329 }
330
331 return self;
332}
#define super(type, obj, method,...)
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40

◆ 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 338 of file Dictionary.c.

338 {
339
340 self = (Dictionary *) super(Object, self, init);
341 if (self) {
342
343 va_list args;
344 va_start(args, self);
345
346 while (true) {
347
348 ident obj = va_arg(args, ident);
349 if (obj) {
350
351 ident key = va_arg(args, ident);
353 } else {
354 break;
355 }
356 }
357
358 va_end(args);
359 }
360
361 return self;
362}

◆ mutableCopy()

MutableDictionary * mutableCopy ( const Dictionary self)
Parameters
selfThe Dictionary.
Returns
A MutableDictionary with the contents of this Dictionary.

Definition at line 368 of file Dictionary.c.

368 {
369
371 if (copy) {
373 }
374
375 return copy;
376}
static void addEntriesFromDictionary(MutableDictionary *self, const Dictionary *dictionary)

◆ 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 382 of file Dictionary.c.

382 {
383
384 if (self->capacity == 0) {
385 return NULL;
386 }
387
388 const size_t bin = HashForObject(HASH_SEED, key) % self->capacity;
389
390 Array *array = self->elements[bin];
391 if (array) {
392
393 const ssize_t index = $(array, indexOfObject, key);
394 if (index > -1 && (index & 1) == 0) {
395 return $(array, objectAtIndex, index + 1);
396 }
397 }
398
399 return NULL;
400}
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

◆ 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 406 of file Dictionary.c.

406 {
407
408 assert(path);
409
410 String *key = str(path);
411
412 ident obj = $(self, objectForKey, key);
413
414 release(key);
415
416 return obj;
417}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
String * str(const char *fmt,...)
Definition: String.c:739
Immutable UTF-8 strings.
Definition: String.h:69

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