Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Set Struct Reference

#include <Set.h>

Overview

Sets.

Definition at line 55 of file Set.h.

Inheritance diagram for Set:
Object

Properties

size_t count
 The count of elements.
 
Object object
 The superclass.
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class_Set (void)
 The Set archetype.
 
void addObject (Set *self, const ident obj)
 Adds the specified Object to this Set.
 
void addObjectsFromArray (Set *self, const Array *array)
 Adds the Objects contained in array to this Set.
 
void addObjectsFromSet (Set *self, const Set *set)
 Adds the Objects contained in set to this Set.
 
ArrayallObjects (const Set *self)
 
bool containsObject (const Set *self, const ident obj)
 
bool containsObjectMatching (const Set *self, Predicate predicate, ident data)
 
void enumerateObjects (const Set *self, SetEnumerator enumerator, ident data)
 Enumerate the elements of this Set with the given function.
 
void filter (Set *self, Predicate predicate, ident data)
 Filters this Set in place using predicate.
 
SetfilteredSet (const Set *self, Predicate predicate, ident data)
 Creates a new Set with elements that pass predicate.
 
Setinit (Set *self)
 Initializes this Set.
 
SetinitWithArray (Set *self, const Array *array)
 Initializes this Set to contain the Objects in array.
 
SetinitWithCapacity (Set *self, size_t capacity)
 Initializes this Set with the specified capacity.
 
SetinitWithObjects (Set *self,...)
 Initializes this Set with the specified objects.
 
SetinitWithSet (Set *self, const Set *set)
 Initializes this Set to contain the Objects in set.
 
SetmappedSet (const Set *self, Functor functor, ident data)
 Transforms the elements in this Set by functor.
 
ident reduce (const Set *self, Reducer reducer, ident accumulator, ident data)
 
void removeAllObjects (Set *self)
 Removes all Objects from this Set.
 
void removeObject (Set *self, const ident obj)
 Removes the specified Object from this Set.
 
Setset (void)
 Returns a new Set.
 
SetsetWithArray (const Array *array)
 Returns a new Set with the contents of array.
 
SetsetWithCapacity (size_t capacity)
 Returns a new Set with the given capacity.
 
SetsetWithObjects (ident obj,...)
 Returns a new Set containing the specified Objects.
 
SetsetWithSet (const Set *set)
 Returns a new Set with the contents of set.
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

SetInterface * interface
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

Property Details

◆ count

size_t Set::count

The count of elements.

Definition at line 77 of file Set.h.

◆ interface

SetInterface* Set::interface
protected

The interface.

Definition at line 66 of file Set.h.

◆ object

Object Set::object

The superclass.

Definition at line 60 of file Set.h.

Method Details

◆ _Set()

Class * _Set ( void  )

The Set archetype.

Returns
The Set Class.

Definition at line 669 of file Set.c.

669 {
670 static Class *clazz;
671 static Once once;
672
673 do_once(&once, {
674 clazz = _initialize(&(const ClassDef) {
675 .name = "Set",
676 .superclass = _Object(),
677 .instanceSize = sizeof(Set),
678 .interfaceOffset = offsetof(Set, interface),
679 .interfaceSize = sizeof(SetInterface),
681 });
682 });
683
684 return clazz;
685}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
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 * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136
Sets.
Definition Set.h:55
SetInterface * interface
The interface.
Definition Set.h:66

◆ addObject()

void addObject ( Set self,
const ident  obj 
)

Adds the specified Object to this Set.

Parameters
selfThe Set.
objAn Object.

Definition at line 175 of file Set.c.

175 {
176
177 addObject_resize(self);
178
179 const size_t bin = HashForObject(HASH_SEED, obj) % self->capacity;
180
181 Array *array = self->elements[bin];
182 if (array == NULL) {
183 array = self->elements[bin] = $(alloc(Array), init);
184 }
185
186 if ($((Array *) array, containsObject, obj) == false) {
187 $(array, addObject, obj);
188 self->count++;
189 }
190}
static Array * array(void)
Definition Array.c:234
#define obj
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Definition Hash.c:72
#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 Set.c:139
Arrays.
Definition Array.h:56
size_t count
The count of elements.
Definition Set.h:77
Set * init(Set *self)
Initializes this Set.
Definition Set.c:369
void addObject(Set *self, const ident obj)
Adds the specified Object to this Set.
Definition Set.c:175
bool containsObject(const Set *self, const ident obj)
Definition Set.c:252

