Compare commits

...

23 Commits

Author SHA1 Message Date
Dibyendu Majumdar 93670a4bb9 issue #98 save current state in parser branch
4 years ago
Dibyendu Majumdar 804c7a61a7 issue #98
4 years ago
Dibyendu Majumdar 0634012784 issue #98
4 years ago
Dibyendu Majumdar e86a338d50 issue #98
4 years ago
Dibyendu Majumdar 39a7b08019 issue #98
4 years ago
Dibyendu Majumdar e54cdacb16 issue #98
4 years ago
Dibyendu Majumdar 1ceee79973 issue #98
4 years ago
Dibyendu Majumdar f1a310df95 issue #98
4 years ago
Dibyendu Majumdar 5ce1cc3c64 issue #98
4 years ago
Dibyendu Majumdar 8600378cce issue #98
4 years ago
Dibyendu Majumdar 77208e2f47 issue #98
4 years ago
Dibyendu Majumdar 1f4941fd2a issue #98
4 years ago
Dibyendu Majumdar 5bf1847884 issue #98
4 years ago
Dibyendu Majumdar 24b8a8e45c issue #98
4 years ago
Dibyendu Majumdar ed4e86cdae issue #98 work on simple literal expressions
4 years ago
Dibyendu Majumdar 0fe4d1a5b1 issue #98 more work on linearizer
4 years ago
Dibyendu Majumdar 6cb6ef838f issue #98 Start work generating linear IR
4 years ago
Dibyendu Majumdar 7b06b524ca issue #98 Start work generating linear IR
4 years ago
Dibyendu Majumdar a68643dca7 issue #98 Start work generating linear IR
4 years ago
Dibyendu Majumdar 226ef82d0f issue #98 Start work generating linear IR
4 years ago
Dibyendu Majumdar 21310c9162 issue #98 Start work generating linear IR
4 years ago
Dibyendu Majumdar 79b558fed4 issue #98
4 years ago
Dibyendu Majumdar 5cc228c663 issue #98 Start work generating linear IR
4 years ago

