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

Go to the source code of this file.

Macros

#define _Class   _Set
 

Functions

Class_Set (void)
 
static ArrayallObjects (const Set *self)
 
static void allObjects_enumerator (const Set *set, ident obj, ident data)
 SetEnumerator for allObjects. More...
 
static _Bool containsObject (const Set *self, const ident obj)
 
static _Bool containsObjectMatching (const Set *self, Predicate predicate, ident data)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static Stringdescription (const Object *self)
 
static void enumerateObjects (const Set *self, SetEnumerator enumerator, ident data)
 
static SetfilteredSet (const Set *self, Predicate predicate, ident data)
 
static int hash (const Object *self)
 
static void initialize (Class *clazz)
 
static SetinitWithArray (Set *self, const Array *array)
 
static void initWithArray_enumerator (const Array *array, ident obj, ident data)
 ArrayEnumerator for initWithArray. More...
 
static SetinitWithObjects (Set *self,...)
 
static SetinitWithSet (Set *self, const Set *set)
 
static void initWithSet_enumerator (const Set *set, ident obj, ident data)
 SetEnumerator for initWithSet. More...
 
static _Bool isEqual (const Object *self, const Object *other)
 
static SetmappedSet (const Set *self, Functor functor, ident data)
 
static MutableSetmutableCopy (const Set *self)
 
static ident reduce (const Set *self, Reducer reducer, ident accumulator, ident data)
 
static SetsetWithArray (const Array *array)
 
static SetsetWithObjects (ident obj,...)
 
static SetsetWithSet (const Set *set)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Set

Definition at line 34 of file Set.c.

Function Documentation

◆ _Set()

Class * _Set ( void  )

Definition at line 454 of file Set.c.

454 {
455 static Class *clazz;
456 static Once once;
457
458 do_once(&once, {
459 clazz = _initialize(&(const ClassDef) {
460 .name = "Set",
461 .superclass = _Object(),
462 .instanceSize = sizeof(Set),
463 .interfaceOffset = offsetof(Set, interface),
464 .interfaceSize = sizeof(SetInterface),
466 });
467 });
468
469 return clazz;
470}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Set.c:426
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 * _Object(void)
The Object archetype.
Definition: Object.c:136
Immutable sets.
Definition: Set.h:55

◆ allObjects()

static Array * allObjects ( const Set self)
static

Definition at line 142 of file Set.c.

142 {
143
145
146 $(self, enumerateObjects, allObjects_enumerator, objects);
147
148 return (Array *) objects;
149}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
static void allObjects_enumerator(const Set *set, ident obj, ident data)
SetEnumerator for allObjects.
Definition: Set.c:134
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
Mutable arrays.
Definition: MutableArray.h:40
MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Initializes this MutableArray with the specified capacity.
Definition: MutableArray.c:195
size_t count
The count of elements.
Definition: Set.h:77

◆ allObjects_enumerator()

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

SetEnumerator for allObjects.

Definition at line 134 of file Set.c.

134 {
136}
#define obj
void addObject(MutableArray *self, const ident obj)
Adds the specified Object to this MutableArray.
Definition: MutableArray.c:99
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75

◆ containsObject()

static _Bool containsObject ( const Set self,
const ident  obj 
)
static

Definition at line 155 of file Set.c.

155 {
156
157 if (self->capacity) {
158 const size_t bin = HashForObject(HASH_SEED, obj) % self->capacity;
159
160 const Array *array = self->elements[bin];
161 if (array) {
162 return $(array, containsObject, obj);
163 }
164 }
165
166 return false;
167}
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
_Bool containsObject(const Array *self, const ident obj)
Definition: Array.c:208
MutableArray * array(void)
Returns a new MutableArray.
Definition: MutableArray.c:153

◆ containsObjectMatching()

static _Bool containsObjectMatching ( const Set self,
Predicate  predicate,
ident  data 
)
static

Definition at line 173 of file Set.c.

173 {
174
175 assert(predicate);
176
177 for (size_t i = 0; i < self->capacity; i++) {
178
179 Array *array = self->elements[i];
180 if (array) {
181
182 for (size_t j = 0; j < array->count; j++) {
183 if (predicate($(array, objectAtIndex, j), data)) {
184 return true;
185 }
186 }
187 }
188 }
189
190 return false;
191}
ident objectAtIndex(const Array *self, int index)
size_t count
The count of elements.
Definition: Array.h:72

◆ copy()

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

Definition at line 41 of file Set.c.

41 {
42
43 const Set *this = (Set *) self;
44
45 Set *that = $(alloc(Set), initWithSet, this);
46
47 return (Object *) that;
48}
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Set * initWithSet(Set *self, const Set *set)
Initializes this Set to contain the Objects in set.
Definition: Set.c:303

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 53 of file Set.c.

53 {
54
55 Set *this = (Set *) self;
56
57 for (size_t i = 0; i < this->capacity; i++) {
58 release(this->elements[i]);
59 }
60
61 free(this->elements);
62
63 super(Object, self, dealloc);
64}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
#define super(type, obj, method,...)
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ description()

