44 qsort_r(base, count, size, comparator,
_quicksort);
57 qsort_s(base, count, size,
_quicksort, comparator);
70 qsort_r(base, count, size,
_quicksort, comparator);
77#define ARRAY_CHUNK_SIZE 64
103 for (
size_t i = 0; i < this->count; i++) {
107 free(this->elements);
135 for (
size_t i = 0; i < this->count; i++) {
156 if (this->count == that->
count) {
158 for (
size_t i = 0; i < this->count; i++) {
160 const Object *thisObject = this->elements[i];
161 const Object *thatObject = that->elements[i];
163 if ($(thisObject,
isEqual, thatObject) ==
false) {
183 if (self->
count == self->capacity) {
187 if (self->elements) {
188 self->elements = realloc(self->elements, self->capacity *
sizeof(
ident));
190 self->elements = calloc(self->capacity,
sizeof(
ident));
193 assert(self->elements);
211 object = va_arg(args,
ident);
270 assert(
array->elements);
299 for (
size_t i = 0; i < self->
count; i++) {
306 if (i < self->count - 1) {
338 for (
size_t i = 0; i < self->
count; i++) {
339 enumerator(self, self->elements[i],
data);
351 for (
size_t i = 0; i < self->
count; i++) {
352 if (predicate(self->elements[i],
data) ==
false) {
381 for (
size_t i = 0; i < self->
count; i++) {
382 if (predicate(self->elements[i],
data)) {
383 return self->elements[i];
407 for (
size_t i = 0; i < self->
count; i++) {
437 self->elements = calloc(self->
count,
sizeof(
ident));
438 assert(self->elements);
440 for (
size_t i = 0; i < self->
count; i++) {
458 self->capacity = capacity;
459 if (self->capacity) {
461 self->elements = calloc(self->capacity,
sizeof(
ident));
462 assert(self->elements);
476 va_start(args, self);
495 self->elements = realloc(self->elements, ++self->
count *
sizeof(
ident));
496 assert(self->elements);
499 element = va_arg(args,
ident);
512 assert(index <= self->count);
516 for (
size_t i = self->
count - 1; i > index; i--) {
517 self->elements[i] = self->elements[i - 1];
520 self->elements[index] =
obj;
540 for (
size_t i = 0; i < self->
count; i++) {
547 self->elements[i] =
obj;
562 for (
size_t i = 0; i < self->
count; i++) {
580 assert(index < self->count);
582 return self->elements[index];
593 for (
size_t i = 0; i < self->
count; i++) {
594 accumulator = reducer(self->elements[i], accumulator,
data);
606 while (self->
count) {
619 while (self->
count) {
656 assert(index < self->count);
658 release(self->elements[index]);
660 for (
size_t i = index; i < self->
count - 1; i++) {
661 self->elements[i] = self->elements[i + 1];
673 assert(index < self->count);
677 release(self->elements[index]);
679 self->elements[index] =
obj;
705#pragma mark - Class lifecycle
768 .instanceSize =
sizeof(
Array),
769 .interfaceOffset = offsetof(
Array, interface),
770 .interfaceSize =
sizeof(ArrayInterface),
static void filter(Array *self, Predicate predicate, ident data)
static void removeObjectAtIndex(Array *self, size_t index)
static Array * initWithVaList(Array *self, va_list args)
static void setObjectAtIndex(Array *self, const ident obj, size_t index)
static Array * arrayWithVaList(va_list args)
static ident find(const Array *self, Predicate predicate, ident data)
static Array * filteredArray(const Array *self, Predicate predicate, ident data)
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 ident objectAtIndex(const Array *self, size_t index)
static Array * arrayWithCapacity(size_t capacity)
static Array * initWithCapacity(Array *self, size_t capacity)
static String * componentsJoinedByCharacters(const Array *self, const char *chars)
static bool isEqual(const Object *self, const Object *other)
static void insertObjectAtIndex(Array *self, ident obj, size_t index)
static void enumerate(const Array *self, ArrayEnumerator enumerator, ident data)
static void map(Array *self, Functor functor, ident data)
static Array * initWithObjects(Array *self,...)
static void removeObject(Array *self, const ident obj)
static Array * sortedArray(const Array *self, Comparator comparator)
static ident lastObject(const Array *self)
static String * description(const Object *self)
static Array * mappedArray(const Array *self, Functor functor, ident data)
static void addObjectsFromArray(Array *self, const Array *array)
static int _quicksort(const void *a, const void *b, void *data)
GNU qsort_r.
static bool containsObject(const Array *self, const ident obj)
static void removeAllObjects(Array *self)
static String * componentsJoinedByString(const Array *self, const String *string)
static Array * arrayWithArray(const Array *array)
static void addObject(Array *self, const ident obj)
static void sort(Array *self, Comparator comparator)
static Array * initWithArray(Array *self, const Array *array)
static void dealloc(Object *self)
static void addObjects(Array *self, const ident obj,...)
static Object * copy(const Object *self)
static void initialize(Class *clazz)
static void removeLastObject(Array *self)
static Array * arrayWithObjects(ident obj,...)
static ident firstObject(const Array *self)
static Array * array(void)
static void removeAllObjectsWithEnumerator(Array *self, ArrayEnumerator enumerator, ident data)
static ssize_t indexOfObject(const Array *self, const ident obj)
static Array * init(Array *self)
static int hash(const Object *self)
void(* ArrayEnumerator)(const Array *array, ident obj, ident data)
A function pointer for Array enumeration (iteration).
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Class * _initialize(const ClassDef *def)
Initializes the given Class.
ident retain(ident obj)
Atomically increment the given Object's reference count.
#define alloc(type)
Allocate and initialize and instance of type.
#define super(type, obj, method,...)
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
int HashForObject(int hash, const ident obj)
Accumulates the hash value of object into hash.
Utilities for calculating hash values.
#define HASH_SEED
The hash seed value.
static bool isKindOfClass(const Object *self, const Class *clazz)
static String * string(void)
static void appendCharacters(String *self, const char *chars)
static void appendString(String *self, const String *string)
void * ident
The identity type, similar to Objective-C id.
bool(* Predicate)(const ident obj, ident data)
The Predicate function type for filtering Objects.
Order(* Comparator)(const ident obj1, const ident obj2)
The Comparator function type for ordering Objects.
ident(* Functor)(const ident obj, ident data)
The Functor function type for transforming Objects.
ident(* Reducer)(const ident obj, ident accumulator, ident data)
The Reducer function type for reducing collections.
#define do_once(once, block)
Executes the given block at most one time.
Array * initWithObjects(Array *self,...)
Initializes this Array to contain the Objects in the NULL-terminated arguments list.
Array * arrayWithCapacity(size_t capacity)
Returns a new Array with the given capacity.
String * componentsJoinedByCharacters(const Array *self, const char *chars)
Returns the components of this Array joined by chars.
void insertObjectAtIndex(Array *self, ident obj, size_t index)
Inserts the Object at the specified index.
Array * arrayWithObjects(ident obj,...)
Returns a new Array containing the given Objects.
Array * initWithArray(Array *self, const Array *array)
Initializes this Array to contain the Objects in array.
Array * initWithCapacity(Array *self, size_t capacity)
Initializes this Array with the specified capacity.
Array * initWithVaList(Array *self, va_list args)
Initializes this Array to contain the Objects in the NULL-terminated va_list.
Array * arrayWithVaList(va_list args)
Returns a new Array containing the Objects in the given va_list.
size_t count
The count of elements.
Array * arrayWithArray(const Array *array)
Returns a new Array containing the contents of array.
ident find(const Array *self, Predicate predicate, ident data)
ident objectAtIndex(const Array *self, size_t index)
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
Object is the root Class of The Objectively Class hierarchy.
Object * copy(const Object *self)
Creates a shallow copy of this Object.
int hash(const Object *self)
void dealloc(Object *self)
Frees all resources held by this Object.
char * chars
The backing null-terminated UTF-8 encoded character array.