Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
PointerArray.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "Array.h"
#include "PointerArray.h"

Go to the source code of this file.

Macros

#define _Class   _PointerArray
 
#define POINTER_ARRAY_CHUNK_SIZE   64
 

Functions

Class_PointerArray (void)
 
static void _remove (PointerArray *self, ident pointer)
 
static void add (PointerArray *self, ident pointer)
 
static void dealloc (Object *self)
 
static ident get (const PointerArray *self, size_t index)
 
static PointerArrayinit (PointerArray *self)
 
static void initialize (Class *clazz)
 
static PointerArrayinitWithDestroy (PointerArray *self, Consumer destroy)
 
static void removeAll (PointerArray *self)
 
static void removeAt (PointerArray *self, size_t index)
 
static void sort (PointerArray *self, Comparator comparator)
 

Macro Definition Documentation

◆ _Class

#define _Class   _PointerArray

Definition at line 31 of file PointerArray.c.

◆ POINTER_ARRAY_CHUNK_SIZE

#define POINTER_ARRAY_CHUNK_SIZE   64

Definition at line 33 of file PointerArray.c.

Function Documentation

◆ _PointerArray()

Class * _PointerArray ( void  )

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}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
Class * _Object(void)
Definition Object.c:136
static void initialize(Class *clazz)
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
Growable arrays of raw C pointers.

◆ _remove()

static void _remove ( PointerArray self,
ident  pointer 
)
static

Definition at line 107 of file PointerArray.c.

107 {
108
109 for (size_t i = 0; i < self->count; i++) {
110 if (self->elements[i] == pointer) {
111 $(self, removeAt, i);
112 return;
113 }
114 }
115}
static void removeAt(PointerArray *self, size_t index)
ident * elements
The backing array of pointers.
size_t count
The count of elements.

◆ add()

static void add ( PointerArray self,
ident  pointer 
)
static

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
size_t capacity
The capacity.

◆ dealloc()

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

Definition at line 40 of file PointerArray.c.

40 {
41
42 PointerArray *this = (PointerArray *) self;
43
44 $(this, removeAll);
45
46 free(this->elements);
47
48 super(Object, self, dealloc);
49}
#define super(type, obj, method,...)
static void removeAll(PointerArray *self)
static void dealloc(Object *self)
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ get()

static ident get ( const PointerArray self,
size_t  index 
)
static

Definition at line 74 of file PointerArray.c.

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

◆ init()

static PointerArray * init ( PointerArray self)
static

Definition at line 85 of file PointerArray.c.

85 {
86 return $(self, initWithDestroy, NULL);
87}
static PointerArray * initWithDestroy(PointerArray *self, Consumer destroy)

◆ initialize()

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

Definition at line 163 of file PointerArray.c.

163 {
164
165 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
166
167 ((PointerArrayInterface *) clazz->interface)->add = add;
168 ((PointerArrayInterface *) clazz->interface)->get = get;
169 ((PointerArrayInterface *) clazz->interface)->init = init;
170 ((PointerArrayInterface *) clazz->interface)->initWithDestroy = initWithDestroy;
171 ((PointerArrayInterface *) clazz->interface)->remove = _remove;
172 ((PointerArrayInterface *) clazz->interface)->removeAll = removeAll;
173 ((PointerArrayInterface *) clazz->interface)->removeAt = removeAt;
174 ((PointerArrayInterface *) clazz->interface)->sort = sort;
175}
static void add(PointerArray *self, ident pointer)
static ident get(const PointerArray *self, size_t index)
static PointerArray * init(PointerArray *self)
static void sort(PointerArray *self, Comparator comparator)
static void _remove(PointerArray *self, ident pointer)
ident interface
The interface of the Class.
Definition Class.h:105
PointerArray * initWithDestroy(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.

◆ initWithDestroy()

static PointerArray * initWithDestroy ( PointerArray self,
Consumer  destroy 
)
static

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}
static void destroy(Class *clazz)
Definition Boole.c:102
Consumer destroy
Optional destructor called when a element is removed.

◆ removeAll()

static void removeAll ( PointerArray self)
static

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

static void removeAt ( PointerArray self,
size_t  index 
)
static

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

static void sort ( PointerArray self,
Comparator  comparator 
)
static

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