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

#include <PointerArray.h>

Overview

Growable arrays of raw C pointers.

Like Vector, but specialized for pointer-sized elements. The destroy callback receives the stored pointer value itself (i.e. elements[i]), not a pointer into the backing buffer.

Definition at line 44 of file PointerArray.h.

Inheritance diagram for PointerArray:
Object

Properties

size_t capacity
 The capacity.
 
size_t count
 The count of elements.
 
Consumer destroy
 Optional destructor called when a element is removed.
 
identelements
 The backing array of pointers.
 
Object object
 The superclass.
 
- 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_PointerArray (void)
 The PointerArray archetype.
 
void add (PointerArray *self, ident pointer)
 Appends the given pointer to this PointerArray.
 
ident get (const PointerArray *self, size_t index)
 
PointerArrayinit (PointerArray *self)
 Initializes this PointerArray.
 
PointerArrayinitWithDestroy (PointerArray *self, Consumer destroy)
 Initializes this PointerArray with a destructor.
 
void remove (PointerArray *self, ident pointer)
 Removes the first occurrence of pointer from this PointerArray.
 
void removeAll (PointerArray *self)
 Removes all elements from this PointerArray without modifying its capacity.
 
void removeAt (PointerArray *self, size_t index)
 Removes the pointer at the specified index.
 
void sort (PointerArray *self, Comparator comparator)
 Sorts this PointerArray in place using comparator.
 
- 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

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

Property Details

◆ capacity

size_t PointerArray::capacity

The capacity.

Definition at line 60 of file PointerArray.h.

◆ count

size_t PointerArray::count

The count of elements.

Definition at line 65 of file PointerArray.h.

◆ destroy

Consumer PointerArray::destroy

Optional destructor called when a element is removed.

The argument is the pointer value itself, not a pointer into the backing buffer.

Definition at line 72 of file PointerArray.h.

◆ elements

ident* PointerArray::elements

The backing array of pointers.

Definition at line 77 of file PointerArray.h.

◆ interface

PointerArrayInterface* PointerArray::interface
protected

The interface.

Definition at line 55 of file PointerArray.h.

◆ object

Object PointerArray::object

The superclass.

Definition at line 49 of file PointerArray.h.

Method Details

◆ _PointerArray()

Class * _PointerArray ( void  )

The PointerArray archetype.

Returns
The PointerArray Class.

Definition at line 181 of file PointerArray.c.

181 {
182 static Class *clazz;
183 static Once once;
184
185 do_once(&once, {
186 clazz = _initialize(&(const ClassDef) {
187 .name = "PointerArray",
188 .superclass = _Object(),
189 .instanceSize = sizeof(PointerArray),
190 .interfaceOffset = offsetof(PointerArray, interface),
191 .interfaceSize = sizeof(PointerArrayInterface),
193 });
194 });
195
196 return clazz;
197}
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
Growable arrays of raw C pointers.
PointerArrayInterface * interface
The interface.

◆ add()

void add ( PointerArray self,
ident  pointer 
)

Appends the given pointer to this PointerArray.

Parameters
selfThe PointerArray.
pointerThe pointer to add.

Definition at line 57 of file PointerArray.c.

57 {
58
59 if (self->count == self->capacity) {
60
62
63 self->elements = realloc(self->elements, self->capacity * sizeof(ident ));
64 assert(self->elements);
65 }
66
67 self->elements[self->count++] = pointer;
68}
#define POINTER_ARRAY_CHUNK_SIZE
void * ident
The identity type, similar to Objective-C id.
Definition Types.h:49
ident * elements
The backing array of pointers.
size_t count
The count of elements.
size_t capacity
The capacity.

◆ get()

ident get ( const PointerArray self,
size_t  index 
)
Parameters
selfThe PointerArray.
indexThe index.
Returns
The element at the given index.

Definition at line 74 of file PointerArray.c.

74 {
75
76 assert(index < self->count);
77
78 return self->elements[index];
79}

◆ init()

PointerArray * init ( PointerArray self)

Initializes this PointerArray.

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

Definition at line 85 of file PointerArray.c.

85 {
86 return $(self, initWithDestroy, NULL);
87}
PointerArray * initWithDestroy(PointerArray *self, Consumer destroy)
Initializes this PointerArray with a destructor.

◆ initWithDestroy()

PointerArray * initWithDestroy ( PointerArray self,
Consumer  destroy 
)

Initializes this PointerArray with a destructor.

Parameters
selfThe PointerArray.
destroyAn optional destructor called when a pointer is removed, or NULL.
Returns
The initialized PointerArray, or NULL on error.

Definition at line 93 of file PointerArray.c.

93 {
94
95 self = (PointerArray *) super(Object, self, init);
96 if (self) {
97 self->destroy = destroy;
98 }
99
100 return self;
101}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
PointerArray * init(PointerArray *self)
Initializes this PointerArray.
Consumer destroy
Optional destructor called when a element is removed.

◆ remove()

void remove ( PointerArray self,
ident  pointer 
)

Removes the first occurrence of pointer from this PointerArray.

Parameters
selfThe PointerArray.
pointerThe element to remove (compared by value).

◆ removeAll()

void removeAll ( PointerArray self)

Removes all elements from this PointerArray without modifying its capacity.

Parameters
selfThe PointerArray.

Definition at line 121 of file PointerArray.c.

121 {
122
123 if (self->destroy) {
124 for (size_t i = 0; i < self->count; i++) {
125 self->destroy(self->elements[i]);
126 }
127 }
128
129 self->count = 0;
130}

◆ removeAt()

void removeAt ( PointerArray self,
size_t  index 
)

Removes the pointer at the specified index.

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

Definition at line 136 of file PointerArray.c.

136 {
137
138 assert(index < self->count);
139
140 if (self->destroy) {
141 self->destroy(self->elements[index]);
142 }
143
144 const size_t tail = self->count - index - 1;
145 memmove(self->elements + index, self->elements + index + 1, tail * sizeof(ident ));
146
147 self->count--;
148}

◆ sort()

void sort ( PointerArray self,
Comparator  comparator 
)

Sorts this PointerArray in place using comparator.

Parameters
selfThe PointerArray.
comparatorA Comparator.

Definition at line 154 of file PointerArray.c.

154 {
155 quicksort(self->elements, self->count, sizeof(ident), comparator, NULL);
156}
void quicksort(ident base, size_t count, size_t size, Comparator comparator, ident data)
A portability wrapper around reentrant qsort.
Definition Array.c:69

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