Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
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 "MutableString.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#pragma mark - Object
68
72static Object *copy(const Object *self) {
73
74 IndexSet *this = (IndexSet *) self;
75 IndexSet *that = $(alloc(IndexSet), initWithIndexes, this->indexes, this->count);
76
77 return (Object *) that;
78}
79
83static void dealloc(Object *self) {
84
85 IndexSet *this = (IndexSet *) self;
86
87 free(this->indexes);
88
89 super(Object, self, dealloc);
90}
91
95static String *description(const Object *self) {
96
97 const IndexSet *this = (IndexSet *) self;
98 MutableString *desc = mstr("[");
99
100 for (size_t i = 0; i < this->count; i++) {
101 $(desc, appendFormat, "%d", this->indexes[i]);
102 if (i < this->count - 1) {
103 $(desc, appendCharacters, ", ");
104 }
105 }
106
107 $(desc, appendCharacters, "]");
108 return (String *) desc;
109}
110
114static int hash(const Object *self) {
115
116 int hash = HASH_SEED;
117
118 const IndexSet *this = (IndexSet *) self;
119
120 for (size_t i = 0; i < this->count; i++) {
121 hash = HashForInteger(hash, this->indexes[i]);
122 }
123
124 return hash;
125}
126
130static _Bool isEqual(const Object *self, const Object *other) {
131
132 if (super(Object, self, isEqual, other)) {
133 return true;
134 }
135
136 if (other && $(other, isKindOfClass, _IndexSet())) {
137
138 const IndexSet *this = (IndexSet *) self;
139 const IndexSet *that = (IndexSet *) other;
140
141 if (this->count == that->count) {
142 return memcmp(this->indexes, that->indexes, this->count * sizeof(size_t)) == 0;
143 }
144 }
145
146 return false;
147}
148
149#pragma mark - IndexSet
150
155static _Bool containsIndex(const IndexSet *self, size_t index) {
156
157 for (size_t i = 0; i < self->count; i++) {
158 if (self->indexes[i] == index) {
159 return true;
160 }
161 }
162
163 return false;
164}
165
170static IndexSet *initWithIndex(IndexSet *self, size_t index) {
171 return $(self, initWithIndexes, &index, 1);
172}
173
178static IndexSet *initWithIndexes(IndexSet *self, size_t *indexes, size_t count) {
179
180 self = (IndexSet *) super(Object, self, init);
181 if (self) {
182
183 self->count = compact(indexes, count);
184 if (self->count) {
185
186 self->indexes = calloc(sizeof(size_t), self->count);
187 assert(self->indexes);
188
189 memcpy(self->indexes, indexes, sizeof(size_t) * self->count);
190 }
191 }
192
193 return self;
194}
195
196#pragma mark - Class lifecycle
197
201static void initialize(Class *clazz) {
202
203 ((ObjectInterface *) clazz->interface)->copy = copy;
204 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
205 ((ObjectInterface *) clazz->interface)->description = description;
206 ((ObjectInterface *) clazz->interface)->hash = hash;
207 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
208
209 ((IndexSetInterface *) clazz->interface)->containsIndex = containsIndex;
210 ((IndexSetInterface *) clazz->interface)->initWithIndex = initWithIndex;
211 ((IndexSetInterface *) clazz->interface)->initWithIndexes = initWithIndexes;
212}
213
219 static Class *clazz;
220 static Once once;
221
222 do_once(&once, {
223 clazz = _initialize(&(const ClassDef) {
224 .name = "IndexSet",
225 .superclass = _Object(),
226 .instanceSize = sizeof(IndexSet),
227 .interfaceOffset = offsetof(IndexSet, interface),
228 .interfaceSize = sizeof(IndexSetInterface),
230 });
231 });
232
233 return clazz;
234}
235
236#undef _Class
237
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition: Hash.c:62
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37
static int compare(const void *a, const void *b)
qsort comparator for indexes.
Definition: IndexSet.c:35
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 initialize(Class *clazz)
Definition: IndexSet.c:201
Immutable collections of unique index values.
Mutable UTF-8 strings.
Immutable 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
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
IndexPath * initWithIndexes(IndexPath *self, size_t *indexes, size_t length)
Initializes this IndexPath with the specified indexes and length.
Definition: IndexPath.c:141
IndexPath * initWithIndex(IndexPath *self, size_t index)
Initializes this IndexPath with the specified index.
Definition: IndexPath.c:133
Immutable collections of unique index values.
Definition: IndexSet.h:41
size_t * indexes
The indexes.
Definition: IndexSet.h:57
Class * _IndexSet(void)
The IndexSet archetype.
Definition: IndexSet.c:218
size_t count
The count of indexes.
Definition: IndexSet.h:62
_Bool containsIndex(const IndexSet *self, size_t index)
Definition: IndexSet.c:155
Mutable UTF-8 strings.
Definition: MutableString.h:40
void appendFormat(MutableString *self, const char *fmt,...)
Appends the specified formatted string.
Definition: MutableString.c:89
void appendCharacters(MutableString *self, const char *chars)
Appends the specified UTF-8 encoded C string.
Definition: MutableString.c:54
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
_Bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition: Object.c:101
String * description(const Object *self)
Definition: Array.c:66
_Bool isEqual(const Object *self, const Object *other)
Tests equality of the other Object.
Definition: Array.c:96
int hash(const Object *self)
Definition: Array.c:80
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
Immutable UTF-8 strings.
Definition: String.h:69
OBJECTIVELY_EXPORT MutableString * mstr(const char *fmt,...)
A convenience function for instantiating MutableStrings.