Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
MutableSet.c File Reference
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include "Hash.h"
#include "MutableArray.h"
#include "MutableSet.h"

Go to the source code of this file.

Macros

#define _Class   _MutableSet
 
#define MUTABLESET_DEFAULT_CAPACITY   64
 
#define MUTABLESET_GROW_FACTOR   2.0
 
#define MUTABLESET_MAX_LOAD   0.75f
 

Functions

Class_MutableSet (void)
 
static void addObject (MutableSet *self, const ident obj)
 
static void addObject_resize (Set *set)
 A helper for resizing Sets as Objects are added to them. More...
 
static void addObjectsFromArray (MutableSet *self, const Array *array)
 
static void addObjectsFromArray_enumerator (const Array *array, ident obj, ident data)
 ArrayEnumerator for addObjectsFromArray. More...
 
static void addObjectsFromSet (MutableSet *self, const Set *set)
 
static void addObjectsFromSet_enumerator (const Set *set, ident obj, ident data)
 SetEnumerator for addObjectsFromSet. More...
 
static Objectcopy (const Object *self)
 
static void filter (MutableSet *self, Predicate predicate, ident data)
 
static MutableSetinit (MutableSet *self)
 
static void initialize (Class *clazz)
 
static MutableSetinitWithCapacity (MutableSet *self, size_t capacity)
 
static void removeAllObjects (MutableSet *self)
 
static void removeObject (MutableSet *self, const ident obj)
 
static MutableSetset (void)
 
static MutableSetsetWithCapacity (size_t capacity)
 

Macro Definition Documentation

◆ _Class

#define _Class   _MutableSet

Definition at line 32 of file MutableSet.c.

◆ MUTABLESET_DEFAULT_CAPACITY

#define MUTABLESET_DEFAULT_CAPACITY   64

Definition at line 34 of file MutableSet.c.

◆ MUTABLESET_GROW_FACTOR

#define MUTABLESET_GROW_FACTOR   2.0

Definition at line 35 of file MutableSet.c.

◆ MUTABLESET_MAX_LOAD

#define MUTABLESET_MAX_LOAD   0.75f

Definition at line 36 of file MutableSet.c.

Function Documentation

◆ _MutableSet()

Class * _MutableSet ( void  )

Definition at line 298 of file MutableSet.c.

298 {
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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: MutableSet.c:278
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
Mutable sets.
Definition: MutableSet.h:40
Class * _Set(void)
The Set archetype.
Definition: Set.c:454

◆ addObject()

static void addObject ( MutableSet self,
const ident  obj 
)
static

Definition at line 96 of file MutableSet.c.

96 {
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}
#define obj
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
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
static void addObject_resize(Set *set)
A helper for resizing Sets as Objects are added to them.
Definition: MutableSet.c:60
Immutable arrays.
Definition: Array.h:56
_Bool containsObject(const Array *self, const ident obj)
Definition: Array.c:208
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Mutable arrays.
Definition: MutableArray.h:40
void addObject(MutableArray *self, const ident obj)
Adds the specified Object to this MutableArray.
Definition: MutableArray.c:99
MutableArray * array(void)
Returns a new MutableArray.
Definition: MutableArray.c:153
MutableSet * set(void)
Returns a new MutableSet.
Definition: MutableSet.c:259
Immutable sets.
Definition: Set.h:55
size_t count
The count of elements.
Definition: Set.h:77

◆ addObject_resize()

static void addObject_resize ( Set set)
static

A helper for resizing Sets as Objects are added to them.

Remarks
Static method invocations are used for all operations.

Definition at line 60 of file MutableSet.c.

60 {
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}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
#define MUTABLESET_GROW_FACTOR
Definition: MutableSet.c:35
#define MUTABLESET_DEFAULT_CAPACITY
Definition: MutableSet.c:34
#define MUTABLESET_MAX_LOAD
Definition: MutableSet.c:36
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
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

◆ addObjectsFromArray()

static void addObjectsFromArray ( MutableSet self,
const Array array 
)
static

Definition at line 126 of file MutableSet.c.

126 {
127
128 if (array) {
130 }
131}
static void addObjectsFromArray_enumerator(const Array *array, ident obj, ident data)
ArrayEnumerator for addObjectsFromArray.
Definition: MutableSet.c:118
void enumerateObjects(const Array *self, ArrayEnumerator enumerator, ident data)
Enumerate the elements of this Array with the given function.
Definition: Array.c:217

◆ addObjectsFromArray_enumerator()

static void addObjectsFromArray_enumerator ( const Array array,
ident  obj,
ident  data 
)
static

ArrayEnumerator for addObjectsFromArray.

Definition at line 118 of file MutableSet.c.

118 {
119 $((MutableSet *) data, addObject, obj);
120}
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75

◆ addObjectsFromSet()

static void addObjectsFromSet ( MutableSet self,
const Set set 
)
static

Definition at line 144 of file MutableSet.c.

144 {
145
146 if (set) {
148 }
149}
static void addObjectsFromSet_enumerator(const Set *set, ident obj, ident data)
SetEnumerator for addObjectsFromSet.
Definition: MutableSet.c:136

◆ addObjectsFromSet_enumerator()

static void addObjectsFromSet_enumerator ( const Set set,
ident  obj,
ident  data 
)
static

SetEnumerator for addObjectsFromSet.

Definition at line 136 of file MutableSet.c.

136 {
137 $((MutableSet *) data, addObject, obj);
138}

◆ copy()

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

Definition at line 43 of file MutableSet.c.

43 {
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}
void addObjectsFromSet(MutableSet *self, const Set *set)
Adds the Objects contained in set to this Set.
Definition: MutableSet.c:144
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

◆ filter()

static void filter ( MutableSet self,
Predicate  predicate,
ident  data 
)
static

Definition at line 155 of file MutableSet.c.

155 {
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}
size_t count
The count of elements.
Definition: Array.h:72
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
Set set
The superclass.
Definition: MutableSet.h:45

◆ init()

static MutableSet * init ( MutableSet self)
static

Definition at line 182 of file MutableSet.c.

182 {
183
185}

◆ initialize()

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

Definition at line 278 of file MutableSet.c.

278 {
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}
ident interface
The interface of the Class.
Definition: Class.h:105
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
MutableSet * setWithCapacity(size_t capacity)
Returns a new MutableSet with the given capacity.
Definition: MutableSet.c:268

◆ initWithCapacity()

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

Definition at line 191 of file MutableSet.c.

191 {
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}
#define super(type, obj, method,...)

◆ removeAllObjects()

static void removeAllObjects ( MutableSet self)
static

Definition at line 211 of file MutableSet.c.

211 {
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}

◆ removeObject()

static void removeObject ( MutableSet self,
const ident  obj 
)
static

Definition at line 229 of file MutableSet.c.

229 {
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}
ssize_t indexOfObject(const Array *self, const ident obj)
Definition: Array.c:271
void removeObjectAtIndex(MutableArray *self, size_t index)
Removes the Object at the specified index.
Definition: MutableArray.c:282

◆ set()

static MutableSet * set ( void  )
static

Definition at line 259 of file MutableSet.c.

259 {
260
261 return $(alloc(MutableSet), init);
262}

◆ setWithCapacity()

static MutableSet * setWithCapacity ( size_t  capacity)
static

Definition at line 268 of file MutableSet.c.

268 {
269
270 return $(alloc(MutableSet), initWithCapacity, capacity);
271}