Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
IndexSet.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "Hash.h"
#include "IndexSet.h"

Go to the source code of this file.

Macros

#define _Class   _IndexSet
 
#define INDEX_SET_CHUNK_SIZE   8
 

Functions

Class_IndexSet (void)
 
static void addIndex (IndexSet *self, size_t index)
 
static void addIndexes (IndexSet *self, size_t *indexes, size_t count)
 
static void addIndexesInRange (IndexSet *self, const Range range)
 
static size_t compact (size_t *indexes, size_t count)
 Sorts and compacts the given array to contain only unique values.
 
static int compare (const void *a, const void *b)
 qsort comparator for indexes.
 
static bool containsIndex (const IndexSet *self, size_t index)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static Stringdescription (const Object *self)
 
static int hash (const Object *self)
 
static IndexSetinit (IndexSet *self)
 
static void initialize (Class *clazz)
 
static IndexSetinitWithCapacity (IndexSet *self, size_t capacity)
 
static IndexSetinitWithIndex (IndexSet *self, size_t index)
 
static IndexSetinitWithIndexes (IndexSet *self, size_t *indexes, size_t count)
 
static bool isEqual (const Object *self, const Object *other)
 
static void removeAllIndexes (IndexSet *self)
 
static void removeIndex (IndexSet *self, size_t index)
 
static void removeIndexes (IndexSet *self, size_t *indexes, size_t count)
 
static void removeIndexesInRange (IndexSet *self, const Range range)
 

Macro Definition Documentation

◆ _Class

#define _Class   _IndexSet

Definition at line 65 of file IndexSet.c.

◆ INDEX_SET_CHUNK_SIZE

#define INDEX_SET_CHUNK_SIZE   8

Definition at line 67 of file IndexSet.c.

Function Documentation

◆ _IndexSet()

Class * _IndexSet ( void  )

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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
static void initialize(Class *clazz)
Definition IndexSet.c:344
Class * _Object(void)
Definition Object.c:136
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

◆ addIndex()

static void addIndex ( IndexSet self,
size_t  index 
)
static

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

◆ addIndexes()

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

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}
static void addIndex(IndexSet *self, size_t index)
Definition IndexSet.c:205

◆ addIndexesInRange()

static void addIndexesInRange ( IndexSet self,
const Range  range 
)
static

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

◆ compact()

static size_t compact ( size_t *  indexes,
size_t  count 
)
static

Sorts and compacts the given array to contain only unique values.

Definition at line 46 of file IndexSet.c.

46 {
47
48 size_t size = 0;
49
50 if (count) {
51 qsort(indexes, count, sizeof(size_t), compare);
52
53 for (size_t i = 1; i < count; i++) {
54 if (indexes[i] != indexes[size]) {
55 indexes[++size] = indexes[i];
56 }
57 }
58
59 size++;
60 }
61
62 return size;
63}
static int compare(const void *a, const void *b)
qsort comparator for indexes.
Definition IndexSet.c:35

◆ compare()

static int compare ( const void *  a,
const void *  b 
)
static

qsort comparator for indexes.

Definition at line 35 of file IndexSet.c.

35 {
36
37 const size_t sa = *(size_t *) a;
38 const size_t sb = *(size_t *) b;
39
40 return sa < sb ? -1 : sa > sb ? 1 : 0;
41}

◆ containsIndex()

static bool containsIndex ( const IndexSet self,
size_t  index 
)
static

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}
size_t * indexes
The indexes.
Definition IndexSet.h:57
size_t count
The count of indexes.
Definition IndexSet.h:62

◆ copy()

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

Definition at line 74 of file IndexSet.c.

74 {
75
76 IndexSet *this = (IndexSet *) self;
77 IndexSet *that = $(alloc(IndexSet), initWithIndexes, this->indexes, this->count);
78
79 return (Object *) that;
80}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
static IndexSet * initWithIndexes(IndexSet *self, size_t *indexes, size_t count)
Definition IndexSet.c:180
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ dealloc()

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

Definition at line 85 of file IndexSet.c.

85 {
86
87 IndexSet *this = (IndexSet *) self;
88
89 free(this->indexes);
90
91 super(Object, self, dealloc);
92}
#define super(type, obj, method,...)
static void dealloc(Object *self)
Definition IndexSet.c:85

◆ description()

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

Definition at line 97 of file IndexSet.c.

97 {
98
99 const IndexSet *this = (IndexSet *) self;
100 String *desc = str("[");
101
102 for (size_t i = 0; i < this->count; i++) {
103 $(desc, appendFormat, "%d", this->indexes[i]);
104 if (i < this->count - 1) {
105 $(desc, appendCharacters, ", ");
106 }
107 }
108
109 $(desc, appendCharacters, "]");
110 return (String *) desc;
111}
String * str(const char *fmt,...)
Definition String.c:1084
static void appendCharacters(String *self, const char *chars)
Definition String.c:506
static void appendFormat(String *self, const char *fmt,...)
Definition String.c:541
UTF-8 strings.
Definition String.h:69

◆ hash()

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

Definition at line 116 of file IndexSet.c.

