Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
List Struct Reference

#include <List.h>

Overview

Doubly-linked lists of raw C pointers.

Definition at line 60 of file List.h.

Inheritance diagram for List:
Object

Properties

size_t count
 The number of elements.
 
Consumer destroy
 Optional destructor called when an element is removed.
 
ListNode * head
 The head node.
 
Object object
 The superclass.
 
ListNode * tail
 The tail node.
 
- Properties inherited from Object
Class * clazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class * _List (void)
 The List archetype.
 
void append (List *self, const ident element)
 Appends the given element to the tail of this List.
 
bool contains (const List *self, const ident element)
 
void enumerate (const List *self, ListEnumerator enumerator, ident element)
 Enumerates this List with the given function.
 
void filter (List *self, Predicate predicate, ident data)
 Filters this List in place using predicate.
 
List * filteredList (const List *self, Predicate predicate, ident data)
 Returns a new List containing elements of this List that pass predicate.
 
ident find (const List *self, Predicate predicate, ident data)
 
List * init (List *self)
 Initializes this List.
 
void insertAfter (List *self, ListNode *node, const ident element)
 Inserts an element after the given node.
 
void map (List *self, Functor functor, ident data)
 Transforms the elements in this List in place using functor.
 
List * mappedList (const List *self, Functor functor, ident data)
 Returns a new List containing the elements of this List transformed by functor.
 
ListNode * nodeForElement (const List *self, const ident element)
 
void prepend (List *self, const ident element)
 Prepends the given element to the head of this List.
 
ident reduce (const List *self, Reducer reducer, ident accumulator, ident data)
 
void remove (List *self, const ident element)
 Removes the first occurrence of element from this List.
 
void removeAll (List *self)
 Removes all elements from this List.
 
void removeNode (List *self, ListNode *node)
 Removes the given node from this List.
 
void sort (List *self, Comparator comparator)
 Sorts this List in-place using the given comparator.
 
void sort (List *self, ListSortFunc sort)
 
- Methods inherited from Object
Class * _Object (void)
 The Object archetype.
 
Object * copy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
String * description (const Object *self)
 
int hash (const Object *self)
 
