Some refactoring.
This commit is contained in:
parent
083da3e14e
commit
8d5540d573
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,4 @@
|
||||
/.vs
|
||||
/bin
|
||||
/obj
|
||||
/**/obj
|
||||
/*.user
|
||||
|
@ -3,7 +3,9 @@ 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}"
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "main", "main\main.vcxproj", "{0B7ABB95-B60F-418B-8386-930B1629058F}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libhashset", "libhashset\libhashset.vcxproj", "{8CF3BD19-28B1-435D-B719-E00B052DFC3A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -21,6 +23,14 @@ Global
|
||||
{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
|
||||
{8CF3BD19-28B1-435D-B719-E00B052DFC3A}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{8CF3BD19-28B1-435D-B719-E00B052DFC3A}.Debug|x64.Build.0 = Debug|x64
|
||||
{8CF3BD19-28B1-435D-B719-E00B052DFC3A}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{8CF3BD19-28B1-435D-B719-E00B052DFC3A}.Debug|x86.Build.0 = Debug|Win32
|
||||
{8CF3BD19-28B1-435D-B719-E00B052DFC3A}.Release|x64.ActiveCfg = Release|x64
|
||||
{8CF3BD19-28B1-435D-B719-E00B052DFC3A}.Release|x64.Build.0 = Release|x64
|
||||
{8CF3BD19-28B1-435D-B719-E00B052DFC3A}.Release|x86.ActiveCfg = Release|Win32
|
||||
{8CF3BD19-28B1-435D-B719-E00B052DFC3A}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
@ -19,10 +19,10 @@ 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);
|
||||
errno_t hash_set_insert(hash_set_t *const instance, const uint64_t value);
|
||||
errno_t hash_set_contains(hash_set_t *const instance, const uint64_t value);
|
||||
|
||||
size_t hash_set_current_size(hash_set_t *const instance);
|
||||
size_t hash_set_size(hash_set_t *const instance);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
193
libhashset/libhashset.vcxproj
Normal file
193
libhashset/libhashset.vcxproj
Normal file
@ -0,0 +1,193 @@
|
||||
<?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>
|
||||
<ClInclude Include="include\hash_set.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\hash_set.c" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{8cf3bd19-28b1-435d-b719-e00b052dfc3a}</ProjectGuid>
|
||||
<RootNamespace>libhashset</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.19041.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v142</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</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>$(ProjectDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(ProjectDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(ProjectDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(ProjectDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>
|
||||
</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<OmitFramePointers>true</OmitFramePointers>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<BufferSecurityCheck>false</BufferSecurityCheck>
|
||||
<ControlFlowGuard>false</ControlFlowGuard>
|
||||
<EnableEnhancedInstructionSet>NoExtensions</EnableEnhancedInstructionSet>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>
|
||||
</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
<Lib>
|
||||
<LinkTimeCodeGeneration>true</LinkTimeCodeGeneration>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>
|
||||
</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<OmitFramePointers>true</OmitFramePointers>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<BufferSecurityCheck>false</BufferSecurityCheck>
|
||||
<ControlFlowGuard>false</ControlFlowGuard>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>
|
||||
</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
<Lib>
|
||||
<LinkTimeCodeGeneration>true</LinkTimeCodeGeneration>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
@ -15,16 +15,13 @@
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\main.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClInclude Include="include\hash_set.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<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>
|
4
libhashset/libhashset.vcxproj.user
Normal file
4
libhashset/libhashset.vcxproj.user
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup />
|
||||
</Project>
|
@ -7,6 +7,8 @@
|
||||
|
||||
/* CRT */
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#if defined(__GNUC__)
|
||||
# define INLINE __inline__
|
||||
@ -39,6 +41,11 @@ struct _hash_set
|
||||
/* PRIVATE FUNCTIONS */
|
||||
/* ========================================================================= */
|
||||
|
||||
static INLINE size_t hash(const uint64_t value, const size_t capacity)
|
||||
{
|
||||
return (size_t) (((UINT64_C(14695981039346656037) + value) * UINT64_C(1099511628211)) % capacity);
|
||||
}
|
||||
|
||||
static inline size_t safe_mult2(const size_t value)
|
||||
{
|
||||
return (value < (SIZE_MAX >> 1)) ? (value << 1) : SIZE_MAX;
|
||||
@ -49,47 +56,26 @@ 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));
|
||||
}
|
||||
|
||||
#define HASH_STEP() do \
|
||||
{ \
|
||||
h = (h ^ (value & 0xFF)) * MAGIC_PRIME; \
|
||||
value >>= CHAR_BIT; \
|
||||
} \
|
||||
while(0)
|
||||
|
||||
static INLINE size_t hash(uint64_t value, const size_t capacity)
|
||||
{
|
||||
#if SIZE_MAX > UINT32_MAX
|
||||
uint64_t h = UINT64_C(14695981039346656037);
|
||||
const uint64_t MAGIC_PRIME = UINT64_C(1099511628211);
|
||||
#else
|
||||
uint32_t h = UINT32_C(2166136261);
|
||||
const uint32_t MAGIC_PRIME = UINT32_C(16777619);
|
||||
#endif
|
||||
|
||||
HASH_STEP(); HASH_STEP(); HASH_STEP(); HASH_STEP();
|
||||
HASH_STEP(); HASH_STEP(); HASH_STEP(); HASH_STEP();
|
||||
|
||||
return (size_t)(h % capacity);
|
||||
}
|
||||
|
||||
static INLINE size_t next_pow2(const size_t minimum)
|
||||
{
|
||||
size_t result = 2U;
|
||||
|
||||
while (result < minimum)
|
||||
{
|
||||
result = safe_mult2(result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static INLINE int alloc_data(struct _hash_set_data *const data, const size_t capacity)
|
||||
static INLINE bool 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;
|
||||
return false;
|
||||
}
|
||||
|
||||
data->used = (uint8_t*) calloc((capacity + 7U) / 8U, sizeof(uint8_t));
|
||||
@ -97,11 +83,11 @@ static INLINE int alloc_data(struct _hash_set_data *const data, const size_t cap
|
||||
{
|
||||
free(data->values);
|
||||
data->values = NULL;
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
data->capacity = capacity;
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
static INLINE void free_data(struct _hash_set_data *const data)
|
||||
@ -122,12 +108,12 @@ static INLINE void free_data(struct _hash_set_data *const data)
|
||||
}
|
||||
}
|
||||
|
||||
static INLINE int is_used(struct _hash_set_data *const data, const size_t index)
|
||||
static INLINE bool 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)
|
||||
static INLINE bool find_value(struct _hash_set_data* const data, const uint64_t value, size_t *const index)
|
||||
{
|
||||
*index = hash(value, data->capacity);
|
||||
|
||||
@ -135,7 +121,7 @@ static INLINE int find_value(struct _hash_set_data* const data, const uint64_t v
|
||||
{
|
||||
if (data->values[*index] == value)
|
||||
{
|
||||
return 1;
|
||||
return true;
|
||||
}
|
||||
if (++*index >= data->capacity)
|
||||
{
|
||||
@ -143,42 +129,46 @@ static INLINE int find_value(struct _hash_set_data* const data, const uint64_t v
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
static INLINE int insert_value(struct _hash_set_data *const data, const size_t index, const uint64_t value)
|
||||
static INLINE bool insert_value(struct _hash_set_data *const data, const size_t index, const uint64_t value)
|
||||
{
|
||||
if (is_used(data, index))
|
||||
{
|
||||
return 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
data->values[index] = value;
|
||||
data->used[index / 8U] |= UINT8_C(1) << (index % 8U);
|
||||
return 1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static INLINE int grow_set(hash_set_t *const instance, const size_t new_capacity)
|
||||
static INLINE errno_t grow_set(hash_set_t *const instance, const size_t new_capacity)
|
||||
{
|
||||
struct _hash_set_data temp;
|
||||
size_t index, k;
|
||||
|
||||
if (!alloc_data(&temp, new_capacity))
|
||||
{
|
||||
return 0;
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < instance->data.capacity; ++i)
|
||||
for (k = 0U; k < instance->data.capacity; ++k)
|
||||
{
|
||||
if (is_used(&instance->data, i))
|
||||
if (is_used(&instance->data, k))
|
||||
{
|
||||
const uint64_t value = instance->data.values[i];
|
||||
size_t index;
|
||||
const uint64_t value = instance->data.values[k];
|
||||
if (find_value(&temp, value, &index))
|
||||
{
|
||||
abort(); /*whoops!*/
|
||||
free_data(&temp);
|
||||
return EFAULT;
|
||||
}
|
||||
if (!insert_value(&temp, index, value))
|
||||
{
|
||||
abort(); /*whoops!*/
|
||||
free_data(&temp);
|
||||
return EFAULT;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -186,7 +176,8 @@ static INLINE int grow_set(hash_set_t *const instance, const size_t new_capacity
|
||||
free_data(&instance->data);
|
||||
instance->data = temp;
|
||||
instance->limit = round(instance->data.capacity * instance->load_factor);
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ========================================================================= */
|
||||
@ -210,6 +201,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.75;
|
||||
instance->fail_fast = BOOLIFY(fail_fast);
|
||||
instance->limit = round(instance->data.capacity * instance->load_factor);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
@ -223,17 +215,18 @@ void hash_set_destroy(hash_set_t *const instance)
|
||||
}
|
||||
}
|
||||
|
||||
int 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)
|
||||
{
|
||||
size_t index;
|
||||
|
||||
if ((!instance) || (!instance->data.values))
|
||||
{
|
||||
return -1;
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
size_t index;
|
||||
if (find_value(&instance->data, value, &index))
|
||||
{
|
||||
return 0;
|
||||
return EEXIST;
|
||||
}
|
||||
|
||||
if ((instance->size >= instance->limit) || (instance->size >= instance->data.capacity))
|
||||
@ -242,50 +235,59 @@ int hash_set_insert(hash_set_t *const instance, const uint64_t value)
|
||||
{
|
||||
if (instance->fail_fast || (instance->size >= instance->data.capacity))
|
||||
{
|
||||
return -1; /*malloc has failed!*/
|
||||
return ENOMEM; /*malloc has failed!*/
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (grow_set(instance, safe_mult2(instance->data.capacity)))
|
||||
const errno_t error = grow_set(instance, safe_mult2(instance->data.capacity));
|
||||
if (error)
|
||||
{
|
||||
if (find_value(&instance->data, value, &index))
|
||||
if (error == ENOMEM)
|
||||
{
|
||||
abort(); /*whoops!*/
|
||||
if (instance->fail_fast || (instance->size >= instance->data.capacity))
|
||||
{
|
||||
return ENOMEM; /*malloc has failed!*/
|
||||
}
|
||||
instance->limit = instance->data.capacity;
|
||||
}
|
||||
else
|
||||
{
|
||||
return error;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (instance->fail_fast || (instance->size >= instance->data.capacity))
|
||||
if (find_value(&instance->data, value, &index))
|
||||
{
|
||||
return -1; /*malloc has failed!*/
|
||||
return EFAULT;
|
||||
}
|
||||
instance->limit = instance->data.capacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!insert_value(&instance->data, index, value))
|
||||
{
|
||||
abort(); /*whoops!*/
|
||||
return EFAULT;
|
||||
}
|
||||
|
||||
++instance->size;
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hash_set_contains(hash_set_t *const instance, const uint64_t value)
|
||||
errno_t hash_set_contains(hash_set_t *const instance, const uint64_t value)
|
||||
{
|
||||
size_t index;
|
||||
|
||||
if ((!instance) || (!instance->data.values))
|
||||
{
|
||||
return -1;
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
size_t index;
|
||||
return find_value(&instance->data, value, &index);
|
||||
return find_value(&instance->data, value, &index) ? 0 : ENOENT;
|
||||
}
|
||||
|
||||
size_t hash_set_current_size(hash_set_t *const instance)
|
||||
size_t hash_set_size(hash_set_t *const instance)
|
||||
{
|
||||
return instance ? instance->size : 0U;
|
||||
}
|
@ -20,17 +20,18 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\main.c" />
|
||||
<ClCompile Include="src\hash_set.c" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\hash_set.h" />
|
||||
<ProjectReference Include="..\libhashset\libhashset.vcxproj">
|
||||
<Project>{8cf3bd19-28b1-435d-b719-e00b052dfc3a}</Project>
|
||||
</ProjectReference>
|
||||
</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>
|
||||
<RootNamespace>main</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.19041.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
@ -80,29 +81,30 @@
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
|
||||
<IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
<IntDir>$(ProjectDir)\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>
|
||||
<IntDir>$(ProjectDir)\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>
|
||||
<IntDir>$(ProjectDir)\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>
|
||||
<IntDir>$(ProjectDir)\obj\$(Platform)\$(Configuration)\</IntDir>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)\libhashset\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@ -117,26 +119,33 @@
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<OmitFramePointers>true</OmitFramePointers>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<BufferSecurityCheck>false</BufferSecurityCheck>
|
||||
<EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
|
||||
<EnableEnhancedInstructionSet>NoExtensions</EnableEnhancedInstructionSet>
|
||||
<ControlFlowGuard>false</ControlFlowGuard>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)\libhashset\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)\libhashset\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
@ -151,17 +160,23 @@
|
||||
<SDLCheck>false</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion>
|
||||
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
|
||||
<OmitFramePointers>true</OmitFramePointers>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<BufferSecurityCheck>false</BufferSecurityCheck>
|
||||
<ControlFlowGuard>false</ControlFlowGuard>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<AdditionalIncludeDirectories>$(SolutionDir)\libhashset\include</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
|
||||
<MinimumRequiredVersion>5.2</MinimumRequiredVersion>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
22
main/main.vcxproj.filters
Normal file
22
main/main.vcxproj.filters
Normal file
@ -0,0 +1,22 @@
|
||||
<?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>
|
||||
</ItemGroup>
|
||||
</Project>
|
4
main/main.vcxproj.user
Normal file
4
main/main.vcxproj.user
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup />
|
||||
</Project>
|
@ -1,3 +1,8 @@
|
||||
/******************************************************************************/
|
||||
/* HashSet for C99, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
||||
/* This work has been released under the CC0 1.0 Universal license! */
|
||||
/******************************************************************************/
|
||||
|
||||
#define _CRT_RAND_S 1
|
||||
|
||||
#include <stdio.h>
|
||||
@ -29,10 +34,10 @@ int main()
|
||||
clock_t next_update = clock() + (2U * CLOCKS_PER_SEC);
|
||||
for (;;)
|
||||
{
|
||||
const int ret = hash_set_insert(set, next_rand() & 0x3FFFFFFFFFFFFFFllu);
|
||||
if (ret != 1)
|
||||
const errno_t error = hash_set_insert(set, next_rand() & 0x3FFFFFFFFFFFFFFllu);
|
||||
if (error)
|
||||
{
|
||||
printf("Error! (%d)\n", ret);
|
||||
printf("Insert has failed! (error: %d)\n", error);
|
||||
break;
|
||||
}
|
||||
if (!(++spinner & 0x7F))
|
||||
@ -40,7 +45,7 @@ int main()
|
||||
const clock_t now = clock();
|
||||
if (now >= next_update)
|
||||
{
|
||||
printf("%zu\n", hash_set_current_size(set));
|
||||
printf("%zu\n", hash_set_size(set));
|
||||
next_update = now + (2U * CLOCKS_PER_SEC);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user