116 {
117
118 int hash = HASH_SEED;
119
120 const IndexSet *this = (IndexSet *) self;
121
122 for (size_t i = 0; i < this->count; i++) {
123 hash = HashForInteger(hash, this->indexes[i]);
124 }
125
126 return hash;
127}
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition Hash.c:66
#define HASH_SEED
The hash seed value.
Definition Hash.h:37
static int hash(const Object *self)
Definition IndexSet.c:116

◆ init()

static IndexSet * init ( IndexSet self)
static

Definition at line 263 of file IndexSet.c.

263 {
264 return $(self, initWithCapacity, INDEX_SET_CHUNK_SIZE);
265}
static IndexSet * initWithCapacity(IndexSet *self, size_t capacity)
Definition IndexSet.c:271

◆ initialize()

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

Definition at line 344 of file IndexSet.c.

344 {
345
346 ((ObjectInterface *) clazz->interface)->copy = copy;
347 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
348 ((ObjectInterface *) clazz->interface)->description = description;
349 ((ObjectInterface *) clazz->interface)->hash = hash;
350 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
351
352 ((IndexSetInterface *) clazz->interface)->containsIndex = containsIndex;
353 ((IndexSetInterface *) clazz->interface)->addIndex = addIndex;
354 ((IndexSetInterface *) clazz->interface)->addIndexes = addIndexes;
355 ((IndexSetInterface *) clazz->interface)->addIndexesInRange = addIndexesInRange;
356 ((IndexSetInterface *) clazz->interface)->init = init;
357 ((IndexSetInterface *) clazz->interface)->initWithCapacity = initWithCapacity;
358 ((IndexSetInterface *) clazz->interface)->initWithIndex = initWithIndex;
359 ((IndexSetInterface *) clazz->interface)->initWithIndexes = initWithIndexes;
360 ((IndexSetInterface *) clazz->interface)->removeAllIndexes = removeAllIndexes;
361 ((IndexSetInterface *) clazz->interface)->removeIndex = removeIndex;
362 ((IndexSetInterface *) clazz->interface)->removeIndexes = removeIndexes;
363 ((IndexSetInterface *) clazz->interface)->removeIndexesInRange = removeIndexesInRange;
364}
static bool containsIndex(const IndexSet *self, size_t index)
Definition IndexSet.c:157
static IndexSet * init(IndexSet *self)
Definition IndexSet.c:263
static void removeIndexesInRange(IndexSet *self, const Range range)
Definition IndexSet.c:332
static void removeAllIndexes(IndexSet *self)
Definition IndexSet.c:288
static bool isEqual(const Object *self, const Object *other)
Definition IndexSet.c:132
static String * description(const Object *self)
Definition IndexSet.c:97
static void removeIndexes(IndexSet *self, size_t *indexes, size_t count)
Definition IndexSet.c:321
static void removeIndex(IndexSet *self, size_t index)
Definition IndexSet.c:303
static IndexSet * initWithIndex(IndexSet *self, size_t index)
Definition IndexSet.c:172
static Object * copy(const Object *self)
Definition IndexSet.c:74
static void addIndexesInRange(IndexSet *self, const Range range)
Definition IndexSet.c:252
static void addIndexes(IndexSet *self, size_t *indexes, size_t count)
Definition IndexSet.c:238
ident interface
The interface of the Class.
Definition Class.h:105
IndexSet * initWithCapacity(IndexSet *self, size_t capacity)
Initializes this IndexSet with the specified capacity.
Definition IndexSet.c:271
IndexSet * initWithIndex(IndexSet *self, size_t index)
Initializes this IndexSet with the specified index.
Definition IndexSet.c:172
void removeAllIndexes(IndexSet *self)
Removes all indexes from this IndexSet.
Definition IndexSet.c:288
IndexSet * initWithIndexes(IndexSet *self, size_t *indexes, size_t count)
Initializes this IndexSet with the specified indexes and count.
Definition IndexSet.c:180
int hash(const Object *self)
Definition Array.c:129
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99

◆ initWithCapacity()

static IndexSet * initWithCapacity ( IndexSet self,
size_t  capacity 
)
static

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}

◆ initWithIndex()

static IndexSet * initWithIndex ( IndexSet self,
size_t  index 
)
static

Definition at line 172 of file IndexSet.c.

172 {
173 return $(self, initWithIndexes, &index, 1);
174}

◆ initWithIndexes()

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

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

◆ isEqual()

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

Definition at line 132 of file IndexSet.c.

132 {
133
134 if (super(Object, self, isEqual, other)) {
135 return true;
136 }
137
138 if (other && $(other, isKindOfClass, _IndexSet())) {
139
140 const IndexSet *this = (IndexSet *) self;
141 const IndexSet *that = (IndexSet *) other;
142
143 if (this->count == that->count) {
144 return memcmp(this->indexes, that->indexes, this->count * sizeof(size_t)) == 0;
145 }
146 }
147
148 return false;
149}
Class * _IndexSet(void)
Definition IndexSet.c:370
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101

◆ removeAllIndexes()

static void removeAllIndexes ( IndexSet self)
static

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

static void removeIndex ( IndexSet self,
size_t  index 
)
static

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

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

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}

◆ removeIndexesInRange()

static void removeIndexesInRange ( IndexSet self,
const Range  range 
)
static

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}