33#define _Class _Dictionary
35#define DICTIONARY_DEFAULT_CAPACITY 64
36#define DICTIONARY_GROW_FACTOR 2.0
37#define DICTIONARY_MAX_LOAD 0.75f
63 for (
size_t i = 0; i < this->capacity; i++) {
115 for (
size_t i = 0; i < this->capacity; i++) {
116 if (this->elements[i]) {
138 if (this->count == that->
count) {
142 for (
size_t i = 0; i < keys->
count; i++) {
148 if ($(thisObject,
isEqual, thatObject) ==
false) {
162#pragma mark - Dictionary
199 return (
Array *) keys;
219 return (
Array *) objects;
300 for (
size_t i = 0; i < self->capacity; i++) {
305 for (
size_t j = 0; j <
array->
count; j += 2) {
310 enumerator(self,
obj, key,
data);
326 for (
size_t i = 0; i < self->capacity; i++) {
331 for (
size_t j = 0; j <
array->
count; j += 2) {
336 if (predicate(
obj, key,
data)) {
364 self->capacity = capacity;
365 if (self->capacity) {
367 self->elements = calloc(self->capacity,
sizeof(
ident));
368 assert(self->elements);
387 self->elements = calloc(self->capacity,
sizeof(
ident));
388 assert(self->elements);
390 for (
size_t i = 0; i <
dictionary->capacity; i++) {
415 va_start(args, self);
441 if (self->capacity == 0) {
451 if (index > -1 && (index & 1) == 0) {
489 return _cast(clazz, value);
502 for (
size_t i = 0; i < self->capacity; i++) {
521 for (
size_t i = 0; i < self->capacity; i++) {
525 for (
size_t j =
array->
count; j > 0; j -= 2) {
530 enumerator(self,
obj, key,
data);
549 if (self->capacity == 0) {
592 if (dict->capacity) {
594 const float load = dict->
count / (float) dict->capacity;
597 size_t capacity = dict->capacity;
598 ident *elements = dict->elements;
603 dict->elements = calloc(dict->capacity,
sizeof(
ident));
604 assert(dict->elements);
606 for (
size_t i = 0; i < capacity; i++) {
611 for (
size_t j = 0; j <
array->
count; j += 2) {
648 if (index > -1 && (index & 1) == 0) {
678 va_start(args, self);
684 const char *path = va_arg(args,
const char *);
701 va_start(args, self);
717#pragma mark - Class lifecycle
768 .name =
"Dictionary",
771 .interfaceOffset = offsetof(
Dictionary, interface),
772 .interfaceSize =
sizeof(DictionaryInterface),
static void removeObjectAtIndex(Array *self, size_t index)
static void setObjectAtIndex(Array *self, const ident obj, size_t index)
static ident objectAtIndex(const Array *self, size_t index)
static Array * arrayWithCapacity(size_t capacity)
static void addObject(Array *self, const ident obj)
static Array * array(void)
static ssize_t indexOfObject(const Array *self, const ident obj)
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Class * _initialize(const ClassDef *def)
Initializes the given Class.
ident _cast(const Class *clazz, const ident obj)
Perform a type-checking cast.
#define alloc(type)
Allocate and initialize and instance of type.
#define super(type, obj, method,...)
#define instanceof(type, obj)
Test if the given pointer is an instance of the specified type.
static void setObjectForKey(Dictionary *self, const ident obj, const ident key)
Class * _Dictionary(void)
static void removeAllObjects(Dictionary *self)
static void setObjectsForKeys(Dictionary *self,...)
static Dictionary * dictionaryWithDictionary(const Dictionary *dictionary)
static Dictionary * init(Dictionary *self)
#define DICTIONARY_DEFAULT_CAPACITY
static void addEntriesFromDictionary_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for addEntriesFromDictionary.
static void description_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
A DictionaryEnumerator for description.
static void setObjectsForKeyPaths(Dictionary *self,...)
static bool containsKeyPath(const Dictionary *self, const char *path)
static Dictionary * dictionaryWithCapacity(size_t capacity)
static bool isEqual(const Object *self, const Object *other)
static void removeObjectForKey(Dictionary *self, const ident key)
static Array * allObjects(const Dictionary *self)
static Dictionary * initWithObjectsAndKeys(Dictionary *self,...)
static void enumerateObjectsAndKeys(const Dictionary *self, DictionaryEnumerator enumerator, ident data)
static String * description(const Object *self)
static Dictionary * dictionary(void)
static Array * allKeys(const Dictionary *self)
static Dictionary * dictionaryWithObjectsAndKeys(ident obj,...)
static ident objectForKeyPathWithClass(const Dictionary *self, const char *path, const Class *clazz)
static ident objectForKey(const Dictionary *self, const ident key)
static void removeObjectForKeyPath(Dictionary *self, const char *path)
static void addEntriesFromDictionary(Dictionary *self, const Dictionary *dictionary)
static void dealloc(Object *self)
static void setObjectForKeyPath(Dictionary *self, const ident obj, const char *path)
static Dictionary * initWithCapacity(Dictionary *self, size_t capacity)
static void setObjectForKey_resize(Dictionary *dict)
A helper for resizing Dictionaries as pairs are added to them.
static Dictionary * initWithDictionary(Dictionary *self, const Dictionary *dictionary)
static void allKeys_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for allKeys.
static void allObjects_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for allObjects.
static Dictionary * filterObjectsAndKeys(const Dictionary *self, DictionaryPredicate predicate, ident data)
static Object * copy(const Object *self)
static void initialize(Class *clazz)
static void removeAllObjectsWithEnumerator(Dictionary *self, DictionaryEnumerator enumerator, ident data)
static bool containsKey(const Dictionary *self, const ident key)
#define DICTIONARY_GROW_FACTOR
static ident objectForKeyPath(const Dictionary *self, const char *path)
#define DICTIONARY_MAX_LOAD
static int hash(const Object *self)
void(* DictionaryEnumerator)(const Dictionary *dictionary, ident obj, ident key, ident data)
A function type for Dictionary enumeration (iteration).
bool(* DictionaryPredicate)(ident obj, ident key, ident data)
A function pointer for Dictionary filtering.
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
static bool isKindOfClass(const Object *self, const Class *clazz)
String * str(const char *fmt,...)
static void appendCharacters(String *self, const char *chars)
static void appendFormat(String *self, const char *fmt,...)
static String * stringWithCharacters(const char *chars)
void * ident
The identity type, similar to Objective-C id.
#define do_once(once, block)
Executes the given block at most one time.
size_t count
The count of elements.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
Dictionary * initWithCapacity(Dictionary *self, size_t capacity)
Initializes this Dictionary with the specified capacity.
Dictionary * dictionaryWithObjectsAndKeys(ident obj,...)
Returns a new Dictionary containing pairs from the given arguments.
Dictionary * init(Dictionary *self)
Initializes this Dictionary.
Dictionary * initWithDictionary(Dictionary *self, const Dictionary *dictionary)
Initializes this Dictionary to contain elements of dictionary.
Dictionary * dictionaryWithCapacity(size_t capacity)
Returns a new Dictionary with the given capacity.
size_t count
The count of elements.
Dictionary * dictionaryWithDictionary(const Dictionary *dictionary)
Returns a new Dictionary containing all pairs from dictionary.
ident objectForKey(const Dictionary *self, const ident key)
Dictionary * initWithObjectsAndKeys(Dictionary *self,...)
Initializes this Dictionary with the NULL-terminated list of Objects and keys.
void enumerateObjectsAndKeys(const Dictionary *self, DictionaryEnumerator enumerator, ident data)
Enumerate the pairs of this Dictionary with the given function.
Object is the root Class of The Objectively Class hierarchy.
int hash(const Object *self)
void dealloc(Object *self)
Frees all resources held by this Object.
char * chars
The backing null-terminated UTF-8 encoded character array.