static String * description ( const Object self)
static
See also
Object::description(const Object *)

Definition at line 69 of file Set.c.

69 {
70
71 const Array *array = $((Set *) self, allObjects);
72
73 return $((Object *) array, description);
74}
Array * allObjects(const Dictionary *self)
Definition: Dictionary.c:189
String * description(const Object *self)
Definition: Array.c:66

◆ enumerateObjects()

static void enumerateObjects ( const Set self,
SetEnumerator  enumerator,
ident  data 
)
static

Definition at line 197 of file Set.c.

197 {
198
199 assert(enumerator);
200
201 for (size_t i = 0; i < self->capacity; i++) {
202
203 Array *array = self->elements[i];
204 if (array) {
205
206 for (size_t j = 0; j < array->count; j++) {
207 enumerator(self, $(array, objectAtIndex, j), data);
208 }
209 }
210 }
211}

◆ filteredSet()

static Set * filteredSet ( const Set self,
Predicate  predicate,
ident  data 
)
static

Definition at line 217 of file Set.c.

217 {
218
219 assert(predicate);
220
222
223 for (size_t i = 0; i < self->capacity; i++) {
224
225 Array *array = self->elements[i];
226 if (array) {
227
228 for (size_t j = 0; j < array->count; j++) {
229 ident obj = $(array, objectAtIndex, j);
230
231 if (predicate(obj, data)) {
232 $(set, addObject, obj);
233 }
234 }
235 }
236 }
237
238 return (Set *) set;
239}
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Mutable sets.
Definition: MutableSet.h:40
MutableSet * set(void)
Returns a new MutableSet.
Definition: MutableSet.c:259

◆ hash()

static int hash ( const Object self)
static
See also
Object::hash(const Object *)

Definition at line 79 of file Set.c.

79 {
80
81 const Set *this = (Set *) self;
82
83 int hash = HashForInteger(HASH_SEED, this->count);
84
85 for (size_t i = 0; i < this->capacity; i++) {
86 if (this->elements[i]) {
87 hash = HashForObject(hash, this->elements[i]);
88 }
89 }
90
91 return hash;
92}
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition: Hash.c:62
int hash(const Object *self)
Definition: Array.c:80

◆ initialize()

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

Definition at line 426 of file Set.c.

426 {
427
428 ((ObjectInterface *) clazz->interface)->copy = copy;
429 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
430 ((ObjectInterface *) clazz->interface)->description = description;
431 ((ObjectInterface *) clazz->interface)->hash = hash;
432 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
433
434 ((SetInterface *) clazz->interface)->allObjects = allObjects;
435 ((SetInterface *) clazz->interface)->containsObject = containsObject;
436 ((SetInterface *) clazz->interface)->containsObjectMatching = containsObjectMatching;
437 ((SetInterface *) clazz->interface)->enumerateObjects = enumerateObjects;
438 ((SetInterface *) clazz->interface)->filteredSet = filteredSet;
439 ((SetInterface *) clazz->interface)->initWithArray = initWithArray;
440 ((SetInterface *) clazz->interface)->initWithSet = initWithSet;
441 ((SetInterface *) clazz->interface)->initWithObjects = initWithObjects;
442 ((SetInterface *) clazz->interface)->mappedSet = mappedSet;
443 ((SetInterface *) clazz->interface)->mutableCopy = mutableCopy;
444 ((SetInterface *) clazz->interface)->reduce = reduce;
445 ((SetInterface *) clazz->interface)->setWithArray = setWithArray;
446 ((SetInterface *) clazz->interface)->setWithObjects = setWithObjects;
447 ((SetInterface *) clazz->interface)->setWithSet = setWithSet;
448}
Array * initWithObjects(Array *self,...)
Initializes this Array to contain the Objects in the NULL-terminated arguments list.
Definition: Array.c:312
ident reduce(const Array *self, Reducer reducer, ident accumulator, ident data)
Definition: Array.c:405
MutableArray * mutableCopy(const Array *self)
Definition: Array.c:381
Array * initWithArray(Array *self, const Array *array)
Initializes this Array to contain the Objects in array.
Definition: Array.c:288
ident interface
The interface of the Class.
Definition: Class.h:105
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
_Bool isEqual(const Object *self, const Object *other)
Tests equality of the other Object.
Definition: Array.c:96
Set * setWithSet(const Set *set)
Returns a new Set with the contents of set.
Definition: Set.c:416
Set * initWithObjects(Set *self,...)
Initializes this Set with the specified objects.
Definition: Set.c:268
Set * mappedSet(const Set *self, Functor functor, ident data)
Transforms the elements in this Set by functor.
Definition: Set.c:319
Set * initWithArray(Set *self, const Array *array)
Initializes this Set to contain the Objects in array.
Definition: Set.c:252
MutableSet * mutableCopy(const Set *self)
Definition: Set.c:349
Set * setWithObjects(ident obj,...)
Returns a new Set containing the specified Objects.
Definition: Set.c:393
Set * filteredSet(const Set *self, Predicate predicate, ident data)
Creates a new Set with elements that pass predicate.
Definition: Set.c:217
_Bool containsObjectMatching(const Set *self, Predicate predicate, ident data)
Definition: Set.c:173
Set * setWithArray(const Array *array)
Returns a new Set with the contents of array.
Definition: Set.c:384

