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

Go to the source code of this file.

Macros

#define _Class   _MutableArray
 
#define ARRAY_CHUNK_SIZE   64
 

Functions

Class_MutableArray (void)
 
static int _quicksort (const void *a, const void *b, void *data)
 GNU qsort_r. More...
 
static void addObject (MutableArray *self, const ident obj)
 
static void addObjects (MutableArray *self, const ident obj,...)
 
static void addObjectsFromArray (MutableArray *self, const Array *array)
 
static MutableArrayarray (void)
 
static MutableArrayarrayWithCapacity (size_t capacity)
 
static Objectcopy (const Object *self)
 
static void filter (MutableArray *self, Predicate predicate, ident data)
 
static MutableArrayinit (MutableArray *self)
 
static void initialize (Class *clazz)
 
static MutableArrayinitWithCapacity (MutableArray *self, size_t capacity)
 
static void insertObjectAtIndex (MutableArray *self, ident obj, size_t index)
 
void quicksort (ident base, size_t count, size_t size, Comparator comparator, ident data)
 A portability wrapper around reentrant qsort. More...
 
static void removeAllObjects (MutableArray *self)
 
static void removeAllObjectsWithEnumerator (MutableArray *self, ArrayEnumerator enumerator, ident data)
 
static void removeLastObject (MutableArray *self)
 
static void removeObject (MutableArray *self, const ident obj)
 
static void removeObjectAtIndex (MutableArray *self, size_t index)
 
static void setObjectAtIndex (MutableArray *self, const ident obj, size_t index)
 
static void sort (MutableArray *self, Comparator comparator)
 

Macro Definition Documentation

◆ _Class

#define _Class   _MutableArray

Definition at line 73 of file MutableArray.c.

◆ ARRAY_CHUNK_SIZE

#define ARRAY_CHUNK_SIZE   64

Definition at line 75 of file MutableArray.c.

Function Documentation

◆ _MutableArray()

Class * _MutableArray ( void  )

Definition at line 349 of file MutableArray.c.

349 {
350 static Class *clazz;
351 static Once once;
352
353 do_once(&once, {
354 clazz = _initialize(&(const ClassDef) {
355 .name = "MutableArray",
356 .superclass = _Array(),
357 .instanceSize = sizeof(MutableArray),
358 .interfaceOffset = offsetof(MutableArray, interface),
359 .interfaceSize = sizeof(MutableArrayInterface),
361 });
362 });
363
364 return clazz;
365}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: MutableArray.c:323
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
Class * _Array(void)
The Array archetype.
Definition: Array.c:470
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
Mutable arrays.
Definition: MutableArray.h:40

◆ _quicksort()

static int _quicksort ( const void *  a,
const void *  b,
void *  data 
)
static

GNU qsort_r.

Definition at line 63 of file MutableArray.c.

63 {
64 return ((Comparator) data)(*((const ident *) a), *((const ident *) b));
65}
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Order(* Comparator)(const ident obj1, const ident obj2)
The Comparator function type for ordering Objects.
Definition: Types.h:82
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75

◆ addObject()

static void addObject ( MutableArray self,
const ident  obj 
)
static

Definition at line 99 of file MutableArray.c.

99 {
100
101 Array *array = (Array *) self;
102 if (array->count == self->capacity) {
103
104 self->capacity += ARRAY_CHUNK_SIZE;
105
106 if (array->elements) {
107 array->elements = realloc(array->elements, self->capacity * sizeof(ident));
108 } else {
109 array->elements = calloc(self->capacity, sizeof(ident));
110 }
111
112 assert(array->elements);
113 }
114
115 array->elements[array->count++] = retain(obj);
116}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
#define obj
#define ARRAY_CHUNK_SIZE
Definition: MutableArray.c:75
Immutable arrays.
Definition: Array.h:56
size_t count
The count of elements.
Definition: Array.h:72
MutableArray * array(void)
Returns a new MutableArray.
Definition: MutableArray.c:153

◆ addObjects()

static void addObjects ( MutableArray self,
const ident  obj,
  ... 
)
static

Definition at line 122 of file MutableArray.c.

122 {
123
124 va_list args;
125 va_start(args, obj);
126
127 ident object = obj;
128 while (object) {
129 $(self, addObject, object);
130 object = va_arg(args, ident);
131 }
132
133 va_end(args);
134}
void addObject(MutableArray *self, const ident obj)
Adds the specified Object to this MutableArray.
Definition: MutableArray.c:99

◆ addObjectsFromArray()

static void addObjectsFromArray ( MutableArray self,
const Array array 
)
static

Definition at line 140 of file MutableArray.c.

