Compare commits
23 Commits
Author | SHA1 | Date |
---|---|---|
Dibyendu Majumdar | 93670a4bb9 | 4 years ago |
Dibyendu Majumdar | 804c7a61a7 | 4 years ago |
Dibyendu Majumdar | 0634012784 | 4 years ago |
Dibyendu Majumdar | e86a338d50 | 4 years ago |
Dibyendu Majumdar | 39a7b08019 | 4 years ago |
Dibyendu Majumdar | e54cdacb16 | 4 years ago |
Dibyendu Majumdar | 1ceee79973 | 4 years ago |
Dibyendu Majumdar | f1a310df95 | 4 years ago |
Dibyendu Majumdar | 5ce1cc3c64 | 4 years ago |
Dibyendu Majumdar | 8600378cce | 4 years ago |
Dibyendu Majumdar | 77208e2f47 | 4 years ago |
Dibyendu Majumdar | 1f4941fd2a | 4 years ago |
Dibyendu Majumdar | 5bf1847884 | 4 years ago |
Dibyendu Majumdar | 24b8a8e45c | 4 years ago |
Dibyendu Majumdar | ed4e86cdae | 4 years ago |
Dibyendu Majumdar | 0fe4d1a5b1 | 4 years ago |
Dibyendu Majumdar | 6cb6ef838f | 4 years ago |
Dibyendu Majumdar | 7b06b524ca | 4 years ago |
Dibyendu Majumdar | a68643dca7 | 4 years ago |
Dibyendu Majumdar | 226ef82d0f | 4 years ago |
Dibyendu Majumdar | 21310c9162 | 4 years ago |
Dibyendu Majumdar | 79b558fed4 | 4 years ago |
Dibyendu Majumdar | 5cc228c663 | 4 years ago |
@ -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
@ -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…
Reference in new issue