Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Array Struct Reference

#include <Array.h>

Overview

Arrays.

Definition at line 56 of file Array.h.

Inheritance diagram for Array:
Object

Properties

size_t count
 The count of elements.
 
Object object
 The superclass.
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class_Array (void)
 The Array archetype.
 
void addObject (Array *self, const ident obj)
 Adds the specified Object to this Array.
 
void addObjects (Array *self, const ident obj,...)
 Adds the specified objects to this Array.
 
void addObjectsFromArray (Array *self, const Array *array)
 Adds the Objects contained in array to this Array.
 
Arrayarray (void)
 Returns a new Array.
 
ArrayarrayWithArray (const Array *array)
 Returns a new Array containing the contents of array.
 
ArrayarrayWithCapacity (size_t capacity)
 Returns a new Array with the given capacity.
 
ArrayarrayWithObjects (ident obj,...)
 Returns a new Array containing the given Objects.
 
ArrayarrayWithVaList (va_list args)
 Returns a new Array containing the Objects in the given va_list.
 
StringcomponentsJoinedByCharacters (const Array *self, const char *chars)
 Returns the components of this Array joined by chars.
 
StringcomponentsJoinedByString (const Array *self, const String *string)
 Returns the components of this Array joined by the specified String.
 
bool containsObject (const Array *self, const ident obj)
 
void enumerate (const Array *self, ArrayEnumerator enumerator, ident data)
 Enumerate the elements of this Array with the given function.
 
void filter (Array *self, Predicate predicate, ident data)
 Filters this Array in place using predicate.
 
ArrayfilteredArray (const Array *self, Predicate predicate, ident data)
 Creates a new Array with elements that pass predicate.
 
ident find (const Array *self, Predicate predicate, ident data)
 
ident firstObject (const Array *self)
 
ssize_t indexOfObject (const Array *self, const ident obj)
 
Arrayinit (Array *self)
 Initializes this Array.
 
ArrayinitWithArray (Array *self, const Array *array)
 Initializes this Array to contain the Objects in array.
 
ArrayinitWithCapacity (Array *self, size_t capacity)
 Initializes this Array with the specified capacity.
 
ArrayinitWithObjects (Array *self,...)
 Initializes this Array to contain the Objects in the NULL-terminated arguments list.
 
ArrayinitWithVaList (Array *self, va_list args)
 Initializes this Array to contain the Objects in the NULL-terminated va_list.
 
void insertObjectAtIndex (Array *self, ident obj, size_t index)
 Inserts the Object at the specified index.
 
ident lastObject (const Array *self)
 
void map (Array *self, Functor functor, ident data)
 Transforms the elements in this Array in place using functor.
 
ArraymappedArray (const Array *self, Functor functor, ident data)
 Transforms the elements in this Array by functor.
 
ident objectAtIndex (const Array *self, size_t index)
 
ident reduce (const Array *self, Reducer reducer, ident accumulator, ident data)
 
void removeAllObjects (Array *self)
 Removes all Objects from this Array.
 
void removeAllObjectsWithEnumerator (Array *self, ArrayEnumerator enumerator, ident data)
 Removes all Objects from this Array, invoking enumerator for each Object.
 
void removeLastObject (Array *self)
 Removes the last Object from this Array.
 
void removeObject (Array *self, const ident obj)
 Removes the specified Object from this Array.
 
void removeObjectAtIndex (Array *self, size_t index)
 Removes the Object at the specified index.
 
void setObjectAtIndex (Array *self, const ident obj, size_t index)
 Replaces the Object at the specified index.
 
void sort (Array *self, Comparator comparator)
 Sorts this Array in place using comparator.
 
ArraysortedArray (const Array *self, Comparator comparator)
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

ArrayInterface * interface
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

Property Details

◆ count

size_t Array::count

The count of elements.

Definition at line 72 of file Array.h.

◆ interface

ArrayInterface* Array::interface
protected

The interface.

Definition at line 67 of file Array.h.

◆ object

Object Array::object

The superclass.

Definition at line 61 of file Array.h.

Method Details

◆ _Array()

Class * _Array ( void  )

The Array archetype.

Returns
The Array Class.

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
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
ArrayInterface * interface
The interface.
Definition Array.h:67
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 * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136

◆ addObject()

void addObject ( Array self,
const ident  obj 
)

Adds the specified Object to this Array.

