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

#include <Vector.h>

Overview

Vectors.

Definition at line 51 of file Vector.h.

Inheritance diagram for Vector:
Object

Properties

size_t capacity
 The capacity.
 
size_t count
 The count of elements.
 
Consumer destroy
 Optional destructor called when an element is removed.
 
ident elements
 The elements.
 
Object object
 The superclass.
 
size_t size
 The size of each element.
 
- 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_Vector (void)
 The Vector archetype.
 
void add (Vector *self, const ident element)
 Adds the specified element to this Vector.
 
void enumerate (const Vector *self, VectorEnumerator enumerator, ident data)
 Enumerates the elements of this Vector with the given function.
 
void filter (const Vector *self, Predicate predicate, ident data)
 Filters the elements of this Vector with the given Predicate.
 
void filter (Vector *self, Predicate predicate, ident data)
 
ident find (const Vector *self, Predicate predicate, ident data)
 
ssize_t indexOf (const Vector *self, const ident element)
 
VectorinitWithElements (Vector *self, size_t size, size_t count, ident elements)
 Initializes this Vector with the specified elements.
 
VectorinitWithSize (Vector *self, size_t size)
 Initializes this Vector with the specified element size.
 
void insert (Vector *self, const ident element, size_t index)
 Inserts the element at the specified index.
 
VectormappedVector (const Vector *self, Functor functor, ident data)
 Returns a new Vector containing the elements of this Vector transformed by functor.
 
ident reduce (const Vector *self, Reducer reducer, ident accumulator, ident data)
 
void removeAll (Vector *self)
 Removes all elements from this Vector without modifying its capacity.
 
void removeAt (Vector *self, size_t index)
 Removes the element at the specified index.
 
void resize (Vector *self, size_t capacity)
 Resizes this Vector to the specified capacity.
 
void sort (Vector *self, Comparator comparator)
 Sorts this Vector in place using comparator.
 
VectorvectorWithElements (size_t size, size_t count, ident elements)
 Creates a new Vector with the specified elements.
 
VectorvectorWithSize (size_t size)
 Creates a new Vector with the specified element size.
 
- 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

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

Property Details

◆ capacity

size_t Vector::capacity

The capacity.

Definition at line 67 of file Vector.h.

◆ count

size_t Vector::count

The count of elements.

Definition at line 72 of file Vector.h.

◆ destroy

Consumer Vector::destroy

Optional destructor called when an element is removed.

The argument is a pointer into the Vector's contiguous element storage, not an owned object pointer. Cast it to the appropriate element pointer type to access the value being destroyed.

Definition at line 90 of file Vector.h.

◆ elements

ident Vector::elements

The elements.

Definition at line 82 of file Vector.h.

◆ interface

VectorInterface* Vector::interface
protected

The interface.

Definition at line 62 of file Vector.h.

◆ object

Object Vector::object

The superclass.

Definition at line 56 of file Vector.h.

◆ size

size_t Vector::size

The size of each element.

Definition at line 77 of file Vector.h.

Method Details

◆ _Vector()

Class * _Vector ( void  )

The Vector archetype.

Returns
The Vector Class.

Definition at line 428 of file Vector.c.

428 {
429 static Class *clazz;
430 static Once once;
431
432 do_once(&once, {
433 clazz = _initialize(&(const ClassDef) {
434 .name = "Vector",
435 .superclass = _Object(),
436 .instanceSize = sizeof(Vector),
437 .interfaceOffset = offsetof(Vector, interface),
438 .interfaceSize = sizeof(VectorInterface),
440 });
441 });
442
443 return clazz;
444}
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
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
Vectors.
Definition Vector.h:51
VectorInterface * interface
The interface.
Definition Vector.h:62

◆ add()

void add ( Vector self,
const ident  element 
)

Adds the specified element to this Vector.

Parameters
selfThe Vector.
elementThe element to add.

Definition at line 116 of file Vector.c.