◆ initWithArray()

static Set * initWithArray ( Set self,
const Array array 
)
static

Definition at line 252 of file Set.c.

252 {
253
254 self = (Set *) super(Object, self, init);
255 if (self) {
256 if (array) {
258 }
259 }
260
261 return self;
262}
static void initWithArray_enumerator(const Array *array, ident obj, ident data)
ArrayEnumerator for initWithArray.
Definition: Set.c:244

◆ initWithArray_enumerator()

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

ArrayEnumerator for initWithArray.

Definition at line 244 of file Set.c.

244 {
246}

◆ initWithObjects()

static Set * initWithObjects ( Set self,
  ... 
)
static

Definition at line 268 of file Set.c.

268 {
269
270 self = (Set *) super(Object, self, init);
271 if (self) {
272
273 va_list args;
274 va_start(args, self);
275
276 while (true) {
277
278 ident obj = va_arg(args, ident);
279 if (obj) {
280 $$(MutableSet, addObject, (MutableSet *) self, obj);
281 } else {
282 break;
283 }
284 }
285
286 va_end(args);
287 }
288
289 return self;
290}

◆ initWithSet()

static Set * initWithSet ( Set self,
const Set set 
)
static

Definition at line 303 of file Set.c.

303 {
304
305 self = (Set *) super(Object, self, init);
306 if (self) {
307 if (set) {
309 }
310 }
311
312 return self;
313}
static void initWithSet_enumerator(const Set *set, ident obj, ident data)
SetEnumerator for initWithSet.
Definition: Set.c:295

◆ initWithSet_enumerator()

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

SetEnumerator for initWithSet.

Definition at line 295 of file Set.c.

295 {
297}

◆ isEqual()

static _Bool isEqual ( const Object self,
const Object other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 97 of file Set.c.

97 {
98
99 if (super(Object, self, isEqual, other)) {
100 return true;
101 }
102
103 if (other && (self->clazz == other->clazz)) {
104
105 const Set *this = (Set *) self;
106 const Set *that = (Set *) other;
107
108 if (this->count == that->count) {
109
110 Array *objects = $(this, allObjects);
111
112 for (size_t i = 0; i < objects->count; i++) {
113 const ident obj = $(objects, objectAtIndex, i);
114
115 if ($(that, containsObject, obj) == false) {
116 release(objects);
117 return false;
118 }
119 }
120
121 release(objects);
122 return true;
123 }
124 }
125
126 return false;
127}
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51

◆ mappedSet()

static Set * mappedSet ( const Set self,
Functor  functor,
ident  data 
)
static

Definition at line 319 of file Set.c.

319 {
320
321 assert(functor);
322
324 assert(set);
325
326 for (size_t i = 0; i < self->capacity; i++) {
327
328 const Array *array = self->elements[i];
329 if (array) {
330
331 for (size_t j = 0; j < array->count; j++) {
332
333 ident obj = functor(array->elements[j], data);
334
335 $(set, addObject, obj);
336
337 release(obj);
338 }
339 }
340 }
341
342 return (Set *) set;
343}

◆ mutableCopy()

static MutableSet * mutableCopy ( const Set self)
static

Definition at line 349 of file Set.c.

349 {
350
352 assert(copy);
353
354 $(copy, addObjectsFromSet, self);
355 return copy;
356}
void addObjectsFromSet(MutableSet *self, const Set *set)
Adds the Objects contained in set to this Set.
Definition: MutableSet.c:144

◆ reduce()

static ident reduce ( const Set self,
Reducer  reducer,
ident  accumulator,
ident  data 
)
static

Definition at line 362 of file Set.c.

362 {
363
364 assert(reducer);
365
366 for (size_t i = 0; i < self->capacity; i++) {
367
368 const Array *array = self->elements[i];
369 if (array) {
370
371 for (size_t j = 0; j < array->count; j++) {
372 accumulator = reducer(array->elements[j], accumulator, data);
373 }
374 }
375 }
376
377 return accumulator;
378}

◆ setWithArray()

static Set * setWithArray ( const Array array)
static

Definition at line 384 of file Set.c.

384 {
385
386 return $(alloc(Set), initWithArray, array);
387}

◆ setWithObjects()

static Set * setWithObjects ( ident  obj,
  ... 
)
static

Definition at line 393 of file Set.c.

393 {
394
395 Set *set = (Set *) $((Object *) alloc(Set), init);
396 if (set) {
397
398 va_list args;
399 va_start(args, obj);
400
401 while (obj) {
403 obj = va_arg(args, ident);
404 }
405
406 va_end(args);
407 }
408
409 return set;
410}

◆ setWithSet()

static Set * setWithSet ( const Set set)
static

Definition at line 416 of file Set.c.

416 {
417
418 return $(alloc(Set), initWithSet, set);
419}