table.h
Go to the documentation of this file.
1 #pragma once
12 #include <stdlib.h>
13 #include <sys/types.h>
14 #include "kuroko.h"
15 #include "value.h"
16 
20 typedef struct {
21  KrkValue key;
22  KrkValue value;
24 
28 typedef struct {
29  size_t count;
30  size_t capacity;
31  size_t used;
33  ssize_t * indexes;
34 } KrkTable;
35 
46 extern void krk_initTable(KrkTable * table);
47 
56 extern void krk_freeTable(KrkTable * table);
57 
69 extern void krk_tableAddAll(KrkTable * from, KrkTable * to);
70 
85 extern struct KrkString * krk_tableFindString(KrkTable * table, const char * chars, size_t length, uint32_t hash);
86 
100 extern int krk_tableSet(KrkTable * table, KrkValue key, KrkValue value);
101 
115 extern int krk_tableGet(KrkTable * table, KrkValue key, KrkValue * value);
116 
130 extern int krk_tableGet_fast(KrkTable * table, struct KrkString * str, KrkValue * value);
131 
143 extern int krk_tableDelete(KrkTable * table, KrkValue key);
144 
156 extern int krk_tableDeleteExact(KrkTable * table, KrkValue key);
157 
168 extern int krk_hashValue(KrkValue value, uint32_t *hashOut);
169 
179 extern void krk_tableAdjustCapacity(KrkTable * table, size_t capacity);
180 
195 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:33
int krk_tableGet(KrkTable *table, KrkValue key, KrkValue *value)
Obtain the value associated with a key in a table.
Definition: table.c:211
int krk_tableDeleteExact(KrkTable *table, KrkValue key)
Remove a key from a hash table, with identity lookup.
Definition: table.c:249
int krk_tableDelete(KrkTable *table, KrkValue key)
Remove a key from a hash table.
Definition: table.c:238
int krk_tableSet(KrkTable *table, KrkValue key, KrkValue value)
Assign a value to a key in a table.
Definition: table.c:148
void krk_tableAdjustCapacity(KrkTable *table, size_t capacity)
Preset the size of a table.
Definition: table.c:115
KrkTableEntry * entries
Definition: table.h:32
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:260
void krk_tableAddAll(KrkTable *from, KrkTable *to)
Add all key-value pairs from 'from' into 'to'.
Definition: table.c:202
void krk_freeTable(KrkTable *table)
Release resources associated with a hash table.
Definition: table.c:41
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:219
size_t count
Definition: table.h:29
size_t capacity
Definition: table.h:30
int krk_tableSetIfExists(KrkTable *table, KrkValue key, KrkValue value)
Update the value of a table entry only if it is found.
Definition: table.c:194
ssize_t * indexes
Definition: table.h:33
size_t used
Definition: table.h:31
Stack reference or primative value.
int krk_hashValue(KrkValue value, uint32_t *hashOut)
Calculate the hash for a value.
Definition: table.c:47
Definitions for primitive stack references.