◆ addObjectsFromArray()

void addObjectsFromArray ( Set self,
const Array array 
)

Adds the Objects contained in array to this Set.

Parameters
selfThe Set.
arrayAn Array.

Definition at line 203 of file Set.c.

203 {
204
205 if (array) {
207 }
208}
static void enumerate(const Array *self, ArrayEnumerator enumerator, ident data)
Definition Array.c:334
static void addObjectsFromArray_enumerator(const Array *array, ident obj, ident data)
ArrayEnumerator for addObjectsFromArray.
Definition Set.c:195

◆ addObjectsFromSet()

void addObjectsFromSet ( Set self,
const Set set 
)

Adds the Objects contained in set to this Set.

Parameters
selfThe Set.
setA Set.

Definition at line 221 of file Set.c.

221 {
222
223 if (set) {
225 }
226}
static void addObjectsFromSet_enumerator(const Set *set, ident obj, ident data)
SetEnumerator for addObjectsFromSet.
Definition Set.c:213
void enumerateObjects(const Set *self, SetEnumerator enumerator, ident data)
Enumerate the elements of this Set with the given function.
Definition Set.c:294
Set * set(void)
Returns a new Set.
Definition Set.c:572

◆ allObjects()

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

Definition at line 239 of file Set.c.

239 {
240
241 Array *objects = $(alloc(Array), initWithCapacity, self->count);
242
243 $(self, enumerateObjects, allObjects_enumerator, objects);
244
245 return objects;
246}
static void allObjects_enumerator(const Dictionary *dict, ident obj, ident key, ident data)
DictionaryEnumerator for allObjects.
Definition Dictionary.c:205
Set * initWithCapacity(Set *self, size_t capacity)
Initializes this Set with the specified capacity.
Definition Set.c:401

◆ containsObject()

bool containsObject ( const Set self,
const ident  obj 
)
Parameters
selfThe Set.
objThe Object to check.
Returns
true if this Set contains the given Object, false otherwise.

Definition at line 252 of file Set.c.

252 {
253
254 if (self->capacity) {
255 const size_t bin = HashForObject(HASH_SEED, obj) % self->capacity;
256
257 const Array *array = self->elements[bin];
258 if (array) {
259 return $(array, containsObject, obj);
260 }
261 }
262
263 return false;
264}

◆ containsObjectMatching()

bool containsObjectMatching ( const Set self,
Predicate  predicate,
ident  data 
)
Parameters
selfThe Set.
predicateThe predicate function.
dataUser data.
Returns
true if this Set contains an Object matching predicate, false otherwise.

Definition at line 270 of file Set.c.

270 {
271
272 assert(predicate);
273
274 for (size_t i = 0; i < self->capacity; i++) {
275
276 Array *array = self->elements[i];
277 if (array) {
278
279 for (size_t j = 0; j < array->count; j++) {
280 if (predicate($(array, objectAtIndex, j), data)) {
281 return true;
282 }
283 }
284 }
285 }
286
287 return false;
288}
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
static Data * data(void)
Definition Data.c:286
size_t count
The count of elements.
Definition Array.h:72

◆ enumerateObjects()

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

Enumerate the elements of this Set with the given function.

Parameters
selfThe Set.
enumeratorThe enumerator function.
dataUser data.
Remarks
The enumerator should return true to break the iteration.

Definition at line 294 of file Set.c.

294 {
295
296 assert(enumerator);
297
298 for (size_t i = 0; i < self->capacity; i++) {
299
300 Array *array = self->elements[i];
301 if (array) {
302
303 for (size_t j = 0; j < array->count; j++) {
304 enumerator(self, $(array, objectAtIndex, j), data);
305 }
306 }
307 }
308}

◆ filter()

void filter ( Set self,
Predicate  predicate,
ident  data 
)

Filters this Set in place using predicate.

Parameters
selfThe Set.
predicateA Predicate.
dataUser data.

Definition at line 314 of file Set.c.