140 {
141
142 if (array) {
143 for (size_t i = 0; i < array->count; i++) {
144 $(self, addObject, array->elements[i]);
145 }
146 }
147}

◆ array()

static MutableArray * array ( void  )
static

Definition at line 153 of file MutableArray.c.

153 {
154
155 return $(alloc(MutableArray), init);
156}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67

◆ arrayWithCapacity()

static MutableArray * arrayWithCapacity ( size_t  capacity)
static

Definition at line 162 of file MutableArray.c.

162 {
163
164 return $(alloc(MutableArray), initWithCapacity, capacity);
165}
MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Initializes this MutableArray with the specified capacity.
Definition: MutableArray.c:195

◆ copy()

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

Definition at line 82 of file MutableArray.c.

82 {
83
84 Array *this = (Array *) self;
85
87
88 $(copy, addObjectsFromArray, this);
89
90 return (Object *) copy;
91}
void addObjectsFromArray(MutableArray *self, const Array *array)
Adds the Objects contained in array to this MutableArray.
Definition: MutableArray.c:140
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40

◆ filter()

static void filter ( MutableArray self,
Predicate  predicate,
ident  data 
)
static

Definition at line 171 of file MutableArray.c.

171 {
172
173 assert(predicate);
174
175 for (size_t i = 0; i < self->array.count; i++) {
176 if (predicate(self->array.elements[i], data) == false) {
177 $(self, removeObjectAtIndex, i--);
178 }
179 }
180}
void removeObjectAtIndex(MutableArray *self, size_t index)
Removes the Object at the specified index.
Definition: MutableArray.c:282
Array array
The superclass.
Definition: MutableArray.h:45

◆ init()

static MutableArray * init ( MutableArray self)
static

Definition at line 186 of file MutableArray.c.

186 {
187
188 return $(self, initWithCapacity, 0);
189}

◆ initialize()

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

Definition at line 323 of file MutableArray.c.

323 {
324
325 ((ObjectInterface *) clazz->interface)->copy = copy;
326
327 ((MutableArrayInterface *) clazz->interface)->addObject = addObject;
328 ((MutableArrayInterface *) clazz->interface)->addObjects = addObjects;
329 ((MutableArrayInterface *) clazz->interface)->addObjectsFromArray = addObjectsFromArray;
330 ((MutableArrayInterface *) clazz->interface)->array = array;
331 ((MutableArrayInterface *) clazz->interface)->arrayWithCapacity = arrayWithCapacity;
332 ((MutableArrayInterface *) clazz->interface)->filter = filter;
333 ((MutableArrayInterface *) clazz->interface)->init = init;
334 ((MutableArrayInterface *) clazz->interface)->initWithCapacity = initWithCapacity;
335 ((MutableArrayInterface *) clazz->interface)->insertObjectAtIndex = insertObjectAtIndex;
336 ((MutableArrayInterface *) clazz->interface)->removeAllObjects = removeAllObjects;
337 ((MutableArrayInterface *) clazz->interface)->removeAllObjectsWithEnumerator = removeAllObjectsWithEnumerator;
338 ((MutableArrayInterface *) clazz->interface)->removeLastObject = removeLastObject;
339 ((MutableArrayInterface *) clazz->interface)->removeObject = removeObject;
340 ((MutableArrayInterface *) clazz->interface)->removeObjectAtIndex = removeObjectAtIndex;
341 ((MutableArrayInterface *) clazz->interface)->setObjectAtIndex = setObjectAtIndex;
342 ((MutableArrayInterface *) clazz->interface)->sort = sort;
343}
ident interface
The interface of the Class.
Definition: Class.h:105
void removeAllObjectsWithEnumerator(MutableArray *self, ArrayEnumerator enumerator, ident data)
Removes all Objects from this MutableArray, invoking enumerator for each Object.
Definition: MutableArray.c:243
void removeLastObject(MutableArray *self)
Removes the last Object from this MutableArray.
Definition: MutableArray.c:259
void filter(MutableArray *self, Predicate predicate, ident data)
Filters this MutableArray in place using predicate.
Definition: MutableArray.c:171
void setObjectAtIndex(MutableArray *self, const ident obj, size_t index)
Replaces the Object at the specified index.
Definition: MutableArray.c:299
MutableArray * arrayWithCapacity(size_t capacity)
Returns a new MutableArray with the given capacity.
Definition: MutableArray.c:162
void addObjects(MutableArray *self, const ident obj,...)
Adds the specified objects to this Array.
Definition: MutableArray.c:122
void insertObjectAtIndex(MutableArray *self, ident obj, size_t index)
Inserts the Object at the specified index.
Definition: MutableArray.c:215
void sort(MutableArray *self, Comparator comparator)
Sorts this MutableArray in place using comparator.
Definition: MutableArray.c:314
void removeAllObjects(MutableArray *self)
Removes all Objects from this MutableArray.
Definition: MutableArray.c:232
void removeObject(MutableArray *self, const ident obj)
Removes the specified Object from this MutableArray.
Definition: MutableArray.c:270