Parameters
selfThe Array.
objThe Object to add.

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
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
size_t count
The count of elements.
Definition Array.h:72

◆ addObjects()

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

Adds the specified objects to this Array.

Parameters
selfThe Array.
objThe NULL-terminated list of objects.

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}
void addObject(Array *self, const ident obj)
Adds the specified Object to this Array.
Definition Array.c:181

◆ addObjectsFromArray()

void addObjectsFromArray ( Array self,
const Array array 
)

Adds the Objects contained in array to this Array.

Parameters
selfThe Array.
arrayAn Array.

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}
Array * array(void)
Returns a new Array.
Definition Array.c:234

◆ array()

Array * array ( void  )

Returns a new Array.

Returns
The new Array, or NULL on error.

Definition at line 234 of file Array.c.

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

◆ arrayWithArray()

Array * arrayWithArray ( const Array array)

Returns a new Array containing the contents of array.

Parameters
arrayAn Array.
Returns
The new Array, or NULL on error.

Definition at line 243 of file Array.c.

243 {
244
245 return $(alloc(Array), initWithArray, array);
246}
Array * initWithArray(Array *self, const Array *array)
Initializes this Array to contain the Objects in array.
Definition Array.c:429

◆ arrayWithCapacity()

Array * arrayWithCapacity ( size_t  capacity)

Returns a new Array with the given capacity.

Parameters
capacityThe desired initial capacity.
Returns
The new Array, or NULL on error.

Definition at line 252 of file Array.c.

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

◆ arrayWithObjects()

Array * arrayWithObjects ( ident  obj,
  ... 
)

Returns a new Array containing the given Objects.

Parameters
objThe first in a NULL-terminated list of Objects.
Returns
The new Array, or NULL on error.

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()

Array * arrayWithVaList ( va_list  args)

Returns a new Array containing the Objects in the given va_list.

Parameters
argsThe NULL-terminated va_list of Objects.
Returns
The new Array, or NULL on error.

Definition at line 286 of file Array.c.

286 {
287
288 return $(alloc(Array), initWithVaList, args);
289}
Array * initWithVaList(Array *self, va_list args)
Initializes this Array to contain the Objects in the NULL-terminated va_list.
Definition Array.c:488

◆ componentsJoinedByCharacters()

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

Returns the components of this Array joined by chars.

Parameters
selfThe Array.
charsThe joining characters.
Returns
A String comprised of the components of this Array, joined by chars.

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}
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
String * description(const Object *self)
Definition Array.c:115
UTF-8 strings.
Definition String.h:69

◆ componentsJoinedByString()

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

Returns the components of this Array joined by the specified String.

Parameters
selfThe Array.
stringThe joining String.
Returns
A String comprised of the components of this Array, joined by string.

Definition at line 318 of file Array.c.

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

◆ containsObject()

bool containsObject ( const Array self,
const ident  obj 
)
Parameters
selfThe Array.
objAn Object.
Returns
true if this Array contains the given Object, false otherwise.

Definition at line 326 of file Array.c.

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

◆ enumerate()

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

Enumerate the elements of this Array with the given function.

Parameters
selfThe Array.
enumeratorThe enumerator function.
dataUser data.

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}
static Data * data(void)
Definition Data.c:286

◆ filter()

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

Filters this Array in place using predicate.

Parameters
selfThe Array.
predicateA Predicate.
dataUser data.

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}
void removeObjectAtIndex(Array *self, size_t index)
Removes the Object at the specified index.
Definition Array.c:654

◆ filteredArray()

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

Creates a new Array with elements that pass predicate.

Parameters
selfThe Array.
predicateThe predicate function.
dataUser data.
Returns
The new, filtered Array.

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}
void filter(Array *self, Predicate predicate, ident data)
Filters this Array in place using predicate.
Definition Array.c:347
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition Array.c:84

◆ find()

ident find ( const Array self,
Predicate  predicate,
ident  data 
)
Parameters
selfThe Array.
predicateThe predicate function.
dataUser data.
Returns
The first element of this Array to pass the predicate function.

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()

ident firstObject ( const Array self)
Parameters
selfThe Array.
Returns
The first Object in this Array, or NULL if empty.

Definition at line 394 of file Array.c.

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

◆ indexOfObject()

ssize_t indexOfObject ( const Array self,
const ident  obj 
)
Parameters
selfThe Array.
objAn Object.
Returns
The index of the given Object, or -1 if not found.

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}
bool isEqual(const Object *self, const Object *other)
Tests equality of the other Object.
Definition Array.c:145