Object * init (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

ListInterface * interface [0]
 The interface type.
 
- Protected Attributes inherited from Object
ObjectInterface * interface [0]
 The interface type.
 

Property Details

◆ count

size_t List::count

The number of elements.

Definition at line 76 of file List.h.

◆ destroy

Consumer List::destroy

Optional destructor called when an element is removed.

Definition at line 91 of file List.h.

◆ head

ListNode* List::head

The head node.

Definition at line 81 of file List.h.

◆ interface

ListInterface* List::interface[0]
protected

The interface type.

Definition at line 71 of file List.h.

◆ object

Object List::object

The superclass.

Definition at line 65 of file List.h.

◆ tail

ListNode* List::tail

The tail node.

Definition at line 86 of file List.h.

Method Details

◆ _List()

Class * _List ( void  )

The List archetype.

Returns
The List Class.

Definition at line 406 of file List.c.

406 {
407 static Class *clazz;
408 static Once once;
409
410 do_once(&once, {
411 clazz = _initialize(&(const ClassDef) {
412 .name = "List",
413 .superclass = _Object(),
414 .instanceSize = sizeof(List),
415 .interfaceSize = sizeof(ListInterface),
416 .initialize = initialize,
417 });
418 });
419
420 return clazz;
421}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
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:90
Doubly-linked lists of raw C pointers.
Definition List.h:60
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

◆ append()

void append ( List *  self,
const ident  element 
)

Appends the given element to the tail of this List.

Parameters
selfThe List.
elementThe element to append.

Definition at line 72 of file List.c.

72 {
73
74 ListNode *node = calloc(1, sizeof(ListNode));
75 assert(node);
76
77 node->element = element;
78 node->prev = self->tail;
79
80 if (self->tail) {
81 self->tail->next = node;
82 } else {
83 self->head = node;
84 }
85
86 self->tail = node;
87 self->count++;
88}
ListNode * head
The head node.
Definition List.h:81
size_t count
The number of elements.
Definition List.h:76
ListNode * tail
The tail node.
Definition List.h:86
A node in a List.
Definition List.h:49
ListNode * prev
Definition List.h:51
ident element
Definition List.h:50
ListNode * next
Definition List.h:52

◆ contains()

bool contains ( const List *  self,
const ident  element 
)
Parameters
selfThe List.
elementThe element to search for (pointer equality).
Returns
True if this List contains the given element.

Definition at line 94 of file List.c.

94 {
95 return $(self, nodeForElement, element) != NULL;
96}
ListNode * nodeForElement(const List *self, const ident element)
Definition List.c:238

◆ enumerate()

void enumerate ( const List *  self,
ListEnumerator  enumerator,
ident  data 
)

Enumerates this List with the given function.

Parameters
selfThe List.
enumeratorThe enumerator function.
dataUser data.

Definition at line 102 of file List.c.

102 {
103
104 assert(enumerator);
105
106 for (ListNode *node = self->head; node; ) {
107 ListNode *next = node->next;
108 if (enumerator(self, node, element)) {
109 break;
110 }
111 node = next;
112 }
113}
static Unicode next(StringReader *self, StringReaderMode mode)

◆ filter()

void filter ( List *  self,
Predicate  predicate,
ident  data 
)

Filters this List in place using predicate.

Parameters
selfThe List.
predicateThe Predicate.
dataUser data.

Definition at line 119 of file List.c.

119 {
120
121 assert(predicate);
122
123 for (ListNode *node = self->head; node; ) {
124 ListNode *next = node->next;
125 if (!predicate(node->element, data)) {
126 $(self, removeNode, node);
127 }
128 node = next;
129 }
130}
static Data * data(void)
Definition Data.c:286
void removeNode(List *self, ListNode *node)
Removes the given node from this List.
Definition List.c:322

◆ filteredList()

List * filteredList ( const List *  self,
Predicate  predicate,
ident  data 
)

Returns a new List containing elements of this List that pass predicate.

Parameters
selfThe List.
predicateThe Predicate.
dataUser data.
Returns
A new filtered List.

Definition at line 136 of file List.c.

136 {
137
138 assert(predicate);
139
140 List *list = $(alloc(List), init);
141 for (ListNode *node = self->head; node; node = node->next) {
142 if (predicate(node->element, data)) {
143 $(list, append, node->element);
144 }
145 }
146
147 return list;
148}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
List * init(List *self)
Initializes this List.
Definition List.c:173
void append(List *self, const ident element)
Appends the given element to the tail of this List.
Definition List.c:72

◆ find()

ident find ( const List *  self,
Predicate  predicate,
ident  data 
)
Parameters
selfThe List.
predicateThe Predicate.
dataUser data.
Returns
The first element to pass predicate, or NULL.

Definition at line 154 of file List.c.

154 {
155
156 assert(predicate);
157
158 for (ListNode *node = self->head; node; node = node->next) {
159 if (predicate(node->element, data)) {
160 return node->element;
161 }
162 }
163
164 return NULL;
165}

◆ init()

List * init ( List *  self)

Initializes this List.

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

Definition at line 173 of file List.c.

173 {
174
175 self = (List *) super(Object, self, init);
176 return self;
177}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46

◆ insertAfter()

void insertAfter ( List *  self,
ListNode *  node,
const ident  element 
)

Inserts an element after the given node.

Parameters
selfThe List.
nodeThe node to insert after, or NULL to prepend.
elementThe element to insert.

Definition at line 183 of file List.c.

183 {
184
185 if (node == NULL || node == self->tail) {
186 $(self, append, element);
187 return;
188 }
189
190 ListNode *newNode = calloc(1, sizeof(ListNode));
191 assert(newNode);
192
193 newNode->element = element;
194 newNode->prev = node;
195 newNode->next = node->next;
196
197 if (node->next) {
198 node->next->prev = newNode;
199 }
200 node->next = newNode;
201
202 self->count++;
203}

◆ map()

void map ( List *  self,
Functor  functor,
ident  data 
)

Transforms the elements in this List in place using functor.

Parameters
selfThe List.
functorThe Functor.
dataUser data.

Definition at line 209 of file List.c.

209 {
210
211 assert(functor);
212
213 for (ListNode *node = self->head; node; node = node->next) {
214 node->element = functor(node->element, data);
215 }
216}

◆ mappedList()

List * mappedList ( const List *  self,
Functor  functor,
ident  data 
)

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

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

Definition at line 222 of file List.c.

222 {
223
224 assert(functor);
225
226 List *list = $(alloc(List), init);
227 for (ListNode *node = self->head; node; node = node->next) {
228 $(list, append, functor(node->element, data));
229 }
230
231 return list;
232}

◆ nodeForElement()

ListNode * nodeForElement ( const List *  self,
const ident  element 
)
Parameters
selfThe List.
elementThe element to find (pointer equality).
Returns
The first node whose data matches, or NULL.

Definition at line 238 of file List.c.

238 {
239
240 for (ListNode *node = self->head; node; node = node->next) {
241 if (node->element == element) {
242 return node;
243 }
244 }
245
246 return NULL;
247}

◆ prepend()

void prepend ( List *  self,
const ident  element 
)

Prepends the given element to the head of this List.

Parameters
selfThe List.
elementThe element to prepend.

Definition at line 253 of file List.c.

253 {
254
255 ListNode *node = calloc(1, sizeof(ListNode));
256 assert(node);
257
258 node->element = element;
259 node->next = self->head;
260
261 if (self->head) {
262 self->head->prev = node;
263 } else {
264 self->tail = node;
265 }
266
267 self->head = node;
268 self->count++;
269}

◆ reduce()

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

Definition at line 275 of file List.c.

275 {
276
277 assert(reducer);
278
279 for (ListNode *node = self->head; node; node = node->next) {
280 accumulator = reducer(node->element, accumulator, data);
281 }
282
283 return accumulator;
284}

◆ remove()

void remove ( List *  self,
const ident  element 
)

Removes the first occurrence of element from this List.

Parameters
selfThe List.
elementThe element to remove.

◆ removeAll()

void removeAll ( List *  self)

Removes all elements from this List.

Parameters
selfThe List.

Definition at line 290 of file List.c.

290 {
291
292 ListNode *node = self->head;
293 while (node) {
294 ListNode *next = node->next;
295 if (self->destroy) {
296 self->destroy(node->element);
297 }
298 free(node);
299 node = next;
300 }
301
302 self->head = self->tail = NULL;
303 self->count = 0;
304}
Consumer destroy
Optional destructor called when an element is removed.
Definition List.h:91

◆ removeNode()

void removeNode ( List *  self,
ListNode *  node 
)

Removes the given node from this List.

Parameters
selfThe List.
nodeThe node to remove.

Definition at line 322 of file List.c.

322 {
323
324 assert(node);
325
326 if (node->prev) {
327 node->prev->next = node->next;
328 } else {
329 self->head = node->next;
330 }
331
332 if (node->next) {
333 node->next->prev = node->prev;
334 } else {
335 self->tail = node->prev;
336 }
337
338 if (self->destroy) {
339 self->destroy(node->element);
340 }
341
342 free(node);
343 self->count--;
344}

◆ sort() [1/2]

void sort ( List *  self,
Comparator  comparator 
)

Sorts this List in-place using the given comparator.

Parameters
selfThe List.
comparatorThe Comparator.

◆ sort() [2/2]

void sort ( List *  self,
ListSortFunc  sort 
)

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