◆ initWithCapacity()

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

Definition at line 195 of file MutableArray.c.

195 {
196
197 self = (MutableArray *) super(Object, self, init);
198 if (self) {
199
200 self->capacity = capacity;
201 if (self->capacity) {
202
203 self->array.elements = calloc(self->capacity, sizeof(ident));
204 assert(self->array.elements);
205 }
206 }
207
208 return self;
209}
#define super(type, obj, method,...)

◆ insertObjectAtIndex()

static void insertObjectAtIndex ( MutableArray self,
ident  obj,
size_t  index 
)
static

Definition at line 215 of file MutableArray.c.

215 {
216
217 assert(index <= self->array.count);
218
219 $(self, addObject, obj);
220
221 for (size_t i = self->array.count - 1; i > index; i--) {
222 self->array.elements[i] = self->array.elements[i - 1];
223 }
224
225 self->array.elements[index] = obj;
226}

◆ quicksort()

void quicksort ( ident  base,
size_t  count,
size_t  size,
Comparator  comparator,
ident  data 
)

A portability wrapper around reentrant qsort.

Parameters
baseThe base of the array to sort.
countThe count of elements in the array
sizeThe size of each element in the array.
comparatorThe Comparator to sort with.
dataUser data.

Definition at line 67 of file MutableArray.c.

67 {
68 qsort_r(base, count, size, _quicksort, comparator);
69}
static int _quicksort(const void *a, const void *b, void *data)
GNU qsort_r.
Definition: MutableArray.c:63

◆ removeAllObjects()

static void removeAllObjects ( MutableArray self)
static

Definition at line 232 of file MutableArray.c.

232 {
233
234 while (self->array.count) {
235 $(self, removeLastObject);
236 }
237}

◆ removeAllObjectsWithEnumerator()

static void removeAllObjectsWithEnumerator ( MutableArray self,
ArrayEnumerator  enumerator,
ident  data 
)
static

Definition at line 243 of file MutableArray.c.

243 {
244
245 assert(enumerator);
246
247 while (self->array.count) {
248
249 enumerator((Array *) self, $((Array *) self, lastObject), data);
250
251 $(self, removeLastObject);
252 }
253}
ident lastObject(const Array *self)
Definition: Array.c:349

◆ removeLastObject()

static void removeLastObject ( MutableArray self)
static

Definition at line 259 of file MutableArray.c.

259 {
260
261 if (self->array.count) {
262 $(self, removeObjectAtIndex, self->array.count - 1);
263 }
264}

◆ removeObject()

static void removeObject ( MutableArray self,
const ident  obj 
)
static

Definition at line 270 of file MutableArray.c.

270 {
271
272 const ssize_t index = $((Array *) self, indexOfObject, obj);
273 if (index > -1) {
274 $(self, removeObjectAtIndex, index);
275 }
276}
ssize_t indexOfObject(const Array *self, const ident obj)
Definition: Array.c:271

◆ removeObjectAtIndex()

static void removeObjectAtIndex ( MutableArray self,
size_t  index 
)
static

Definition at line 282 of file MutableArray.c.

282 {
283
284 assert(index < self->array.count);
285
286 release(self->array.elements[index]);
287
288 for (size_t i = index; i < self->array.count - 1; i++) {
289 self->array.elements[i] = self->array.elements[i + 1];
290 }
291
292 self->array.count--;
293}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196

◆ setObjectAtIndex()

static void setObjectAtIndex ( MutableArray self,
const ident  obj,
size_t  index 
)
static

Definition at line 299 of file MutableArray.c.

299 {
300
301 assert(index < self->array.count);
302
303 retain(obj);
304
305 release(self->array.elements[index]);
306
307 self->array.elements[index] = obj;
308}

◆ sort()

static void sort ( MutableArray self,
Comparator  comparator 
)
static

Definition at line 314 of file MutableArray.c.

314 {
315 quicksort(self->array.elements, self->array.count, sizeof(ident), comparator, NULL);
316}
void quicksort(ident base, size_t count, size_t size, Comparator comparator, ident data)
A portability wrapper around reentrant qsort.
Definition: MutableArray.c:67