From 22c9a2d5a054bd3e0b74ac46a13e67cada2fe450 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 1 Jul 2015 14:27:16 -0400 Subject: [PATCH] tt_dict_singleton --- critbit.c | 9 ++++++++- critbit.h | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/critbit.c b/critbit.c index 09cde62..35523f2 100644 --- a/critbit.c +++ b/critbit.c @@ -125,13 +125,20 @@ static int set_walk(tt_arena_t *a, } } +tt_node_ptr_t tt_dict_singleton(tt_arena_t *a, + tt_atom_t key, + tt_node_ptr_t trie) +{ + return tt_cons_dict(a, RET_IF_NO_PTR(tt_cons_leaf(a, trie, key)), 1); +} + tt_node_ptr_t tt_dict_set(tt_arena_t *a, tt_node_ptr_t t, tt_atom_t key, tt_node_ptr_t trie) { if (TT_EMPTY_DICT_P(t)) { - return tt_cons_dict(a, RET_IF_NO_PTR(tt_cons_leaf(a, trie, key)), 1); + return tt_dict_singleton(a, key, trie); } assert(tt_ptr_tag(t) == TT_TAG_DICT); diff --git a/critbit.h b/critbit.h index 2800941..9a72921 100644 --- a/critbit.h +++ b/critbit.h @@ -10,6 +10,12 @@ extern int tt_dict_size(tt_arena_t *a, tt_node_ptr_t t); /* Returns TT_NO_PTR when key not present. Does not manipulate references. */ extern tt_node_ptr_t tt_dict_get(tt_arena_t *a, tt_node_ptr_t t, tt_atom_t key); +/* Returns TT_NO_PTR when allocation failed. Otherwise, a length-one dict. + Grabs `trie` if required. */ +extern tt_node_ptr_t tt_dict_singleton(tt_arena_t *a, + tt_atom_t key, + tt_node_ptr_t trie); + /* Returns TT_NO_PTR when allocation failed. Otherwise, yields a dict. Grabs `trie` if required. */ extern tt_node_ptr_t tt_dict_set(tt_arena_t *a,