Initial commit.
This commit is contained in:
commit
9f22ab7005
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/.vs
|
||||
/bin
|
||||
/obj
|
||||
/*.user
|
31
Set.sln
Normal file
31
Set.sln
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.33027.164
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Set", "Set.vcxproj", "{0B7ABB95-B60F-418B-8386-930B1629058F}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{0B7ABB95-B60F-418B-8386-930B1629058F}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{0B7ABB95-B60F-418B-8386-930B1629058F}.Debug|x64.Build.0 = Debug|x64
|
||||
{0B7ABB95-B60F-418B-8386-930B1629058F}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{0B7ABB95-B60F-418B-8386-930B1629058F}.Debug|x86.Build.0 = Debug|Win32
|
||||
{0B7ABB95-B60F-418B-8386-930B1629058F}.Release|x64.ActiveCfg = Release|x64
|
||||
{0B7ABB95-B60F-418B-8386-930B1629058F}.Release|x64.Build.0 = Release|x64
|
||||
{0B7ABB95-B60F-418B-8386-930B1629058F}.Release|x86.ActiveCfg = Release|Win32
|
||||
{0B7ABB95-B60F-418B-8386-930B1629058F}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {DC8E0EA3-7ABA-4BA8-B2E1-D9A43934BA40}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
170
Set.vcxproj
Normal file
170
Set.vcxproj
Normal file
@ -0,0 +1,170 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\main.c" />
|
||||
<ClCompile Include="src\hash_set.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\hash_set.h" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{0b7abb95-b60f-418b-8386-930b1629058f}</ProjectGuid>
|
||||
<RootNamespace>Set</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>
|
||||
<OmitFramePointers>true</OmitFramePointers>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<BufferSecurityCheck>false</BufferSecurityCheck>
|
||||
<EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
|
||||
<ControlFlowGuard>false</ControlFlowGuard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>
|
||||
<OmitFramePointers>true</OmitFramePointers>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<BufferSecurityCheck>false</BufferSecurityCheck>
|
||||
<ControlFlowGuard>false</ControlFlowGuard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
30
Set.vcxproj.filters
Normal file
30
Set.vcxproj.filters
Normal file
@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\main.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\hash_set.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\hash_set.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
298
src/hash_set.c
Normal file
298
src/hash_set.c
Normal file
@ -0,0 +1,298 @@
|
||||
/******************************************************************************/
|
||||
/* HashSet for C99, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
||||
/* This work has been released under the CC0 1.0 Universal license! */
|
||||
/******************************************************************************/
|
||||
|
||||
#include "hash_set.h"
|
||||
|
||||
/* CRT */
|
||||
#include <string.h>
|
||||
|
||||
#if defined(__GNUC__)
|
||||
# define INLINE __inline__
|
||||
#elif defined(_MSC_VER)
|
||||
# define INLINE __inline
|
||||
#else
|
||||
# define INLINE
|
||||
#endif
|
||||
|
||||
struct _hash_set_data
|
||||
{
|
||||
uint64_t *values;
|
||||
uint8_t *used;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
struct _hash_set
|
||||
{
|
||||
double load_factor;
|
||||
int fail_fast;
|
||||
struct _hash_set_data data;
|
||||
size_t size, limit;
|
||||
};
|
||||
|
||||
#define BOUND(MIN,VAL,MAX) (((VAL) < (MIN)) ? (MIN) : (((VAL) > (MAX)) ? (MAX) : (VAL)))
|
||||
|
||||
#define BOOLIFY(X) (!!(X))
|
||||
|
||||
/* ========================================================================= */
|
||||
/* PRIVATE FUNCTIONS */
|
||||
/* ========================================================================= */
|
||||
|
||||
static INLINE size_t hash(uint64_t value, const size_t capacity)
|
||||
{
|
||||
#if SIZE_MAX > UINT32_MAX
|
||||
uint64_t h = UINT64_C(14695981039346656037);
|
||||
for (size_t i = 0U; i < sizeof(uint64_t); ++i, value >>= CHAR_BIT)
|
||||
{
|
||||
h ^= value & 0xFF;
|
||||
h *= UINT64_C(1099511628211);
|
||||
}
|
||||
#else
|
||||
uint32_t h = UINT32_C(2166136261);
|
||||
for (size_t i = 0U; i < sizeof(uint64_t); ++i, value >>= CHAR_BIT)
|
||||
{
|
||||
h ^= value & 0xFF;
|
||||
h *= UINT32_C(16777619);
|
||||
}
|
||||
#endif
|
||||
return ((size_t) h) % capacity;
|
||||
}
|
||||
|
||||
static INLINE size_t round(double d)
|
||||
{
|
||||
return (d >= 0.0) ? ((size_t)(d + 0.5)) : ((size_t)(d - ((double)((size_t)(d - 1))) + 0.5)) + ((size_t)(d - 1));
|
||||
}
|
||||
|
||||
static INLINE size_t safe_mult(const size_t a, const size_t b)
|
||||
{
|
||||
if ((a == 0U) || (b == 0U))
|
||||
{
|
||||
return 0U;
|
||||
}
|
||||
|
||||
const size_t result = a * b;
|
||||
return(result / a != b) ? SIZE_MAX : result;
|
||||
}
|
||||
|
||||
static INLINE size_t next_pow2(const size_t minimum)
|
||||
{
|
||||
size_t result = 2U;
|
||||
while (result < minimum)
|
||||
{
|
||||
result = safe_mult(result, 2U);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static INLINE int alloc_data(struct _hash_set_data *const data, const size_t capacity)
|
||||
{
|
||||
memset(data, 0, sizeof(struct _hash_set_data));
|
||||
|
||||
data->values = (uint64_t*) calloc(capacity, sizeof(uint64_t));
|
||||
if (!data->values)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
data->used = (uint8_t*) calloc((capacity + 7U) / 8U, sizeof(uint8_t));
|
||||
if (!data->used)
|
||||
{
|
||||
free(data->values);
|
||||
data->values = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
data->capacity = capacity;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static INLINE void free_data(struct _hash_set_data *const data)
|
||||
{
|
||||
if (data)
|
||||
{
|
||||
if (data->values)
|
||||
{
|
||||
free(data->values);
|
||||
data->values = NULL;
|
||||
}
|
||||
|
||||
if (data->used)
|
||||
{
|
||||
free(data->used);
|
||||
data->used = NULL;
|
||||
}
|
||||
|
||||
data->capacity = 0U;
|
||||
}
|
||||
}
|
||||
|
||||
static INLINE int is_used(struct _hash_set_data *const data, const size_t index)
|
||||
{
|
||||
return (data->used[index / 8U] >> (index % 8U)) & 1U;
|
||||
}
|
||||
|
||||
static INLINE int find_value(struct _hash_set_data* const data, const uint64_t value, size_t *const index_out)
|
||||
{
|
||||
size_t index = hash(value, data->capacity);
|
||||
|
||||
while (is_used(data, index))
|
||||
{
|
||||
if (data->values[index] == value)
|
||||
{
|
||||
if (index_out)
|
||||
{
|
||||
*index_out = index;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (++index >= data->capacity)
|
||||
{
|
||||
index = 0U;
|
||||
}
|
||||
}
|
||||
|
||||
if (index_out)
|
||||
{
|
||||
*index_out = index;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static INLINE void insert_value(struct _hash_set_data *const data, const size_t index, const uint64_t value)
|
||||
{
|
||||
data->values[index] = value;
|
||||
data->used[index / 8U] |= 1U << (index % 8U);
|
||||
}
|
||||
|
||||
static INLINE void swap_data(struct _hash_set_data *const a, struct _hash_set_data *const b)
|
||||
{
|
||||
struct _hash_set_data temp = *a;
|
||||
*a = *b;
|
||||
*b = temp;
|
||||
}
|
||||
|
||||
static INLINE int grow_set(hash_set_t *const instance, const size_t new_capacity)
|
||||
{
|
||||
struct _hash_set_data temp;
|
||||
if (!alloc_data(&temp, new_capacity))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < instance->data.capacity; ++i)
|
||||
{
|
||||
if (is_used(&instance->data, i))
|
||||
{
|
||||
const uint64_t value = instance->data.values[i];
|
||||
size_t index;
|
||||
if (find_value(&temp, value, &index))
|
||||
{
|
||||
abort(); /*whoops!*/
|
||||
}
|
||||
insert_value(&temp, index, value);
|
||||
}
|
||||
}
|
||||
|
||||
swap_data(&instance->data, &temp);
|
||||
free_data(&temp);
|
||||
instance->limit = round(instance->data.capacity * instance->load_factor);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ========================================================================= */
|
||||
/* PUBLIC FUNCTIONS */
|
||||
/* ========================================================================= */
|
||||
|
||||
hash_set_t *hash_set_create(const double load_factor, const int fail_fast, const size_t initial_capacity)
|
||||
{
|
||||
hash_set_t *const instance = (hash_set_t*) calloc(1U, sizeof(hash_set_t));
|
||||
if (!instance)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!alloc_data(&instance->data, (initial_capacity > 0U) ? next_pow2(initial_capacity) : 1024U))
|
||||
{
|
||||
free(instance);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
instance->load_factor = (load_factor > 0.0) ? BOUND(0.001953125, load_factor, 1.0) : 0.75;
|
||||
instance->fail_fast = BOOLIFY(fail_fast);
|
||||
instance->limit = round(instance->data.capacity * instance->load_factor);
|
||||
return instance;
|
||||
}
|
||||
|
||||
void hash_set_destroy(hash_set_t *const instance)
|
||||
{
|
||||
if (instance)
|
||||
{
|
||||
free_data(&instance->data);
|
||||
instance->size = 0U;
|
||||
instance->limit = 0U;
|
||||
}
|
||||
}
|
||||
|
||||
int hash_set_insert(hash_set_t *const instance, const uint64_t value)
|
||||
{
|
||||
if ((!instance) || (!instance->data.values))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t index;
|
||||
if (find_value(&instance->data, value, &index))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((instance->size >= instance->limit) || (instance->size >= instance->data.capacity))
|
||||
{
|
||||
if (instance->data.capacity == SIZE_MAX)
|
||||
{
|
||||
if (instance->fail_fast || (instance->size >= instance->data.capacity))
|
||||
{
|
||||
return -1; /*malloc has failed!*/
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (grow_set(instance, safe_mult(instance->data.capacity, 2U)))
|
||||
{
|
||||
if (find_value(&instance->data, value, &index))
|
||||
{
|
||||
abort(); /*whoops!*/
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (instance->fail_fast || (instance->size >= instance->data.capacity))
|
||||
{
|
||||
return -1; /*malloc has failed!*/
|
||||
}
|
||||
instance->limit = instance->data.capacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
insert_value(&instance->data, index, value);
|
||||
++instance->size;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int hash_set_contains(hash_set_t *const instance, const uint64_t value)
|
||||
{
|
||||
if ((!instance) || (!instance->data.values))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return find_value(&instance->data, value, NULL);
|
||||
}
|
||||
|
||||
size_t hash_set_current_size(hash_set_t *const instance)
|
||||
{
|
||||
return instance ? instance->size : 0U;
|
||||
}
|
30
src/hash_set.h
Normal file
30
src/hash_set.h
Normal file
@ -0,0 +1,30 @@
|
||||
/******************************************************************************/
|
||||
/* HashSet for C99, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
||||
/* This work has been released under the CC0 1.0 Universal license! */
|
||||
/******************************************************************************/
|
||||
|
||||
#ifndef _INC_HASH_SET_H
|
||||
#define _INC_HASH_SET_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct _hash_set;
|
||||
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);
|
||||
void hash_set_destroy(hash_set_t *const instance);
|
||||
|
||||
int hash_set_insert(hash_set_t *const instance, const uint64_t value);
|
||||
int hash_set_contains(hash_set_t *const instance, const uint64_t value);
|
||||
|
||||
size_t hash_set_current_size(hash_set_t *const instance);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
46
src/main.c
Normal file
46
src/main.c
Normal file
@ -0,0 +1,46 @@
|
||||
#define _CRT_RAND_S 1
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "hash_set.h"
|
||||
|
||||
static uint64_t next_rand(void)
|
||||
{
|
||||
uint32_t a, b;
|
||||
if (rand_s(&a) || rand_s(&b))
|
||||
{
|
||||
abort();
|
||||
}
|
||||
return (((uint64_t)a) << 32) | b;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
hash_set_t *const set = hash_set_create(-1.0, 0, 0U);
|
||||
if (!set)
|
||||
{
|
||||
puts("Allocation has failed!");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
clock_t now, next_update = clock();
|
||||
for (;;)
|
||||
{
|
||||
const int ret = hash_set_insert(set, next_rand() & 0xFFFFFFFFFFFFFFllu);
|
||||
if (ret != 1)
|
||||
{
|
||||
printf("Error! (%d)\n", ret);
|
||||
break;
|
||||
}
|
||||
if ((now = clock()) >= next_update)
|
||||
{
|
||||
printf("%zu\n", hash_set_current_size(set));
|
||||
next_update = now + (3U * CLOCKS_PER_SEC);
|
||||
}
|
||||
}
|
||||
|
||||
hash_set_destroy(set);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user