Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
Set Struct Reference

#include <Set.h>

Overview

Immutable sets.

Definition at line 55 of file Set.h.

Inheritance diagram for Set:
Object MutableSet

Properties

size_t count
 The count of elements. More...
 
Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_Set (void)
 The Set archetype. More...
 
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. More...
 
SetfilteredSet (const Set *self, Predicate predicate, ident data)
 Creates a new Set with elements that pass predicate. More...
 
SetinitWithArray (Set *self, const Array *array)
 Initializes this Set to contain the Objects in array. More...
 
SetinitWithObjects (Set *self,...)
 Initializes this Set with the specified objects. More...
 
SetinitWithSet (Set *self, const Set *set)
 Initializes this Set to contain the Objects in set. More...
 
SetmappedSet (const Set *self, Functor functor, ident data)
 Transforms the elements in this Set by functor. More...
 
MutableSetmutableCopy (const Set *self)
 
ident reduce (const Set *self, Reducer reducer, ident accumulator, ident data)
 
SetsetWithArray (const Array *array)
 Returns a new Set with the contents of array. More...
 
SetsetWithObjects (ident obj,...)
 Returns a new Set containing the specified Objects. More...
 
SetsetWithSet (const Set *set)
 Returns a new Set with the contents of set. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

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

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 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 * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Immutable sets.
Definition: Set.h:55
SetInterface * interface
The interface.
Definition: Set.h:66

◆ allObjects()

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

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 MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Definition: MutableArray.c:195
static void allObjects_enumerator(const Set *set, ident obj, ident data)
SetEnumerator for allObjects.
Definition: Set.c:134
Immutable arrays.
Definition: Array.h:56
Mutable arrays.
Definition: MutableArray.h:40
size_t count
The count of elements.
Definition: Set.h:77
void enumerateObjects(const Set *self, SetEnumerator enumerator, ident data)
Enumerate the elements of this Set with the given function.
Definition: Set.c:197

◆ 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 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}
#define obj
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 MutableArray * array(void)
Definition: MutableArray.c:153
_Bool containsObject(const Set *self, const ident obj)
Definition: Set.c:155

◆ 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 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}
static ident objectAtIndex(const Array *self, size_t index)
Definition: Array.c:394
static MutableData * data(void)
Definition: MutableData.c:75
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 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()

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 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}
static void addObject(MutableArray *self, const ident obj)
Definition: MutableArray.c:99
static MutableSet * set(void)
Definition: MutableSet.c:259
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Mutable sets.
Definition: MutableSet.h:40
Object * init(Object *self)
Initializes this Object.
Definition: Object.c:83

◆ 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 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}
#define super(type, obj, method,...)
static void initWithArray_enumerator(const Array *array, ident obj, ident data)
ArrayEnumerator for initWithArray.
Definition: Set.c:244
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ 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 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()

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 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

◆ 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 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}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196

◆ mutableCopy()

MutableSet * mutableCopy ( const Set self)
Parameters
selfThe Set.
Returns
A MutableSet with the contents of this Set.

Definition at line 349 of file Set.c.

349 {
350
352 assert(copy);
353
354 $(copy, addObjectsFromSet, self);
355 return copy;
356}
static void addObjectsFromSet(MutableSet *self, const Set *set)
Definition: MutableSet.c:144
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40

◆ 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 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()

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 384 of file Set.c.

384 {
385
386 return $(alloc(Set), initWithArray, array);
387}
Set * initWithArray(Set *self, const Array *array)
Initializes this Set to contain the Objects in array.
Definition: Set.c:252

◆ 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 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()

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 416 of file Set.c.

416 {
417
418 return $(alloc(Set), initWithSet, set);
419}
Set * initWithSet(Set *self, const Set *set)
Initializes this Set to contain the Objects in set.
Definition: Set.c:303

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