Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
Array.c File Reference
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include "Array.h"
#include "Hash.h"
#include "MutableArray.h"
#include "MutableString.h"

Go to the source code of this file.

Macros

#define _Class   _Array
 

Functions

Class_Array (void)
 
static ArrayarrayWithArray (const Array *array)
 
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 enumerateObjects (const Array *self, ArrayEnumerator enumerator, ident data)
 
static ArrayfilteredArray (const Array *self, Predicate predicate, ident data)
 
static ident findObject (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 void initialize (Class *clazz)
 
static ArrayinitWithArray (Array *self, const Array *array)
 
static ArrayinitWithObjects (Array *self,...)
 
static ArrayinitWithVaList (Array *self, va_list args)
 
static _Bool isEqual (const Object *self, const Object *other)
 
static ident lastObject (const Array *self)
 
static ArraymappedArray (const Array *self, Functor functor, ident data)
 
static MutableArraymutableCopy (const Array *self)
 
static ident objectAtIndex (const Array *self, size_t index)
 
static ident reduce (const Array *self, Reducer reducer, ident accumulator, ident data)
 
static ArraysortedArray (const Array *self, Comparator comparator)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Array

Definition at line 33 of file Array.c.

Function Documentation

◆ _Array()

Class * _Array ( void  )

Definition at line 470 of file Array.c.

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

◆ arrayWithArray()

static Array * arrayWithArray ( const Array array)
static

Definition at line 133 of file Array.c.

133 {
134
135 return $(alloc(Array), initWithArray, array);
136}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
Array * initWithArray(Array *self, const Array *array)
Initializes this Array to contain the Objects in array.
Definition: Array.c:288
MutableArray * array(void)
Returns a new MutableArray.
Definition: MutableArray.c:153

◆ arrayWithObjects()

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

Definition at line 142 of file Array.c.

142 {
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}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
#define obj
#define super(type, obj, method,...)
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
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
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 167 of file Array.c.

167 {
168
169 return $(alloc(Array), initWithVaList, args);
170}
Array * initWithVaList(Array *self, va_list args)
Initializes this Array to contain the Objects in the NULL-terminated va_list.
Definition: Array.c:327

◆ componentsJoinedByCharacters()

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

Definition at line 176 of file Array.c.

176 {
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}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
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
String * description(const Object *self)
Definition: Array.c:66
Immutable UTF-8 strings.
Definition: String.h:69

◆ componentsJoinedByString()

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

Definition at line 200 of file Array.c.

200 {
201 return $(self, componentsJoinedByCharacters, string->chars);
202}
String * componentsJoinedByCharacters(const Array *self, const char *chars)
Returns the components of this Array joined by chars.
Definition: Array.c:176
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 208 of file Array.c.

208 {
209
210 return $(self, indexOfObject, obj) != -1;
211}
ssize_t indexOfObject(const Array *self, const ident obj)
Definition: Array.c:271

◆ copy()

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

Definition at line 40 of file Array.c.

40 {
41
42 const Array *this = (Array *) self;
43
44 return (Object *) $(alloc(Array), initWithArray, this);
45}

◆ dealloc()

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

Definition at line 50 of file Array.c.

50 {
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}
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ description()

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

Definition at line 66 of file Array.c.

66 {
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}
DateFormatter * initWithFormat(DateFormatter *self, const char *fmt)
Initializes a DateFormatter with the specified format string.
Definition: DateFormatter.c:76

◆ enumerateObjects()

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

Definition at line 217 of file Array.c.

217 {
218
219 assert(enumerator);
220
221 for (size_t i = 0; i < self->count; i++) {
222 enumerator(self, self->elements[i], data);
223 }
224}
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75

◆ filteredArray()

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

Definition at line 230 of file Array.c.

230 {
231
232 assert(predicate);
233
234 MutableArray *copy = $(self, mutableCopy);
235
236 $(copy, filter, predicate, data);
237
238 return (Array *) copy;
239}
MutableArray * mutableCopy(const Array *self)
Definition: Array.c:381
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
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40

◆ findObject()

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

Definition at line 245 of file Array.c.

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

◆ firstObject()

static ident firstObject ( const Array self)
static

Definition at line 262 of file Array.c.

262 {
263
264 return self->count ? $(self, objectAtIndex, 0) : NULL;
265}
ident objectAtIndex(const Array *self, int index)

◆ hash()

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

Definition at line 80 of file Array.c.

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

◆ indexOfObject()

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

Definition at line 271 of file Array.c.

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

◆ initialize()

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

Definition at line 436 of file Array.c.

436 {
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}
Array * initWithObjects(Array *self,...)
Initializes this Array to contain the Objects in the NULL-terminated arguments list.
Definition: Array.c:312
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
Array * mappedArray(const Array *self, Functor functor, ident data)
Transforms the elements in this Array by functor.
Definition: Array.c:358
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 * 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
Array * sortedArray(const Array *self, Comparator comparator)
Definition: Array.c:420
_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
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
ident interface
The interface of the Class.
Definition: Class.h:105

◆ initWithArray()

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

Definition at line 288 of file Array.c.

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

◆ initWithObjects()

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

Definition at line 312 of file Array.c.

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

◆ initWithVaList()

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

Definition at line 327 of file Array.c.

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

◆ isEqual()

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

Definition at line 96 of file Array.c.

96 {
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}
Class * _Array(void)
The Array archetype.
Definition: Array.c:470
_Bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition: Object.c:101

◆ lastObject()

static ident lastObject ( const Array self)
static

Definition at line 349 of file Array.c.

349 {
350
351 return self->count ? $(self, objectAtIndex, self->count - 1) : NULL;
352}

◆ mappedArray()

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

Definition at line 358 of file Array.c.

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

◆ mutableCopy()

static MutableArray * mutableCopy ( const Array self)
static

Definition at line 381 of file Array.c.

381 {
382
384 assert(copy);
385
386 $(copy, addObjectsFromArray, self);
387 return copy;
388}
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

◆ objectAtIndex()

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

Definition at line 394 of file Array.c.

394 {
395
396 assert(index < self->count);
397
398 return self->elements[index];
399}

◆ reduce()

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

Definition at line 405 of file Array.c.

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

◆ sortedArray()

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

Definition at line 420 of file Array.c.

420 {
421
422 assert(comparator);
423
424 MutableArray *array = $(self, mutableCopy);
425
426 $(array, sort, comparator);
427
428 return (Array *) array;
429}
void sort(MutableArray *self, Comparator comparator)
Sorts this MutableArray in place using comparator.
Definition: MutableArray.c:314