116 {
117
118 if (self->count == self->capacity) {
119
121
122 if (self->elements) {
123 self->elements = realloc(self->elements, self->capacity * self->size);
124 } else {
125 self->elements = calloc(self->capacity, self->size);
126 }
127
128 assert(self->elements);
129 }
130
131 memcpy(self->elements + self->count * self->size, element, self->size);
132 self->count++;
133}
#define VECTOR_CHUNK_SIZE
Definition Vector.c:35
size_t count
The count of elements.
Definition Vector.h:72
size_t capacity
The capacity.
Definition Vector.h:67
ident elements
The elements.
Definition Vector.h:82
size_t size
The size of each element.
Definition Vector.h:77

◆ enumerate()

void enumerate ( const Vector self,
VectorEnumerator  enumerator,
ident  data 
)

Enumerates the elements of this Vector with the given function.

Parameters
selfThe Vector.
enumeratorThe enumerator function.
dataUser data.

Definition at line 139 of file Vector.c.

139 {
140
141 assert(enumerator);
142
143 for (size_t i = 0; i < self->count; i++) {
144 enumerator(self, self->elements + i * self->size, data);
145 }
146}
static Data * data(void)
Definition Data.c:286

◆ filter() [1/2]

void filter ( const Vector self,
Predicate  predicate,
ident  data 
)

Filters the elements of this Vector with the given Predicate.

Parameters
selfThe Vector.
predicateThe Predicate.
dataUser data.

◆ filter() [2/2]

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

Definition at line 152 of file Vector.c.

152 {
153
154 assert(predicate);
155
156 for (size_t i = 0; i < self->count; i++) {
157 if (!predicate(self->elements + i * self->size, data)) {
158 $(self, removeAt, i);
159 }
160 }
161}
void removeAt(Vector *self, size_t index)
Removes the element at the specified index.
Definition Vector.c:291

◆ find()

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

Definition at line 167 of file Vector.c.

167 {
168
169 assert(predicate);
170
171 for (size_t i = 0; i < self->count; i++) {
172 if (predicate(self->elements + i * self->size, data)) {
173 return self->elements + i * self->size;
174 }
175 }
176
177 return NULL;
178}

◆ indexOf()

ssize_t indexOf ( const Vector self,
const ident  element 
)
Parameters
selfThe Vector.
elementThe element.
Returns
The index of the given element, or -1 if not found.

Definition at line 184 of file Vector.c.

184 {
185
186 for (size_t i = 0; i < self->count; i++) {
187 if (memcmp(self->elements + i * self->size, element, self->size) == 0) {
188 return i;
189 }
190 }
191
192 return -1;
193}

◆ initWithElements()

Vector * initWithElements ( Vector self,
size_t  size,
size_t  count,
ident  elements 
)

Initializes this Vector with the specified elements.

Parameters
selfThe Vector.
sizeThe element size.
countThe count of elements.
elementsThe elements, which will be freed when this Vector is released.
Returns
The initialized Vector, or NULL on error.

Definition at line 199 of file Vector.c.

199 {
200
201 self = $(self, initWithSize, size);
202 if (self) {
203 self->count = count;
204 self->elements = elements;
205 }
206
207 return self;
208}
Vector * initWithSize(Vector *self, size_t size)
Initializes this Vector with the specified element size.
Definition Vector.c:214

◆ initWithSize()

Vector * initWithSize ( Vector self,
size_t  size 
)

Initializes this Vector with the specified element size.

Parameters
selfThe Vector.
sizeThe element size.
Returns
The initialized Vector, or NULL on error.

Definition at line 214 of file Vector.c.

214 {
215
216 self = (Vector *) super(Object, self, init);
217 if (self) {
218 self->size = size;
219 assert(self->size);
220 }
221 return self;
222}
#define super(type, obj, method,...)
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

◆ insert()

void insert ( Vector self,
const ident  element,
size_t  index 
)

Inserts the element at the specified index.

Parameters
selfThe Vector.
elementThe element to insert.
indexThe index at which to insert.

Definition at line 228 of file Vector.c.

