Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Array.c File Reference
#include "Config.h"
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include "Array.h"
#include "Hash.h"
#include "String.h"

Go to the source code of this file.

Macros

#define _Class   _Array
 
#define ARRAY_CHUNK_SIZE   64
 

Functions

Class_Array (void)
 
static int _quicksort (const void *a, const void *b, void *data)
 GNU qsort_r.
 
static void addObject (Array *self, const ident obj)
 
static void addObjects (Array *self, const ident obj,...)
 
static void addObjectsFromArray (Array *self, const Array *array)
 
static Arrayarray (void)
 
static ArrayarrayWithArray (const Array *array)
 
static ArrayarrayWithCapacity (size_t capacity)
 
static ArrayarrayWithObjects (ident obj,...)
 
static ArrayarrayWithVaList (va_list args)
 
static StringcomponentsJoinedByCharacters (const Array *self, const char *chars)
 
static StringcomponentsJoinedByString (const Array *self, const String *string)
 
static bool containsObject (const Array *self, const ident obj)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static Stringdescription (const Object *self)
 
static void enumerate (const Array *self, ArrayEnumerator enumerator, ident data)
 
static void filter (Array *self, Predicate predicate, ident data)
 
static ArrayfilteredArray (const Array *self, Predicate predicate, ident data)
 
static ident find (const Array *self, Predicate predicate, ident data)
 
static ident firstObject (const Array *self)
 
static int hash (const Object *self)
 
static ssize_t indexOfObject (const Array *self, const ident obj)
 
static Arrayinit (Array *self)
 
static void initialize (Class *clazz)
 
static ArrayinitWithArray (Array *self, const Array *array)
 
static ArrayinitWithCapacity (Array *self, size_t capacity)
 
static ArrayinitWithObjects (Array *self,...)
 
static ArrayinitWithVaList (Array *self, va_list args)
 
static void insertObjectAtIndex (Array *self, ident obj, size_t index)
 
static bool isEqual (const Object *self, const Object *other)
 
static ident lastObject (const Array *self)
 
static void map (Array *self, Functor functor, ident data)
 
static ArraymappedArray (const Array *self, Functor functor, ident data)
 
static ident objectAtIndex (const Array *self, size_t index)
 
void quicksort (ident base, size_t count, size_t size, Comparator comparator, ident data)
 A portability wrapper around reentrant qsort.
 
static ident reduce (const Array *self, Reducer reducer, ident accumulator, ident data)
 
static void removeAllObjects (Array *self)
 
static void removeAllObjectsWithEnumerator (Array *self, ArrayEnumerator enumerator, ident data)
 
static void removeLastObject (Array *self)
 
static void removeObject (Array *self, const ident obj)
 
static void removeObjectAtIndex (Array *self, size_t index)
 
static void setObjectAtIndex (Array *self, const ident obj, size_t index)
 
static void sort (Array *self, Comparator comparator)
 
static ArraysortedArray (const Array *self, Comparator comparator)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Array

Definition at line 75 of file Array.c.

◆ ARRAY_CHUNK_SIZE

#define ARRAY_CHUNK_SIZE   64

Definition at line 77 of file Array.c.

Function Documentation

◆ _Array()

Class * _Array ( void  )

Definition at line 760 of file Array.c.

760 {
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}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
Class * _Object(void)
Definition Object.c:136
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
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

◆ _quicksort()

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

GNU qsort_r.

Definition at line 65 of file Array.c.

65 {
66 return ((Comparator) data)(*((const ident *) a), *((const ident *) b));
67}
static Data * data(void)
Definition Data.c:286
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

◆ addObject()

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

Definition at line 181 of file Array.c.

181 {
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}
#define ARRAY_CHUNK_SIZE
Definition Array.c:77
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
#define obj
size_t count
The count of elements.
Definition Array.h:72

◆ addObjects()

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

Definition at line 203 of file Array.c.

203 {
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}
static void addObject(Array *self, const ident obj)
Definition Array.c:181

◆ addObjectsFromArray()

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

Definition at line 221 of file Array.c.

221 {
222
223 if (array) {
224 for (size_t i = 0; i < array->count; i++) {
225 $(self, addObject, array->elements[i]);
226 }
227 }
228}
static Array * array(void)
Definition Array.c:234

◆ array()

static Array * array ( void  )
static

Definition at line 234 of file Array.c.

234 {
235
236 return $(alloc(Array), init);
237}
static Array * init(Array *self)
Definition Array.c:420
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176

◆ arrayWithArray()

static Array * arrayWithArray ( const Array array)
static

Definition at line 243 of file Array.c.

