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

#include <IndexSet.h>

Overview

Collections of unique index values.

Definition at line 41 of file IndexSet.h.

Inheritance diagram for IndexSet:
Object

Properties

size_t count
 The count of indexes.
 
size_t * indexes
 The indexes.
 
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_IndexSet (void)
 The IndexSet archetype.
 
void addIndex (IndexSet *self, size_t index)
 Adds the specified index to this IndexSet.
 
void addIndexes (IndexSet *self, size_t *indexes, size_t count)
 Adds the specified indexes to this IndexSet.
 
void addIndexesInRange (IndexSet *self, const Range range)
 Adds indexes in the specified Range to this IndexSet.
 
bool containsIndex (const IndexSet *self, size_t index)
 
IndexSetinit (IndexSet *self)
 Initializes this IndexSet.
 
IndexSetinitWithCapacity (IndexSet *self, size_t capacity)
 Initializes this IndexSet with the specified capacity.
 
IndexSetinitWithIndex (IndexSet *self, size_t index)
 Initializes this IndexSet with the specified index.
 
IndexSetinitWithIndexes (IndexSet *self, size_t *indexes, size_t count)
 Initializes this IndexSet with the specified indexes and count.
 
void removeAllIndexes (IndexSet *self)
 Removes all indexes from this IndexSet.
 
void removeIndex (IndexSet *self, size_t index)
 Removes the specified index from this IndexSet.
 
void removeIndexes (IndexSet *self, size_t *indexes, size_t count)
 Removes the specified indexes from this IndexSet.
 
void removeIndexesInRange (IndexSet *self, const Range range)
 Removes indexes in the specified Range from this IndexSet.
 
- 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

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

Property Details

◆ count

size_t IndexSet::count

The count of indexes.

Definition at line 62 of file IndexSet.h.

◆ indexes

size_t* IndexSet::indexes

The indexes.

Definition at line 57 of file IndexSet.h.

◆ interface

IndexSetInterface* IndexSet::interface
protected

The interface.

Definition at line 52 of file IndexSet.h.

◆ object

Object IndexSet::object

The superclass.

Definition at line 46 of file IndexSet.h.

Method Details

◆ _IndexSet()

Class * _IndexSet ( void  )

The IndexSet archetype.

Returns
The IndexSet Class.

Definition at line 370 of file IndexSet.c.

370 {
371 static Class *clazz;
372 static Once once;
373
374 do_once(&once, {
375 clazz = _initialize(&(const ClassDef) {
376 .name = "IndexSet",
377 .superclass = _Object(),
378 .instanceSize = sizeof(IndexSet),
379 .interfaceOffset = offsetof(IndexSet, interface),
380 .interfaceSize = sizeof(IndexSetInterface),
382 });
383 });
384
385 return clazz;
386}
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
Collections of unique index values.
Definition IndexSet.h:41
IndexSetInterface * interface
The interface.
Definition IndexSet.h:52
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

◆ addIndex()

void addIndex ( IndexSet self,
size_t  index 
)

Adds the specified index to this IndexSet.

Parameters
selfThe IndexSet.
indexThe index to add.

Definition at line 205 of file IndexSet.c.

205 {
206
207 IndexSet *this = self;
208
209 size_t i;
210 for (i = 0; i < this->count; i++) {
211 if (this->indexes[i] == index) {
212 return;
213 }
214 if (this->indexes[i] > index) {
215 break;
216 }
217 }
218
219 if (this->count == self->capacity) {
220 self->capacity += INDEX_SET_CHUNK_SIZE;
221
222 this->indexes = realloc(this->indexes, self->capacity * sizeof(size_t));
223 assert(this->indexes);
224 }
225
226 for (size_t j = this->count; j > i; j--) {
227 this->indexes[j] = this->indexes[j - 1];
228 }
229
230 this->indexes[i] = index;
231 this->count++;
232}
#define INDEX_SET_CHUNK_SIZE
Definition IndexSet.c:67
size_t * indexes
The indexes.
Definition IndexSet.h:57
size_t count
The count of indexes.
Definition IndexSet.h:62

◆ addIndexes()

void addIndexes ( IndexSet self,
size_t *  indexes,
size_t  count 
)

Adds the specified indexes to this IndexSet.

Parameters
selfThe IndexSet.
indexesThe indexes to add.
countThe count of indexes.

Definition at line 238 of file IndexSet.c.

238 {
239
240 for (size_t i = 0; i < count; i++) {
241 $(self, addIndex, indexes[i]);
242 }
243}
void addIndex(IndexSet *self, size_t index)
Adds the specified index to this IndexSet.
Definition IndexSet.c:205

◆ addIndexesInRange()

void addIndexesInRange ( IndexSet self,
const Range  range 
)

Adds indexes in the specified Range to this IndexSet.

Parameters
selfThe IndexSet.
rangeThe Range of indexes to add.

Definition at line 252 of file IndexSet.c.

252 {
253
254 for (size_t i = range.location; i < range.location + range.length; i++) {
255 $(self, addIndex, i);
256 }
257}
ssize_t location
The location.
Definition Types.h:59
size_t length
The length.
Definition Types.h:64

