table.h
Go to the documentation of this file.
1 #pragma once
12 #include <stdlib.h>
13 #include "kuroko.h"
14 #include "value.h"
15 #include "threads.h"
16 
20 typedef struct {
21  KrkValue key;
22  KrkValue value;
24 
28 typedef struct {
29  size_t count;
30  size_t capacity;
31  KrkTableEntry * entries;
32 } KrkTable;
33 
44 extern void krk_initTable(KrkTable * table);
45 
54 extern void krk_freeTable(KrkTable * table);
55 
67 extern void krk_tableAddAll(KrkTable * from, KrkTable * to);
68 
83 extern struct KrkString * krk_tableFindString(KrkTable * table, const char * chars, size_t length, uint32_t hash);
84 
98 extern int krk_tableSet(KrkTable * table, KrkValue key, KrkValue value);
99 
113 extern int krk_tableGet(KrkTable * table, KrkValue key, KrkValue * value);
114 
128 extern int krk_tableGet_fast(KrkTable * table, struct KrkString * str, KrkValue * value);
129 
141 extern int krk_tableDelete(KrkTable * table, KrkValue key);
142 
154 extern int krk_tableDeleteExact(KrkTable * table, KrkValue key);
155 
169 extern KrkTableEntry * krk_findEntry(KrkTableEntry * entries, size_t capacity, KrkValue key);
170 
181 extern int krk_hashValue(KrkValue value, uint32_t *hashOut);
182 
192 extern void krk_tableAdjustCapacity(KrkTable * table, size_t capacity);
193 
208 extern int krk_tableSetIfExists(KrkTable * table, KrkValue key, KrkValue value);
Top-level header with configuration macros.
uint32_t hash
Cached hash value for table keys.
Definition: object.h:44
Immutable sequence of Unicode codepoints.
Definition: object.h:93
char * chars
UTF8 canonical data.
Definition: object.h:97
size_t length
String length in bytes.
Definition: object.h:95
One (key,value) pair in a table.
Definition: table.h:20
Simple hash table of arbitrary keys to values.
Definition: table.h:28
void krk_initTable(KrkTable *table)
Initialize a hash table.
Definition: table.c:14
int krk_tableGet(KrkTable *table, KrkValue key, KrkValue *value)
Obtain the value associated with a key in a table.
Definition: table.c:178
int krk_tableDeleteExact(KrkTable *table, KrkValue key)
Remove a key from a hash table, with identity lookup.
Definition: table.c:220
int krk_tableDelete(KrkTable *table, KrkValue key)
Remove a key from a hash table.
Definition: table.c:208
int krk_tableSet(KrkTable *table, KrkValue key, KrkValue value)
Assign a value to a key in a table.
Definition: table.c:145
void krk_tableAdjustCapacity(KrkTable *table, size_t capacity)
Preset the size of a table.
Definition: table.c:116
struct KrkString * krk_tableFindString(KrkTable *table, const char *chars, size_t length, uint32_t hash)
Find a character sequence in the string interning table.
Definition: table.c:232
void krk_tableAddAll(KrkTable *from, KrkTable *to)
Add all key-value pairs from 'from' into 'to'.
Definition: table.c:169
void krk_freeTable(KrkTable *table)
Release resources associated with a hash table.
Definition: table.c:20
int krk_tableGet_fast(KrkTable *table, struct KrkString *str, KrkValue *value)
Obtain the value associated with a string key in a table.
Definition: table.c:189
KrkTableEntry * krk_findEntry(KrkTableEntry *entries, size_t capacity, KrkValue key)
Internal table scan function.
Definition: table.c:66
int krk_tableSetIfExists(KrkTable *table, KrkValue key, KrkValue value)
Update the value of a table entry only if it is found.
Definition: table.c:159
Stack reference or primative value.
int krk_hashValue(KrkValue value, uint32_t *hashOut)
Calculate the hash for a value.
Definition: table.c:25
Convience header for providing atomic operations to threads.
Definitions for primitive stack references.