issue #98 Start work generating linear IR
parent
a68643dca7
commit
7b06b524ca
@ -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,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,423 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
@ -0,0 +1,356 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
@ -0,0 +1,418 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in new issue