243 {
244
245 return $(alloc(Array), initWithArray, array);
246}
static Array * initWithArray(Array *self, const Array *array)
Definition Array.c:429

◆ arrayWithCapacity()

static Array * arrayWithCapacity ( size_t  capacity)
static

Definition at line 252 of file Array.c.

252 {
253
254 return $(alloc(Array), initWithCapacity, capacity);
255}
static Array * initWithCapacity(Array *self, size_t capacity)
Definition Array.c:453

◆ arrayWithObjects()

static Array * arrayWithObjects ( ident  obj,
  ... 
)
static

Definition at line 261 of file Array.c.

261 {
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}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ arrayWithVaList()

static Array * arrayWithVaList ( va_list  args)
static

Definition at line 286 of file Array.c.

286 {
287
288 return $(alloc(Array), initWithVaList, args);
289}
static Array * initWithVaList(Array *self, va_list args)
Definition Array.c:488

◆ componentsJoinedByCharacters()

static String * componentsJoinedByCharacters ( const Array self,
const char *  chars 
)
static

Definition at line 295 of file Array.c.

295 {
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}
static String * description(const Object *self)
Definition Array.c:115
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
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.
Definition String.h:69

◆ componentsJoinedByString()

static String * componentsJoinedByString ( const Array self,
const String string 
)
static

Definition at line 318 of file Array.c.

318 {
319 return $(self, componentsJoinedByCharacters, string->chars);
320}
static String * componentsJoinedByCharacters(const Array *self, const char *chars)
Definition Array.c:295
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition String.h:85

◆ containsObject()

static bool containsObject ( const Array self,
const ident  obj 
)
static

Definition at line 326 of file Array.c.

326 {
327 return $(self, indexOfObject, obj) != -1;
328}
static ssize_t indexOfObject(const Array *self, const ident obj)
Definition Array.c:403

◆ copy()

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

Definition at line 84 of file Array.c.

84 {
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}
static void addObjectsFromArray(Array *self, const Array *array)
Definition Array.c:221
static Object * copy(const Object *self)
Definition Array.c:84

◆ dealloc()

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

Definition at line 99 of file Array.c.

99 {
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}
static void dealloc(Object *self)
Definition Array.c:99

◆ description()

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

Definition at line 115 of file Array.c.

115 {
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}
static DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)

◆ enumerate()

static void enumerate ( const Array self,
ArrayEnumerator  enumerator,
ident  data 
)
static

Definition at line 334 of file Array.c.

334 {
335
336 assert(enumerator);
337
338 for (size_t i = 0; i < self->count; i++) {
339 enumerator(self, self->elements[i], data);
340 }
341}

◆ filter()

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

Definition at line 347 of file Array.c.

347 {
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}
static void removeObjectAtIndex(Array *self, size_t index)
Definition Array.c:654

◆ filteredArray()

static Array * filteredArray ( const Array self,
Predicate  predicate,
ident  data 
)
static

Definition at line 362 of file Array.c.

362 {
363
364 assert(predicate);
365
366 Array *copy = (Array *) $((Object *) self, copy);
367
368 $(copy, filter, predicate, data);
369
370 return copy;
371}
static void filter(Array *self, Predicate predicate, ident data)
Definition Array.c:347

◆ find()

static ident find ( const Array self,
Predicate  predicate,
ident  data 
)
static

Definition at line 377 of file Array.c.

377 {
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}

◆ firstObject()

static ident firstObject ( const Array self)
static

Definition at line 394 of file Array.c.

394 {
395
396 return self->count ? $(self, objectAtIndex, 0) : NULL;
397}
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578

◆ hash()

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

Definition at line 129 of file Array.c.

129 {
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}
static int hash(const Object *self)
Definition Array.c:129
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
#define HASH_SEED
The hash seed value.
Definition Hash.h:37

◆ indexOfObject()

static ssize_t indexOfObject ( const Array self,
const ident  obj 
)
static

Definition at line 403 of file Array.c.

403 {
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}
static bool isEqual(const Object *self, const Object *other)
Definition Array.c:145

◆ init()

static Array * init ( Array self)
static

Definition at line 420 of file Array.c.

420 {
421
422 return $(self, initWithCapacity, 0);
423}

◆ initialize()

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

Definition at line 710 of file Array.c.