314 {
315
316 assert(predicate);
317
318 self->count = 0;
319
320 for (size_t i = 0; i < self->capacity; i++) {
321
322 Array *array = self->elements[i];
323 if (array) {
324
325 $(array, filter, predicate, data);
326
327 if (array->count == 0) {
328 release(array);
329 self->elements[i] = NULL;
330 } else {
331 self->count += array->count;
332 }
333 }
334 }
335}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
void filter(Set *self, Predicate predicate, ident data)
Filters this Set in place using predicate.
Definition Set.c:314

◆ filteredSet()

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

Creates a new Set with elements that pass predicate.

Parameters
selfThe Set.
predicateThe predicate function.
dataUser data.
Returns
The new, filtered Set.

Definition at line 341 of file Set.c.

341 {
342
343 assert(predicate);
344
345 Set *set = $(alloc(Set), init);
346
347 for (size_t i = 0; i < self->capacity; i++) {
348
349 Array *array = self->elements[i];
350 if (array) {
351
352 for (size_t j = 0; j < array->count; j++) {
353 ident obj = $(array, objectAtIndex, j);
354
355 if (predicate(obj, data)) {
356 $(set, addObject, obj);
357 }
358 }
359 }
360 }
361
362 return set;
363}
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49

◆ init()

Set * init ( Set self)

Initializes this Set.

Parameters
selfThe Set.
Returns
The initialized Set, or NULL on error.

Definition at line 369 of file Set.c.

369 {
370
371 return $(self, initWithCapacity, SET_DEFAULT_CAPACITY);
372}
#define SET_DEFAULT_CAPACITY
Definition Set.c:34

◆ initWithArray()

Set * initWithArray ( Set self,
const Array array 
)

Initializes this Set to contain the Objects in array.

Parameters
selfThe Set.
arrayAn Array.
Returns
The initialized Set, or NULL on error.

Definition at line 385 of file Set.c.

385 {
386
387 self = (Set *) super(Object, self, init);
388 if (self) {
389 if (array) {
391 }
392 }
393
394 return self;
395}
#define super(type, obj, method,...)
static void initWithArray_enumerator(const Array *array, ident obj, ident data)
ArrayEnumerator for initWithArray.
Definition Set.c:377
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ initWithCapacity()

Set * initWithCapacity ( Set self,
size_t  capacity 
)

Initializes this Set with the specified capacity.

Parameters
selfThe Set.
capacityThe desired initial capacity.
Returns
The initialized Set, or NULL on error.

Definition at line 401 of file Set.c.

401 {
402
403 self = (Set *) super(Object, self, init);
404 if (self) {
405
406 self->capacity = capacity;
407 if (self->capacity) {
408
409 self->elements = calloc(self->capacity, sizeof(ident));
410 assert(self->elements);
411 }
412 }
413
414 return self;
415}

◆ initWithObjects()

Set * initWithObjects ( Set self,
  ... 
)

Initializes this Set with the specified objects.

Parameters
selfThe Set.
Returns
The initialized Set, or NULL on error.

Definition at line 421 of file Set.c.

421 {
422
423 self = (Set *) super(Object, self, init);
424 if (self) {
425
426 va_list args;
427 va_start(args, self);
428
429 while (true) {
430
431 ident obj = va_arg(args, ident);
432 if (obj) {
433 $(self, addObject, obj);
434 } else {
435 break;
436 }
437 }
438
439 va_end(args);
440 }
441
442 return self;
443}

◆ initWithSet()

Set * initWithSet ( Set self,
const Set set 
)

Initializes this Set to contain the Objects in set.

Parameters
selfThe Set.
setA Set.
Returns
The initialized Set, or NULL on error.

Definition at line 456 of file Set.c.

456 {
457
458 self = (Set *) super(Object, self, init);
459 if (self) {
460 if (set) {
462 }
463 }
464
465 return self;
466}
static void initWithSet_enumerator(const Set *set, ident obj, ident data)
SetEnumerator for initWithSet.
Definition Set.c:448

◆ mappedSet()

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

Transforms the elements in this Set by functor.

Parameters
selfThe Set.
functorThe Functor.
dataUser data.
Returns
A Set containing the transformed elements of this Set.

Definition at line 472 of file Set.c.

