axmol/external/chipmunk/src/cpHashSet.c

254 lines
5.6 KiB
C
Raw Normal View History

2010-09-04 18:18:14 +08:00
/* Copyright (c) 2007 Scott Lembcke
*
* 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 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.
*/
#include "chipmunk_private.h"
2010-09-04 18:18:14 +08:00
#include "prime.h"
typedef struct cpHashSetBin {
2012-06-12 09:38:38 +08:00
void *elt;
cpHashValue hash;
struct cpHashSetBin *next;
} cpHashSetBin;
2010-09-04 18:18:14 +08:00
struct cpHashSet {
2012-06-12 09:38:38 +08:00
unsigned int entries, size;
cpHashSetEqlFunc eql;
void *default_value;
cpHashSetBin **table;
cpHashSetBin *pooledBins;
cpArray *allocatedBuffers;
};
2010-09-04 18:18:14 +08:00
void
cpHashSetFree(cpHashSet *set)
{
2012-06-12 09:38:38 +08:00
if(set){
cpfree(set->table);
cpArrayFreeEach(set->allocatedBuffers, cpfree);
cpArrayFree(set->allocatedBuffers);
cpfree(set);
}
2010-09-04 18:18:14 +08:00
}
cpHashSet *
cpHashSetNew(int size, cpHashSetEqlFunc eqlFunc)
2010-09-04 18:18:14 +08:00
{
2012-06-12 09:38:38 +08:00
cpHashSet *set = (cpHashSet *)cpcalloc(1, sizeof(cpHashSet));
set->size = next_prime(size);
set->entries = 0;
set->eql = eqlFunc;
set->default_value = NULL;
set->table = (cpHashSetBin **)cpcalloc(set->size, sizeof(cpHashSetBin *));
set->pooledBins = NULL;
set->allocatedBuffers = cpArrayNew(0);
return set;
2010-09-04 18:18:14 +08:00
}
void
cpHashSetSetDefaultValue(cpHashSet *set, void *default_value)
2010-09-04 18:18:14 +08:00
{
2012-06-12 09:38:38 +08:00
set->default_value = default_value;
2010-09-04 18:18:14 +08:00
}
static int
setIsFull(cpHashSet *set)
{
2012-06-12 09:38:38 +08:00
return (set->entries >= set->size);
2010-09-04 18:18:14 +08:00
}
static void
cpHashSetResize(cpHashSet *set)
{
2012-06-12 09:38:38 +08:00
// Get the next approximate doubled prime.
unsigned int newSize = next_prime(set->size + 1);
// Allocate a new table.
cpHashSetBin **newTable = (cpHashSetBin **)cpcalloc(newSize, sizeof(cpHashSetBin *));
// Iterate over the chains.
for(unsigned int i=0; i<set->size; i++){
// Rehash the bins into the new table.
cpHashSetBin *bin = set->table[i];
while(bin){
cpHashSetBin *next = bin->next;
cpHashValue idx = bin->hash%newSize;
bin->next = newTable[idx];
newTable[idx] = bin;
bin = next;
}
}
cpfree(set->table);
set->table = newTable;
set->size = newSize;
2010-09-04 18:18:14 +08:00
}
static inline void
recycleBin(cpHashSet *set, cpHashSetBin *bin)
{
2012-06-12 09:38:38 +08:00
bin->next = set->pooledBins;
set->pooledBins = bin;
bin->elt = NULL;
2010-09-04 18:18:14 +08:00
}
static cpHashSetBin *
getUnusedBin(cpHashSet *set)
{
2012-06-12 09:38:38 +08:00
cpHashSetBin *bin = set->pooledBins;
if(bin){
set->pooledBins = bin->next;
return bin;
} else {
// Pool is exhausted, make more
int count = CP_BUFFER_BYTES/sizeof(cpHashSetBin);
cpAssertHard(count, "Internal Error: Buffer size is too small.");
cpHashSetBin *buffer = (cpHashSetBin *)cpcalloc(1, CP_BUFFER_BYTES);
cpArrayPush(set->allocatedBuffers, buffer);
// push all but the first one, return it instead
for(int i=1; i<count; i++) recycleBin(set, buffer + i);
return buffer;
}
2010-09-04 18:18:14 +08:00
}
int
cpHashSetCount(cpHashSet *set)
{
2012-06-12 09:38:38 +08:00
return set->entries;
}
2010-09-04 18:18:14 +08:00
void *
cpHashSetInsert(cpHashSet *set, cpHashValue hash, void *ptr, void *data, cpHashSetTransFunc trans)
2010-09-04 18:18:14 +08:00
{
2012-06-12 09:38:38 +08:00
cpHashValue idx = hash%set->size;
// Find the bin with the matching element.
cpHashSetBin *bin = set->table[idx];
while(bin && !set->eql(ptr, bin->elt))
bin = bin->next;
// Create it if necessary.
if(!bin){
bin = getUnusedBin(set);
bin->hash = hash;
bin->elt = (trans ? trans(ptr, data) : data);
bin->next = set->table[idx];
set->table[idx] = bin;
set->entries++;
if(setIsFull(set)) cpHashSetResize(set);
}
return bin->elt;
2010-09-04 18:18:14 +08:00
}
void *
cpHashSetRemove(cpHashSet *set, cpHashValue hash, void *ptr)
{
2012-06-12 09:38:38 +08:00
cpHashValue idx = hash%set->size;
cpHashSetBin **prev_ptr = &set->table[idx];
cpHashSetBin *bin = set->table[idx];
// Find the bin
while(bin && !set->eql(ptr, bin->elt)){
prev_ptr = &bin->next;
bin = bin->next;
}
// Remove it if it exists.
if(bin){
// Update the previous linked list pointer
(*prev_ptr) = bin->next;
set->entries--;
void *elt = bin->elt;
recycleBin(set, bin);
return elt;
}
return NULL;
2010-09-04 18:18:14 +08:00
}
void *
cpHashSetFind(cpHashSet *set, cpHashValue hash, void *ptr)
2012-06-12 09:38:38 +08:00
{
cpHashValue idx = hash%set->size;
cpHashSetBin *bin = set->table[idx];
while(bin && !set->eql(ptr, bin->elt))
bin = bin->next;
return (bin ? bin->elt : set->default_value);
2010-09-04 18:18:14 +08:00
}
void
cpHashSetEach(cpHashSet *set, cpHashSetIteratorFunc func, void *data)
2010-09-04 18:18:14 +08:00
{
2012-06-12 09:38:38 +08:00
for(unsigned int i=0; i<set->size; i++){
cpHashSetBin *bin = set->table[i];
while(bin){
cpHashSetBin *next = bin->next;
func(bin->elt, data);
bin = next;
}
}
2010-09-04 18:18:14 +08:00
}
void
cpHashSetFilter(cpHashSet *set, cpHashSetFilterFunc func, void *data)
{
2012-06-12 09:38:38 +08:00
for(unsigned int i=0; i<set->size; i++){
// The rest works similarly to cpHashSetRemove() above.
cpHashSetBin **prev_ptr = &set->table[i];
cpHashSetBin *bin = set->table[i];
while(bin){
cpHashSetBin *next = bin->next;
if(func(bin->elt, data)){
prev_ptr = &bin->next;
} else {
(*prev_ptr) = next;
set->entries--;
recycleBin(set, bin);
}
bin = next;
}
}
2010-09-04 18:18:14 +08:00
}