Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Array.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 <stdarg.h>
26#include <stdlib.h>
27
28#include "Array.h"
29#include "Hash.h"
30#include "MutableArray.h"
31#include "MutableString.h"
32
33#define _Class _Array
34
35#pragma mark - Object
36
40static Object *copy(const Object *self) {
41
42 const Array *this = (Array *) self;
43
44 return (Object *) $(alloc(Array), initWithArray, this);
45}
46
50static void dealloc(Object *self) {
51
52 Array *this = (Array *) self;
53
54 for (size_t i = 0; i < this->count; i++) {
55 release(this->elements[i]);
56 }
57
58 free(this->elements);
59
60 super(Object, self, dealloc);
61}
62
66static String *description(const Object *self) {
67
68 String *components = $((Array *) self, componentsJoinedByCharacters, ", ");
69
70 String *desc = $(alloc(String), initWithFormat, "[%s]", components->chars ?: "");
71
72 release(components);
73
74 return desc;
75}
76
80static int hash(const Object *self) {
81
82 Array *this = (Array *) self;
83
84 int hash = HashForInteger(HASH_SEED, this->count);
85
86 for (size_t i = 0; i < this->count; i++) {
87 hash = HashForObject(hash, this->elements[i]);
88 }
89
90 return hash;
91}
92
96static _Bool isEqual(const Object *self, const Object *other) {
97
98 if (super(Object, self, isEqual, other)) {
99 return true;
100 }
101
102 if (other && $(other, isKindOfClass, _Array())) {
103
104 const Array *this = (Array *) self;
105 const Array *that = (Array *) other;
106
107 if (this->count == that->count) {
108
109 for (size_t i = 0; i < this->count; i++) {
110
111 const Object *thisObject = this->elements[i];
112 const Object *thatObject = that->elements[i];
113
114 if ($(thisObject, isEqual, thatObject) == false) {
115 return false;
116 }
117 }
118
119 return true;
120 }
121 }
122
123 return false;
124}
125
126#pragma mark - Array
127
128
134
135 return $(alloc(Array), initWithArray, array);
136}
137
143
145 if (array) {
146 va_list args;
147 va_start(args, obj);
148
149 while (obj) {
150 array->elements = realloc(array->elements, ++array->count * sizeof(ident));
151 assert(array->elements);
152
153 array->elements[array->count - 1] = retain(obj);
154 obj = va_arg(args, ident);
155 }
156
157 va_end(args);
158 }
159
160 return array;
161}
162
167static Array *arrayWithVaList(va_list args) {
168
169 return $(alloc(Array), initWithVaList, args);
170}
171
176static String *componentsJoinedByCharacters(const Array *self, const char *chars) {
177
178 MutableString *string = $(alloc(MutableString), init);
179
180 for (size_t i = 0; i < self->count; i++) {
181
182 String *desc = $((Object *) self->elements[i], description);
183 $(string, appendString, desc);
184
185 release(desc);
186
187 if (i < self->count - 1) {
188 $(string, appendCharacters, chars);
189 }
190 }
191
192 return (String *) string;
193}
194
200static String *componentsJoinedByString(const Array *self, const String *string) {
201 return $(self, componentsJoinedByCharacters, string->chars);
202}
203
208static _Bool containsObject(const Array *self, const ident obj) {
209
210 return $(self, indexOfObject, obj) != -1;
211}
212
217static void enumerateObjects(const Array *self, ArrayEnumerator enumerator, ident data) {
218
219 assert(enumerator);
220
221 for (size_t i = 0; i < self->count; i++) {
222 enumerator(self, self->elements[i], data);
223 }
224}
225
230static Array *filteredArray(const Array *self, Predicate predicate, ident data) {
231
232 assert(predicate);
233
234 MutableArray *copy = $(self, mutableCopy);
235
236 $(copy, filter, predicate, data);
237
238 return (Array *) copy;
239}
240
245static ident findObject(const Array *self, Predicate predicate, ident data) {
246
247 assert(predicate);
248
249 for (size_t i = 0; i < self->count; i++) {
250 if (predicate(self->elements[i], data)) {
251 return self->elements[i];
252 }
253 }
254
255 return NULL;
256}
257
262static ident firstObject(const Array *self) {
263
264 return self->count ? $(self, objectAtIndex, 0) : NULL;
265}
266
271static ssize_t indexOfObject(const Array *self, const ident obj) {
272
273 assert(obj);
274
275 for (size_t i = 0; i < self->count; i++) {
276 if ($((Object * ) self->elements[i], isEqual, obj)) {
277 return i;
278 }
279 }
280
281 return -1;
282}
283
288static Array *initWithArray(Array *self, const Array *array) {
289
290 self = (Array *) super(Object, self, init);
291 if (self) {
292
293 self->count = array->count;
294 if (self->count) {
295
296 self->elements = calloc(self->count, sizeof(ident));
297 assert(self->elements);
298
299 for (size_t i = 0; i < self->count; i++) {
300 self->elements[i] = retain(array->elements[i]);
301 }
302 }
303 }
304
305 return self;
306}
307
312static Array *initWithObjects(Array *self, ...) {
313
314 va_list args;
315 va_start(args, self);
316
317 self = $(self, initWithVaList, args);
318
319 va_end(args);
320 return self;
321}
322
327static Array *initWithVaList(Array *self, va_list args) {
328
329 self = (Array *) super(Object, self, init);
330 if (self) {
331
332 ident element = va_arg(args, ident);
333 while (element) {
334 self->elements = realloc(self->elements, ++self->count * sizeof(ident));
335 assert(self->elements);
336
337 self->elements[self->count - 1] = retain(element);
338 element = va_arg(args, ident);
339 }
340 }
341
342 return self;
343}
344
349static ident lastObject(const Array *self) {
350
351 return self->count ? $(self, objectAtIndex, self->count - 1) : NULL;
352}
353
358static Array *mappedArray(const Array *self, Functor functor, ident data) {
359
360 assert(functor);
361
363 assert(array);
364
365 for (size_t i = 0; i < self->count; i++) {
366
367 ident obj = functor(self->elements[i], data);
368
369 $(array, addObject, obj);
370
371 release(obj);
372 }
373
374 return (Array *) array;
375}
376
381static MutableArray *mutableCopy(const Array *self) {
382
384 assert(copy);
385
386 $(copy, addObjectsFromArray, self);
387 return copy;
388}
389
394static ident objectAtIndex(const Array *self, size_t index) {
395
396 assert(index < self->count);
397
398 return self->elements[index];
399}
400
405static ident reduce(const Array *self, Reducer reducer, ident accumulator, ident data) {
406
407 assert(reducer);
408
409 for (size_t i = 0; i < self->count; i++) {
410 accumulator = reducer(self->elements[i], accumulator, data);
411 }
412
413 return accumulator;
414}
415
420static Array *sortedArray(const Array *self, Comparator comparator) {
421
422 assert(comparator);
423
424 MutableArray *array = $(self, mutableCopy);
425
426 $(array, sort, comparator);
427
428 return (Array *) array;
429}
430
431#pragma mark - Class lifecycle
432
436static void initialize(Class *clazz) {
437
438 ((ObjectInterface *) clazz->interface)->copy = copy;
439 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
440 ((ObjectInterface *) clazz->interface)->description = description;
441 ((ObjectInterface *) clazz->interface)->hash = hash;
442 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
443
444 ((ArrayInterface *) clazz->interface)->arrayWithArray = arrayWithArray;
445 ((ArrayInterface *) clazz->interface)->arrayWithObjects = arrayWithObjects;
446 ((ArrayInterface *) clazz->interface)->arrayWithVaList = arrayWithVaList;
448 ((ArrayInterface *) clazz->interface)->componentsJoinedByString = componentsJoinedByString;
449 ((ArrayInterface *) clazz->interface)->containsObject = containsObject;
450 ((ArrayInterface *) clazz->interface)->enumerateObjects = enumerateObjects;
451 ((ArrayInterface *) clazz->interface)->filteredArray = filteredArray;
452 ((ArrayInterface *) clazz->interface)->findObject = findObject;
453 ((ArrayInterface *) clazz->interface)->firstObject = firstObject;
454 ((ArrayInterface *) clazz->interface)->indexOfObject = indexOfObject;
455 ((ArrayInterface *) clazz->interface)->initWithArray = initWithArray;
456 ((ArrayInterface *) clazz->interface)->initWithObjects = initWithObjects;
457 ((ArrayInterface *) clazz->interface)->initWithVaList = initWithVaList;
458 ((ArrayInterface *) clazz->interface)->lastObject = lastObject;
459 ((ArrayInterface *) clazz->interface)->mappedArray = mappedArray;
460 ((ArrayInterface *) clazz->interface)->mutableCopy = mutableCopy;
461 ((ArrayInterface *) clazz->interface)->objectAtIndex = objectAtIndex;
462 ((ArrayInterface *) clazz->interface)->reduce = reduce;
463 ((ArrayInterface *) clazz->interface)->sortedArray = sortedArray;
464}
465
470Class *_Array(void) {
471 static Class *clazz;
472 static Once once;
473
474 do_once(&once, {
475 clazz = _initialize(&(const ClassDef) {
476 .name = "Array",
477 .superclass = _Object(),
478 .instanceSize = sizeof(Array),
479 .interfaceOffset = offsetof(Array, interface),
480 .interfaceSize = sizeof(ArrayInterface),
482 });
483 });
484
485 return clazz;
486}
487
488#undef _Class
static void initialize(Class *clazz)
Definition: Array.c:436
Immutable arrays.
void(* ArrayEnumerator)(const Array *array, ident obj, ident data)
A function pointer for Array enumeration (iteration).
Definition: Array.h:49
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
#define obj
#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
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Definition: Hash.c:66
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37
Mutable arrays.
Mutable UTF-8 strings.
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
_Bool(* Predicate)(const ident obj, ident data)
The Predicate function type for filtering Objects.
Definition: Types.h:111
Order(* Comparator)(const ident obj1, const ident obj2)
The Comparator function type for ordering Objects.
Definition: Types.h:82
ident(* Functor)(const ident obj, ident data)
The Functor function type for transforming Objects.
Definition: Types.h:103
ident(* Reducer)(const ident obj, ident accumulator, ident data)
The Reducer function type for reducing collections.
Definition: Types.h:127
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
Immutable arrays.
Definition: Array.h:56
Array * initWithObjects(Array *self,...)
Initializes this Array to contain the Objects in the NULL-terminated arguments list.
Definition: Array.c:312
Class * _Array(void)
The Array archetype.
Definition: Array.c:470
ident reduce(const Array *self, Reducer reducer, ident accumulator, ident data)
Definition: Array.c:405
String * componentsJoinedByString(const Array *self, const String *string)
Returns the components of this Array joined by the specified String.
Definition: Array.c:200
ident findObject(const Array *self, Predicate predicate, ident data)
Definition: Array.c:245
String * componentsJoinedByCharacters(const Array *self, const char *chars)
Returns the components of this Array joined by chars.
Definition: Array.c:176
Array * mappedArray(const Array *self, Functor functor, ident data)
Transforms the elements in this Array by functor.
Definition: Array.c:358
MutableArray * mutableCopy(const Array *self)
Definition: Array.c:381
Array * arrayWithObjects(ident obj,...)
Returns a new Array containing the given Objects.
Definition: Array.c:142
void enumerateObjects(const Array *self, ArrayEnumerator enumerator, ident data)
Enumerate the elements of this Array with the given function.
Definition: Array.c:217
Array * initWithArray(Array *self, const Array *array)
Initializes this Array to contain the Objects in array.
Definition: Array.c:288
Array * initWithVaList(Array *self, va_list args)
Initializes this Array to contain the Objects in the NULL-terminated va_list.
Definition: Array.c:327
Array * filteredArray(const Array *self, Predicate predicate, ident data)
Creates a new Array with elements that pass predicate.
Definition: Array.c:230
ident lastObject(const Array *self)
Definition: Array.c:349
ssize_t indexOfObject(const Array *self, const ident obj)
Definition: Array.c:271
Array * sortedArray(const Array *self, Comparator comparator)
Definition: Array.c:420
ident objectAtIndex(const Array *self, int index)
_Bool containsObject(const Array *self, const ident obj)
Definition: Array.c:208
Array * arrayWithVaList(va_list args)
Returns a new Array containing the Objects in the given va_list.
Definition: Array.c:167
size_t count
The count of elements.
Definition: Array.h:72
Array * arrayWithArray(const Array *array)
Returns a new Array containing the contents of array.
Definition: Array.c:133
ident firstObject(const Array *self)
Definition: Array.c:262
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
DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
Initializes a DateFormatter with the specified format string.
Definition: DateFormatter.c:76
Mutable arrays.
Definition: MutableArray.h:40
void filter(MutableArray *self, Predicate predicate, ident data)
Filters this MutableArray in place using predicate.
Definition: MutableArray.c:171
MutableArray * arrayWithCapacity(size_t capacity)
Returns a new MutableArray with the given capacity.
Definition: MutableArray.c:162
void addObject(MutableArray *self, const ident obj)
Adds the specified Object to this MutableArray.
Definition: MutableArray.c:99
void sort(MutableArray *self, Comparator comparator)
Sorts this MutableArray in place using comparator.
Definition: MutableArray.c:314
MutableArray * array(void)
Returns a new MutableArray.
Definition: MutableArray.c:153
MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Initializes this MutableArray with the specified capacity.
Definition: MutableArray.c:195
void addObjectsFromArray(MutableArray *self, const Array *array)
Adds the Objects contained in array to this MutableArray.
Definition: MutableArray.c:140
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
Mutable UTF-8 strings.
Definition: MutableString.h:40
void appendString(MutableString *self, const String *string)
Appends the specified String to this MutableString.
MutableString * string(void)
Returns a new MutableString.
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
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85