◆ containsIndex()

bool containsIndex ( const IndexSet self,
size_t  index 
)
Parameters
selfThe IndexSet.
indexThe index.
Returns
True if this IndexSet contains index, false otherwise.

Definition at line 157 of file IndexSet.c.

157 {
158
159 for (size_t i = 0; i < self->count; i++) {
160 if (self->indexes[i] == index) {
161 return true;
162 }
163 }
164
165 return false;
166}

◆ init()

IndexSet * init ( IndexSet self)

Initializes this IndexSet.

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

Definition at line 263 of file IndexSet.c.

263 {
264 return $(self, initWithCapacity, INDEX_SET_CHUNK_SIZE);
265}
IndexSet * initWithCapacity(IndexSet *self, size_t capacity)
Initializes this IndexSet with the specified capacity.
Definition IndexSet.c:271

◆ initWithCapacity()

IndexSet * initWithCapacity ( IndexSet self,
size_t  capacity 
)

Initializes this IndexSet with the specified capacity.

Parameters
selfThe IndexSet.
capacityThe capacity.
Returns
The initialized IndexSet, or NULL on error.

Definition at line 271 of file IndexSet.c.

271 {
272
273 self = (IndexSet *) super(Object, self, init);
274 if (self) {
275 self->capacity = capacity;
276
277 self->indexes = malloc(self->capacity * sizeof(size_t));
278 assert(self->indexes);
279 }
280
281 return self;
282}
#define super(type, obj, method,...)
IndexSet * init(IndexSet *self)
Initializes this IndexSet.
Definition IndexSet.c:263
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ initWithIndex()

IndexSet * initWithIndex ( IndexSet self,
size_t  index 
)

Initializes this IndexSet with the specified index.

Parameters
selfThe IndexSet.
indexThe index.
Returns
The intialized IndexSet, or NULL on error.

Definition at line 172 of file IndexSet.c.

172 {
173 return $(self, initWithIndexes, &index, 1);
174}
IndexSet * initWithIndexes(IndexSet *self, size_t *indexes, size_t count)
Initializes this IndexSet with the specified indexes and count.
Definition IndexSet.c:180

◆ initWithIndexes()

IndexSet * initWithIndexes ( IndexSet self,
size_t *  indexes,
size_t  count 
)

Initializes this IndexSet with the specified indexes and count.

Parameters
selfThe IndexSet.
indexesThe indexes.
countThe count of indexes.
Returns
The intialized IndexSet, or NULL on error.

Definition at line 180 of file IndexSet.c.

180 {
181
182 self = (IndexSet *) super(Object, self, init);
183 if (self) {
184
185 self->count = compact(indexes, count);
186 self->capacity = self->count;
187 if (self->count) {
188
189 self->indexes = calloc(sizeof(size_t), self->count);
190 assert(self->indexes);
191
192 memcpy(self->indexes, indexes, sizeof(size_t) * self->count);
193 }
194 }
195
196 return self;
197}
static size_t compact(size_t *indexes, size_t count)
Sorts and compacts the given array to contain only unique values.
Definition IndexSet.c:46

◆ removeAllIndexes()

void removeAllIndexes ( IndexSet self)

Removes all indexes from this IndexSet.

Parameters
selfThe IndexSet.

Definition at line 288 of file IndexSet.c.

288 {
289
290 IndexSet *this = self;
291
292 free(this->indexes);
293 this->indexes = NULL;
294
295 this->count = 0;
296 self->capacity = 0;
297}

◆ removeIndex()

void removeIndex ( IndexSet self,
size_t  index 
)

Removes the specified index from this IndexSet.

Parameters
selfThe IndexSet.
indexThe index to remove.

Definition at line 303 of file IndexSet.c.

303 {
304
305 IndexSet *this = self;
306 for (size_t i = 0; i < this->count; i++) {
307 if (this->indexes[i] == index) {
308 this->count--;
309 for (size_t j = i; j < this->count; j++) {
310 this->indexes[j] = this->indexes[j + 1];
311 }
312 return;
313 }
314 }
315}

◆ removeIndexes()

void removeIndexes ( IndexSet self,
size_t *  indexes,
size_t  count 
)

Removes the specified indexes from this IndexSet.

Parameters
selfThe IndexSet.
indexesThe indexes to remove.
countThe count of indexes.

Definition at line 321 of file IndexSet.c.

321 {
322
323 for (size_t i = 0; i < count; i++) {
324 $(self, removeIndex, indexes[i]);
325 }
326}
void removeIndex(IndexSet *self, size_t index)
Removes the specified index from this IndexSet.
Definition IndexSet.c:303

◆ removeIndexesInRange()

void removeIndexesInRange ( IndexSet self,
const Range  range 
)

Removes indexes in the specified Range from this IndexSet.

Parameters
selfThe IndexSet.
rangeThe Range of indexes to remove.

Definition at line 332 of file IndexSet.c.

332 {
333
334 for (size_t i = range.location; i < range.location + range.length; i++) {
335 $(self, removeIndex, i);
336 }
337}

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