Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
lists.h File Reference
#include <stddef.h>
#include "serene/rt/core.h"
Include dependency graph for lists.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  srn_list_node_t
 A list is a singly linked sequence of values. More...
 
struct  srn_list_t
 Since all the values are immutable and persistent. More...
 

Typedefs

typedef struct srn_list_node_t srn_list_node_t
 A list is a singly linked sequence of values.
 
typedef struct srn_list_t srn_list_t
 

Functions

srn_value_tsrn_list_make (srn_context_t *ctx, srn_metadata_t *metadata, srn_value_t *const *items, size_t count)
 Build a list from an array of elements.
 
srn_value_tsrn_list_cons (srn_context_t *ctx, srn_metadata_t *metadata, srn_value_t *value, srn_value_t *list)
 Prepend value to list, returning a new list.
 

Typedef Documentation

◆ srn_list_node_t

typedef struct srn_list_node_t srn_list_node_t

A list is a singly linked sequence of values.

Nodes hold a value and a pointer to the next node; the list header caches the length and points at the head node. Empty lists are represented by nil_v; a list value with tag VList always has at least one element.

◆ srn_list_t

typedef struct srn_list_t srn_list_t

Function Documentation

◆ srn_list_cons()

srn_value_t * srn_list_cons ( srn_context_t * ctx,
srn_metadata_t * metadata,
srn_value_t * value,
srn_value_t * list )

Prepend value to list, returning a new list.

list may be nil_v (treated as the empty list); the input list is not modified — its nodes are shared with the returned list.

Definition at line 48 of file lists.c.

49 {
50 PANIC_IF_NULL(ctx);
51 PANIC_IF_NULL(value);
52 PANIC_IF_NULL(list);
53
54 srn_list_node_t *next_head = nullptr;
55 size_t next_len = 0;
56 if (!IS_NIL(list)) {
57 srn_list_t *l = AS_LIST(list);
58 next_head = l->head;
59 next_len = l->len;
60 }
61
63 node->value = value;
64 node->next = next_head;
65
66 srn_list_t *result = ALLOC(ctx, srn_list_t);
67 result->len = next_len + 1;
68 result->head = node;
69 return srn_value_make(ctx, VList, metadata, (void *)result);
70}
#define ALLOC(ctx, T)
Definition context.h:82
srn_value_t * srn_value_make(srn_context_t *ctx, srn_value_tag_t tag, srn_metadata_t *metadata, void *payload)
Creates a new serene value.
Definition core.c:24
#define AS_LIST(value_ref)
Definition core.h:174
#define IS_NIL(value_ref)
Definition core.h:165
@ VList
Definition core.h:118
A list is a singly linked sequence of values.
Definition lists.h:30
struct srn_list_node_t * next
Definition lists.h:32
srn_value_t * value
Definition lists.h:31
Since all the values are immutable and persistent.
Definition lists.h:36
srn_pair_t * head
Definition lists.h:38
size_t len
Definition lists.h:36
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
Here is the call graph for this function:

◆ srn_list_make()

srn_value_t * srn_list_make ( srn_context_t * ctx,
srn_metadata_t * metadata,
srn_value_t *const * items,
size_t count )

Build a list from an array of elements.

When count == 0, returns nil_v (empty lists have no allocated representation). Otherwise allocates one node per element plus the list header, all in ctx.

Definition at line 24 of file lists.c.

25 {
26 PANIC_IF_NULL(ctx);
27 if (count == 0) {
28 return &nil_v;
29 }
30 PANIC_IF_NULL(items);
31
32 // Build the chain from the tail back to the head so each node's `next`
33 // already points at an initialized successor.
34 srn_list_node_t *head = nullptr;
35 for (size_t i = count; i-- > 0;) {
37 node->value = items[i];
38 node->next = head;
39 head = node;
40 }
41
42 srn_list_t *list = ALLOC(ctx, srn_list_t);
43 list->len = count;
44 list->head = head;
45 return srn_value_make(ctx, VList, metadata, (void *)list);
46}
static srn_value_t nil_v
Definition core.h:156
Here is the call graph for this function: