Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
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 "Config.h"
25
26#include <assert.h>
27#include <stdarg.h>
28#include <stdlib.h>
29
30#include "Array.h"
31#include "Hash.h"
32#include "String.h"
33
34#if defined(__APPLE__)
35
39static int _quicksort(void *data, const void *a, const void *b) {
40 return ((Comparator) data)(*((const ident *) a), *((const ident *) b));
41}
42
43void quicksort(ident base, size_t count, size_t size, Comparator comparator, ident data) {
44 qsort_r(base, count, size, comparator, _quicksort);
45}
46
47#elif defined(_WIN32)
48
52static int _quicksort(void *data, const void *a, const void *b) {
53 return ((Comparator) data)(*((const ident *) a), *((const ident *) b));
54}
55
56void quicksort(ident base, size_t count, size_t size, Comparator comparator, ident data) {
57 qsort_s(base, count, size, _quicksort, comparator);
58}
59
60#else
61
65static int _quicksort(const void *a, const void *b, void *data) {
66 return ((Comparator) data)(*((const ident *) a), *((const ident *) b));
67}
68
69void quicksort(ident base, size_t count, size_t size, Comparator comparator, ident data) {
70 qsort_r(base, count, size, _quicksort, comparator);
71}
72
73#endif
74
75#define _Class _Array
76
77#define ARRAY_CHUNK_SIZE 64
78
79#pragma mark - Object
80
84static Object *copy(const Object *self) {
85
86 const Array *this = (Array *) self;
87
88 Array *copy = $(alloc(Array), initWithCapacity, this->count);
89 assert(copy);
90
91 $(copy, addObjectsFromArray, this);
92
93 return (Object *) copy;
94}
95
99static void dealloc(Object *self) {
100
101 Array *this = (Array *) self;
102
103 for (size_t i = 0; i < this->count; i++) {
104 release(this->elements[i]);
105 }
106
107 free(this->elements);
108
109 super(Object, self, dealloc);
110}
111
115static String *description(const Object *self) {
116
117 String *components = $((Array *) self, componentsJoinedByCharacters, ", ");
118
119 String *desc = $(alloc(String), initWithFormat, "[%s]", components->chars ?: "");
120
121 release(components);
122
123 return desc;
124}
125
129static int hash(const Object *self) {
130
131 Array *this = (Array *) self;
132
133 int hash = HashForInteger(HASH_SEED, this->count);
134
135 for (size_t i = 0; i < this->count; i++) {
136 hash = HashForObject(hash, this->elements[i]);
137 }
138
139 return hash;
140}
141
145static bool isEqual(const Object *self, const Object *other) {
146
147 if (super(Object, self, isEqual, other)) {
148 return true;
149 }
150
151 if (other && $(other, isKindOfClass, _Array())) {
152
153 const Array *this = (Array *) self;
154 const Array *that = (Array *) other;
155
156 if (this->count == that->count) {
157
158 for (size_t i = 0; i < this->count; i++) {
159
160 const Object *thisObject = this->elements[i];
161 const Object *thatObject = that->elements[i];
162
163 if ($(thisObject, isEqual, thatObject) == false) {
164 return false;
165 }
166 }
167
168 return true;
169 }
170 }
171
172 return false;
173}
174
175#pragma mark - Array
176
181static void addObject(Array *self, const ident obj) {
182
183 if (self->count == self->capacity) {
184
185 self->capacity += ARRAY_CHUNK_SIZE;
186
187 if (self->elements) {
188 self->elements = realloc(self->elements, self->capacity * sizeof(ident));
189 } else {
190 self->elements = calloc(self->capacity, sizeof(ident));
191 }
192
193 assert(self->elements);
194 }
195
196 self->elements[self->count++] = retain(obj);
197}
198
203static void addObjects(Array *self, const ident obj, ...) {
204
205 va_list args;
206 va_start(args, obj);
207
208 ident object = obj;
209 while (object) {
210 $(self, addObject, object);
211 object = va_arg(args, ident);
212 }
213
214 va_end(args);
215}
216
221static void addObjectsFromArray(Array *self, const Array *array) {
222
223 if (array) {
224 for (size_t i = 0; i < array->count; i++) {
225 $(self, addObject, array->elements[i]);
226 }
227 }
228}
229
234static Array *array(void) {
235
236 return $(alloc(Array), init);
237}
238
244
245 return $(alloc(Array), initWithArray, array);
246}
247
252static Array *arrayWithCapacity(size_t capacity) {
253
254 return $(alloc(Array), initWithCapacity, capacity);
255}
256
262
264 if (array) {
265 va_list args;
266 va_start(args, obj);
267
268 while (obj) {
269 array->elements = realloc(array->elements, ++array->count * sizeof(ident));
270 assert(array->elements);
271
272 array->elements[array->count - 1] = retain(obj);
273 obj = va_arg(args, ident);
274 }
275
276 va_end(args);
277 }
278
279 return array;
280}
281
286static Array *arrayWithVaList(va_list args) {
287
288 return $(alloc(Array), initWithVaList, args);
289}
290
295static String *componentsJoinedByCharacters(const Array *self, const char *chars) {
296
297 String *string = $(alloc(String), init);
298
299 for (size_t i = 0; i < self->count; i++) {
300
301 String *desc = $((Object *) self->elements[i], description);
302 $(string, appendString, desc);
303
304 release(desc);
305
306 if (i < self->count - 1) {
307 $(string, appendCharacters, chars);
308 }
309 }
310
311 return (String *) string;
312}
313
318static String *componentsJoinedByString(const Array *self, const String *string) {
319 return $(self, componentsJoinedByCharacters, string->chars);
320}
321
326static bool containsObject(const Array *self, const ident obj) {
327 return $(self, indexOfObject, obj) != -1;
328}
329
334static void enumerate(const Array *self, ArrayEnumerator enumerator, ident data) {
335
336 assert(enumerator);
337
338 for (size_t i = 0; i < self->count; i++) {
339 enumerator(self, self->elements[i], data);
340 }
341}
342
347static void filter(Array *self, Predicate predicate, ident data) {
348
349 assert(predicate);
350
351 for (size_t i = 0; i < self->count; i++) {
352 if (predicate(self->elements[i], data) == false) {
353 $(self, removeObjectAtIndex, i--);
354 }
355 }
356}
357
362static Array *filteredArray(const Array *self, Predicate predicate, ident data) {
363
364 assert(predicate);
365
366 Array *copy = (Array *) $((Object *) self, copy);
367
368 $(copy, filter, predicate, data);
369
370 return copy;
371}
372
377static ident find(const Array *self, Predicate predicate, ident data) {
378
379 assert(predicate);
380
381 for (size_t i = 0; i < self->count; i++) {
382 if (predicate(self->elements[i], data)) {
383 return self->elements[i];
384 }
385 }
386
387 return NULL;
388}
389
394static ident firstObject(const Array *self) {
395
396 return self->count ? $(self, objectAtIndex, 0) : NULL;
397}
398
403static ssize_t indexOfObject(const Array *self, const ident obj) {
404
405 assert(obj);
406
407 for (size_t i = 0; i < self->count; i++) {
408 if ($((Object * ) self->elements[i], isEqual, obj)) {
409 return i;
410 }
411 }
412
413 return -1;
414}
415
420static Array *init(Array *self) {
421
422 return $(self, initWithCapacity, 0);
423}
424
429static Array *initWithArray(Array *self, const Array *array) {
430
431 self = (Array *) super(Object, self, init);
432 if (self) {
433
434 self->count = array->count;
435 if (self->count) {
436
437 self->elements = calloc(self->count, sizeof(ident));
438 assert(self->elements);
439
440 for (size_t i = 0; i < self->count; i++) {
441 self->elements[i] = retain(array->elements[i]);
442 }
443 }
444 }
445
446 return self;
447}
448
453static Array *initWithCapacity(Array *self, size_t capacity) {
454
455 self = (Array *) super(Object, self, init);
456 if (self) {
457
458 self->capacity = capacity;
459 if (self->capacity) {
460
461 self->elements = calloc(self->capacity, sizeof(ident));
462 assert(self->elements);
463 }
464 }
465
466 return self;
467}
468
473static Array *initWithObjects(Array *self, ...) {
474
475 va_list args;
476 va_start(args, self);
477
478 self = $(self, initWithVaList, args);
479
480 va_end(args);
481 return self;
482}
483
488static Array *initWithVaList(Array *self, va_list args) {
489
490 self = (Array *) super(Object, self, init);
491 if (self) {
492
493 ident element = va_arg(args, ident);
494 while (element) {
495 self->elements = realloc(self->elements, ++self->count * sizeof(ident));
496 assert(self->elements);
497
498 self->elements[self->count - 1] = retain(element);
499 element = va_arg(args, ident);
500 }
501 }
502
503 return self;
504}
505
510static void insertObjectAtIndex(Array *self, ident obj, size_t index) {
511
512 assert(index <= self->count);
513
514 $(self, addObject, obj);
515
516 for (size_t i = self->count - 1; i > index; i--) {
517 self->elements[i] = self->elements[i - 1];
518 }
519
520 self->elements[index] = obj;
521}
522
527static ident lastObject(const Array *self) {
528
529 return self->count ? $(self, objectAtIndex, self->count - 1) : NULL;
530}
531
536static void map(Array *self, Functor functor, ident data) {
537
538 assert(functor);
539
540 for (size_t i = 0; i < self->count; i++) {
541 ident obj = functor(self->elements[i], data);
542
543 retain(obj);
544
545 release(self->elements[i]);
546
547 self->elements[i] = obj;
548 }
549}
550
555static Array *mappedArray(const Array *self, Functor functor, ident data) {
556
557 assert(functor);
558
560 assert(array);
561
562 for (size_t i = 0; i < self->count; i++) {
563
564 ident obj = functor(self->elements[i], data);
565
566 $(array, addObject, obj);
567
568 release(obj);
569 }
570
571 return array;
572}
573
578static ident objectAtIndex(const Array *self, size_t index) {
579
580 assert(index < self->count);
581
582 return self->elements[index];
583}
584
589static ident reduce(const Array *self, Reducer reducer, ident accumulator, ident data) {
590
591 assert(reducer);
592
593 for (size_t i = 0; i < self->count; i++) {
594 accumulator = reducer(self->elements[i], accumulator, data);
595 }
596
597 return accumulator;
598}
599
604static void removeAllObjects(Array *self) {
605
606 while (self->count) {
607 $(self, removeLastObject);
608 }
609}
610
616
617 assert(enumerator);
618
619 while (self->count) {
620
621 enumerator(self, $(self, lastObject), data);
622
623 $(self, removeLastObject);
624 }
625}
626
631static void removeLastObject(Array *self) {
632
633 if (self->count) {
634 $(self, removeObjectAtIndex, self->count - 1);
635 }
636}
637
642static void removeObject(Array *self, const ident obj) {
643
644 const ssize_t index = $(self, indexOfObject, obj);
645 if (index > -1) {
646 $(self, removeObjectAtIndex, index);
647 }
648}
649
654static void removeObjectAtIndex(Array *self, size_t index) {
655
656 assert(index < self->count);
657
658 release(self->elements[index]);
659
660 for (size_t i = index; i < self->count - 1; i++) {
661 self->elements[i] = self->elements[i + 1];
662 }
663
664 self->count--;
665}
666
671static void setObjectAtIndex(Array *self, const ident obj, size_t index) {
672
673 assert(index < self->count);
674
675 retain(obj);
676
677 release(self->elements[index]);
678
679 self->elements[index] = obj;
680}
681
686static void sort(Array *self, Comparator comparator) {
687 quicksort(self->elements, self->count, sizeof(ident), comparator, NULL);
688}
689
694static Array *sortedArray(const Array *self, Comparator comparator) {
695
696 assert(comparator);
697
698 Array *array = (Array *) $((Object *) self, copy);
699
700 $(array, sort, comparator);
701
702 return array;
703}
704
705#pragma mark - Class lifecycle
706
710static void initialize(Class *clazz) {
711
712 ((ObjectInterface *) clazz->interface)->copy = copy;
713 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
714 ((ObjectInterface *) clazz->interface)->description = description;
715 ((ObjectInterface *) clazz->interface)->hash = hash;
716 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
717
718 ((ArrayInterface *) clazz->interface)->addObject = addObject;
719 ((ArrayInterface *) clazz->interface)->addObjects = addObjects;
720 ((ArrayInterface *) clazz->interface)->addObjectsFromArray = addObjectsFromArray;
721 ((ArrayInterface *) clazz->interface)->array = array;
722 ((ArrayInterface *) clazz->interface)->arrayWithArray = arrayWithArray;
723 ((ArrayInterface *) clazz->interface)->arrayWithCapacity = arrayWithCapacity;
724 ((ArrayInterface *) clazz->interface)->arrayWithObjects = arrayWithObjects;
725 ((ArrayInterface *) clazz->interface)->arrayWithVaList = arrayWithVaList;
727 ((ArrayInterface *) clazz->interface)->componentsJoinedByString = componentsJoinedByString;
728 ((ArrayInterface *) clazz->interface)->containsObject = containsObject;
729 ((ArrayInterface *) clazz->interface)->enumerate = enumerate;
730 ((ArrayInterface *) clazz->interface)->filter = filter;
731 ((ArrayInterface *) clazz->interface)->filteredArray = filteredArray;
732 ((ArrayInterface *) clazz->interface)->find = find;
733 ((ArrayInterface *) clazz->interface)->firstObject = firstObject;
734 ((ArrayInterface *) clazz->interface)->indexOfObject = indexOfObject;
735 ((ArrayInterface *) clazz->interface)->init = init;
736 ((ArrayInterface *) clazz->interface)->initWithArray = initWithArray;
737 ((ArrayInterface *) clazz->interface)->initWithCapacity = initWithCapacity;
738 ((ArrayInterface *) clazz->interface)->initWithObjects = initWithObjects;
739 ((ArrayInterface *) clazz->interface)->initWithVaList = initWithVaList;
740 ((ArrayInterface *) clazz->interface)->insertObjectAtIndex = insertObjectAtIndex;
741 ((ArrayInterface *) clazz->interface)->lastObject = lastObject;
742 ((ArrayInterface *) clazz->interface)->map = map;
743 ((ArrayInterface *) clazz->interface)->mappedArray = mappedArray;
744 ((ArrayInterface *) clazz->interface)->objectAtIndex = objectAtIndex;
745 ((ArrayInterface *) clazz->interface)->reduce = reduce;
746 ((ArrayInterface *) clazz->interface)->removeAllObjects = removeAllObjects;
747 ((ArrayInterface *) clazz->interface)->removeAllObjectsWithEnumerator = removeAllObjectsWithEnumerator;
748 ((ArrayInterface *) clazz->interface)->removeLastObject = removeLastObject;
749 ((ArrayInterface *) clazz->interface)->removeObject = removeObject;
750 ((ArrayInterface *) clazz->interface)->removeObjectAtIndex = removeObjectAtIndex;
751 ((ArrayInterface *) clazz->interface)->setObjectAtIndex = setObjectAtIndex;
752 ((ArrayInterface *) clazz->interface)->sort = sort;
753 ((ArrayInterface *) clazz->interface)->sortedArray = sortedArray;
754}
755
760Class *_Array(void) {
761 static Class *clazz;
762 static Once once;
763
764 do_once(&once, {
765 clazz = _initialize(&(const ClassDef) {
766 .name = "Array",
767 .superclass = _Object(),
768 .instanceSize = sizeof(Array),
769 .interfaceOffset = offsetof(Array, interface),
770 .interfaceSize = sizeof(ArrayInterface),
772 });
773 });
774
775 return clazz;
776}
777
778#undef _Class
static void filter(Array *self, Predicate predicate, ident data)
Definition Array.c:347
static void removeObjectAtIndex(Array *self, size_t index)
Definition Array.c:654
static Array * initWithVaList(Array *self, va_list args)
Definition Array.c:488
Class * _Array(void)
Definition Array.c:760
static void setObjectAtIndex(Array *self, const ident obj, size_t index)
Definition Array.c:671
static Array * arrayWithVaList(va_list args)
Definition Array.c:286
static ident find(const Array *self, Predicate predicate, ident data)
Definition Array.c:377
static Array * filteredArray(const Array *self, Predicate predicate, ident data)
Definition Array.c:362
void quicksort(ident base, size_t count, size_t size, Comparator comparator, ident data)
A portability wrapper around reentrant qsort.
Definition Array.c:69
static ident reduce(const Array *self, Reducer reducer, ident accumulator, ident data)
Definition Array.c:589
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
static Array * arrayWithCapacity(size_t capacity)
Definition Array.c:252
static Array * initWithCapacity(Array *self, size_t capacity)
Definition Array.c:453
static String * componentsJoinedByCharacters(const Array *self, const char *chars)
Definition Array.c:295
static bool isEqual(const Object *self, const Object *other)
Definition Array.c:145
#define ARRAY_CHUNK_SIZE
Definition Array.c:77
static void insertObjectAtIndex(Array *self, ident obj, size_t index)
Definition Array.c:510
static void enumerate(const Array *self, ArrayEnumerator enumerator, ident data)
Definition Array.c:334
static void map(Array *self, Functor functor, ident data)
Definition Array.c:536
static Array * initWithObjects(Array *self,...)
Definition Array.c:473
static void removeObject(Array *self, const ident obj)
Definition Array.c:642
static Array * sortedArray(const Array *self, Comparator comparator)
Definition Array.c:694
static ident lastObject(const Array *self)
Definition Array.c:527
static String * description(const Object *self)
Definition Array.c:115
static Array * mappedArray(const Array *self, Functor functor, ident data)
Definition Array.c:555
static void addObjectsFromArray(Array *self, const Array *array)
Definition Array.c:221
static int _quicksort(const void *a, const void *b, void *data)
GNU qsort_r.
Definition Array.c:65
static bool containsObject(const Array *self, const ident obj)
Definition Array.c:326
static void removeAllObjects(Array *self)
Definition Array.c:604
static String * componentsJoinedByString(const Array *self, const String *string)
Definition Array.c:318
static Array * arrayWithArray(const Array *array)
Definition Array.c:243
static void addObject(Array *self, const ident obj)
Definition Array.c:181
static void sort(Array *self, Comparator comparator)
Definition Array.c:686
static Array * initWithArray(Array *self, const Array *array)
Definition Array.c:429
static void dealloc(Object *self)
Definition Array.c:99
static void addObjects(Array *self, const ident obj,...)
Definition Array.c:203
static Object * copy(const Object *self)
Definition Array.c:84
static void initialize(Class *clazz)
Definition Array.c:710
static void removeLastObject(Array *self)
Definition Array.c:631
static Array * arrayWithObjects(ident obj,...)
Definition Array.c:261
static ident firstObject(const Array *self)
Definition Array.c:394
static Array * array(void)
Definition Array.c:234
static void removeAllObjectsWithEnumerator(Array *self, ArrayEnumerator enumerator, ident data)
Definition Array.c:615
static ssize_t indexOfObject(const Array *self, const ident obj)
Definition Array.c:403
static Array * init(Array *self)
Definition Array.c:420
static int hash(const Object *self)
Definition Array.c:129
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:195
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
#define obj
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
#define super(type, obj, method,...)
static Data * data(void)
Definition Data.c:286
static DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition Hash.c:66
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Definition Hash.c:72
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
Definition Hash.h:37
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101
Class * _Object(void)
Definition Object.c:136
static String * string(void)
Definition String.c:905
static void appendCharacters(String *self, const char *chars)
Definition String.c:506
static void appendString(String *self, const String *string)
Definition String.c:555
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
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:473
Array * arrayWithCapacity(size_t capacity)
Returns a new Array with the given capacity.
Definition Array.c:252
String * componentsJoinedByCharacters(const Array *self, const char *chars)
Returns the components of this Array joined by chars.
Definition Array.c:295
void insertObjectAtIndex(Array *self, ident obj, size_t index)
Inserts the Object at the specified index.
Definition Array.c:510
Array * arrayWithObjects(ident obj,...)
Returns a new Array containing the given Objects.
Definition Array.c:261
Array * initWithArray(Array *self, const Array *array)
Initializes this Array to contain the Objects in array.
Definition Array.c:429
Array * initWithCapacity(Array *self, size_t capacity)
Initializes this Array with the specified capacity.
Definition Array.c:453
Array * initWithVaList(Array *self, va_list args)
Initializes this Array to contain the Objects in the NULL-terminated va_list.
Definition Array.c:488
Array * arrayWithVaList(va_list args)
Returns a new Array containing the Objects in the given va_list.
Definition Array.c:286
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:243
ident find(const Array *self, Predicate predicate, ident data)
Definition Array.c:377
ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
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
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:84
int hash(const Object *self)
Definition Array.c:129
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99
UTF-8 strings.
Definition String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition String.h:85