710 {
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}
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
static ident reduce(const Array *self, Reducer reducer, ident accumulator, ident data)
Definition Array.c:589
static Array * arrayWithCapacity(size_t capacity)
Definition Array.c:252
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 Array * mappedArray(const Array *self, Functor functor, ident data)
Definition Array.c:555
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 sort(Array *self, Comparator comparator)
Definition Array.c:686
static void addObjects(Array *self, const ident obj,...)
Definition Array.c:203
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 void removeAllObjectsWithEnumerator(Array *self, ArrayEnumerator enumerator, ident data)
Definition Array.c:615
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
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
ident interface
The interface of the Class.
Definition Class.h:105
int hash(const Object *self)
Definition Array.c:129
void dealloc(Object *self)
Frees all resources held by this Object.
Definition Array.c:99

◆ initWithArray()

static Array * initWithArray ( Array self,
const Array array 
)
static

Definition at line 429 of file Array.c.

429 {
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}

◆ initWithCapacity()

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

Definition at line 453 of file Array.c.

453 {
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}

◆ initWithObjects()

static Array * initWithObjects ( Array self,
  ... 
)
static

Definition at line 473 of file Array.c.

473 {
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}

◆ initWithVaList()

static Array * initWithVaList ( Array self,
va_list  args 
)
static

Definition at line 488 of file Array.c.

488 {
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}

◆ insertObjectAtIndex()

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

Definition at line 510 of file Array.c.

510 {
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}

◆ isEqual()

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

Definition at line 145 of file Array.c.

145 {
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}
Class * _Array(void)
Definition Array.c:760
static bool isKindOfClass(const Object *self, const Class *clazz)
Definition Object.c:101

◆ lastObject()

static ident lastObject ( const Array self)
static

Definition at line 527 of file Array.c.

527 {
528
529 return self->count ? $(self, objectAtIndex, self->count - 1) : NULL;
530}

◆ map()

static void map ( Array self,
Functor  functor,
ident  data 
)
static

Definition at line 536 of file Array.c.

536 {
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}

◆ mappedArray()

static Array * mappedArray ( const Array self,
Functor  functor,
ident  data 
)
static

Definition at line 555 of file Array.c.

555 {
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}

◆ objectAtIndex()

static ident objectAtIndex ( const Array self,
size_t  index 
)
static

Definition at line 578 of file Array.c.

578 {
579
580 assert(index < self->count);
581
582 return self->elements[index];
583}

◆ 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 69 of file Array.c.

69 {
70 qsort_r(base, count, size, _quicksort, comparator);
71}
static int _quicksort(const void *a, const void *b, void *data)
GNU qsort_r.
Definition Array.c:65

◆ reduce()

static ident reduce ( const Array self,
Reducer  reducer,
ident  accumulator,
ident  data 
)
static

Definition at line 589 of file Array.c.

589 {
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}

◆ removeAllObjects()

static void removeAllObjects ( Array self)
static

Definition at line 604 of file Array.c.

604 {
605
606 while (self->count) {
607 $(self, removeLastObject);
608 }
609}

◆ removeAllObjectsWithEnumerator()

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

Definition at line 615 of file Array.c.

615 {
616
617 assert(enumerator);
618
619 while (self->count) {
620
621 enumerator(self, $(self, lastObject), data);
622
623 $(self, removeLastObject);
624 }
625}

◆ removeLastObject()

static void removeLastObject ( Array self)
static

Definition at line 631 of file Array.c.

631 {
632
633 if (self->count) {
634 $(self, removeObjectAtIndex, self->count - 1);
635 }
636}

◆ removeObject()

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

Definition at line 642 of file Array.c.

642 {
643
644 const ssize_t index = $(self, indexOfObject, obj);
645 if (index > -1) {
646 $(self, removeObjectAtIndex, index);
647 }
648}

◆ removeObjectAtIndex()

static void removeObjectAtIndex ( Array self,
size_t  index 
)
static

Definition at line 654 of file Array.c.

654 {
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}

◆ setObjectAtIndex()

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

Definition at line 671 of file Array.c.

671 {
672
673 assert(index < self->count);
674
675 retain(obj);
676
677 release(self->elements[index]);
678
679 self->elements[index] = obj;
680}

◆ sort()

static void sort ( Array self,
Comparator  comparator 
)
static

Definition at line 686 of file Array.c.

686 {
687 quicksort(self->elements, self->count, sizeof(ident), comparator, NULL);
688}
void quicksort(ident base, size_t count, size_t size, Comparator comparator, ident data)
A portability wrapper around reentrant qsort.
Definition Array.c:69

◆ sortedArray()

static Array * sortedArray ( const Array self,
Comparator  comparator 
)
static

Definition at line 694 of file Array.c.

694 {
695
696 assert(comparator);
697
698 Array *array = (Array *) $((Object *) self, copy);
699
700 $(array, sort, comparator);
701
702 return array;
703}