228 {
229
230 assert(index <= self->count);
231
232 $(self, add, element);
233
234 for (size_t i = self->count - 1; i > index; i--) {
235 memcpy(self->elements + i * self->size, self->elements + (i - 1) * self->size, self->size);
236 }
237
238 memcpy(self->elements + index * self->size, element, self->size);
239}
void add(Vector *self, const ident element)
Adds the specified element to this Vector.
Definition Vector.c:116

◆ mappedVector()

Vector * mappedVector ( const Vector self,
Functor  functor,
ident  data 
)

Returns a new Vector containing the elements of this Vector transformed by functor.

Parameters
selfThe Vector.
functorThe Functor.
dataUser data.
Returns
A new mapped Vector.

Definition at line 245 of file Vector.c.

245 {
246
247 assert(functor);
248
249 Vector *vector = $(alloc(Vector), initWithSize, self->size);
250 for (size_t i = 0; i < self->count; i++) {
251 ident result = functor(self->elements + i * self->size, data);
252 $(vector, add, result);
253 }
254 return vector;
255}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49

◆ reduce()

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

Definition at line 261 of file Vector.c.

261 {
262
263 assert(reducer);
264
265 for (size_t i = 0; i < self->count; i++) {
266 accumulator = reducer(self->elements + i * self->size, accumulator, data);
267 }
268
269 return accumulator;
270}

◆ removeAll()

void removeAll ( Vector self)

Removes all elements from this Vector without modifying its capacity.

Parameters
selfThe Vector

Definition at line 276 of file Vector.c.

276 {
277
278 if (self->destroy) {
279 for (size_t i = 0; i < self->count; i++) {
280 self->destroy(self->elements + i * self->size);
281 }
282 }
283
284 self->count = 0;
285}
Consumer destroy
Optional destructor called when an element is removed.
Definition Vector.h:90

◆ removeAt()

void removeAt ( Vector self,
size_t  index 
)

Removes the element at the specified index.

Parameters
selfThe Vector.
indexThe index of the element to remove.

Definition at line 291 of file Vector.c.

291 {
292
293 assert(index < self->count);
294
295 if (self->destroy) {
296 self->destroy(self->elements + index * self->size);
297 }
298
299 const size_t size = (self->count - index - 1) * self->size;
300
301 memmove(self->elements + index * self->size, self->elements + (index + 1) * self->size, size);
302
303 self->count--;
304}

◆ resize()

void resize ( Vector self,
size_t  capacity 
)

Resizes this Vector to the specified capacity.

Parameters
selfThe Vector.
capacityThe desired capacity.

Definition at line 310 of file Vector.c.

310 {
311
312 if (self->destroy) {
313 for (size_t i = capacity; i < self->count; i++) {
314 self->destroy(self->elements + i * self->size);
315 }
316 }
317
318 self->elements = realloc(self->elements, capacity * self->size);
319 assert(self->elements);
320
321 self->capacity = capacity;
322 self->count = min(self->count, self->capacity);
323}
#define min(a, b)
Definition Types.h:159

◆ sort()

void sort ( Vector self,
Comparator  comparator 
)

Sorts this Vector in place using comparator.

Parameters
selfThe Vector.
comparatorA Comparator.

Definition at line 372 of file Vector.c.

372 {
373 qsort_r(self->elements, self->count, self->size, _sort, comparator);
374}
static void _sort(List *self, Comparator comparator)
Definition List.c:330

◆ vectorWithElements()

Vector * vectorWithElements ( size_t  size,
size_t  count,
ident  elements 
)

Creates a new Vector with the specified elements.

Parameters
sizeThe element size.
countThe count of elements.
elementsTHe elements, which will be freed when this Vector is released.
Returns
The new Vector.

Definition at line 382 of file Vector.c.

382 {
384}
Vector * initWithElements(Vector *self, size_t size, size_t count, ident elements)
Initializes this Vector with the specified elements.
Definition Vector.c:199

◆ vectorWithSize()

Vector * vectorWithSize ( size_t  size)

Creates a new Vector with the specified element size.

Parameters
sizeThe element size.
Returns
The new Vector.

Definition at line 390 of file Vector.c.

390 {
391 return $(alloc(Vector), initWithSize, size);
392}

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