Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
Array Struct Reference

#include <Array.h>

Overview

Immutable arrays.

Definition at line 56 of file Array.h.

Inheritance diagram for Array:
Object MutableArray

Properties

size_t count
 The count of elements. More...
 
Object object
 The superclass. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_Array (void)
 The Array archetype. More...
 
ArrayarrayWithArray (const Array *array)
 Returns a new Array containing the contents of array. More...
 
ArrayarrayWithObjects (ident obj,...)
 Returns a new Array containing the given Objects. More...
 
ArrayarrayWithVaList (va_list args)
 Returns a new Array containing the Objects in the given va_list. More...
 
StringcomponentsJoinedByCharacters (const Array *self, const char *chars)
 Returns the components of this Array joined by chars. More...
 
StringcomponentsJoinedByString (const Array *self, const String *string)
 Returns the components of this Array joined by the specified String. More...
 
_Bool containsObject (const Array *self, const ident obj)
 
void enumerateObjects (const Array *self, ArrayEnumerator enumerator, ident data)
 Enumerate the elements of this Array with the given function. More...
 
ArrayfilteredArray (const Array *self, Predicate predicate, ident data)
 Creates a new Array with elements that pass predicate. More...
 
ident findObject (const Array *self, Predicate predicate, ident data)
 
ident firstObject (const Array *self)
 
ssize_t indexOfObject (const Array *self, const ident obj)
 
ArrayinitWithArray (Array *self, const Array *array)
 Initializes this Array to contain the Objects in array. More...
 
ArrayinitWithObjects (Array *self,...)
 Initializes this Array to contain the Objects in the NULL-terminated arguments list. More...
 
ArrayinitWithVaList (Array *self, va_list args)
 Initializes this Array to contain the Objects in the NULL-terminated va_list. More...
 
ident lastObject (const Array *self)
 
ArraymappedArray (const Array *self, Functor functor, ident data)
 Transforms the elements in this Array by functor. More...
 
MutableArraymutableCopy (const Array *self)
 
ident objectAtIndex (const Array *self, int index)
 
ident reduce (const Array *self, Reducer reducer, ident accumulator, ident data)
 
ArraysortedArray (const Array *self, Comparator comparator)
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

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

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 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
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:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136

◆ 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 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
static MutableArray * array(void)
Definition: MutableArray.c:153
Array * initWithArray(Array *self, const Array *array)
Initializes this Array to contain the Objects in array.
Definition: Array.c:288

◆ 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 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
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Object * init(Object *self)
Initializes this Object.
Definition: Object.c:83

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

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 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
static MutableString * string(void)
static void appendString(MutableString *self, const String *string)
static void appendCharacters(MutableString *self, const char *chars)
Definition: MutableString.c:54
Mutable UTF-8 strings.
Definition: MutableString.h:40
String * description(const Object *self)
Definition: Array.c:66
Immutable 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 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()

_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 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

◆ enumerateObjects()

void enumerateObjects ( 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 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}
static MutableData * data(void)
Definition: MutableData.c:75

◆ 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 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}
static void filter(MutableArray *self, Predicate predicate, ident data)
Definition: MutableArray.c:171
MutableArray * mutableCopy(const Array *self)
Definition: Array.c:381
Mutable arrays.
Definition: MutableArray.h:40
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40

◆ findObject()

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

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

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)

◆ 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 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

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

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

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

◆ lastObject()

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

Definition at line 349 of file Array.c.

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

◆ 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 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}
static MutableArray * arrayWithCapacity(size_t capacity)
Definition: MutableArray.c:162
static void addObject(MutableArray *self, const ident obj)
Definition: MutableArray.c:99

◆ mutableCopy()

MutableArray * mutableCopy ( const Array self)
Parameters
selfThe Array.
Returns
A MutableArray with the contents of this Array.

Definition at line 381 of file Array.c.

381 {
382
384 assert(copy);
385
386 $(copy, addObjectsFromArray, self);
387 return copy;
388}
static MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Definition: MutableArray.c:195
static void addObjectsFromArray(MutableArray *self, const Array *array)
Definition: MutableArray.c:140

◆ objectAtIndex()

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

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

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 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}
static void sort(MutableArray *self, Comparator comparator)
Definition: MutableArray.c:314

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