Added options parameter.
This commit is contained in:
parent
895f73d4b6
commit
a839a2dfd2
@ -13,10 +13,12 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define HASHSET_OPT_FAILFAST UINT16_C(0x1)
|
||||||
|
|
||||||
struct _hash_set;
|
struct _hash_set;
|
||||||
typedef struct _hash_set hash_set_t;
|
typedef struct _hash_set hash_set_t;
|
||||||
|
|
||||||
hash_set_t *hash_set_create(const double load_factor, const int fail_fast, const size_t initial_capacity);
|
hash_set_t *hash_set_create(const size_t initial_capacity, const double load_factor, const uint16_t options);
|
||||||
void hash_set_destroy(hash_set_t *const instance);
|
void hash_set_destroy(hash_set_t *const instance);
|
||||||
|
|
||||||
errno_t hash_set_insert(hash_set_t *const instance, const uint64_t value);
|
errno_t hash_set_insert(hash_set_t *const instance, const uint64_t value);
|
||||||
|
@ -28,15 +28,13 @@ struct _hash_set_data
|
|||||||
struct _hash_set
|
struct _hash_set
|
||||||
{
|
{
|
||||||
double load_factor;
|
double load_factor;
|
||||||
int fail_fast;
|
uint16_t options;
|
||||||
size_t size, limit;
|
size_t size, limit;
|
||||||
struct _hash_set_data data;
|
struct _hash_set_data data;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define BOUND(MIN,VAL,MAX) (((VAL) < (MIN)) ? (MIN) : (((VAL) > (MAX)) ? (MAX) : (VAL)))
|
#define BOUND(MIN,VAL,MAX) (((VAL) < (MIN)) ? (MIN) : (((VAL) > (MAX)) ? (MAX) : (VAL)))
|
||||||
|
|
||||||
#define BOOLIFY(X) (!!(X))
|
|
||||||
|
|
||||||
/* ========================================================================= */
|
/* ========================================================================= */
|
||||||
/* PRIVATE FUNCTIONS */
|
/* PRIVATE FUNCTIONS */
|
||||||
/* ========================================================================= */
|
/* ========================================================================= */
|
||||||
@ -193,7 +191,7 @@ static INLINE errno_t grow_set(hash_set_t *const instance, const size_t new_capa
|
|||||||
/* PUBLIC FUNCTIONS */
|
/* PUBLIC FUNCTIONS */
|
||||||
/* ========================================================================= */
|
/* ========================================================================= */
|
||||||
|
|
||||||
hash_set_t *hash_set_create(const double load_factor, const int fail_fast, const size_t initial_capacity)
|
hash_set_t *hash_set_create(const size_t initial_capacity, const double load_factor, const uint16_t options)
|
||||||
{
|
{
|
||||||
hash_set_t *const instance = (hash_set_t*) calloc(1U, sizeof(hash_set_t));
|
hash_set_t *const instance = (hash_set_t*) calloc(1U, sizeof(hash_set_t));
|
||||||
if (!instance)
|
if (!instance)
|
||||||
@ -208,7 +206,7 @@ hash_set_t *hash_set_create(const double load_factor, const int fail_fast, const
|
|||||||
}
|
}
|
||||||
|
|
||||||
instance->load_factor = (load_factor > 0.0) ? BOUND(0.001953125, load_factor, 1.0) : 0.666;
|
instance->load_factor = (load_factor > 0.0) ? BOUND(0.001953125, load_factor, 1.0) : 0.666;
|
||||||
instance->fail_fast = BOOLIFY(fail_fast);
|
instance->options = options;
|
||||||
instance->limit = round(instance->data.capacity * instance->load_factor);
|
instance->limit = round(instance->data.capacity * instance->load_factor);
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
@ -242,7 +240,7 @@ errno_t hash_set_insert(hash_set_t *const instance, const uint64_t value)
|
|||||||
{
|
{
|
||||||
if (instance->data.capacity == SIZE_MAX)
|
if (instance->data.capacity == SIZE_MAX)
|
||||||
{
|
{
|
||||||
if (instance->fail_fast || (instance->size >= instance->data.capacity))
|
if ((instance->options & HASHSET_OPT_FAILFAST) || (instance->size >= instance->data.capacity))
|
||||||
{
|
{
|
||||||
return ENOMEM; /*malloc has failed!*/
|
return ENOMEM; /*malloc has failed!*/
|
||||||
}
|
}
|
||||||
@ -255,7 +253,7 @@ errno_t hash_set_insert(hash_set_t *const instance, const uint64_t value)
|
|||||||
instance->limit = instance->data.capacity;
|
instance->limit = instance->data.capacity;
|
||||||
if (error == ENOMEM)
|
if (error == ENOMEM)
|
||||||
{
|
{
|
||||||
if (instance->fail_fast || (instance->size >= instance->data.capacity))
|
if ((instance->options & HASHSET_OPT_FAILFAST) || (instance->size >= instance->data.capacity))
|
||||||
{
|
{
|
||||||
return ENOMEM; /*malloc has failed!*/
|
return ENOMEM; /*malloc has failed!*/
|
||||||
}
|
}
|
||||||
|
@ -109,6 +109,8 @@
|
|||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<LargeAddressAware>true</LargeAddressAware>
|
||||||
|
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
@ -137,6 +139,7 @@
|
|||||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||||
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
|
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
|
||||||
|
<LargeAddressAware>true</LargeAddressAware>
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||||
@ -150,6 +153,7 @@
|
|||||||
<Link>
|
<Link>
|
||||||
<SubSystem>Console</SubSystem>
|
<SubSystem>Console</SubSystem>
|
||||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||||
|
<MinimumRequiredVersion>5.2</MinimumRequiredVersion>
|
||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||||
|
@ -27,7 +27,7 @@ int main()
|
|||||||
clock_t last_update = clock();
|
clock_t last_update = clock();
|
||||||
uint8_t spinner = 0U;
|
uint8_t spinner = 0U;
|
||||||
|
|
||||||
hash_set_t *const hash_set = hash_set_create(-1.0, 0, 0U);
|
hash_set_t *const hash_set = hash_set_create(0U, -1.0, HASHSET_OPT_FAILFAST);
|
||||||
if (!hash_set)
|
if (!hash_set)
|
||||||
{
|
{
|
||||||
puts("Allocation has failed!");
|
puts("Allocation has failed!");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user