Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
IndexSet.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "Hash.h"
29#include "IndexSet.h"
30#include "String.h"
31
35static int compare(const void *a, const void *b) {
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}
42
46static size_t compact(size_t *indexes, size_t count) {
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}
64
65#define _Class _IndexSet
66
67#define INDEX_SET_CHUNK_SIZE 8
68
69#pragma mark - Object
70
74static Object *copy(const Object *self) {
75
76 IndexSet *this = (IndexSet *) self;
77 IndexSet *that = $(alloc(IndexSet), initWithIndexes, this->indexes, this->count);
78
79 return (Object *) that;
80}
81
85static void dealloc(Object *self) {
86
87 IndexSet *this = (IndexSet *) self;
88
89 free(this->indexes);
90
91 super(Object, self, dealloc);
92}
93
97static String *description(const Object *self) {
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}
112
116static int hash(const Object *self) {
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}
128
132static bool isEqual(const Object *self, const Object *other) {
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}
150
151#pragma mark - IndexSet
152
157static bool containsIndex(const IndexSet *self, size_t index) {
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}
167
172static IndexSet *initWithIndex(IndexSet *self, size_t index) {
173 return $(self, initWithIndexes, &index, 1);
174}
175
180static IndexSet *initWithIndexes(IndexSet *self, size_t *indexes, size_t count) {
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}
198
199#pragma mark - IndexSet mutation
200
205static void addIndex(IndexSet *self, size_t index) {
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}
233
238static void addIndexes(IndexSet *self, size_t *indexes, size_t count) {
239
240 for (size_t i = 0; i < count; i++) {
241 $(self, addIndex, indexes[i]);
242 }
243}
244
252static void addIndexesInRange(IndexSet *self, const Range range) {
253
254 for (size_t i = range.location; i < range.location + range.length; i++) {
255 $(self, addIndex, i);
256 }
257}
258
263static IndexSet *init(IndexSet *self) {
264 return $(self, initWithCapacity, INDEX_SET_CHUNK_SIZE);
265}
266
271static IndexSet *initWithCapacity(IndexSet *self, size_t capacity) {
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}
283
288static void removeAllIndexes(IndexSet *self) {
289
290 IndexSet *this = self;
291
292 free(this->indexes);
293 this->indexes = NULL;
294
295 this->count = 0;
296 self->capacity = 0;
297}
298
303static void removeIndex(IndexSet *self, size_t index) {
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}
316
321static void removeIndexes(IndexSet *self, size_t *indexes, size_t count) {
322
323 for (size_t i = 0; i < count; i++) {
324 $(self, removeIndex, indexes[i]);
325 }
326}
327
332static void removeIndexesInRange(IndexSet *self, const Range range) {
333
334 for (size_t i = range.location; i < range.location + range.length; i++) {
335 $(self, removeIndex, i);
336 }
337}
338
339#pragma mark - Class lifecycle
340
344static void initialize(Class *clazz) {
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}
365
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}
387
388#undef _Class
389
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition Hash.c:66
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition Hash.h:37
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 IndexSet * initWithIndexes(IndexSet *self, size_t *indexes, size_t count)
Definition IndexSet.c:180
static int compare(const void *a, const void *b)
qsort comparator for indexes.
Definition IndexSet.c:35
static IndexSet * initWithCapacity(IndexSet *self, size_t capacity)
Definition IndexSet.c:271
static String * description(const Object *self)
Definition IndexSet.c:97
Class * _IndexSet(void)
Definition IndexSet.c:370
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
static void dealloc(Object *self)
Definition IndexSet.c:85
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 initialize(Class *clazz)
Definition IndexSet.c:344
static void addIndex(IndexSet *self, size_t index)
Definition IndexSet.c:205
static void addIndexesInRange(IndexSet *self, const Range range)
Definition IndexSet.c:252
#define INDEX_SET_CHUNK_SIZE
Definition IndexSet.c:67
static int hash(const Object *self)
Definition IndexSet.c:116
static void addIndexes(IndexSet *self, size_t *indexes, size_t count)
Definition IndexSet.c:238
Collections of unique index values.
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101
Class * _Object(void)
Definition Object.c:136
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.
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
ident interface
The interface of the Class.
Definition Class.h:105
Collections of unique index values.
Definition IndexSet.h:41
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
size_t * indexes
The indexes.
Definition IndexSet.h:57
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
size_t count
The count of indexes.
Definition IndexSet.h:62
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
int hash(const Object *self)
Definition Array.c:129
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99
A location and length into contiguous collections.
Definition Types.h:54
ssize_t location
The location.
Definition Types.h:59
size_t length
The length.
Definition Types.h:64
UTF-8 strings.
Definition String.h:69