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

Go to the source code of this file.

Macros

#define _Class   _MutableIndexSet
 
#define INDEX_SET_CHUNK_SIZE   8
 

Functions

Class_MutableIndexSet (void)
 
static void addIndex (MutableIndexSet *self, size_t index)
 
static void addIndexes (MutableIndexSet *self, size_t *indexes, size_t count)
 
static void addIndexesInRange (MutableIndexSet *self, const Range range)
 
static void dealloc (Object *self)
 
static MutableIndexSetinit (MutableIndexSet *self)
 
static void initialize (Class *clazz)
 
static MutableIndexSetinitWithCapacity (MutableIndexSet *self, size_t capacity)
 
static void removeAllIndexes (MutableIndexSet *self)
 
static void removeIndex (MutableIndexSet *self, size_t index)
 
static void removeIndexes (MutableIndexSet *self, size_t *indexes, size_t count)
 
static void removeIndexesInRange (MutableIndexSet *self, const Range range)
 

Macro Definition Documentation

◆ _Class

#define _Class   _MutableIndexSet

Definition at line 29 of file MutableIndexSet.c.

◆ INDEX_SET_CHUNK_SIZE

#define INDEX_SET_CHUNK_SIZE   8

Definition at line 31 of file MutableIndexSet.c.

Function Documentation

◆ _MutableIndexSet()

Class * _MutableIndexSet ( void  )

Definition at line 211 of file MutableIndexSet.c.

211 {
212 static Class *clazz;
213 static Once once;
214
215 do_once(&once, {
216 clazz = _initialize(&(const ClassDef) {
217 .name = "MutableIndexSet",
218 .superclass = _IndexSet(),
219 .instanceSize = sizeof(MutableIndexSet),
220 .interfaceOffset = offsetof(MutableIndexSet, interface),
221 .interfaceSize = sizeof(MutableIndexSetInterface),
223 });
224 });
225
226 return clazz;
227}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
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
Class * _IndexSet(void)
The IndexSet archetype.
Definition: IndexSet.c:218
Mutable collections of unique index values.

◆ addIndex()

static void addIndex ( MutableIndexSet self,
size_t  index 
)
static

Definition at line 51 of file MutableIndexSet.c.

51 {
52
53 IndexSet *this = &self->indexSet;
54
55 size_t i;
56 for (i = 0; i < this->count; i++) {
57 if (this->indexes[i] == index) {
58 return;
59 }
60 if (this->indexes[i] > index) {
61 break;
62 }
63 }
64
65 if (this->count == self->capacity) {
67
68 this->indexes = realloc(this->indexes, self->capacity * sizeof(size_t));
69 assert(this->indexes);
70 }
71
72 for (size_t j = this->count; j > i; j--) {
73 this->indexes[j] = this->indexes[j - 1];
74 }
75
76 this->indexes[i] = index;
77 this->count++;
78}
#define INDEX_SET_CHUNK_SIZE
Immutable collections of unique index values.
Definition: IndexSet.h:41
size_t capacity
The capacity.
IndexSet indexSet
The superclass.

◆ addIndexes()

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

Definition at line 84 of file MutableIndexSet.c.

84 {
85
86 for (size_t i = 0; i < count; i++) {
87 $(self, addIndex, indexes[i]);
88 }
89}
void addIndex(MutableIndexSet *self, size_t index)
Adds the specified index to this MutableIndexSet.

◆ addIndexesInRange()

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

Definition at line 98 of file MutableIndexSet.c.

98 {
99
100 for (size_t i = range.location; i < range.length; i++) {
101 $(self, addIndex, i);
102 }
103}
ssize_t location
The location.
Definition: Types.h:59
size_t length
The length.
Definition: Types.h:64

◆ dealloc()

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

Definition at line 38 of file MutableIndexSet.c.

38 {
39
40 //..
41
42 super(Object, self, dealloc);
43}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ init()

static MutableIndexSet * init ( MutableIndexSet self)
static

Definition at line 109 of file MutableIndexSet.c.

109 {
110 return $(self, initWithCapacity, INDEX_SET_CHUNK_SIZE);
111}
MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Initializes this MutableArray with the specified capacity.
Definition: MutableArray.c:195

◆ initialize()

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

Definition at line 192 of file MutableIndexSet.c.

192 {
193
194 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
195
196 ((MutableIndexSetInterface *) clazz->interface)->addIndex = addIndex;
197 ((MutableIndexSetInterface *) clazz->interface)->addIndexes = addIndexes;
198 ((MutableIndexSetInterface *) clazz->interface)->addIndexesInRange = addIndexesInRange;
199 ((MutableIndexSetInterface *) clazz->interface)->init = init;
200 ((MutableIndexSetInterface *) clazz->interface)->initWithCapacity = initWithCapacity;
201 ((MutableIndexSetInterface *) clazz->interface)->removeAllIndexes = removeAllIndexes;
202 ((MutableIndexSetInterface *) clazz->interface)->removeIndex = removeIndex;
203 ((MutableIndexSetInterface *) clazz->interface)->removeIndexes = removeIndexes;
204 ((MutableIndexSetInterface *) clazz->interface)->removeIndexesInRange = removeIndexesInRange;
205}
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
void addIndexes(MutableIndexSet *self, size_t *indexes, size_t count)
Adds the specified indexes to this MutableIndexSet.
void removeIndexesInRange(MutableIndexSet *self, const Range range)
Removes indexes in the specified Range from this MutableIndexSet.
void removeIndex(MutableIndexSet *self, size_t index)
Removes the specified index from this MutableIndexSet.
void removeIndexes(MutableIndexSet *self, size_t *indexes, size_t count)
Removes the specified indexes from this MutableIndexSet.
void addIndexesInRange(MutableIndexSet *self, const Range range)
Adds indexes in the specified Range to this MutableIndexSet.
void removeAllIndexes(MutableIndexSet *self)
Removes all indexes from this MutableIndexSet.

◆ initWithCapacity()

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

Definition at line 117 of file MutableIndexSet.c.

117 {
118
119 self = (MutableIndexSet *) super(Object, self, init);
120 if (self) {
121 self->capacity = capacity;
122
123 IndexSet *this = & self->indexSet;
124
125 this->indexes = malloc(self->capacity * sizeof(size_t));
126 assert(this->indexes);
127 }
128
129 return self;
130}

◆ removeAllIndexes()

static void removeAllIndexes ( MutableIndexSet self)
static

Definition at line 136 of file MutableIndexSet.c.

136 {
137
138 IndexSet *this = &self->indexSet;
139
140 free(this->indexes);
141 this->indexes = NULL;
142
143 this->count = 0;
144 self->capacity = 0;
145}

◆ removeIndex()

static void removeIndex ( MutableIndexSet self,
size_t  index 
)
static

Definition at line 151 of file MutableIndexSet.c.

151 {
152
153 IndexSet *this = &self->indexSet;
154 for (size_t i = 0; i < this->count; i++) {
155 if (this->indexes[i] == index) {
156 this->count--;
157 for (size_t j = i; j < this->count; j++) {
158 this->indexes[j] = this->indexes[j + 1];
159 }
160 return;
161 }
162 }
163}

◆ removeIndexes()

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

Definition at line 169 of file MutableIndexSet.c.

169 {
170
171 for (size_t i = 0; i < count; i++) {
172 $(self, removeIndex, indexes[i]);
173 }
174}

◆ removeIndexesInRange()

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

Definition at line 180 of file MutableIndexSet.c.

180 {
181
182 for (size_t i = range.location; i < range.length; i++) {
183 $(self, removeIndex, i);
184 }
185}