◆ init()

Array * init ( Array self)

Initializes this Array.

Parameters
selfThe Array.
Returns
The initialized Array, or NULL on error.

Definition at line 420 of file Array.c.

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

◆ initWithArray()

Array * initWithArray ( Array self,
const Array array 
)

Initializes this Array to contain the Objects in array.

Parameters
selfThe Array.
arrayAn Array.
Returns
The initialized Array, or NULL on error.

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()

Array * initWithCapacity ( Array self,
size_t  capacity 
)

Initializes this Array with the specified capacity.

Parameters
selfThe Array.
capacityThe desired initial capacity.
Returns
The initialized Array, or NULL on error.

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()

Array * initWithObjects ( Array self,
  ... 
)

Initializes this Array to contain the Objects in the NULL-terminated arguments list.

Parameters
selfThe Array.
Returns
The initialized Array, or NULL on error.

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()

Array * initWithVaList ( Array self,
va_list  args 
)

Initializes this Array to contain the Objects in the NULL-terminated va_list.

Parameters
selfThe Array.
argsThe NULL-terminated va_list of Objects.
Returns
The initialized Array, or NULL on error.

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()

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

Inserts the Object at the specified index.

Parameters
selfThe Array.
objThe Object to insert.
indexThe index at which to insert.

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}

◆ lastObject()

ident lastObject ( const Array self)
Parameters
selfThe Array.
Returns
The last Object in this Array, or NULL if empty.

Definition at line 527 of file Array.c.

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

◆ map()

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

Transforms the elements in this Array in place using functor.

Parameters
selfThe Array.
functorThe Functor.
dataUser data.

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()

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

Transforms the elements in this Array by functor.

Parameters
selfThe Array.
functorThe Functor.
dataUser data.
Returns
An Array containing the transformed elements of this Array.

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()

ident objectAtIndex ( const Array self,
size_t  index 
)
Parameters
selfThe Array.
indexThe index of the desired Object.
Returns
The Object at the specified index.

Definition at line 578 of file Array.c.

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

◆ reduce()

ident reduce ( const Array self,
Reducer  reducer,
ident  accumulator,
ident  data 
)
Parameters
selfThe Array.
reducerThe Reducer.
accumulatorThe initial accumulator value.
dataUser data.
Returns
The reduction result.

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()

void removeAllObjects ( Array self)

Removes all Objects from this Array.

Parameters
selfThe Array.

Definition at line 604 of file Array.c.

604 {
605
606 while (self->count) {
607 $(self, removeLastObject);
608 }
609}
void removeLastObject(Array *self)
Removes the last Object from this Array.
Definition Array.c:631

◆ removeAllObjectsWithEnumerator()

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

Removes all Objects from this Array, invoking enumerator for each Object.

Parameters
selfThe Array.
enumeratorThe enumerator.
dataThe data.

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}
ident lastObject(const Array *self)
Definition Array.c:527

◆ removeLastObject()

void removeLastObject ( Array self)

Removes the last Object from this Array.

Parameters
selfThe Array

Definition at line 631 of file Array.c.

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

◆ removeObject()

void removeObject ( Array self,
const ident  obj 
)

Removes the specified Object from this Array.

Parameters
selfThe Array.
objThe Object to remove.

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()

void removeObjectAtIndex ( Array self,
size_t  index 
)

Removes the Object at the specified index.

Parameters
selfThe Array.
indexThe index of the Object to remove.

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()

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

Replaces the Object at the specified index.

Parameters
selfThe Array.
objThe Object with which to replace.
indexThe index of the Object to replace.
Remarks
The index must not exceed the size of the Array.

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()

void sort ( Array self,
Comparator  comparator 
)

Sorts this Array in place using comparator.

Parameters
selfThe Array.
comparatorThe Comparator.

Definition at line 686 of file Array.c.

686 {
687 quicksort(self->elements, self->count, sizeof(ident), comparator, NULL);
688}
OBJECTIVELY_EXPORT 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()

Array * sortedArray ( const Array self,
Comparator  comparator 
)
Parameters
selfThe Array.
comparatorThe Comparator
Returns
A copy of this Array, sorted by the given Comparator.

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}
void sort(Array *self, Comparator comparator)
Sorts this Array in place using comparator.
Definition Array.c:686

The documentation for this struct was generated from the following files: