cons
Loading...
Searching...
No Matches
cons.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: MIT */
2
8#ifndef CONS_H
9#define CONS_H
10
11/*
12 * for NULL (the empty list)
13 */
14#include <stddef.h>
15
16/*
17 * for bool type
18 */
19#include <stdbool.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25struct cons;
26
36#define CONS_NIL ((struct cons *)NULL)
37
39#define CONS_NIL_P(_cell) ((_cell) == CONS_NIL)
40
42#define CONS_NOT_NIL_P(_cell) ((_cell) != CONS_NIL)
43
45#define CONS(_car, _cdr) ((struct cons){.car = (_car), .cdr = (_cdr)})
46
48#define CONS_NULL CONS(NULL, CONS_NIL)
49
73struct cons {
83 void *car;
84
89 struct cons *cdr;
90};
91
97static inline void *cons_car(const struct cons *cell) { return cell->car; }
98
104static inline struct cons *cons_cdr(const struct cons *cell) { return cell->cdr; }
105
111static inline void cons_rplaca(struct cons *cell, void *car) { cell->car = car; }
112
118static inline void cons_rplacd(struct cons *cell, struct cons *cdr) { cell->cdr = cdr; }
119
121static inline void cons_init(struct cons *cell, void *car) {
122 cons_rplaca(cell, car);
123 cons_rplacd(cell, CONS_NIL);
124}
125
149struct cons **cons(struct cons **list, struct cons *cell);
150
161struct cons **cons_prepend(struct cons **list, struct cons *cell);
162
187struct cons **cons_loop(struct cons **list, bool (*pred)(struct cons **list, struct cons *cell, void *user), void *user);
188
197struct cons **cons_find(struct cons **list, void *cell);
198
219struct cons *cons_delete(struct cons **list, void *car);
220
233struct cons *cons_remove(struct cons **list, struct cons *cell);
234
244void cons_reverse(struct cons **list);
245
255size_t cons_length(const struct cons *cell);
256
269struct cons *cons_last(struct cons *cell);
270
284struct cons *cons_nth(struct cons *cell, size_t nth);
285
297struct cons *cons_member(struct cons *cell, void *car);
298
315struct cons *cons_append(struct cons *cell1, struct cons *cell2);
316
326struct cons *cons_heap(void *car);
327
329void cons_free(struct cons *cell);
330
331#ifdef __cplusplus
332}
333#endif
334
335#endif /* CONS_H */
static struct cons * cons_cdr(const struct cons *cell)
Accessor for the cdr field of a cons cell.
Definition cons.h:104
struct cons * cons_delete(struct cons **list, void *car)
Destructively deletes the first cons cell with the specified car value from the list.
Definition cons.c:71
void cons_free(struct cons *cell)
Frees a heap-allocated cons cell.
Definition cons.c:168
size_t cons_length(const struct cons *cell)
Computes the length of a linked list of cons cells.
Definition cons.c:111
void cons_reverse(struct cons **list)
Reverses a linked list of cons cells in place.
Definition cons.c:91
#define CONS_NIL
The empty list (a NULL pointer).
Definition cons.h:36
static void cons_rplacd(struct cons *cell, struct cons *cdr)
Mutator for the cdr field of a cons cell.
Definition cons.h:118
static void * cons_car(const struct cons *cell)
Accessor for the car field of a cons cell.
Definition cons.h:97
struct cons ** cons_find(struct cons **list, void *cell)
Finds the first cons cell in a list that matches a given identity.
Definition cons.c:62
struct cons * cons_append(struct cons *cell1, struct cons *cell2)
Appends one list of cons cells to another.
Definition cons.c:151
struct cons * cons_member(struct cons *cell, void *car)
Returns the first cons cell in a linked list of cons cells whose car field matches the specified valu...
Definition cons.c:142
struct cons ** cons_loop(struct cons **list, bool(*pred)(struct cons **list, struct cons *cell, void *user), void *user)
Loops through a list of cons cells, applying a predicate function to each cell.
Definition cons.c:35
struct cons * cons_last(struct cons *cell)
Returns the last cons cell in a linked list of cons cells.
Definition cons.c:120
struct cons * cons_remove(struct cons **list, struct cons *cell)
Destructively removes the specified cons cell from the list.
Definition cons.c:81
struct cons * cons_heap(void *car)
Allocates a new cons cell on the heap.
Definition cons.c:160
static void cons_rplaca(struct cons *cell, void *car)
Mutator for the car field of a cons cell.
Definition cons.h:111
static void cons_init(struct cons *cell, void *car)
Initialises a cons cell with the given car value and cdr set to CONS_NIL.
Definition cons.h:121
struct cons ** cons_prepend(struct cons **list, struct cons *cell)
Prepends a cons cell to a list, returning the updated list pointer.
Definition cons.c:30
struct cons * cons_nth(struct cons *cell, size_t nth)
Returns the n'th cons cell in a linked list of cons cells.
Definition cons.c:134
Construct cell structure for building linked lists.
Definition cons.h:73
struct cons * cdr
Contents of Decrement Register (CDR).
Definition cons.h:89
void * car
Contents of Address Register (CAR).
Definition cons.h:83