2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
* Copyright 2008 Blender Foundation. All rights reserved. */
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2012-02-12 14:40:08 +00:00
|
|
|
#pragma once
|
2010-09-25 01:54:58 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2011-09-09 02:52:20 +00:00
|
|
|
*/
|
2010-09-25 01:54:58 +00:00
|
|
|
|
2013-09-01 15:01:15 +00:00
|
|
|
#include "BLI_compiler_attrs.h"
|
|
|
|
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2011-09-09 02:52:20 +00:00
|
|
|
typedef struct {
|
|
|
|
uintptr_t key;
|
|
|
|
void *val;
|
|
|
|
} SmallHashEntry;
|
2010-09-25 01:54:58 +00:00
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* How much stack space to use before dynamically allocating memory.
|
|
|
|
* set to match one of the values in 'hashsizes' to avoid too many mallocs.
|
|
|
|
*/
|
2014-02-02 02:19:11 +11:00
|
|
|
#define SMSTACKSIZE 131
|
2010-09-25 01:54:58 +00:00
|
|
|
typedef struct SmallHash {
|
2014-01-30 20:51:44 +11:00
|
|
|
unsigned int nbuckets;
|
|
|
|
unsigned int nentries;
|
|
|
|
unsigned int cursize;
|
2014-02-02 02:19:11 +11:00
|
|
|
|
|
|
|
SmallHashEntry *buckets;
|
|
|
|
SmallHashEntry buckets_stack[SMSTACKSIZE];
|
2010-09-25 01:54:58 +00:00
|
|
|
} SmallHash;
|
|
|
|
|
2011-09-09 02:52:20 +00:00
|
|
|
typedef struct {
|
2014-12-18 13:12:25 +01:00
|
|
|
const SmallHash *sh;
|
2013-06-22 20:20:06 +00:00
|
|
|
unsigned int i;
|
2011-04-13 21:48:16 +00:00
|
|
|
} SmallHashIter;
|
|
|
|
|
2022-01-07 11:38:08 +11:00
|
|
|
void BLI_smallhash_init_ex(SmallHash *sh, unsigned int nentries_reserve) ATTR_NONNULL(1);
|
2014-01-30 20:51:44 +11:00
|
|
|
void BLI_smallhash_init(SmallHash *sh) ATTR_NONNULL(1);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* \note does *not* free *sh itself! only the direct data!
|
|
|
|
*/
|
2014-01-30 20:51:44 +11:00
|
|
|
void BLI_smallhash_release(SmallHash *sh) ATTR_NONNULL(1);
|
|
|
|
void BLI_smallhash_insert(SmallHash *sh, uintptr_t key, void *item) ATTR_NONNULL(1);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
2022-01-06 13:54:52 +11:00
|
|
|
* Inserts a new value to a key that may already be in #GHash.
|
2021-12-09 20:01:44 +11:00
|
|
|
*
|
|
|
|
* Avoids #BLI_smallhash_remove, #BLI_smallhash_insert calls (double lookups)
|
|
|
|
*
|
|
|
|
* \returns true if a new key has been added.
|
|
|
|
*/
|
2014-12-09 17:18:05 +01:00
|
|
|
bool BLI_smallhash_reinsert(SmallHash *sh, uintptr_t key, void *item) ATTR_NONNULL(1);
|
2014-01-30 20:51:44 +11:00
|
|
|
bool BLI_smallhash_remove(SmallHash *sh, uintptr_t key) ATTR_NONNULL(1);
|
2014-12-18 13:12:25 +01:00
|
|
|
void *BLI_smallhash_lookup(const SmallHash *sh, uintptr_t key)
|
|
|
|
ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT;
|
|
|
|
void **BLI_smallhash_lookup_p(const SmallHash *sh, uintptr_t key)
|
|
|
|
ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT;
|
|
|
|
bool BLI_smallhash_haskey(const SmallHash *sh, uintptr_t key) ATTR_NONNULL(1);
|
2018-02-15 23:36:11 +11:00
|
|
|
int BLI_smallhash_len(const SmallHash *sh) ATTR_NONNULL(1);
|
2013-09-01 15:01:15 +00:00
|
|
|
void *BLI_smallhash_iternext(SmallHashIter *iter, uintptr_t *key)
|
|
|
|
ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT;
|
2014-12-09 17:18:05 +01:00
|
|
|
void **BLI_smallhash_iternext_p(SmallHashIter *iter, uintptr_t *key)
|
|
|
|
ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT;
|
2014-12-18 13:12:25 +01:00
|
|
|
void *BLI_smallhash_iternew(const SmallHash *sh, SmallHashIter *iter, uintptr_t *key)
|
|
|
|
ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT;
|
|
|
|
void **BLI_smallhash_iternew_p(const SmallHash *sh, SmallHashIter *iter, uintptr_t *key)
|
|
|
|
ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT;
|
2014-01-30 20:51:44 +11:00
|
|
|
/* void BLI_smallhash_print(SmallHash *sh); */ /* UNUSED */
|
2011-04-13 21:48:16 +00:00
|
|
|
|
2014-08-23 21:15:15 +10:00
|
|
|
#ifdef DEBUG
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Measure how well the hash function performs
|
|
|
|
* (1.0 is perfect - no stepping needed).
|
|
|
|
*
|
|
|
|
* Smaller is better!
|
|
|
|
*/
|
2014-08-23 21:15:15 +10:00
|
|
|
double BLI_smallhash_calc_quality(SmallHash *sh);
|
|
|
|
#endif
|
|
|
|
|
2020-03-02 15:04:53 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|