472 {
473
474 assert(functor);
475
476 Set *set = $(alloc(Set), initWithCapacity, self->count);
477 assert(set);
478
479 for (size_t i = 0; i < self->capacity; i++) {
480
481 const Array *array = self->elements[i];
482 if (array) {
483
484 for (size_t j = 0; j < array->count; j++) {
485
486 ident obj = functor(array->elements[j], data);
487
488 $(set, addObject, obj);
489
490 release(obj);
491 }
492 }
493 }
494
495 return set;
496}

◆ reduce()

ident reduce ( const Set self,
Reducer  reducer,
ident  accumulator,
ident  data 
)
Parameters
selfThe Set.
reducerThe Reducer.
accumulatorThe initial accumulator value.
dataUser data.
Returns
The reduction result.

Definition at line 502 of file Set.c.

502 {
503
504 assert(reducer);
505
506 for (size_t i = 0; i < self->capacity; i++) {
507
508 const Array *array = self->elements[i];
509 if (array) {
510
511 for (size_t j = 0; j < array->count; j++) {
512 accumulator = reducer(array->elements[j], accumulator, data);
513 }
514 }
515 }
516
517 return accumulator;
518}

◆ removeAllObjects()

void removeAllObjects ( Set self)

Removes all Objects from this Set.

Parameters
selfThe Set.

Definition at line 524 of file Set.c.

524 {
525
526 for (size_t i = 0; i < self->capacity; i++) {
527
528 Array *array = self->elements[i];
529 if (array) {
530 release(array);
531 self->elements[i] = NULL;
532 }
533 }
534
535 self->count = 0;
536}

◆ removeObject()

void removeObject ( Set self,
const ident  obj 
)

Removes the specified Object from this Set.

Parameters
selfThe Set.
objThe Object to remove.

Definition at line 542 of file Set.c.

542 {
543
544 if (self->capacity == 0) {
545 return;
546 }
547
548 const size_t bin = HashForObject(HASH_SEED, obj) % self->capacity;
549
550 Array *array = self->elements[bin];
551 if (array) {
552
553 const ssize_t index = $(array, indexOfObject, obj);
554 if (index > -1) {
555
556 $(array, removeObjectAtIndex, index);
557
558 if (((Array *) array)->count == 0) {
559 release(array);
560 self->elements[bin] = NULL;
561 }
562
563 self->count--;
564 }
565 }
566}
static void removeObjectAtIndex(Array *self, size_t index)
Definition Array.c:654
static ssize_t indexOfObject(const Array *self, const ident obj)
Definition Array.c:403

◆ set()

Set * set ( void  )

Returns a new Set.

Returns
The new Set, or NULL on error.

Definition at line 572 of file Set.c.

572 {
573
574 return $(alloc(Set), init);
575}

◆ setWithArray()

Set * setWithArray ( const Array array)

Returns a new Set with the contents of array.

Parameters
arrayAn Array.
Returns
The new Set, or NULL on error.

Definition at line 581 of file Set.c.

581 {
582
583 return $(alloc(Set), initWithArray, array);
584}
Set * initWithArray(Set *self, const Array *array)
Initializes this Set to contain the Objects in array.
Definition Set.c:385

◆ setWithCapacity()

Set * setWithCapacity ( size_t  capacity)

Returns a new Set with the given capacity.

Parameters
capacityThe desired initial capacity.
Returns
The new Set, or NULL on error.

Definition at line 590 of file Set.c.

590 {
591
592 return $(alloc(Set), initWithCapacity, capacity);
593}

◆ setWithObjects()

Set * setWithObjects ( ident  obj,
  ... 
)

Returns a new Set containing the specified Objects.

Parameters
objA NULL-terminated list of Objects.
Returns
The new Set, or NULL on error.

Definition at line 599 of file Set.c.

599 {
600
601 Set *set = (Set *) $((Object *) alloc(Set), init);
602 if (set) {
603
604 va_list args;
605 va_start(args, obj);
606
607 while (obj) {
608 $(set, addObject, obj);
609 obj = va_arg(args, ident);
610 }
611
612 va_end(args);
613 }
614
615 return set;
616}

◆ setWithSet()

Set * setWithSet ( const Set set)

Returns a new Set with the contents of set.

Parameters
setA set.
Returns
The new Set, or NULL on error.

Definition at line 622 of file Set.c.

622 {
623
624 return $(alloc(Set), initWithSet, set);
625}
Set * initWithSet(Set *self, const Set *set)
Initializes this Set to contain the Objects in set.
Definition Set.c:456

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