Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
maps.h File Reference
Include dependency graph for maps.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  srn_map_t
 A persistent, immutable map. More...

Typedefs

typedef struct srn_map_t srn_map_t
 A persistent, immutable map.

Functions

srn_value_tsrn_map_empty (srn_context_t *ctx, srn_metadata_t *metadata)
srn_value_tsrn_map_assoc (srn_context_t *ctx, srn_metadata_t *metadata, srn_value_t *map, srn_value_t *key, srn_value_t *value)

Typedef Documentation

◆ srn_map_t

typedef struct srn_map_t srn_map_t

A persistent, immutable map.

Backed by the CHAMT in serene/rt/impl/hashmap.h. Keys and values are both srn_value_t*.

Function Documentation

◆ srn_map_assoc()

srn_value_t * srn_map_assoc ( srn_context_t * ctx,
srn_metadata_t * metadata,
srn_value_t * map,
srn_value_t * key,
srn_value_t * value )

Definition at line 71 of file maps.c.

74 {
75 PANIC_IF_NULL(ctx);
76 PANIC_IF_NULL(map);
77 PANIC_IF_NULL(key);
78 PANIC_IF_NULL(value);
79
80 if (!IS_A(map, VMap)) {
81 return srn_errors_make_error(ctx, metadata, ABSURD, "expected a map");
82 }
83
84 // Hashing panics on the unhashable tags, and a panic from a data driven
85 // value is wrong in an API that reports failures as values.
86 if (!srn_value_hashable(key)) {
87 return srn_errors_make_error(ctx, metadata, ABSURD, "map key is not hashable");
88 }
89
90 hmap_key_t k = {.data = key, .len = sizeof(*key)};
91
92 srn_map_t *old = AS_MAP(map);
93 srn_map_t *neu = ALLOC(ctx, srn_map_t);
94
95 neu->inner = value_map.insert(&value_map, &old->inner, &k, value);
96 return srn_value_make(ctx, VMap, metadata, (void *)neu);
97}
#define ALLOC(ctx, T)
Definition context.h:84
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:36
@ VMap
Definition core.h:126
#define AS_MAP(value_ref)
Definition core.h:182
#define IS_A(value_ref, field)
Definition core.h:167
@ ABSURD
Definition errors.h:76
static const hmap_control_t value_map
Definition maps.c:52
bool srn_value_hashable(const srn_value_t *v)
Whether v can be a hash key.
Definition protocols.c:139
Note: For key equality we use the memcpy function.
Definition hashmap.h:66
A persistent, immutable map.
Definition maps.h:29
hmap_t inner
Definition maps.h:30
#define PANIC_IF_NULL(ptr)
Definition utils.h:66
Here is the call graph for this function:
Here is the caller graph for this function:

◆ srn_map_empty()

srn_value_t * srn_map_empty ( srn_context_t * ctx,
srn_metadata_t * metadata )

Definition at line 64 of file maps.c.

64 {
65 PANIC_IF_NULL(ctx);
66 srn_map_t *m = ALLOC(ctx, srn_map_t);
67 m->inner = hmap_empty(ctx);
68 return srn_value_make(ctx, VMap, metadata, (void *)m);
69}
hmap_t hmap_empty(const srn_context_t *ctx)
Create, initialize and return a new hashmap pinned to ctx.
Definition hashmap.c:655
Here is the call graph for this function:
Here is the caller graph for this function: