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

#include <MutableArray.h>

Overview

Mutable arrays.

Definition at line 40 of file MutableArray.h.

Inheritance diagram for MutableArray:
Array Object

Properties

Array array
 The superclass. More...
 
- Properties inherited from Array
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_MutableArray (void)
 The MutableArray archetype. More...
 
void addObject (MutableArray *self, const ident obj)
 Adds the specified Object to this MutableArray. More...
 
void addObjects (MutableArray *self, const ident obj,...)
 Adds the specified objects to this Array. More...
 
void addObjectsFromArray (MutableArray *self, const Array *array)
 Adds the Objects contained in array to this MutableArray. More...
 
MutableArrayarray (void)
 Returns a new MutableArray. More...
 
MutableArrayarrayWithCapacity (size_t capacity)
 Returns a new MutableArray with the given capacity. More...
 
void filter (MutableArray *self, Predicate predicate, ident data)
 Filters this MutableArray in place using predicate. More...
 
MutableArrayinit (MutableArray *self)
 Initializes this MutableArray. More...
 
MutableArrayinitWithCapacity (MutableArray *self, size_t capacity)
 Initializes this MutableArray with the specified capacity. More...
 
void insertObjectAtIndex (MutableArray *self, ident obj, size_t index)
 Inserts the Object at the specified index. More...
 
void removeAllObjects (MutableArray *self)
 Removes all Objects from this MutableArray. More...
 
void removeAllObjectsWithEnumerator (MutableArray *self, ArrayEnumerator enumerator, ident data)
 Removes all Objects from this MutableArray, invoking enumerator for each Object. More...
 
void removeAllObjectsWithEnumerator (MutableDictionary *self, DictionaryEnumerator enumerator, ident data)
 Removes all Objects from this MutableDictionary, invoking enumerator for each Object and key pair. More...
 
void removeLastObject (MutableArray *self)
 Removes the last Object from this MutableArray. More...
 
void removeObject (MutableArray *self, const ident obj)
 Removes the specified Object from this MutableArray. More...
 
void removeObjectAtIndex (MutableArray *self, size_t index)
 Removes the Object at the specified index. More...
 
void setObjectAtIndex (MutableArray *self, const ident obj, size_t index)
 Replaces the Object at the specified index. More...
 
void sort (MutableArray *self, Comparator comparator)
 Sorts this MutableArray in place using comparator. More...
 
- Methods inherited from Array
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

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

Property Details

◆ array

MutableArray * array ( void  )

The superclass.

Definition at line 45 of file MutableArray.h.

◆ interface

MutableArrayInterface* MutableArray::interface
protected

The interface.

Definition at line 51 of file MutableArray.h.

Method Details

◆ _MutableArray()

Class * _MutableArray ( void  )

The MutableArray archetype.

Returns
The MutableArray Class.

Definition at line 349 of file MutableArray.c.

349 {
350 static Class *clazz;
351 static Once once;
352
353 do_once(&once, {
354 clazz = _initialize(&(const ClassDef) {
355 .name = "MutableArray",
356 .superclass = _Array(),
357 .instanceSize = sizeof(MutableArray),
358 .interfaceOffset = offsetof(MutableArray, interface),
359 .interfaceSize = sizeof(MutableArrayInterface),
361 });
362 });
363
364 return clazz;
365}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: MutableArray.c:323
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
Class * _Array(void)
The Array archetype.
Definition: Array.c:470
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
Mutable arrays.
Definition: MutableArray.h:40
MutableArrayInterface * interface
The interface.
Definition: MutableArray.h:51
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51

◆ addObject()

void addObject ( MutableArray self,
const ident  obj 
)

Adds the specified Object to this MutableArray.

Parameters
selfThe MutableArray.
objThe Object to add.

Definition at line 99 of file MutableArray.c.

99 {
100
101 Array *array = (Array *) self;
102 if (array->count == self->capacity) {
103
104 self->capacity += ARRAY_CHUNK_SIZE;
105
106 if (array->elements) {
107 array->elements = realloc(array->elements, self->capacity * sizeof(ident));
108 } else {
109 array->elements = calloc(self->capacity, sizeof(ident));
110 }
111
112 assert(array->elements);
113 }
114
115 array->elements[array->count++] = retain(obj);
116}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
#define obj
#define ARRAY_CHUNK_SIZE
Definition: MutableArray.c:75
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
Immutable arrays.
Definition: Array.h:56
size_t count
The count of elements.
Definition: Array.h:72
Array array
The superclass.
Definition: MutableArray.h:45

◆ addObjects()

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

Adds the specified objects to this Array.

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

Definition at line 122 of file MutableArray.c.

122 {
123
124 va_list args;
125 va_start(args, obj);
126
127 ident object = obj;
128 while (object) {
129 $(self, addObject, object);
130 object = va_arg(args, ident);
131 }
132
133 va_end(args);
134}
void addObject(MutableArray *self, const ident obj)
Adds the specified Object to this MutableArray.
Definition: MutableArray.c:99

◆ addObjectsFromArray()

void addObjectsFromArray ( MutableArray self,
const Array array 
)

Adds the Objects contained in array to this MutableArray.

Parameters
selfThe MutableArray.
arrayAn Array.

Definition at line 140 of file MutableArray.c.

140 {
141
142 if (array) {
143 for (size_t i = 0; i < array->count; i++) {
144 $(self, addObject, array->elements[i]);
145 }
146 }
147}

◆ array()

MutableArray * array ( void  )

Returns a new MutableArray.

Returns
The new MutableArray, or NULL on error.

Definition at line 153 of file MutableArray.c.

153 {
154
155 return $(alloc(MutableArray), init);
156}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
MutableArray * init(MutableArray *self)
Initializes this MutableArray.
Definition: MutableArray.c:186

◆ arrayWithCapacity()

MutableArray * arrayWithCapacity ( size_t  capacity)

Returns a new MutableArray with the given capacity.

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

Definition at line 162 of file MutableArray.c.

162 {
163
164 return $(alloc(MutableArray), initWithCapacity, capacity);
165}
MutableArray * initWithCapacity(MutableArray *self, size_t capacity)
Initializes this MutableArray with the specified capacity.
Definition: MutableArray.c:195

◆ filter()

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

Filters this MutableArray in place using predicate.

Parameters
selfThe MutableArray.
predicateA Predicate.
dataUser data.

Definition at line 171 of file MutableArray.c.

171 {
172
173 assert(predicate);
174
175 for (size_t i = 0; i < self->array.count; i++) {
176 if (predicate(self->array.elements[i], data) == false) {
177 $(self, removeObjectAtIndex, i--);
178 }
179 }
180}
static MutableData * data(void)
Definition: MutableData.c:75
void removeObjectAtIndex(MutableArray *self, size_t index)
Removes the Object at the specified index.
Definition: MutableArray.c:282

◆ init()

MutableArray * init ( MutableArray self)

Initializes this MutableArray.

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

Definition at line 186 of file MutableArray.c.

186 {
187
188 return $(self, initWithCapacity, 0);
189}

◆ initWithCapacity()

MutableArray * initWithCapacity ( MutableArray self,
size_t  capacity 
)

Initializes this MutableArray with the specified capacity.

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

Definition at line 195 of file MutableArray.c.

195 {
196
197 self = (MutableArray *) super(Object, self, init);
198 if (self) {
199
200 self->capacity = capacity;
201 if (self->capacity) {
202
203 self->array.elements = calloc(self->capacity, sizeof(ident));
204 assert(self->array.elements);
205 }
206 }
207
208 return self;
209}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46

◆ insertObjectAtIndex()

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

Inserts the Object at the specified index.

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

Definition at line 215 of file MutableArray.c.

215 {
216
217 assert(index <= self->array.count);
218
219 $(self, addObject, obj);
220
221 for (size_t i = self->array.count - 1; i > index; i--) {
222 self->array.elements[i] = self->array.elements[i - 1];
223 }
224
225 self->array.elements[index] = obj;
226}

◆ removeAllObjects()

void removeAllObjects ( MutableArray self)

Removes all Objects from this MutableArray.