@ -50,9 +50,14 @@ if (COMPUTED_GOTO AND MSVC)
endif ()
include_directories("${PROJECT_SOURCE_DIR}/include")
if ($ENV{CLION_IDE})
# CLion seems unable to handle include paths set on sources
include_directories("${PROJECT_SOURCE_DIR}/dmr_c/src")
endif()
# define the Lua core source files
set(RAVI_AST_SOURCES src/ravi_ast_parse.c src/ravi_ast_print.c src/ravi_ast_typecheck.c)
set(RAVI_AST_SOURCES src/ravi_ast_parse.c src/ravi_ast_print.c src/ravi_ast_typecheck.c src/ravi_ast_linearize.c
src/ravi_fnv_hash.c src/ravi_hash_table.c src/ravi_set.c src/ravi_int_set.c)
set(LUA_CORE_SRCS src/lapi.c src/lcode.c src/lctype.c src/ldebug.c src/ldo.c src/ldump.c
src/lfunc.c src/lgc.c src/llex.c src/lmem.c src/lobject.c src/lopcodes.c
src/lparser.c src/lstate.c src/lstring.c src/ltable.c src/ltm.c src/lundump.c

@ -54,7 +54,7 @@ struct ptr_list_iter {
/* The ptr list */
extern int ptrlist_size(const struct ptr_list *self);
extern void **ptrlist_add(struct ptr_list **self, void *ptr, struct allocator *alloc);
extern void **ptrlist_add(struct ptr_list **self, void *ptr, struct allocator *ptr_list_allocator);
extern void *ptrlist_nth_entry(struct ptr_list *list, unsigned int idx);
extern void *ptrlist_first(struct ptr_list *list);
extern void *ptrlist_last(struct ptr_list *list);

@ -23,6 +23,7 @@ b) Perform type checking (Ravi enhancement)
#include "ravi_ast.h"
#include "ravi_membuf.h"
#include "ravi_set.h"
#include "allocate.h"
#include "ptrlist.h"
@ -33,12 +34,14 @@ b) Perform type checking (Ravi enhancement)
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#define MAXVARS 125
//////////////////////////
struct lua_symbol_list;
struct linearizer;
/*
* Userdata object to hold the abstract syntax tree;
@ -53,6 +56,7 @@ struct ast_container {
struct allocator block_scope_allocator;
struct allocator symbol_allocator;
struct ast_node *main_function;
struct linearizer *linearizer;
bool killed; /* flag to check if this is already destroyed */
};
@ -69,6 +73,7 @@ struct var_type {
type_code is RAVI_TUSERDATA */
};
struct pseudo;
struct lua_symbol;
DECLARE_PTR_LIST(lua_symbol_list, struct lua_symbol);
@ -90,6 +95,7 @@ struct lua_symbol {
struct {
const TString *var_name; /* name of the variable */
struct block_scope *block; /* NULL if global symbol, as globals are never added to a scope */
struct pseudo *pseudo; /* backend data for the symbol */
} var;
struct {
const TString *label_name;
@ -158,12 +164,12 @@ struct ast_node {
} local_stmt; /* local declarations */
struct {
struct ast_node_list *var_expr_list; /* Optional var expressions, comma separated */
struct ast_node_list *expr_list; /* Comma separated expressions */
struct ast_node_list *expr_list; /* Comma separated expressions */
} expression_stmt; /* Also covers assignments */
struct {
struct ast_node *name; /* base symbol to be looked up */
struct ast_node_list *selectors; /* Optional */
struct ast_node *method_name; /* Optional */
struct ast_node *method_name; /* Optional */
struct ast_node *function_expr; /* Function's AST */
} function_stmt;
struct {
@ -203,15 +209,15 @@ struct ast_node {
const TString *s;
} u;
} literal_expr;
struct {
struct { /* primaryexp -> NAME | '(' expr ')', NAME is parsed as AST_SYMBOL_EXPR */
struct var_type type;
struct lua_symbol *var;
} symbol_expr;
struct {
struct { /* AST_Y_INDEX_EXPR or AST_FIELD_SELECTOR_EXPR */
struct var_type type;
struct ast_node *expr; /* '[' expr ']' */
} index_expr;
struct {
struct { /* AST_UNARY_EXPR */
struct var_type type;
UnOpr unary_op;
struct ast_node *expr;
@ -234,17 +240,19 @@ struct ast_node {
struct lua_symbol_list *upvalues; /* List of upvalues */
struct lua_symbol_list *locals; /* List of locals */
} function_expr; /* a literal expression whose result is a value of type function */
struct {
struct { /* AST_INDEXED_ASSIGN_EXPR - used in table constructor */
struct var_type type;
struct ast_node *
index_expr; /* If NULL means this is a list field with next available index, else specfies index expression */
struct ast_node *index_expr; /* If NULL means this is a list field with next available index, else specifies index
expression */
struct ast_node *value_expr;
} indexed_assign_expr; /* Assign values in table constructor */
struct {
struct { /* constructor -> '{' [ field { sep field } [sep] ] '}' where sep -> ',' | ';' */
struct var_type type;
struct ast_node_list *expr_list;
} table_expr; /* table constructor expression */
} table_expr; /* table constructor expression AST_TABLE_EXPR occurs in function call and simple expr */
struct {
/* suffixedexp -> primaryexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs } */
/* suffix_list may have AST_FIELD_SELECTOR_EXPR, AST_Y_INDEX_EXPR, AST_FUNCTION_CALL_EXPR */
struct var_type type;
struct ast_node *primary_expr;
struct ast_node_list *suffix_list;
@ -255,24 +263,17 @@ struct ast_node {
* overwrite the type of the variables in an inconsistent way.
*/
struct var_type type;
TString *method_name; /* Optional method_name */
TString *method_name; /* Optional method_name */
struct ast_node_list *arg_list; /* Call arguments */
} function_call_expr;
};
};
#define set_typecode(vt, t) \
(vt).type_code = t
#define set_type(vt, t) \
(vt).type_code = t, \
(vt).type_name = NULL
#define set_typename(vt, t, name) \
(vt).type_code = t, \
(vt).type_name = (name)
#define set_typecode(vt, t) (vt).type_code = t
#define set_type(vt, t) (vt).type_code = t, (vt).type_name = NULL
#define set_typename(vt, t, name) (vt).type_code = t, (vt).type_name = (name)
#define is_type_same(a, b) ((a).type_code == (b).type_code && (a).type_name == (b).type_name)
#define copy_type(a, b) \
(a).type_code = (b).type_code, \
(a).type_name = (b).type_name
#define copy_type(a, b) (a).type_code = (b).type_code, (a).type_name = (b).type_name
struct parser_state {
LexState *ls;
@ -283,7 +284,221 @@ struct parser_state {
LUAMOD_API int raviopen_ast_library(lua_State *L);
void raviA_print_ast_node(membuff_t *buf, struct ast_node *node, int level); /* output the AST structure recusrively */
void raviA_ast_typecheck(struct ast_container *container); /* Perform type checks and assign types to AST */
void raviA_print_ast_node(membuff_t *buf, struct ast_node *node, int level); /* output the AST structure recusrively */
void raviA_ast_typecheck(struct ast_container *container); /* Perform type checks and assign types to AST */
/*
Linearizer
*/
struct instruction;
struct node;
struct basic_block;
struct edge;
struct cfg;
struct proc;
struct constant;
DECLARE_PTR_LIST(instruction_list, struct instruction);
DECLARE_PTR_LIST(edge_list, struct edge);
DECLARE_PTR_LIST(pseudo_list, struct pseudo);
DECLARE_PTR_LIST(proc_list, struct proc);
#define container_of(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member)))
/* order is important here ! */
enum opcode {
op_nop,
op_ret,
op_loadk,
op_loadnil,
op_loadbool,
op_add,
op_addff,
op_addfi,
op_addii,
op_sub,
op_subff,
op_subfi,
op_subif,
op_subii,
op_mul,
op_mulff,
op_mulfi,
op_mulii,
op_div,
op_divff,
op_divfi,
op_divif,
op_divii,
op_idiv,
op_band,
op_bandii,
op_bor,
op_borii,
op_bxor,
op_bxorii,
op_shl,
op_shlii,
op_shr,
op_shrii,
op_eq,
op_eqii,
op_eqff,
op_lt,
op_ltii,
op_ltff,
op_le,
op_leii,
op_leff,
op_mod,
op_pow,
op_closure,
op_unm,
op_unmi,
op_unmf,
op_len,
op_leni,
op_toint,
op_toflt,
op_toclosure,
op_tostring,
op_toiarray,
op_tofarray,
op_totable,
op_totype,
op_not,
op_bnot,
op_loadglobal,
op_newtable,
op_newiarray,
op_newfarray,
op_put, /* target is any */
op_put_ikey,
op_put_skey,
op_tput, /* target is table */
op_tput_ikey,
op_tput_skey,
op_iaput, /* target is integer[]*/
op_iaput_ival,
op_faput, /* target is number[] */
op_faput_fval
};
enum pseudo_type {
PSEUDO_SYMBOL,
PSEUDO_TEMP_FLT,
PSEUDO_TEMP_INT,
PSEUDO_TEMP_ANY,
PSEUDO_CONSTANT,
PSEUDO_PROC,
PSEUDO_NIL,
PSEUDO_TRUE,
PSEUDO_FALSE
};
/* pseudo represents a pseudo (virtual) register */
struct pseudo {
unsigned type : 4, regnum : 16;
union {
struct lua_symbol *symbol; /* PSEUDO_SYMBOL */
const struct constant *constant; /* PSEUDO_CONSTANT */
ravitype_t temp_type; /* PSEUDO_TEMP* */
struct proc *proc; /* PSEUDO_PROC */
};
};
/* single instruction */
struct instruction {
unsigned opcode : 8;
struct pseudo_list *operands;
struct pseudo_list *targets;
};
struct edge {
struct node *from;
struct node *to;
};
#define NODE_FIELDS \
uint32_t index; \
struct edge_list *pred; \
struct edge_list *succ
struct node {
NODE_FIELDS;
};
/* Basic block is a specialization of node */
struct basic_block {
NODE_FIELDS;
struct instruction_list *insns;
};
#define CFG_FIELDS \
unsigned node_count; \
unsigned allocated; \
struct node **nodes; \
struct node *entry; \
struct node *exit
struct cfg {
CFG_FIELDS;
};
struct pseudo_generator {
uint8_t next_reg;
int16_t free_pos;
uint8_t free_regs[256];
};
struct constant {
uint8_t type;
uint16_t index; /* index number starting from 0 assigned to each constant - acts like a reg num */
union {
lua_Integer i;
lua_Number n;
const TString *s;
};
};
/* proc is a type of cfg */
struct proc {
CFG_FIELDS;
struct linearizer *linearizer;
struct proc_list *procs; /* procs defined in this proc */
struct proc *parent; /* enclosing proc */
struct ast_node *function_expr; /* function ast that we are compiling */
struct block_scope *current_scope;
struct basic_block *current_bb;
struct pseudo_generator local_pseudos; /* locals */
struct pseudo_generator temp_int_pseudos; /* temporaries known to be integer type */
struct pseudo_generator temp_flt_pseudos; /* temporaries known to be number type */
struct pseudo_generator temp_pseudos; /* All other temporaries */
struct set *constants;
unsigned num_constants;
};
static inline struct basic_block *n2bb(struct node *n) { return (struct basic_block *)n; }
static inline struct node *bb2n(struct basic_block *bb) { return (struct node *)bb; }
struct linearizer {
struct allocator instruction_allocator;
struct allocator edge_allocator;
struct allocator pseudo_allocator;
struct allocator ptrlist_allocator;
struct allocator basic_block_allocator;
struct allocator proc_allocator;
struct allocator unsized_allocator;
struct allocator constant_allocator;
struct ast_container *ast_container;
struct proc *main_proc; /* The root of the compiled chunk of code */
struct proc_list *all_procs; /* All procs allocated by the linearizer */
struct proc *current_proc; /* proc being compiled */
};
void raviA_init_linearizer(struct linearizer *linearizer, struct ast_container *container);
void raviA_destroy_linearizer(struct linearizer *linearizer);
void raviA_ast_linearize(struct linearizer *linearizer);
void raviA_show_linearizer(struct linearizer *linearizer, membuff_t *mb);
#endif
#endif

@ -0,0 +1,45 @@
/*
* Copyright © 2009 Intel Corporation
* Copyright © 2014 Broadcom
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*/
/* Quick FNV-1 hash implementation based on:
* http://www.isthe.com/chongo/tech/comp/fnv/
*/
#include <inttypes.h>
uint32_t fnv1_hash_string(const char *key);
uint32_t fnv1_hash_data(const void *data, size_t size);
int string_key_equals(const void *a, const void *b);
#define hash_table_create_for_string() \
hash_table_create((uint32_t (*)(const void *key))fnv1_hash_string, \
string_key_equals)
#define set_create_for_string() \
set_create((uint32_t (*)(const void *key))fnv1_hash_string, \
string_key_equals)

@ -0,0 +1,109 @@
/*
* Copyright © 2009 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*
*/
#ifndef RAVI_HASH_TABLE_H
#define RAVI_HASH_TABLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <inttypes.h>
struct hash_entry {
uint32_t hash;
const void *key;
void *data;
};
struct hash_table {
struct hash_entry *table;
uint32_t (*hash_function)(const void *key);
int (*key_equals_function)(const void *a, const void *b);
uint32_t size;
uint32_t rehash;
uint32_t max_entries;
uint32_t size_index;
uint32_t entries;
uint32_t deleted_entries;
};
struct hash_table *
hash_table_create(uint32_t (*hash_function)(const void *key),
int (*key_equals_function)(const void *a,
const void *b));
void
hash_table_destroy(struct hash_table *ht,
void (*delete_function)(struct hash_entry *entry));
struct hash_entry *
hash_table_insert(struct hash_table *ht, const void *key, void *data);
struct hash_entry *
hash_table_search(struct hash_table *ht, const void *key);
void
hash_table_remove(struct hash_table *ht, const void *key);
void
hash_table_remove_entry(struct hash_table *ht, struct hash_entry *entry);
struct hash_entry *
hash_table_next_entry(struct hash_table *ht,
struct hash_entry *entry);
struct hash_entry *
hash_table_random_entry(struct hash_table *ht,
int (*predicate)(struct hash_entry *entry));
/**
* This foreach function is safe against deletion (which just replaces
* an entry's data with the deleted marker), but not against insertion
* (which may rehash the table, making entry a dangling pointer).
*/
#define hash_table_foreach(ht, entry) \
for (entry = hash_table_next_entry(ht, NULL); \
entry != NULL; \
entry = hash_table_next_entry(ht, entry))
/* Alternate interfaces to reduce repeated calls to hash function. */
struct hash_entry *
hash_table_search_pre_hashed(struct hash_table *ht,
uint32_t hash,
const void *key);
struct hash_entry *
hash_table_insert_pre_hashed(struct hash_table *ht,
uint32_t hash,
const void *key, void *data);
#ifdef __cplusplus
} /* extern C */
#endif
#endif

@ -0,0 +1,82 @@
/*
* Copyright © 2009,2013 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*
*/
#ifndef RAVI_INT_SET_H
#define RAVI_INT_SET_H
#include <inttypes.h>
#include <stdbool.h>
struct int_set_entry {
uint32_t value;
unsigned int occupied : 1;
unsigned int deleted : 1;
};
struct int_set {
struct int_set_entry *table;
uint32_t size;
uint32_t rehash;
uint32_t max_entries;
uint32_t size_index;
uint32_t entries;
uint32_t deleted_entries;
};
struct int_set *
int_set_create(void);
void
int_set_destroy(struct int_set *set);
struct int_set_entry *
int_set_add(struct int_set *set, uint32_t value);
bool
int_set_contains(struct int_set *set, uint32_t value);
void
int_set_remove(struct int_set *set, uint32_t value);
struct int_set_entry *
int_set_search(struct int_set *set, uint32_t value);
void
int_set_remove_entry(struct int_set *set, struct int_set_entry *entry);
struct int_set_entry *
int_set_next_entry(struct int_set *set, struct int_set_entry *entry);
/* Return a random entry in the set that satisfies predicate.
*
* The 'predicate' function pointer may be NULL in which any random
* entry will be returned. */
struct int_set_entry *
int_set_random_entry(struct int_set *set,
int (*predicate)(struct int_set_entry *entry));
#endif

@ -0,0 +1,98 @@
/*
* Copyright © 2009 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*
*/
#ifndef RAVI_SET_H
#define RAVI_SET_H
#include <inttypes.h>
#include <stdbool.h>
struct set_entry {
uint32_t hash;
const void *key;
};
struct set {
struct set_entry *table;
uint32_t (*hash_function)(const void *key);
int (*key_equals_function)(const void *a, const void *b);
uint32_t size;
uint32_t rehash;
uint32_t max_entries;
uint32_t size_index;
uint32_t entries;
uint32_t deleted_entries;
};
struct set *
set_create(uint32_t (*hash_function)(const void *key),
int (*key_equals_function)(const void *a,
const void *b));
void
set_destroy(struct set *set,
void (*delete_function)(struct set_entry *entry));
struct set_entry *
set_add(struct set *set, const void *key);
bool
set_contains(struct set *set, const void *key);
void
set_remove(struct set *set, const void *key);
struct set_entry *
set_search(struct set *set, const void *key);
void
set_remove_entry(struct set *set, struct set_entry *entry);
struct set_entry *
set_next_entry(struct set *set, struct set_entry *entry);
struct set_entry *
set_random_entry(struct set *set,
int (*predicate)(struct set_entry *entry));
/**
* This foreach function is safe against deletion (which just replaces
* an entry's data with the deleted marker), but not against insertion
* (which may rehash the table, making entry a dangling pointer).
*/
#define set_foreach(ht, entry) \
for (entry = set_next_entry(ht, NULL); \
entry != NULL; \
entry = set_next_entry(ht, entry))
/* Alternate interfaces to reduce repeated calls to hash function. */
struct set_entry *
set_search_pre_hashed(struct set *set, uint32_t hash, const void *key);
struct set_entry *
set_add_pre_hashed(struct set *set, uint32_t hash, const void *key);
#endif

@ -0,0 +1,403 @@
Testing return
L0
RET
L1
Testing return 1
L0
RET {1 Kint(0)}
L1
Testing return 42, 4.2, true, "hello"
L0
RET {42 Kint(0), 4.200000000000 Kflt(1), true, 'hello' Ks(2)}
L1
Testing return a
L0
LOADGLOBAL {a} {T(0)}
RET {T(0)}
L1
Testing return 1+2
L0
ADDii {1 Kint(0), 2 Kint(1)} {Tint(0)}
RET {Tint(0)}
L1
Testing return 2^3-5*4
L0
POW {2 Kint(0), 3 Kint(1)} {Tflt(0)}
MULii {5 Kint(2), 4 Kint(3)} {Tint(0)}
SUBfi {Tflt(0), Tint(0)} {Tflt(1)}
RET {Tflt(1)}
L1
Testing return 1+1
L0
ADDii {1 Kint(0), 1 Kint(0)} {Tint(0)}
RET {Tint(0)}
L1
Testing return 1+1+1
L0
ADDii {1 Kint(0), 1 Kint(0)} {Tint(0)}
ADDii {Tint(0), 1 Kint(0)} {Tint(1)}
RET {Tint(1)}
L1
Testing return 2-3/5*4
L0
DIVii {3 Kint(1), 5 Kint(2)} {Tflt(0)}
MULfi {Tflt(0), 4 Kint(3)} {Tflt(1)}
SUBif {2 Kint(0), Tflt(1)} {Tflt(0)}
RET {Tflt(0)}
L1
Testing return 4.2//5
L0
IDIV {4.200000000000 Kflt(0), 5 Kint(1)} {T(0)}
RET {T(0)}
L1
Testing return 0.0
L0
RET {0.000000000000 Kflt(0)}
L1
Testing return 0
L0
RET {0 Kint(0)}
L1
Testing return -0//1
L0
UNMi {0 Kint(0)} {Tint(0)}
IDIV {Tint(0), 1 Kint(1)} {Tint(1)}
RET {Tint(1)}
L1
Testing return 3^-1
L0
UNMi {1 Kint(1)} {Tint(0)}
POW {3 Kint(0), Tint(0)} {Tflt(0)}
RET {Tflt(0)}
L1
Testing return (1 + 1)^(50 + 50)
L0
ADDii {1 Kint(0), 1 Kint(0)} {Tint(0)}
ADDii {50 Kint(1), 50 Kint(1)} {Tint(1)}
POW {Tint(0), Tint(1)} {Tflt(0)}
RET {Tflt(0)}
L1
Testing return (-2)^(31 - 2)
L0
UNMi {2 Kint(0)} {Tint(0)}
SUBii {31 Kint(1), 2 Kint(0)} {Tint(1)}
POW {Tint(0), Tint(1)} {Tflt(0)}
RET {Tflt(0)}
L1
Testing return (-3^0 + 5) // 3.0
L0
POW {3 Kint(0), 0 Kint(1)} {Tflt(0)}
UNMf {Tflt(0)} {Tflt(1)}
ADDfi {Tflt(1), 5 Kint(2)} {Tflt(2)}
IDIV {Tflt(2), 3 Kint(0)} {T(0)} - wrong result type
RET {T(0)}
L1
Testing return -3 % 5
L0
UNMi {3 Kint(0)} {Tint(0)}
MOD {Tint(0), 5 Kint(1)} {Tint(1)}
RET {Tint(1)}
L1
Testing return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3
L0
POW {2.000000000000 Kflt(0), 8 Kint(1)} {Tflt(0)}
UNMi {1 Kint(2)} {Tint(0)}
UNMi {Tint(0)} {Tint(1)}
ADDfi {Tflt(0), Tint(1)} {Tflt(1)}
MOD {Tflt(1), 8 Kint(1)} {Tflt(0)}
UNMf {Tflt(0)} {Tflt(1)}
DIVfi {Tflt(1), 2.000000000000 Kflt(0)} {Tflt(2)}
MULfi {Tflt(2), 4 Kint(3)} {Tflt(1)}
SUBfi {Tflt(1), 3 Kint(4)} {Tflt(2)}
RET {Tflt(2)}
L1
Testing return -((2^8 + -(-1)) % 8)//2 * 4 - 3
L0
POW {2 Kint(0), 8 Kint(1)} {Tflt(0)}
UNMi {1 Kint(2)} {Tint(0)}
UNMi {Tint(0)} {Tint(1)}
ADDfi {Tflt(0), Tint(1)} {Tflt(1)}
MOD {Tflt(1), 8 Kint(1)} {Tflt(0)}
UNMf {Tflt(0)} {Tflt(1)}
IDIV {Tflt(1), 2 Kint(0)} {T(0)}
MUL {T(0), 4 Kint(3)} {T(1)}
SUB {T(1), 3 Kint(4)} {T(0)}
RET {T(0)}
L1
Testing return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD
L0
BANDii {170 Kint(2), 253 Kint(3)} {Tint(0)}
BXOR {204.000000000000 Kflt(1), Tint(0)} {Tint(1)}
BOR {240.000000000000 Kflt(0), Tint(1)} {Tint(0)}
RET {Tint(0)}
L1
Testing return ~(~0xFF0 | 0xFF0)
L0
BNOT {4080 Kint(0)} {T(0)}
BOR {T(0), 4080 Kint(0)} {T(1)}
BNOT {T(1)} {T(0)}
RET {T(0)}
L1
Testing return ~~-100024.0
L0
UNMf {100024.000000000000 Kflt(0)} {Tflt(0)}
BNOT {Tflt(0)} {T(0)}
BNOT {T(0)} {T(1)}
RET {T(1)}
L1
Testing return ((100 << 6) << -4) >> 2
L0
SHLii {100 Kint(0), 6 Kint(1)} {Tint(0)}
UNMi {4 Kint(2)} {Tint(1)}
SHLii {Tint(0), Tint(1)} {Tint(2)}
SHRii {Tint(2), 2 Kint(3)} {Tint(1)}
RET {Tint(1)}
L1
Testing return 2^3^2 == 2^(3^2)
L0
POW {3 Kint(1), 2 Kint(0)} {Tflt(0)}
POW {2 Kint(0), Tflt(0)} {Tflt(1)}
POW {3 Kint(1), 2 Kint(0)} {Tflt(0)}
POW {2 Kint(0), Tflt(0)} {Tflt(2)}
EQff {Tflt(1), Tflt(2)} {T(0)}
RET {T(0)}
L1
Testing return 2^3*4 == (2^3)*4
L0
POW {2 Kint(0), 3 Kint(1)} {Tflt(0)}
MULfi {Tflt(0), 4 Kint(2)} {Tflt(1)}
POW {2 Kint(0), 3 Kint(1)} {Tflt(0)}
MULfi {Tflt(0), 4 Kint(2)} {Tflt(2)}
EQff {Tflt(1), Tflt(2)} {T(0)}
RET {T(0)}
L1
Testing return 2.0^-2 == 1/4, -2^- -2 == - - -4
L0
UNMi {2.000000000000 Kflt(0)} {Tint(0)}
POW {2.000000000000 Kflt(0), Tint(0)} {Tflt(0)}
DIVii {1 Kint(1), 4 Kint(2)} {Tflt(1)}
EQff {Tflt(0), Tflt(1)} {T(0)}
UNMi {2.000000000000 Kflt(0)} {Tint(0)}
UNMi {Tint(0)} {Tint(1)}
POW {2.000000000000 Kflt(0), Tint(1)} {Tflt(1)}
UNMf {Tflt(1)} {Tflt(0)}
UNMi {4 Kint(2)} {Tint(1)}
UNMi {Tint(1)} {Tint(2)}
UNMi {Tint(2)} {Tint(3)}
EQ {Tflt(0), Tint(3)} {T(1)}
RET {T(0), T(1)}
L1
Testing return -3-1-5 == 0+0-9
L0
UNMi {3 Kint(0)} {Tint(0)}
SUBii {Tint(0), 1 Kint(1)} {Tint(1)}
SUBii {Tint(1), 5 Kint(2)} {Tint(0)}
ADDii {0 Kint(3), 0 Kint(3)} {Tint(1)}
SUBii {Tint(1), 9 Kint(4)} {Tint(2)}
EQii {Tint(0), Tint(2)} {T(0)}
RET {T(0)}
L1
Testing return -2^2 == -4, (-2)^2 == 4, 2*2-3-1 == 0
L0
POW {2 Kint(0), 2 Kint(0)} {Tflt(0)}
UNMf {Tflt(0)} {Tflt(1)}
UNMi {4 Kint(1)} {Tint(0)}
EQ {Tflt(1), Tint(0)} {T(0)}
UNMi {2 Kint(0)} {Tint(0)}
POW {Tint(0), 2 Kint(0)} {Tflt(1)}
EQ {Tflt(1), 4 Kint(1)} {T(1)}
MULii {2 Kint(0), 2 Kint(0)} {Tint(0)}
SUBii {Tint(0), 3 Kint(2)} {Tint(1)}
SUBii {Tint(1), 1 Kint(3)} {Tint(0)}
EQii {Tint(0), 0 Kint(4)} {T(2)}
RET {T(0), T(1), T(2)}
L1
Testing return -3%5 == 2, -3+5 == 2
L0
UNMi {3 Kint(0)} {Tint(0)}
MOD {Tint(0), 5 Kint(1)} {Tint(1)}
EQii {Tint(1), 2 Kint(2)} {T(0)}
UNMi {3 Kint(0)} {Tint(1)}
ADDii {Tint(1), 5 Kint(1)} {Tint(0)}
EQii {Tint(0), 2 Kint(2)} {T(1)}
RET {T(0), T(1)}
L1
Testing return 0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4
L0
BANDii {170 Kint(2), 253 Kint(3)} {Tint(0)}
BXORii {204 Kint(1), Tint(0)} {Tint(1)}
BORii {240 Kint(0), Tint(1)} {Tint(0)}
EQii {Tint(0), 244 Kint(4)} {T(0)}
RET {T(0)}
L1
Testing return 0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4
L0
BANDii {253 Kint(0), 170 Kint(1)} {Tint(0)}
BXORii {Tint(0), 204 Kint(2)} {Tint(1)}
BORii {Tint(1), 240 Kint(3)} {Tint(0)}
EQii {Tint(0), 244 Kint(4)} {T(0)}
RET {T(0)}
L1
Testing return 0xF0 & 0x0F + 1 == 0x10
L0
ADDii {15 Kint(1), 1 Kint(2)} {Tint(0)}
BANDii {240 Kint(0), Tint(0)} {Tint(1)}
EQii {Tint(1), 16 Kint(3)} {T(0)}
RET {T(0)}
L1
Testing return 3^4//2^3//5 == 2
L0
POW {3 Kint(0), 4 Kint(1)} {Tflt(0)}
POW {2 Kint(2), 3 Kint(0)} {Tflt(1)}
IDIV {Tflt(0), Tflt(1)} {T(0)}
IDIV {T(0), 5 Kint(3)} {T(1)}
EQ {T(1), 2 Kint(2)} {T(0)}
RET {T(0)}
L1
Testing return -3+4*5//2^3^2//9+4%10/3 == (-3)+(((4*5)//(2^(3^2)))//9)+((4%10)/3)
L0
UNMi {3 Kint(0)} {Tint(0)}
MULii {4 Kint(1), 5 Kint(2)} {Tint(1)}
POW {3 Kint(0), 2 Kint(3)} {Tflt(0)}
POW {2 Kint(3), Tflt(0)} {Tflt(1)}
IDIV {Tint(1), Tflt(1)} {T(0)}
IDIV {T(0), 9 Kint(4)} {T(1)}
ADD {Tint(0), T(1)} {T(0)}
MOD {4 Kint(1), 10 Kint(5)} {Tint(0)}
DIVii {Tint(0), 3 Kint(0)} {Tflt(1)}
ADD {T(0), Tflt(1)} {T(1)}
UNMi {3 Kint(0)} {Tint(0)}
MULii {4 Kint(1), 5 Kint(2)} {Tint(1)}
POW {3 Kint(0), 2 Kint(3)} {Tflt(1)}
POW {2 Kint(3), Tflt(1)} {Tflt(0)}
IDIV {Tint(1), Tflt(0)} {T(0)}
IDIV {T(0), 9 Kint(4)} {T(2)}
ADD {Tint(0), T(2)} {T(0)}
MOD {4 Kint(1), 10 Kint(5)} {Tint(0)}
DIVii {Tint(0), 3 Kint(0)} {Tflt(0)}
ADD {T(0), Tflt(0)} {T(2)}
EQ {T(1), T(2)} {T(0)}
RET {T(0)}
L1
Testing return @integer 1
L0
RET {1 Kint(0)}
L1
Testing return @string "hello"
L0
RET {'hello' Ks(0)}
L1
Testing return @table {}
L0
NEWTABLE {T(0)}
RET {T(0)}
L1
Testing return @integer[] {}
L0
NEWIARRAY {T(0)}
RET {T(0)}
L1
Testing return @number[] {}
L0
NEWFARRAY {T(0)}
RET {T(0)}
L1
Testing return @closure function() end
L0
CLOSURE {Proc(000002190048ACE8)} {T(0)} - Proc not dumped
RET {T(0)}
L1
Testing return @number 54.4
L0
RET {54.400000000000 Kflt(0)}
L1
Testing return @User.Type a
L0
LOADGLOBAL {a} {T(0)}
TOTYPE {'User.Type' Ks(0)} {T(0)}
RET {T(0)}
L1
Testing return {1,2,3}
L0
NEWTABLE {T(0)}
TPUTik {1 Kint(0), 1 Kint(0)} {T(0)}
TPUTik {2 Kint(1), 2 Kint(1)} {T(0)}
TPUTik {3 Kint(2), 3 Kint(2)} {T(0)}
RET {T(0)}
L1
Testing return {[1] = a}
L0
NEWTABLE {T(0)}
LOADGLOBAL {a} {T(1)}
TPUTik {1 Kint(0), T(1)} {T(0)}
RET {T(0)}
L1
Testing return {a = b}
L0
NEWTABLE {T(0)}
LOADGLOBAL {b} {T(1)}
TPUTsk {'a' Ks(0), T(1)} {T(0)}
RET {T(0)}
L1
Testing return @integer[]{[1] = 5.5, [2] = 4}
L0
NEWIARRAY {T(0)}
IAPUT {1 Kint(0), 5.500000000000 Kflt(1)} {T(0)}
IAPUTiv {2 Kint(2), 4 Kint(3)} {T(0)}
RET {T(0)}
L1
Testing return @number[] {[1] = 4, [2] = 5.4}
L0
NEWFARRAY {T(0)}
FAPUT {1 Kint(0), 4 Kint(1)} {T(0)}
FAPUTfv {2 Kint(2), 5.400000000000 Kflt(3)} {T(0)}
RET {T(0)}
L1

@ -0,0 +1,68 @@
-- This script contains tests for the new code generator
-- specifically the linearizer part of the code generator.
local function gen(chunk: string)
print('Testing ' .. chunk)
local parsed, err = ast.parse(chunk)
assert(parsed)
parsed:linearize()
print(parsed:showlinear())
end
gen 'return'
gen 'return 1'
gen 'return 42, 4.2, true, "hello"'
gen 'return a'
gen 'return 1+2'
gen 'return 2^3-5*4'
gen 'return 1+1'
gen 'return 1+1+1'
gen 'return 2-3/5*4'
gen 'return 4.2//5'
gen 'return 0.0'
gen 'return 0'
gen 'return -0//1'
gen 'return 3^-1'
gen 'return (1 + 1)^(50 + 50)'
gen 'return (-2)^(31 - 2)'
gen 'return (-3^0 + 5) // 3.0'
gen 'return -3 % 5'
gen 'return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3'
gen 'return -((2^8 + -(-1)) % 8)//2 * 4 - 3'
gen 'return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD'
gen 'return ~(~0xFF0 | 0xFF0)'
gen 'return ~~-100024.0'
gen 'return ((100 << 6) << -4) >> 2'
gen 'return 2^3^2 == 2^(3^2)'
gen 'return 2^3*4 == (2^3)*4'
gen 'return 2.0^-2 == 1/4, -2^- -2 == - - -4'
gen 'return -3-1-5 == 0+0-9'
gen 'return -2^2 == -4, (-2)^2 == 4, 2*2-3-1 == 0'
gen 'return -3%5 == 2, -3+5 == 2'
gen 'return 0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4'
gen 'return 0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4'
gen 'return 0xF0 & 0x0F + 1 == 0x10'
gen 'return 3^4//2^3//5 == 2'
gen 'return -3+4*5//2^3^2//9+4%10/3 == (-3)+(((4*5)//(2^(3^2)))//9)+((4%10)/3)'
gen 'return @integer 1'
gen 'return @string "hello"'
gen 'return @table {}'
gen 'return @integer[] {}'
gen 'return @number[] {}'
gen 'return @closure function() end'
gen 'return @number 54.4'
gen 'return @User.Type a'
gen 'return {1,2,3}'
gen 'return {[1] = a}'
gen 'return {a = b}'
gen 'return @integer[]{[1] = 5.5, [2] = 4}'
gen 'return @number[] {[1] = 4, [2] = 5.4}'

File diff suppressed because it is too large Load Diff

@ -3,7 +3,6 @@ Copyright (C) 2018-2020 Dibyendu Majumdar
*/
#include <ravi_ast.h>
#include "ravi_ast.h"
static const char *type_name(ravitype_t tt) {
switch (tt) {

@ -2,7 +2,6 @@
Copyright (C) 2018-2020 Dibyendu Majumdar
*/
#include <ravi_ast.h>
#include "ravi_ast.h"
/* Type checker - WIP */
static void typecheck_ast_node(struct ast_container *container, struct ast_node *function, struct ast_node *node);
@ -362,6 +361,7 @@ static void typecheck_while_or_repeat_statement(struct ast_container *container,
static void typecheck_ast_node(struct ast_container *container, struct ast_node *function, struct ast_node *node) {
switch (node->type) {
case AST_FUNCTION_EXPR: {
/* args need type assertions but those have no ast - i.e. code gen should do it */
typecheck_ast_list(container, function, node->function_expr.function_statement_list);
break;
}

@ -0,0 +1,72 @@
/*
* Copyright © 2009 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*/
/* Quick FNV-1a hash implementation based on:
* http://www.isthe.com/chongo/tech/comp/fnv/
*
* FNV-1a may not be the best hash out there -- Jenkins's lookup3 is supposed
* to be quite good, and it may beat FNV. But FNV has the advantage that it
* involves almost no code.
*/
#include <string.h>
#include <ravi_fnv_hash.h>
uint32_t
fnv1_hash_string(const char *key)
{
uint32_t hash = 2166136261ul;
const uint8_t *bytes = (uint8_t *)key;
while (*bytes != 0) {
hash ^= *bytes;
hash = hash * 0x01000193;
bytes++;
}
return hash;
}
uint32_t
fnv1_hash_data(const void *data, size_t size)
{
uint32_t hash = 2166136261ul;
const uint8_t *bytes = (uint8_t *)data;
while (size-- != 0) {
hash ^= *bytes;
hash = hash * 0x01000193;
bytes++;
}
return hash;
}
int
string_key_equals(const void *a, const void *b)
{
return strcmp(a, b) == 0;
}

@ -0,0 +1,425 @@
/*
* Copyright © 2009 Intel Corporation
* Copyright © 1988-2004 Keith Packard and Bart Massey.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors
* or their institutions shall not be used in advertising or
* otherwise to promote the sale, use or other dealings in this
* Software without prior written authorization from the
* authors.
*
* Authors:
* Eric Anholt <eric@anholt.net>
* Keith Packard <keithp@keithp.com>
*/
#include <assert.h>
#include <stdlib.h>
#include <ravi_hash_table.h>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
/*
* From Knuth -- a good choice for hash/rehash values is p, p-2 where
* p and p-2 are both prime. These tables are sized to have an extra 10%
* free to avoid exponential performance degradation as the hash table fills
*/
static const uint32_t deleted_key_value;
static const void *deleted_key = &deleted_key_value;
static const struct {
uint32_t max_entries, size, rehash;
} hash_sizes[] = {
{ 2, 5, 3 },
{ 4, 7, 5 },
{ 8, 13, 11 },
{ 16, 19, 17 },
{ 32, 43, 41 },
{ 64, 73, 71 },
{ 128, 151, 149 },
{ 256, 283, 281 },
{ 512, 571, 569 },
{ 1024, 1153, 1151 },
{ 2048, 2269, 2267 },
{ 4096, 4519, 4517 },
{ 8192, 9013, 9011 },
{ 16384, 18043, 18041 },
{ 32768, 36109, 36107 },
{ 65536, 72091, 72089 },
{ 131072, 144409, 144407 },
{ 262144, 288361, 288359 },
{ 524288, 576883, 576881 },
{ 1048576, 1153459, 1153457 },
{ 2097152, 2307163, 2307161 },
{ 4194304, 4613893, 4613891 },
{ 8388608, 9227641, 9227639 },
{ 16777216, 18455029, 18455027 },
{ 33554432, 36911011, 36911009 },
{ 67108864, 73819861, 73819859 },
{ 134217728, 147639589, 147639587 },
{ 268435456, 295279081, 295279079 },
{ 536870912, 590559793, 590559791 },
{ 1073741824, 1181116273, 1181116271},
{ 2147483648ul, 2362232233ul, 2362232231ul}
};
static int
entry_is_free(const struct hash_entry *entry)
{
return entry->key == NULL;
}
static int
entry_is_deleted(const struct hash_entry *entry)
{
return entry->key == deleted_key;
}
static int
entry_is_present(const struct hash_entry *entry)
{
return entry->key != NULL && entry->key != deleted_key;
}
struct hash_table *
hash_table_create(uint32_t (*hash_function)(const void *key),
int (*key_equals_function)(const void *a,
const void *b))
{
struct hash_table *ht;
ht = malloc(sizeof(*ht));
if (ht == NULL)
return NULL;
ht->size_index = 0;
ht->size = hash_sizes[ht->size_index].size;
ht->rehash = hash_sizes[ht->size_index].rehash;
ht->max_entries = hash_sizes[ht->size_index].max_entries;
ht->hash_function = hash_function;
ht->key_equals_function = key_equals_function;
ht->table = calloc(ht->size, sizeof(*ht->table));
ht->entries = 0;
ht->deleted_entries = 0;
if (ht->table == NULL) {
free(ht);
return NULL;
}
return ht;
}
/**
* Frees the given hash table.
*
* If delete_function is passed, it gets called on each entry present before
* freeing.
*/
void
hash_table_destroy(struct hash_table *ht,
void (*delete_function)(struct hash_entry *entry))
{
if (!ht)
return;
if (delete_function) {
struct hash_entry *entry;
hash_table_foreach(ht, entry) {
delete_function(entry);
}
}
free(ht->table);
free(ht);
}
/**
* Finds a hash table entry with the given key.
*
* Returns NULL if no entry is found. Note that the data pointer may be
* modified by the user.
*/
struct hash_entry *
hash_table_search(struct hash_table *ht, const void *key)
{
uint32_t hash = ht->hash_function(key);
return hash_table_search_pre_hashed(ht, hash, key);
}
/**
* Finds a hash table entry with the given key and hash of that key.
*
* Returns NULL if no entry is found. Note that the data pointer may be
* modified by the user.
*/
struct hash_entry *
hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
const void *key)
{
uint32_t start_hash_address = hash % ht->size;
uint32_t hash_address = start_hash_address;
do {
uint32_t double_hash;
struct hash_entry *entry = ht->table + hash_address;
if (entry_is_free(entry)) {
return NULL;
} else if (entry_is_present(entry) && entry->hash == hash) {
if (ht->key_equals_function(key, entry->key)) {
return entry;
}
}
double_hash = 1 + hash % ht->rehash;
hash_address = (hash_address + double_hash) % ht->size;
} while (hash_address != start_hash_address);
return NULL;
}
static void
hash_table_rehash(struct hash_table *ht, int new_size_index)
{
struct hash_table old_ht;
struct hash_entry *table, *entry;
if (new_size_index >= ARRAY_SIZE(hash_sizes))
return;
table = calloc(hash_sizes[new_size_index].size, sizeof(*ht->table));
if (table == NULL)
return;
old_ht = *ht;
ht->table = table;
ht->size_index = new_size_index;
ht->size = hash_sizes[ht->size_index].size;
ht->rehash = hash_sizes[ht->size_index].rehash;
ht->max_entries = hash_sizes[ht->size_index].max_entries;
ht->entries = 0;
ht->deleted_entries = 0;
hash_table_foreach(&old_ht, entry) {
hash_table_insert_pre_hashed(ht, entry->hash,
entry->key, entry->data);
}
free(old_ht.table);
}
/**
* Inserts the key into the table.
*
* Note that insertion may rearrange the table on a resize or rehash,
* so previously found hash_entries are no longer valid after this function.
*/
struct hash_entry *
hash_table_insert(struct hash_table *ht, const void *key, void *data)
{
uint32_t hash = ht->hash_function(key);
/* Make sure nobody tries to add one of the magic values as a
* key. If you need to do so, either do so in a wrapper, or
* store keys with the magic values separately in the struct
* hash_table.
*/
assert(key != NULL);
return hash_table_insert_pre_hashed(ht, hash, key, data);
}
/**
* Inserts the key with the given hash into the table.
*
* Note that insertion may rearrange the table on a resize or rehash,
* so previously found hash_entries are no longer valid after this function.
*/
struct hash_entry *
hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
const void *key, void *data)
{
uint32_t start_hash_address, hash_address;
struct hash_entry *available_entry = NULL;
if (ht->entries >= ht->max_entries) {
hash_table_rehash(ht, ht->size_index + 1);
} else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
hash_table_rehash(ht, ht->size_index);
}
start_hash_address = hash % ht->size;
hash_address = start_hash_address;
do {
struct hash_entry *entry = ht->table + hash_address;
uint32_t double_hash;
if (!entry_is_present(entry)) {
/* Stash the first available entry we find */
if (available_entry == NULL)
available_entry = entry;
if (entry_is_free(entry))
break;
}
/* Implement replacement when another insert happens
* with a matching key. This is a relatively common
* feature of hash tables, with the alternative
* generally being "insert the new value as well, and
* return it first when the key is searched for".
*
* Note that the hash table doesn't have a delete
* callback. If freeing of old data pointers is
* required to avoid memory leaks, perform a search
* before inserting.
*/
if (!entry_is_deleted(entry) &&
entry->hash == hash &&
ht->key_equals_function(key, entry->key)) {
entry->key = key;
entry->data = data;
return entry;
}
double_hash = 1 + hash % ht->rehash;
hash_address = (hash_address + double_hash) % ht->size;
} while (hash_address != start_hash_address);
if (available_entry) {
if (entry_is_deleted(available_entry))
ht->deleted_entries--;
available_entry->hash = hash;
available_entry->key = key;
available_entry->data = data;
ht->entries++;
return available_entry;
}
/* We could hit here if a required resize failed. An unchecked-malloc
* application could ignore this result.
*/
return NULL;
}
/**
* This function searches for, and removes an entry from the hash table.
*
* If the caller has previously found a struct hash_entry pointer,
* (from calling hash_table_search or remembering it from
* hash_table_insert), then hash_table_remove_entry can be called
* instead to avoid an extra search.
*/
void
hash_table_remove(struct hash_table *ht, const void *key)
{
struct hash_entry *entry;
entry = hash_table_search(ht, key);
hash_table_remove_entry(ht, entry);
}
/**
* This function deletes the given hash table entry.
*
* Note that deletion doesn't otherwise modify the table, so an iteration over
* the table deleting entries is safe.
*/
void
hash_table_remove_entry(struct hash_table *ht, struct hash_entry *entry)
{
if (!entry)
return;
entry->key = deleted_key;
ht->entries--;
ht->deleted_entries++;
}
/**
* This function is an iterator over the hash table.
*
* Pass in NULL for the first entry, as in the start of a for loop. Note that
* an iteration over the table is O(table_size) not O(entries).
*/
struct hash_entry *
hash_table_next_entry(struct hash_table *ht, struct hash_entry *entry)
{
if (entry == NULL)
entry = ht->table;
else
entry = entry + 1;
for (; entry != ht->table + ht->size; entry++) {
if (entry_is_present(entry)) {
return entry;
}
}
return NULL;
}
#ifndef _WIN32
/**
* Returns a random entry from the hash table.
*
* This may be useful in implementing random replacement (as opposed
* to just removing everything) in caches based on this hash table
* implementation. @predicate may be used to filter entries, or may
* be set to NULL for no filtering.
*/
struct hash_entry *
hash_table_random_entry(struct hash_table *ht,
int (*predicate)(struct hash_entry *entry))
{
struct hash_entry *entry;
uint32_t i = random() % ht->size;
if (ht->entries == 0)
return NULL;
for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
for (entry = ht->table; entry != ht->table + i; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
return NULL;
}
#endif

@ -0,0 +1,358 @@
/*
* Copyright © 2009,2013 Intel Corporation
* Copyright © 1988-2004 Keith Packard and Bart Massey.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors
* or their institutions shall not be used in advertising or
* otherwise to promote the sale, use or other dealings in this
* Software without prior written authorization from the
* authors.
*
* Authors:
* Eric Anholt <eric@anholt.net>
* Keith Packard <keithp@keithp.com>
* Carl Worth <cworth@cworth.org>
*/
#include <stdlib.h>
#include <ravi_int_set.h>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
/*
* From Knuth -- a good choice for hash/rehash values is p, p-2 where
* p and p-2 are both prime. These tables are sized to have an extra 10%
* free to avoid exponential performance degradation as the hash table fills
*/
static const struct {
uint32_t max_entries, size, rehash;
} hash_sizes[] = {
{ 2, 5, 3 },
{ 4, 7, 5 },
{ 8, 13, 11 },
{ 16, 19, 17 },
{ 32, 43, 41 },
{ 64, 73, 71 },
{ 128, 151, 149 },
{ 256, 283, 281 },
{ 512, 571, 569 },
{ 1024, 1153, 1151 },
{ 2048, 2269, 2267 },
{ 4096, 4519, 4517 },
{ 8192, 9013, 9011 },
{ 16384, 18043, 18041 },
{ 32768, 36109, 36107 },
{ 65536, 72091, 72089 },
{ 131072, 144409, 144407 },
{ 262144, 288361, 288359 },
{ 524288, 576883, 576881 },
{ 1048576, 1153459, 1153457 },
{ 2097152, 2307163, 2307161 },
{ 4194304, 4613893, 4613891 },
{ 8388608, 9227641, 9227639 },
{ 16777216, 18455029, 18455027 },
{ 33554432, 36911011, 36911009 },
{ 67108864, 73819861, 73819859 },
{ 134217728, 147639589, 147639587 },
{ 268435456, 295279081, 295279079 },
{ 536870912, 590559793, 590559791 },
{ 1073741824, 1181116273, 1181116271},
{ 2147483648ul, 2362232233ul, 2362232231ul}
};
static int
entry_is_free(struct int_set_entry *entry)
{
return ! entry->occupied;
}
static int
entry_is_deleted(struct int_set_entry *entry)
{
return entry->deleted;
}
static int
entry_is_present(struct int_set_entry *entry)
{
return entry->occupied && ! entry->deleted;
}
struct int_set *
int_set_create(void)
{
struct int_set *set;
set = malloc(sizeof(*set));
if (set == NULL)
return NULL;
set->size_index = 0;
set->size = hash_sizes[set->size_index].size;
set->rehash = hash_sizes[set->size_index].rehash;
set->max_entries = hash_sizes[set->size_index].max_entries;
set->table = calloc(set->size, sizeof(*set->table));
set->entries = 0;
set->deleted_entries = 0;
if (set->table == NULL) {
free(set);
return NULL;
}
return set;
}
/**
* Frees the given set.
*/
void
int_set_destroy(struct int_set *set)
{
if (!set)
return;
free(set->table);
free(set);
}
/* Does the set contain an entry with the given value.
*/
bool
int_set_contains(struct int_set *set, uint32_t value)
{
struct int_set_entry *entry;
entry = int_set_search(set, value);
return entry != NULL;
}
/**
* Finds a set entry with the given value
*
* Returns NULL if no entry is found.
*/
struct int_set_entry *
int_set_search(struct int_set *set, uint32_t value)
{
uint32_t hash_address;
hash_address = value % set->size;
do {
uint32_t double_hash;
struct int_set_entry *entry = set->table + hash_address;
if (entry_is_free(entry)) {
return NULL;
} else if (entry_is_present(entry) && entry->value == value) {
return entry;
}
double_hash = 1 + value % set->rehash;
hash_address = (hash_address + double_hash) % set->size;
} while (hash_address != value % set->size);
return NULL;
}
static void
int_set_rehash(struct int_set *set, int new_size_index)
{
struct int_set old_set;
struct int_set_entry *table, *entry;
if (new_size_index >= ARRAY_SIZE(hash_sizes))
return;
table = calloc(hash_sizes[new_size_index].size, sizeof(*set->table));
if (table == NULL)
return;
old_set = *set;
set->table = table;
set->size_index = new_size_index;
set->size = hash_sizes[set->size_index].size;
set->rehash = hash_sizes[set->size_index].rehash;
set->max_entries = hash_sizes[set->size_index].max_entries;
set->entries = 0;
set->deleted_entries = 0;
for (entry = old_set.table;
entry != old_set.table + old_set.size;
entry++) {
if (entry_is_present(entry)) {
int_set_add(set, entry->value);
}
}
free(old_set.table);
}
/**
* Inserts the given value into the set.
*
* Note that insertion may rearrange the table on a resize or rehash,
* so previously found int_set_entry pointers are no longer valid
* after this function.
*/
struct int_set_entry *
int_set_add(struct int_set *set, uint32_t value)
{
uint32_t hash_address;
struct int_set_entry *available_entry = NULL;
if (set->entries >= set->max_entries) {
int_set_rehash(set, set->size_index + 1);
} else if (set->deleted_entries + set->entries >= set->max_entries) {
int_set_rehash(set, set->size_index);
}
hash_address = value % set->size;
do {
struct int_set_entry *entry = set->table + hash_address;
uint32_t double_hash;
if (!entry_is_present(entry)) {
if (available_entry == NULL)
available_entry = entry;
if (entry_is_free(entry))
break;
if (entry_is_deleted(entry)) {
set->deleted_entries--;
entry->deleted = 0;
}
}
if (entry->value == value) {
return entry;
}
double_hash = 1 + value % set->rehash;
hash_address = (hash_address + double_hash) % set->size;
} while (hash_address != value % set->size);
if (available_entry) {
available_entry->value = value;
available_entry->occupied = 1;
set->entries++;
return available_entry;
}
/* We could hit here if a required resize failed. An unchecked-malloc
* application could ignore this result.
*/
return NULL;
}
/**
* This function searches for, and removes an entry from the set.
*
* If the caller has previously found a struct int_set_entry pointer,
* (from calling int_set_search or remembering it from int_set_add),
* then int_set_remove_entry can be called instead to avoid an extra
* search.
*/
void
int_set_remove(struct int_set *set, uint32_t value)
{
struct int_set_entry *entry;
entry = int_set_search(set, value);
int_set_remove_entry(set, entry);
}
/**
* This function deletes the given set entry.
*
* Note that deletion doesn't otherwise modify the table, so an iteration over
* the table deleting entries is safe.
*/
void
int_set_remove_entry(struct int_set *set, struct int_set_entry *entry)
{
if (!entry)
return;
entry->deleted = 1;
set->entries--;
set->deleted_entries++;
}
/**
* This function is an iterator over the set.
*
* Pass in NULL for the first entry, as in the start of a for loop. Note that
* an iteration over the table is O(table_size) not O(entries).
*/
struct int_set_entry *
int_set_next_entry(struct int_set *set, struct int_set_entry *entry)
{
if (entry == NULL)
entry = set->table;
else
entry = entry + 1;
for (; entry != set->table + set->size; entry++) {
if (entry_is_present(entry)) {
return entry;
}
}
return NULL;
}
#ifndef _WIN32
struct int_set_entry *
int_set_random_entry(struct int_set *set,
int (*predicate)(struct int_set_entry *entry))
{
struct int_set_entry *entry;
uint32_t i = random() % set->size;
if (set->entries == 0)
return NULL;
for (entry = set->table + i; entry != set->table + set->size; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
for (entry = set->table; entry != set->table + i; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
return NULL;
}
#endif

@ -0,0 +1,420 @@
/*
* Copyright © 2009 Intel Corporation
* Copyright © 1988-2004 Keith Packard and Bart Massey.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors
* or their institutions shall not be used in advertising or
* otherwise to promote the sale, use or other dealings in this
* Software without prior written authorization from the
* authors.
*
* Authors:
* Eric Anholt <eric@anholt.net>
* Keith Packard <keithp@keithp.com>
*/
#include <assert.h>
#include <stdlib.h>
#include <ravi_set.h>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
/*
* From Knuth -- a good choice for hash/rehash values is p, p-2 where
* p and p-2 are both prime. These tables are sized to have an extra 10%
* free to avoid exponential performance degradation as the hash table fills
*/
static const uint32_t deleted_key_value;
static const void *deleted_key = &deleted_key_value;
static const struct {
uint32_t max_entries, size, rehash;
} hash_sizes[] = {
{ 2, 5, 3 },
{ 4, 7, 5 },
{ 8, 13, 11 },
{ 16, 19, 17 },
{ 32, 43, 41 },
{ 64, 73, 71 },
{ 128, 151, 149 },
{ 256, 283, 281 },
{ 512, 571, 569 },
{ 1024, 1153, 1151 },
{ 2048, 2269, 2267 },
{ 4096, 4519, 4517 },
{ 8192, 9013, 9011 },
{ 16384, 18043, 18041 },
{ 32768, 36109, 36107 },
{ 65536, 72091, 72089 },
{ 131072, 144409, 144407 },
{ 262144, 288361, 288359 },
{ 524288, 576883, 576881 },
{ 1048576, 1153459, 1153457 },
{ 2097152, 2307163, 2307161 },
{ 4194304, 4613893, 4613891 },
{ 8388608, 9227641, 9227639 },
{ 16777216, 18455029, 18455027 },
{ 33554432, 36911011, 36911009 },
{ 67108864, 73819861, 73819859 },
{ 134217728, 147639589, 147639587 },
{ 268435456, 295279081, 295279079 },
{ 536870912, 590559793, 590559791 },
{ 1073741824, 1181116273, 1181116271},
{ 2147483648ul, 2362232233ul, 2362232231ul}
};
static int
entry_is_free(const struct set_entry *entry)
{
return entry->key == NULL;
}
static int
entry_is_deleted(const struct set_entry *entry)
{
return entry->key == deleted_key;
}
static int
entry_is_present(const struct set_entry *entry)
{
return entry->key != NULL && entry->key != deleted_key;
}
struct set *
set_create(uint32_t (*hash_function)(const void *key),
int (*key_equals_function)(const void *a,
const void *b))
{
struct set *set;
set = malloc(sizeof(*set));
if (set == NULL)
return NULL;
set->size_index = 0;
set->size = hash_sizes[set->size_index].size;
set->rehash = hash_sizes[set->size_index].rehash;
set->max_entries = hash_sizes[set->size_index].max_entries;
set->hash_function = hash_function;
set->key_equals_function = key_equals_function;
set->table = calloc(set->size, sizeof(*set->table));
set->entries = 0;
set->deleted_entries = 0;
if (set->table == NULL) {
free(set);
return NULL;
}
return set;
}
/**
* Frees the given set.
*
* If delete_function is passed, it gets called on each entry present before
* freeing.
*/
void
set_destroy(struct set *set, void (*delete_function)(struct set_entry *entry))
{
if (!set)
return;
if (delete_function) {
struct set_entry *entry;
set_foreach(set, entry) {
delete_function(entry);
}
}
free(set->table);
free(set);
}
/* Does the set contain an entry with the given key.
*/
bool
set_contains(struct set *set, const void *key)
{
struct set_entry *entry;
entry = set_search(set, key);
return entry != NULL;
}
/**
* Finds a set entry with the given key.
*
* Returns NULL if no entry is found.
*/
struct set_entry *
set_search(struct set *set, const void *key)
{
uint32_t hash = set->hash_function(key);
return set_search_pre_hashed (set, hash, key);
}
/**
* Finds a set entry with the given key and hash of that key.
*
* Returns NULL if no entry is found.
*/
struct set_entry *
set_search_pre_hashed(struct set *set, uint32_t hash, const void *key)
{
uint32_t hash_address;
hash_address = hash % set->size;
do {
uint32_t double_hash;
struct set_entry *entry = set->table + hash_address;
if (entry_is_free(entry)) {
return NULL;
} else if (entry_is_present(entry) && entry->hash == hash) {
if (set->key_equals_function(key, entry->key)) {
return entry;
}
}
double_hash = 1 + hash % set->rehash;
hash_address = (hash_address + double_hash) % set->size;
} while (hash_address != hash % set->size);
return NULL;
}
static void
set_rehash(struct set *set, int new_size_index)
{
struct set old_set;
struct set_entry *table, *entry;
if (new_size_index >= ARRAY_SIZE(hash_sizes))
return;
table = calloc(hash_sizes[new_size_index].size, sizeof(*set->table));
if (table == NULL)
return;
old_set = *set;
set->table = table;
set->size_index = new_size_index;
set->size = hash_sizes[set->size_index].size;
set->rehash = hash_sizes[set->size_index].rehash;
set->max_entries = hash_sizes[set->size_index].max_entries;
set->entries = 0;
set->deleted_entries = 0;
set_foreach(&old_set, entry) {
set_add_pre_hashed(set, entry->hash, entry->key);
}
free(old_set.table);
}
/**
* Inserts the key into the set.
*
* Note that insertion may rearrange the set on a resize or rehash, so
* previously found set_entry pointers are no longer valid after this
* function.
*/
struct set_entry *
set_add(struct set *set, const void *key)
{
uint32_t hash = set->hash_function(key);
/* Make sure nobody tries to add one of the magic values as a
* key. If you need to do so, either do so in a wrapper, or
* store keys with the magic values separately in the struct
* set.
*/
assert(key != NULL);
return set_add_pre_hashed(set, hash, key);
}
/**
* Inserts the key with the given hash into the set.
*
* Note that insertion may rearrange the set on a resize or rehash, so
* previously found set_entry pointers are no longer valid after this
* function.
*/
struct set_entry *
set_add_pre_hashed(struct set *set, uint32_t hash, const void *key)
{
uint32_t hash_address;
struct set_entry *available_entry = NULL;
if (set->entries >= set->max_entries) {
set_rehash(set, set->size_index + 1);
} else if (set->deleted_entries + set->entries >= set->max_entries) {
set_rehash(set, set->size_index);
}
hash_address = hash % set->size;
do {
struct set_entry *entry = set->table + hash_address;
uint32_t double_hash;
if (!entry_is_present(entry)) {
/* Stash the first available entry we find */
if (available_entry == NULL)
available_entry = entry;
if (entry_is_free(entry))
break;
}
/* Implement replacement when another insert happens
* with a matching key. This is a relatively common
* feature of hash tables, with the alternative
* generally being "insert the new value as well, and
* return it first when the key is searched for".
*
* Note that the set doesn't have a delete callback.
* If freeing of old keys is required to avoid memory leaks,
* perform a search before inserting.
*/
if (!entry_is_deleted(entry) &&
entry->hash == hash &&
set->key_equals_function(key, entry->key)) {
entry->key = key;
return entry;
}
double_hash = 1 + hash % set->rehash;
hash_address = (hash_address + double_hash) % set->size;
} while (hash_address != hash % set->size);
if (available_entry) {
if (entry_is_deleted(available_entry))
set->deleted_entries--;
available_entry->hash = hash;
available_entry->key = key;
set->entries++;
return available_entry;
}
/* We could hit here if a required resize failed. An unchecked-malloc
* application could ignore this result.
*/
return NULL;
}
/**
* This function searches for, and removes an entry from the set.
*
* If the caller has previously found a struct set_entry pointer,
* (from calling set_search or remembering it from set_add), then
* set_remove_entry can be called instead to avoid an extra search.
*/
void
set_remove(struct set *set, const void *key)
{
struct set_entry *entry;
entry = set_search(set, key);
set_remove_entry(set, entry);
}
/**
* This function deletes the set given set entry.
*
* Note that deletion doesn't otherwise modify the set, so an
* iteration over the set deleting entries is safe.
*/
void
set_remove_entry(struct set *set, struct set_entry *entry)
{
if (!entry)
return;
entry->key = deleted_key;
set->entries--;
set->deleted_entries++;
}
/**
* This function is an iterator over the set.
*
* Pass in NULL for the first entry, as in the start of a for loop.
* Note that an iteration over the set is O(table_size) not
* O(entries).
*/
struct set_entry *
set_next_entry(struct set *set, struct set_entry *entry)
{
if (entry == NULL)
entry = set->table;
else
entry = entry + 1;
for (; entry != set->table + set->size; entry++) {
if (entry_is_present(entry)) {
return entry;
}
}
return NULL;
}
#ifndef _WIN32
struct set_entry *
set_random_entry(struct set *set,
int (*predicate)(struct set_entry *entry))
{
struct set_entry *entry;
uint32_t i = random() % set->size;
if (set->entries == 0)
return NULL;
for (entry = set->table + i; entry != set->table + set->size; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
for (entry = set->table; entry != set->table + i; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
return NULL;
}
#endif
Loading…
Cancel
Save