Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
MutableSet.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <stdarg.h>
26#include <stdlib.h>
27
28#include "Hash.h"
29#include "MutableArray.h"
30#include "MutableSet.h"
31
32#define _Class _MutableSet
33
34#define MUTABLESET_DEFAULT_CAPACITY 64
35#define MUTABLESET_GROW_FACTOR 2.0
36#define MUTABLESET_MAX_LOAD 0.75f
37
38#pragma mark - Object
39
43static Object *copy(const Object *self) {
44
45 const Set *this = (Set *) self;
46
47 MutableSet *copy = $(alloc(MutableSet), initWithCapacity, this->capacity);
48
49 $(copy, addObjectsFromSet, this);
50
51 return (Object *) copy;
52}
53
54#pragma mark - MutableSet
55
60static void addObject_resize(Set *set) {
61
62 if (set->capacity) {
63
64 const float load = set->count / (float) set->capacity;
65 if (load >= MUTABLESET_MAX_LOAD) {
66
67 size_t capacity = set->capacity;
68 ident *elements = set->elements;
69
70 set->capacity = set->capacity * MUTABLESET_GROW_FACTOR;
71 set->count = 0;
72
73 set->elements = calloc(set->capacity, sizeof(ident));
74 assert(set->elements);
75
76 for (size_t i = 0; i < capacity; i++) {
77
78 Array *array = elements[i];
79 if (array) {
82 }
83 }
84
85 free(elements);
86 }
87 } else {
89 }
90}
91
96static void addObject(MutableSet *self, const ident obj) {
97
98 Set *set = (Set *) self;
99
101
102 const size_t bin = HashForObject(HASH_SEED, obj) % set->capacity;
103
104 MutableArray *array = set->elements[bin];
105 if (array == NULL) {
106 array = set->elements[bin] = $(alloc(MutableArray), init);
107 }
108
109 if ($((Array *) array, containsObject, obj) == false) {
110 $(array, addObject, obj);
111 set->count++;
112 }
113}
114
119 $((MutableSet *) data, addObject, obj);
120}
121
126static void addObjectsFromArray(MutableSet *self, const Array *array) {
127
128 if (array) {
130 }
131}
132
137 $((MutableSet *) data, addObject, obj);
138}
139
144static void addObjectsFromSet(MutableSet *self, const Set *set) {
145
146 if (set) {
148 }
149}
150
155static void filter(MutableSet *self, Predicate predicate, ident data) {
156
157 assert(predicate);
158
159 self->set.count = 0;
160
161 for (size_t i = 0; i < self->set.capacity; i++) {
162
163 MutableArray *array = self->set.elements[i];
164 if (array) {
165
166 $(array, filter, predicate, data);
167
168 if (array->array.count == 0) {
169 release(array);
170 self->set.elements[i] = NULL;
171 } else {
172 self->set.count += array->array.count;
173 }
174 }
175 }
176}
177
182static MutableSet *init(MutableSet *self) {
183
185}
186
191static MutableSet *initWithCapacity(MutableSet *self, size_t capacity) {
192
193 self = (MutableSet *) super(Object, self, init);
194 if (self) {
195
196 self->set.capacity = capacity;
197 if (self->set.capacity) {
198
199 self->set.elements = calloc(self->set.capacity, sizeof(ident));
200 assert(self->set.elements);
201 }
202 }
203
204 return self;
205}
206
211static void removeAllObjects(MutableSet *self) {
212
213 for (size_t i = 0; i < self->set.capacity; i++) {
214
215 Array *array = self->set.elements[i];
216 if (array) {
217 release(array);
218 self->set.elements[i] = NULL;
219 }
220 }
221
222 self->set.count = 0;
223}
224
229static void removeObject(MutableSet *self, const ident obj) {
230
231 if (self->set.capacity == 0) {
232 return;
233 }
234
235 const size_t bin = HashForObject(HASH_SEED, obj) % self->set.capacity;
236
237 MutableArray *array = self->set.elements[bin];
238 if (array) {
239
240 const ssize_t index = $((Array *) array, indexOfObject, obj);
241 if (index > -1) {
242
243 $(array, removeObjectAtIndex, index);
244
245 if (((Array *) array)->count == 0) {
246 release(array);
247 self->set.elements[bin] = NULL;
248 }
249
250 self->set.count--;
251 }
252 }
253}
254
259static MutableSet *set(void) {
260
261 return $(alloc(MutableSet), init);
262}
263
268static MutableSet *setWithCapacity(size_t capacity) {
269
270 return $(alloc(MutableSet), initWithCapacity, capacity);
271}
272
273#pragma mark - Class lifecycle
274
278static void initialize(Class *clazz) {
279
280 ((ObjectInterface *) clazz->interface)->copy = copy;
281
282 ((MutableSetInterface *) clazz->interface)->addObject = addObject;
283 ((MutableSetInterface *) clazz->interface)->addObjectsFromArray = addObjectsFromArray;
284 ((MutableSetInterface *) clazz->interface)->addObjectsFromSet = addObjectsFromSet;
285 ((MutableSetInterface *) clazz->interface)->filter = filter;
286 ((MutableSetInterface *) clazz->interface)->init = init;
287 ((MutableSetInterface *) clazz->interface)->initWithCapacity = initWithCapacity;
288 ((MutableSetInterface *) clazz->interface)->removeAllObjects = removeAllObjects;
289 ((MutableSetInterface *) clazz->interface)->removeObject = removeObject;
290 ((MutableSetInterface *) clazz->interface)->set = set;
291 ((MutableSetInterface *) clazz->interface)->setWithCapacity = setWithCapacity;
292}
293
299 static Class *clazz;
300 static Once once;
301
302 do_once(&once, {
303 clazz = _initialize(&(const ClassDef) {
304 .name = "MutableSet",
305 .superclass = _Set(),
306 .instanceSize = sizeof(MutableSet),
307 .interfaceOffset = offsetof(MutableSet, interface),
308 .interfaceSize = sizeof(MutableSetInterface),
310 });
311 });
312
313 return clazz;
314}
315
316#undef _Class
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define obj
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Definition: Hash.c:66
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37
Mutable arrays.
#define MUTABLESET_GROW_FACTOR
Definition: MutableSet.c:35
#define MUTABLESET_DEFAULT_CAPACITY
Definition: MutableSet.c:34
#define MUTABLESET_MAX_LOAD
Definition: MutableSet.c:36
static void addObjectsFromArray_enumerator(const Array *array, ident obj, ident data)
ArrayEnumerator for addObjectsFromArray.
Definition: MutableSet.c:118
static void addObjectsFromSet_enumerator(const Set *set, ident obj, ident data)
SetEnumerator for addObjectsFromSet.
Definition: MutableSet.c:136
static void initialize(Class *clazz)
Definition: MutableSet.c:278
static void addObject_resize(Set *set)
A helper for resizing Sets as Objects are added to them.
Definition: MutableSet.c:60
Mutable sets.
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
_Bool(* Predicate)(const ident obj, ident data)
The Predicate function type for filtering Objects.
Definition: Types.h:111
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
Immutable arrays.
Definition: Array.h:56
void enumerateObjects(const Array *self, ArrayEnumerator enumerator, ident data)
Enumerate the elements of this Array with the given function.
Definition: Array.c:217
ssize_t indexOfObject(const Array *self, const ident obj)
Definition: Array.c:271
_Bool containsObject(const Array *self, const ident obj)
Definition: Array.c:208
size_t count
The count of elements.
Definition: Array.h:72
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
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Mutable arrays.
Definition: MutableArray.h:40
void removeObjectAtIndex(MutableArray *self, size_t index)
Removes the Object at the specified index.
Definition: MutableArray.c:282
Array array
The superclass.
Definition: MutableArray.h:45
void filter(MutableArray *self, Predicate predicate, ident data)
Filters this MutableArray in place using predicate.
Definition: MutableArray.c:171
void addObject(MutableArray *self, const ident obj)
Adds the specified Object to this MutableArray.
Definition: MutableArray.c:99
void removeAllObjects(MutableArray *self)
Removes all Objects from this MutableArray.
Definition: MutableArray.c:232
void removeObject(MutableArray *self, const ident obj)
Removes the specified Object from this MutableArray.
Definition: MutableArray.c:270
MutableArray * array(void)
Returns a new MutableArray.
Definition: MutableArray.c:153
MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Initializes this MutableArray with the specified capacity.
Definition: MutableArray.c:195
void addObjectsFromArray(MutableArray *self, const Array *array)
Adds the Objects contained in array to this MutableArray.
Definition: MutableArray.c:140
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
Mutable sets.
Definition: MutableSet.h:40
void addObjectsFromSet(MutableSet *self, const Set *set)
Adds the Objects contained in set to this Set.
Definition: MutableSet.c:144
MutableSet * set(void)
Returns a new MutableSet.
Definition: MutableSet.c:259
MutableSet * setWithCapacity(size_t capacity)
Returns a new MutableSet with the given capacity.
Definition: MutableSet.c:268
Set set
The superclass.
Definition: MutableSet.h:45
Class * _MutableSet(void)
The MutableSet archetype.
Definition: MutableSet.c:298
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
Immutable sets.
Definition: Set.h:55
size_t count
The count of elements.
Definition: Set.h:77
Class * _Set(void)
The Set archetype.
Definition: Set.c:454