Parameters
selfThe MutableArray.

Definition at line 232 of file MutableArray.c.

232 {
233
234 while (self->array.count) {
235 $(self, removeLastObject);
236 }
237}
void removeLastObject(MutableArray *self)
Removes the last Object from this MutableArray.
Definition: MutableArray.c:259

◆ removeAllObjectsWithEnumerator() [1/2]

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

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

Parameters
selfThe MutableArray.
enumeratorThe enumerator.
dataThe data.

Definition at line 243 of file MutableArray.c.

243 {
244
245 assert(enumerator);
246
247 while (self->array.count) {
248
249 enumerator((Array *) self, $((Array *) self, lastObject), data);
250
251 $(self, removeLastObject);
252 }
253}
ident lastObject(const Array *self)
Definition: Array.c:349

◆ removeAllObjectsWithEnumerator() [2/2]

void removeAllObjectsWithEnumerator ( MutableDictionary self,
DictionaryEnumerator  enumerator,
ident  data 
)

Removes all Objects from this MutableDictionary, invoking enumerator for each Object and key pair.

Parameters
selfThe MutableDictionary.
enumeratorThe enumerator.
dataThe data.

Definition at line 143 of file MutableDictionary.c.

143 {
144
145 assert(enumerator);
146
147 for (size_t i = 0; i < self->dictionary.capacity; i++) {
148
149 Array *array = self->dictionary.elements[i];
150 if (array) {
151 for (size_t j = array->count; j > 0; j -= 2) {
152
153 ident obj = array->elements[j - 1];
154 ident key = array->elements[j - 2];
155
156 enumerator((Dictionary *) self, obj, key, data);
157
160 }
161
162 self->dictionary.elements[i] = release(array);
163 }
164 }
165
166 self->dictionary.count = 0;
167}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Immutable key-value stores.
Definition: Dictionary.h:60
size_t count
The count of elements.
Definition: Dictionary.h:82
Dictionary dictionary
The superclass.

◆ removeLastObject()

void removeLastObject ( MutableArray self)

Removes the last Object from this MutableArray.

Parameters
selfThe MutableArray

Definition at line 259 of file MutableArray.c.

259 {
260
261 if (self->array.count) {
262 $(self, removeObjectAtIndex, self->array.count - 1);
263 }
264}

◆ removeObject()

void removeObject ( MutableArray self,
const ident  obj 
)

Removes the specified Object from this MutableArray.

Parameters
selfThe MutableArray.
objThe Object to remove.

Definition at line 270 of file MutableArray.c.

270 {
271
272 const ssize_t index = $((Array *) self, indexOfObject, obj);
273 if (index > -1) {
274 $(self, removeObjectAtIndex, index);
275 }
276}
ssize_t indexOfObject(const Array *self, const ident obj)
Definition: Array.c:271

◆ removeObjectAtIndex()

void removeObjectAtIndex ( MutableArray self,
size_t  index 
)

Removes the Object at the specified index.

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

Definition at line 282 of file MutableArray.c.

282 {
283
284 assert(index < self->array.count);
285
286 release(self->array.elements[index]);
287
288 for (size_t i = index; i < self->array.count - 1; i++) {
289 self->array.elements[i] = self->array.elements[i + 1];
290 }
291
292 self->array.count--;
293}

◆ setObjectAtIndex()

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

Replaces the Object at the specified index.

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

Definition at line 299 of file MutableArray.c.

299 {
300
301 assert(index < self->array.count);
302
303 retain(obj);
304
305 release(self->array.elements[index]);
306
307 self->array.elements[index] = obj;
308}

◆ sort()

void sort ( MutableArray self,
Comparator  comparator 
)

Sorts this MutableArray in place using comparator.

Parameters
selfThe MutableArray.
comparatorA Comparator.

Definition at line 314 of file MutableArray.c.

314 {
315 quicksort(self->array.elements, self->array.count, sizeof(ident), comparator, NULL);
316}
void quicksort(ident base, size_t count, size_t size, Comparator comparator, ident data)
A portability wrapper around reentrant qsort.
Definition: MutableArray.c:67

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