Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
IndexSet.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "Hash.h"
#include "IndexSet.h"
#include "MutableString.h"

Go to the source code of this file.

Macros

#define _Class   _IndexSet
 

Functions

Class_IndexSet (void)
 
static size_t compact (size_t *indexes, size_t count)
 Sorts and compacts the given array to contain only unique values. More...
 
static int compare (const void *a, const void *b)
 qsort comparator for indexes. More...
 
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 void initialize (Class *clazz)
 
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)
 

Macro Definition Documentation

◆ _Class

#define _Class   _IndexSet

Definition at line 65 of file IndexSet.c.

Function Documentation

◆ _IndexSet()

Class * _IndexSet ( void  )

Definition at line 218 of file IndexSet.c.

218 {
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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: IndexSet.c:201
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
Immutable collections of unique index values.
Definition: IndexSet.h:41
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ 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 155 of file IndexSet.c.

155 {
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}
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 72 of file IndexSet.c.

72 {
73
74 IndexSet *this = (IndexSet *) self;
75 IndexSet *that = $(alloc(IndexSet), initWithIndexes, this->indexes, this->count);
76
77 return (Object *) that;
78}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
IndexPath * initWithIndexes(IndexPath *self, size_t *indexes, size_t length)
Initializes this IndexPath with the specified indexes and length.
Definition: IndexPath.c:141
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 83 of file IndexSet.c.

83 {
84
85 IndexSet *this = (IndexSet *) self;
86
87 free(this->indexes);
88
89 super(Object, self, dealloc);
90}
#define super(type, obj, method,...)
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ description()

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

Definition at line 95 of file IndexSet.c.

95 {
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}
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
Immutable UTF-8 strings.
Definition: String.h:69
OBJECTIVELY_EXPORT MutableString * mstr(const char *fmt,...)
A convenience function for instantiating MutableStrings.

◆ hash()

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

Definition at line 114 of file IndexSet.c.

114 {
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}
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition: Hash.c:62
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37
int hash(const Object *self)
Definition: Array.c:80

◆ initialize()

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

Definition at line 201 of file IndexSet.c.

201 {
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}
ident interface
The interface of the Class.
Definition: Class.h:105
IndexPath * initWithIndex(IndexPath *self, size_t index)
Initializes this IndexPath with the specified index.
Definition: IndexPath.c:133
_Bool containsIndex(const IndexSet *self, size_t index)
Definition: IndexSet.c:155
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
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

◆ initWithIndex()

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

Definition at line 170 of file IndexSet.c.

170 {
171 return $(self, initWithIndexes, &index, 1);
172}

◆ initWithIndexes()

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

Definition at line 178 of file IndexSet.c.

178 {
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}
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
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67

◆ isEqual()

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

Definition at line 130 of file IndexSet.c.

130 {
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}
Class * _IndexSet(void)
The IndexSet archetype.
Definition: IndexSet.c:218
_Bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition: Object.c:101