Objectively
Ultra-lightweight object oriented framework for GNU 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.
 
ListNodehead
 The head node.
 
Object object
 The superclass.
 
ListNodetail
 The tail node.
 
- 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_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.
 
ListfilteredList (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)
 
Listinit (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.
 
ListmappedList (const List *self, Functor functor, ident data)
 Returns a new List containing the elements of this List transformed by functor.
 
ListNodenodeForElement (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.
 
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

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

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
protected

The interface.

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 385 of file List.c.

385 {
386 static Class *clazz;
387 static Once once;
388
389 do_once(&once, {
390 clazz = _initialize(&(const ClassDef) {
391 .name = "List",
392 .superclass = _Object(),
393 .instanceSize = sizeof(List),
394 .interfaceOffset = offsetof(List, interface),
395 .interfaceSize = sizeof(ListInterface),
397 });
398 });
399
400 return clazz;
401}
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
Doubly-linked lists of raw C pointers.
Definition List.h:60
ListInterface * interface
The interface.
Definition List.h:71
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 52 of file List.c.

52 {
53
54 ListNode *node = calloc(1, sizeof(ListNode));
55 assert(node);
56
57 node->element = element;
58 node->prev = self->tail;
59
60 if (self->tail) {
61 self->tail->next = node;
62 } else {
63 self->head = node;
64 }
65
66 self->tail = node;
67 self->count++;
68}
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 74 of file List.c.

74 {
75 return $(self, nodeForElement, element) != NULL;
76}
ListNode * nodeForElement(const List *self, const ident element)
Definition List.c:218

◆ 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 82 of file List.c.

82 {
83
84 assert(enumerator);
85
86 for (ListNode *node = self->head; node; ) {
87 ListNode *next = node->next;
88 if (enumerator(self, node, element)) {
89 break;
90 }
91 node = next;
92 }
93}
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 99 of file List.c.

99 {
100
101 assert(predicate);
102
103 for (ListNode *node = self->head; node; ) {
104 ListNode *next = node->next;
105 if (!predicate(node->element, data)) {
106 $(self, removeNode, node);
107 }
108 node = next;
109 }
110}
static Data * data(void)
Definition Data.c:286
void removeNode(List *self, ListNode *node)
Removes the given node from this List.
Definition List.c:302

◆ 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 116 of file List.c.

116 {
117
118 assert(predicate);
119
120 List *list = $(alloc(List), init);
121 for (ListNode *node = self->head; node; node = node->next) {
122 if (predicate(node->element, data)) {
123 $(list, append, node->element);
124 }
125 }
126
127 return list;
128}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
List * init(List *self)
Initializes this List.
Definition List.c:153
void append(List *self, const ident element)
Appends the given element to the tail of this List.
Definition List.c:52

◆ 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 134 of file List.c.

134 {
135
136 assert(predicate);
137
138 for (ListNode *node = self->head; node; node = node->next) {
139 if (predicate(node->element, data)) {
140 return node->element;
141 }
142 }
143
144 return NULL;
145}

◆ init()

List * init ( List self)

Initializes this List.

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

Definition at line 153 of file List.c.

153 {
154
155 self = (List *) super(Object, self, init);
156 return self;
157}
#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 163 of file List.c.

163 {
164
165 if (node == NULL || node == self->tail) {
166 $(self, append, element);
167 return;
168 }
169
170 ListNode *newNode = calloc(1, sizeof(ListNode));
171 assert(newNode);
172
173 newNode->element = element;
174 newNode->prev = node;
175 newNode->next = node->next;
176
177 if (node->next) {
178 node->next->prev = newNode;
179 }
180 node->next = newNode;
181
182 self->count++;
183}

◆ 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 189 of file List.c.

189 {
190
191 assert(functor);
192
193 for (ListNode *node = self->head; node; node = node->next) {
194 node->element = functor(node->element, data);
195 }
196}

◆ 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 202 of file List.c.

202 {
203
204 assert(functor);
205
206 List *list = $(alloc(List), init);
207 for (ListNode *node = self->head; node; node = node->next) {
208 $(list, append, functor(node->element, data));
209 }
210
211 return list;
212}

◆ 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 218 of file List.c.

218 {
219
220 for (ListNode *node = self->head; node; node = node->next) {
221 if (node->element == element) {
222 return node;
223 }
224 }
225
226 return NULL;
227}

◆ 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 233 of file List.c.

233 {
234
235 ListNode *node = calloc(1, sizeof(ListNode));
236 assert(node);
237
238 node->element = element;
239 node->next = self->head;
240
241 if (self->head) {
242 self->head->prev = node;
243 } else {
244 self->tail = node;
245 }
246
247 self->head = node;
248 self->count++;
249}

◆ 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 255 of file List.c.

255 {
256
257 assert(reducer);
258
259 for (ListNode *node = self->head; node; node = node->next) {
260 accumulator = reducer(node->element, accumulator, data);
261 }
262
263 return accumulator;
264}

◆ 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 270 of file List.c.

270 {
271
272 ListNode *node = self->head;
273 while (node) {
274 ListNode *next = node->next;
275 if (self->destroy) {
276 self->destroy(node->element);
277 }
278 free(node);
279 node = next;
280 }
281
282 self->head = self->tail = NULL;
283 self->count = 0;
284}
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 302 of file List.c.

302 {
303
304 assert(node);
305
306 if (node->prev) {
307 node->prev->next = node->next;
308 } else {
309 self->head = node->next;
310 }
311
312 if (node->next) {
313 node->next->prev = node->prev;
314 } else {
315 self->tail = node->prev;
316 }
317
318 if (self->destroy) {
319 self->destroy(node->element);
320 }
321
322 free(node);
323 self->count--;
324}

◆ 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: