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

#include <MutableSet.h>

Overview

Mutable sets.

Definition at line 40 of file MutableSet.h.

Inheritance diagram for MutableSet:
Set Object

Properties

Set set
 The superclass. More...
 
- Properties inherited from Set
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_MutableSet (void)
 The MutableSet archetype. More...
 
void addObject (MutableSet *self, const ident obj)
 Adds the specified Object to this Set. More...
 
void addObjectsFromArray (MutableSet *self, const Array *array)
 Adds the Objects contained in array to this Set. More...
 
void addObjectsFromSet (MutableSet *self, const Set *set)
 Adds the Objects contained in set to this Set. More...
 
void filter (MutableSet *self, Predicate predicate, ident data)
 Filters this MutableSet in place using predicate. More...
 
MutableSetinit (MutableSet *self)
 Initializes this MutableSet. More...
 
MutableSetinitWithCapacity (MutableSet *self, size_t capacity)
 
MutableSetinitWithCapacity (MutableSet *self, size_t capacity)
 Initializes this Set with the specified capacity. More...
 
void removeAllObjects (MutableSet *self)
 Removes all Objects from this Set. More...
 
void removeObject (MutableSet *self, const ident obj)
 Removes the specified Object from this Set. More...
 
MutableSetset (void)
 Returns a new MutableSet. More...
 
MutableSetsetWithCapacity (size_t capacity)
 Returns a new MutableSet with the given capacity. More...
 
- Methods inherited from Set
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

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

Property Details

◆ interface

MutableSetInterface* MutableSet::interface
protected

The interface.

Definition at line 51 of file MutableSet.h.

◆ set

MutableSet * set ( void  )

The superclass.

Definition at line 45 of file MutableSet.h.

Method Details

◆ _MutableSet()

Class * _MutableSet ( void  )

The MutableSet archetype.

Returns
The MutableSet Class.

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
MutableSetInterface * interface
The interface.
Definition: MutableSet.h:51
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Set(void)
The Set archetype.
Definition: Set.c:454

◆ addObject()

void addObject ( MutableSet self,
const ident  obj 
)

Adds the specified Object to this Set.

Parameters
selfThe MutableSet.
objAn Object.

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 MutableArray * array(void)
Definition: MutableArray.c:153
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
Mutable arrays.
Definition: MutableArray.h:40
void addObject(MutableSet *self, const ident obj)
Adds the specified Object to this Set.
Definition: MutableSet.c:96
MutableSet * init(MutableSet *self)
Initializes this MutableSet.
Definition: MutableSet.c:182
Set set
The superclass.
Definition: MutableSet.h:45
Immutable sets.
Definition: Set.h:55
size_t count
The count of elements.
Definition: Set.h:77
_Bool containsObject(const Set *self, const ident obj)
Definition: Set.c:155

◆ addObjectsFromArray()

void addObjectsFromArray ( MutableSet self,
const Array array 
)

Adds the Objects contained in array to this Set.

Parameters
selfThe MutableSet.
arrayAn Array.

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 Set *self, SetEnumerator enumerator, ident data)
Enumerate the elements of this Set with the given function.
Definition: Set.c:197

◆ addObjectsFromSet()

void addObjectsFromSet ( MutableSet self,
const Set set 
)

Adds the Objects contained in set to this Set.

Parameters
selfThe MutableSet.
setA Set.

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

◆ filter()

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

Filters this MutableSet in place using predicate.

Parameters
selfThe MutableSet.
predicateA Predicate.
dataUser data.

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}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
static MutableData * data(void)
Definition: MutableData.c:75
size_t count
The count of elements.
Definition: Array.h:72
Array array
The superclass.
Definition: MutableArray.h:45
void filter(MutableSet *self, Predicate predicate, ident data)
Filters this MutableSet in place using predicate.
Definition: MutableSet.c:155

◆ init()

MutableSet * init ( MutableSet self)

Initializes this MutableSet.

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

Definition at line 182 of file MutableSet.c.

182 {
183
185}
#define MUTABLESET_DEFAULT_CAPACITY
Definition: MutableSet.c:34
MutableSet * initWithCapacity(MutableSet *self, size_t capacity)
Definition: MutableSet.c:191

◆ initWithCapacity() [1/2]

MutableSet * initWithCapacity ( MutableSet self,
size_t  capacity 
)

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,...)
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ initWithCapacity() [2/2]

MutableSet * initWithCapacity ( MutableSet self,
size_t  capacity 
)

Initializes this Set with the specified capacity.

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

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}

◆ removeAllObjects()

void removeAllObjects ( MutableSet self)

Removes all Objects from this Set.

Parameters
selfThe MutableSet.

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

void removeObject ( MutableSet self,
const ident  obj 
)

Removes the specified Object from this Set.

Parameters
selfThe MutableSet.
objThe Object to remove.

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}
static ssize_t indexOfObject(const Array *self, const ident obj)
Definition: Array.c:271
static void removeObjectAtIndex(MutableArray *self, size_t index)
Definition: MutableArray.c:282

◆ set()

MutableSet * set ( void  )

Returns a new MutableSet.

Returns
The new MutableSet, or NULL on error.

Definition at line 259 of file MutableSet.c.

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

◆ setWithCapacity()

MutableSet * setWithCapacity ( size_t  capacity)

Returns a new MutableSet with the given capacity.

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

Definition at line 268 of file MutableSet.c.

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

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