2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2005-08-21 20:48:45 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2005-08-21 20:48:45 +00:00
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-08-21 20:48:45 +00:00
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is: none of this file.
|
|
|
|
|
*
|
2011-12-28 10:20:37 +00:00
|
|
|
* Contributor(s): Daniel Dunbar, Joseph Eagar
|
2005-08-21 20:48:45 +00:00
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2005-08-21 20:48:45 +00:00
|
|
|
* A general (pointer -> pointer) hash table ADT
|
|
|
|
|
*/
|
|
|
|
|
|
2011-02-27 20:37:56 +00:00
|
|
|
/** \file blender/blenlib/intern/edgehash.c
|
|
|
|
|
* \ingroup bli
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2005-08-21 20:48:45 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2013-05-10 12:06:40 +00:00
|
|
|
#include <limits.h>
|
2005-08-21 20:48:45 +00:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
#include "BLI_edgehash.h"
|
|
|
|
|
#include "BLI_mempool.h"
|
2005-08-21 20:48:45 +00:00
|
|
|
|
2013-05-10 12:06:40 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
# pragma GCC diagnostic ignored "-Wstrict-overflow"
|
|
|
|
|
# pragma GCC diagnostic error "-Wsign-conversion"
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
/**************inlined code************/
|
2013-05-08 12:54:07 +00:00
|
|
|
static const unsigned int _ehash_hashsizes[] = {
|
2011-12-28 10:20:37 +00:00
|
|
|
1, 3, 5, 11, 17, 37, 67, 131, 257, 521, 1031, 2053, 4099, 8209,
|
|
|
|
|
16411, 32771, 65537, 131101, 262147, 524309, 1048583, 2097169,
|
|
|
|
|
4194319, 8388617, 16777259, 33554467, 67108879, 134217757,
|
2005-08-21 20:48:45 +00:00
|
|
|
268435459
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-12 15:02:10 +00:00
|
|
|
#define EDGE_HASH(v0, v1) ((v0 * 39) ^ (v1 * 31))
|
2011-12-28 10:20:37 +00:00
|
|
|
|
|
|
|
|
/* ensure v0 is smaller */
|
|
|
|
|
#define EDGE_ORD(v0, v1) \
|
2011-12-31 15:10:38 +00:00
|
|
|
if (v0 > v1) { \
|
2011-12-28 10:20:37 +00:00
|
|
|
v0 ^= v1; \
|
|
|
|
|
v1 ^= v0; \
|
|
|
|
|
v0 ^= v1; \
|
2012-05-27 20:13:59 +00:00
|
|
|
} (void)0
|
2005-08-21 20:48:45 +00:00
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
typedef struct EdgeEntry EdgeEntry;
|
|
|
|
|
struct EdgeEntry {
|
|
|
|
|
EdgeEntry *next;
|
|
|
|
|
unsigned int v0, v1;
|
2005-08-21 20:48:45 +00:00
|
|
|
void *val;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct EdgeHash {
|
2011-12-28 10:20:37 +00:00
|
|
|
EdgeEntry **buckets;
|
|
|
|
|
BLI_mempool *epool;
|
2013-05-10 12:06:40 +00:00
|
|
|
unsigned int nbuckets, nentries, cursize;
|
2005-08-21 20:48:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
EdgeHash *BLI_edgehash_new(void)
|
|
|
|
|
{
|
2011-12-28 10:20:37 +00:00
|
|
|
EdgeHash *eh = MEM_callocN(sizeof(*eh), "EdgeHash");
|
|
|
|
|
eh->cursize = 0;
|
|
|
|
|
eh->nentries = 0;
|
|
|
|
|
eh->nbuckets = _ehash_hashsizes[eh->cursize];
|
2005-08-21 20:48:45 +00:00
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
eh->buckets = MEM_callocN(eh->nbuckets * sizeof(*eh->buckets), "eh buckets 2");
|
2012-03-01 22:59:18 +00:00
|
|
|
eh->epool = BLI_mempool_create(sizeof(EdgeEntry), 512, 512, BLI_MEMPOOL_SYSMALLOC);
|
2011-12-28 10:20:37 +00:00
|
|
|
|
2005-08-21 20:48:45 +00:00
|
|
|
return eh;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
|
|
|
|
|
void BLI_edgehash_insert(EdgeHash *eh, unsigned int v0, unsigned int v1, void *val)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2005-08-21 20:48:45 +00:00
|
|
|
unsigned int hash;
|
2011-12-28 10:20:37 +00:00
|
|
|
EdgeEntry *e = BLI_mempool_alloc(eh->epool);
|
2005-08-21 20:48:45 +00:00
|
|
|
|
2012-02-29 16:29:09 +00:00
|
|
|
/* this helps to track down errors with bad edge data */
|
|
|
|
|
BLI_assert(v0 != v1);
|
|
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
EDGE_ORD(v0, v1); /* ensure v0 is smaller */
|
|
|
|
|
|
|
|
|
|
hash = EDGE_HASH(v0, v1) % eh->nbuckets;
|
2005-08-21 20:48:45 +00:00
|
|
|
|
|
|
|
|
e->v0 = v0;
|
|
|
|
|
e->v1 = v1;
|
|
|
|
|
e->val = val;
|
2011-12-28 10:20:37 +00:00
|
|
|
e->next = eh->buckets[hash];
|
2012-05-12 15:02:10 +00:00
|
|
|
eh->buckets[hash] = e;
|
2011-12-28 10:20:37 +00:00
|
|
|
|
2012-05-12 15:02:10 +00:00
|
|
|
if (++eh->nentries > eh->nbuckets * 3) {
|
2012-10-20 08:02:18 +00:00
|
|
|
EdgeEntry **old = eh->buckets;
|
2013-05-10 12:06:40 +00:00
|
|
|
unsigned int i, nold = eh->nbuckets;
|
2011-12-28 10:20:37 +00:00
|
|
|
|
|
|
|
|
eh->nbuckets = _ehash_hashsizes[++eh->cursize];
|
|
|
|
|
eh->buckets = MEM_mallocN(eh->nbuckets * sizeof(*eh->buckets), "eh buckets");
|
|
|
|
|
memset(eh->buckets, 0, eh->nbuckets * sizeof(*eh->buckets));
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < nold; i++) {
|
2012-05-12 15:02:10 +00:00
|
|
|
for (e = old[i]; e; ) {
|
2011-12-28 10:20:37 +00:00
|
|
|
EdgeEntry *n = e->next;
|
|
|
|
|
|
|
|
|
|
hash = EDGE_HASH(e->v0, e->v1) % eh->nbuckets;
|
|
|
|
|
e->next = eh->buckets[hash];
|
2012-05-12 15:02:10 +00:00
|
|
|
eh->buckets[hash] = e;
|
2011-12-28 10:20:37 +00:00
|
|
|
|
|
|
|
|
e = n;
|
2005-08-21 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-12-28 10:20:37 +00:00
|
|
|
|
|
|
|
|
MEM_freeN(old);
|
2005-08-21 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
void **BLI_edgehash_lookup_p(EdgeHash *eh, unsigned int v0, unsigned int v1)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2005-08-21 20:48:45 +00:00
|
|
|
unsigned int hash;
|
2011-12-28 10:20:37 +00:00
|
|
|
EdgeEntry *e;
|
2005-08-21 20:48:45 +00:00
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
EDGE_ORD(v0, v1); /* ensure v0 is smaller */
|
|
|
|
|
|
|
|
|
|
hash = EDGE_HASH(v0, v1) % eh->nbuckets;
|
|
|
|
|
for (e = eh->buckets[hash]; e; e = e->next)
|
|
|
|
|
if (v0 == e->v0 && v1 == e->v1)
|
2005-08-21 20:48:45 +00:00
|
|
|
return &e->val;
|
2011-12-28 10:20:37 +00:00
|
|
|
|
2005-08-21 20:48:45 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
void *BLI_edgehash_lookup(EdgeHash *eh, unsigned int v0, unsigned int v1)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2011-12-28 10:20:37 +00:00
|
|
|
void **value_p = BLI_edgehash_lookup_p(eh, v0, v1);
|
2005-08-21 20:48:45 +00:00
|
|
|
|
2012-05-12 15:02:10 +00:00
|
|
|
return value_p ? *value_p : NULL;
|
2005-08-21 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-22 12:00:37 +00:00
|
|
|
bool BLI_edgehash_haskey(EdgeHash *eh, unsigned int v0, unsigned int v1)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2011-12-28 10:20:37 +00:00
|
|
|
return BLI_edgehash_lookup_p(eh, v0, v1) != NULL;
|
2005-08-21 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
int BLI_edgehash_size(EdgeHash *eh)
|
|
|
|
|
{
|
2013-05-10 12:06:40 +00:00
|
|
|
return (int)eh->nentries;
|
2005-08-21 20:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void BLI_edgehash_clear(EdgeHash *eh, EdgeHashFreeFP valfreefp)
|
|
|
|
|
{
|
2013-05-10 12:06:40 +00:00
|
|
|
unsigned int i;
|
2005-08-21 20:48:45 +00:00
|
|
|
|
2012-05-12 15:02:10 +00:00
|
|
|
for (i = 0; i < eh->nbuckets; i++) {
|
2011-12-28 10:20:37 +00:00
|
|
|
EdgeEntry *e;
|
2005-08-21 20:48:45 +00:00
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
for (e = eh->buckets[i]; e; ) {
|
|
|
|
|
EdgeEntry *n = e->next;
|
2005-08-21 20:48:45 +00:00
|
|
|
|
|
|
|
|
if (valfreefp) valfreefp(e->val);
|
2011-12-28 10:20:37 +00:00
|
|
|
BLI_mempool_free(eh->epool, e);
|
2005-08-21 20:48:45 +00:00
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
e = n;
|
2005-08-21 20:48:45 +00:00
|
|
|
}
|
2011-12-28 10:20:37 +00:00
|
|
|
eh->buckets[i] = NULL;
|
2005-08-21 20:48:45 +00:00
|
|
|
}
|
2005-08-23 02:05:45 +00:00
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
eh->nentries = 0;
|
2005-08-21 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void BLI_edgehash_free(EdgeHash *eh, EdgeHashFreeFP valfreefp)
|
|
|
|
|
{
|
2005-08-21 21:39:44 +00:00
|
|
|
BLI_edgehash_clear(eh, valfreefp);
|
2011-12-28 10:20:37 +00:00
|
|
|
|
|
|
|
|
BLI_mempool_destroy(eh->epool);
|
|
|
|
|
|
|
|
|
|
MEM_freeN(eh->buckets);
|
2005-08-21 20:48:45 +00:00
|
|
|
MEM_freeN(eh);
|
|
|
|
|
}
|
2005-08-21 21:39:44 +00:00
|
|
|
|
2005-08-23 02:29:22 +00:00
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
|
|
|
|
|
struct EdgeHashIterator {
|
|
|
|
|
EdgeHash *eh;
|
2013-05-10 12:06:40 +00:00
|
|
|
unsigned int curBucket;
|
2011-12-28 10:20:37 +00:00
|
|
|
EdgeEntry *curEntry;
|
2005-08-23 02:29:22 +00:00
|
|
|
};
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh)
|
|
|
|
|
{
|
2011-12-28 10:20:37 +00:00
|
|
|
EdgeHashIterator *ehi = MEM_mallocN(sizeof(*ehi), "eh iter");
|
|
|
|
|
ehi->eh = eh;
|
|
|
|
|
ehi->curEntry = NULL;
|
2013-05-10 12:06:40 +00:00
|
|
|
ehi->curBucket = UINT_MAX; /* wraps to zero */
|
2005-08-23 02:29:22 +00:00
|
|
|
while (!ehi->curEntry) {
|
|
|
|
|
ehi->curBucket++;
|
2011-12-28 10:20:37 +00:00
|
|
|
if (ehi->curBucket == ehi->eh->nbuckets)
|
2005-08-23 02:29:22 +00:00
|
|
|
break;
|
2011-12-28 10:20:37 +00:00
|
|
|
ehi->curEntry = ehi->eh->buckets[ehi->curBucket];
|
2005-08-23 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
return ehi;
|
|
|
|
|
}
|
2011-12-17 00:52:36 +00:00
|
|
|
void BLI_edgehashIterator_free(EdgeHashIterator *ehi)
|
|
|
|
|
{
|
2011-12-28 10:20:37 +00:00
|
|
|
MEM_freeN(ehi);
|
2005-08-23 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-28 10:20:37 +00:00
|
|
|
void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, unsigned int *v0_r, unsigned int *v1_r)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2005-08-23 02:29:22 +00:00
|
|
|
if (ehi->curEntry) {
|
|
|
|
|
*v0_r = ehi->curEntry->v0;
|
|
|
|
|
*v1_r = ehi->curEntry->v1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-17 00:52:36 +00:00
|
|
|
void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi)
|
|
|
|
|
{
|
2012-05-12 15:02:10 +00:00
|
|
|
return ehi->curEntry ? ehi->curEntry->val : NULL;
|
2005-08-23 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void BLI_edgehashIterator_setValue(EdgeHashIterator *ehi, void *val)
|
|
|
|
|
{
|
2011-12-28 10:20:37 +00:00
|
|
|
if (ehi->curEntry) {
|
|
|
|
|
ehi->curEntry->val = val;
|
|
|
|
|
}
|
2008-05-12 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void BLI_edgehashIterator_step(EdgeHashIterator *ehi)
|
|
|
|
|
{
|
2005-08-23 02:29:22 +00:00
|
|
|
if (ehi->curEntry) {
|
2011-12-28 10:20:37 +00:00
|
|
|
ehi->curEntry = ehi->curEntry->next;
|
2005-08-23 02:29:22 +00:00
|
|
|
while (!ehi->curEntry) {
|
|
|
|
|
ehi->curBucket++;
|
2011-12-28 10:20:37 +00:00
|
|
|
if (ehi->curBucket == ehi->eh->nbuckets) {
|
2005-08-23 02:29:22 +00:00
|
|
|
break;
|
2011-12-28 10:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ehi->curEntry = ehi->eh->buckets[ehi->curBucket];
|
2005-08-23 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-22 12:00:37 +00:00
|
|
|
bool BLI_edgehashIterator_isDone(EdgeHashIterator *ehi)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2013-04-22 12:00:37 +00:00
|
|
|
return (ehi->curEntry == NULL);
|
2005-08-23 02:29:22 +00:00
|
|
|
}
|
|
|
|
|
|