From 3f197143f6c75ae0e0169981a6a491c0729f3e89 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Wed, 30 Nov 2022 15:41:29 +0100 Subject: [PATCH] Added example program for hash map. --- example/Makefile | 2 +- example/hash-map/Makefile | 52 +++ example/hash-map/hash-map-example.vcxproj | 442 ++++++++++++++++++ .../hash-map-example.vcxproj.filters} | 0 example/hash-map/src/input.c | 76 +++ example/hash-map/src/input.h | 22 + example/hash-map/src/main.c | 59 +++ example/hash-set/Makefile | 2 +- ...ample.vcxproj => hash-set-example.vcxproj} | 0 .../hash-set/hash-set-example.vcxproj.filters | 30 ++ example/hash-set/src/input.c | 2 +- example/hash-set/src/main.c | 8 +- hashset.sln | 23 +- libhashset/src/generic_hash_map.h | 7 +- 14 files changed, 715 insertions(+), 10 deletions(-) create mode 100644 example/hash-map/Makefile create mode 100644 example/hash-map/hash-map-example.vcxproj rename example/{hash-set/hashset-example.vcxproj.filters => hash-map/hash-map-example.vcxproj.filters} (100%) create mode 100644 example/hash-map/src/input.c create mode 100644 example/hash-map/src/input.h create mode 100644 example/hash-map/src/main.c rename example/hash-set/{hashset-example.vcxproj => hash-set-example.vcxproj} (100%) create mode 100644 example/hash-set/hash-set-example.vcxproj.filters diff --git a/example/Makefile b/example/Makefile index 4f44c6a..ad3309c 100644 --- a/example/Makefile +++ b/example/Makefile @@ -1,4 +1,4 @@ -SUBDIRS := hash-set +SUBDIRS := hash-set hash-map BUILD_ALL := $(patsubst %,build\:%,$(SUBDIRS)) CLEAN_ALL := $(patsubst %,clean\:%,$(SUBDIRS)) diff --git a/example/hash-map/Makefile b/example/hash-map/Makefile new file mode 100644 index 0000000..b76cec6 --- /dev/null +++ b/example/hash-map/Makefile @@ -0,0 +1,52 @@ +DUMPMACHINE := $(shell $(CC) -dumpmachine) + +ifneq ($(DEBUG),) + XCFLAGS = -Og -g +else +ifneq ($(ASAN),) + XCFLAGS = -O1 -g -fsanitize=address -fno-omit-frame-pointer -static-libasan +else + XCFLAGS = -Ofast -DNDEBUG +ifneq ($(firstword $(filter x86_64-%,$(DUMPMACHINE))),) + XCFLAGS += -march=x86-64 -mtune=nocona +else ifneq ($(firstword $(filter i686-%,$(DUMPMACHINE))),) + XCFLAGS += -march=pentiumpro -mtune=intel +endif +ifneq ($(FLTO),) + XCFLAGS += -flto +endif + XCFLAGS += -s -static +endif +endif + +ifneq ($(firstword $(filter %-mingw32 %-cygwin,$(DUMPMACHINE))),) + EXE_SUFFIX := .exe +ifneq ($(firstword $(filter i686-%,$(DUMPMACHINE))),) + XCFLAGS += -Wl,--large-address-aware +endif +endif + +CFLAGS = -std=c99 -D_DEFAULT_SOURCE -Wpedantic -I../../libhashset/include $(XCFLAGS) + +SRC_PATH := src +BIN_PATH := bin +ALL_PATH := $(SRC_PATH) $(BIN_PATH) + +BIN_FILE := $(BIN_PATH)/example-hash-map$(EXE_SUFFIX) +SRC_FILE := $(wildcard $(SRC_PATH)/*.c) +LIB_FILE := ../../libhashset/lib/libhashset-1.a + +.PHONY: all build clean + +all: clean build + +build: $(ALL_PATH) $(BIN_FILE) + +$(BIN_FILE): $(SRC_FILE) $(LIB_FILE) + $(CC) $(CFLAGS) -o $@ $^ + +$(ALL_PATH): + mkdir -p $@ + +clean: + rm -f $(BIN_FILE) diff --git a/example/hash-map/hash-map-example.vcxproj b/example/hash-map/hash-map-example.vcxproj new file mode 100644 index 0000000..7d536d1 --- /dev/null +++ b/example/hash-map/hash-map-example.vcxproj @@ -0,0 +1,442 @@ + + + + + Debug + ARM64 + + + Debug + Win32 + + + Shared + ARM64 + + + Shared + Win32 + + + Shared + x64 + + + Static + ARM64 + + + Static + Win32 + + + Debug + x64 + + + Static + x64 + + + + + + + + + + + + {8cf3bd19-28b1-435d-b719-e00b052dfc3a} + + + + 16.0 + Win32Proj + {C703A94D-2755-40AD-A8D4-C169E14DCF5F} + example-hash-map + 10.0.19041.0 + example-hash-map + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + false + v142 + true + Unicode + + + Application + false + v142 + true + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\bin\$(PlatformToolset)\$(Platform)\$(Configuration)\ + $(ProjectDir)\obj\$(PlatformToolset)\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\bin\$(PlatformToolset)\$(Platform)\$(Configuration)\ + $(ProjectDir)\obj\$(PlatformToolset)\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\bin\$(PlatformToolset)\$(Platform)\$(Configuration)\ + $(ProjectDir)\obj\$(PlatformToolset)\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)\bin\$(PlatformToolset)\$(Platform)\$(Configuration)\ + $(ProjectDir)\obj\$(PlatformToolset)\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)\bin\$(PlatformToolset)\$(Platform)\$(Configuration)\ + $(ProjectDir)\obj\$(PlatformToolset)\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\bin\$(PlatformToolset)\$(Platform)\$(Configuration)\ + $(ProjectDir)\obj\$(PlatformToolset)\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\bin\$(PlatformToolset)\$(Platform)\$(Configuration)\ + $(ProjectDir)\obj\$(PlatformToolset)\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\bin\$(PlatformToolset)\$(Platform)\$(Configuration)\ + $(ProjectDir)\obj\$(PlatformToolset)\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\bin\$(PlatformToolset)\$(Platform)\$(Configuration)\ + $(ProjectDir)\obj\$(PlatformToolset)\$(Platform)\$(Configuration)\ + + + + Level3 + false + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)\libhashset\include + + + Console + true + true + 5.1 + + + + + + Level4 + true + true + false + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + AnySuitable + true + MultiThreaded + false + StreamingSIMDExtensions + false + MaxSpeed + Speed + true + $(SolutionDir)\libhashset\include + Fast + + + Console + true + true + false + UseLinkTimeCodeGeneration + 5.1 + true + + + + + + Level4 + true + true + false + WIN32;NDEBUG;_CONSOLE;HASHSET_DLL;%(PreprocessorDefinitions) + true + AnySuitable + true + MultiThreadedDLL + false + StreamingSIMDExtensions + false + MaxSpeed + Speed + true + $(SolutionDir)\libhashset\include + Fast + + + Console + true + true + false + UseLinkTimeCodeGeneration + 5.1 + true + + + + copy /B /Y /N "$(SolutionDir)lib\$(PlatformToolset)\$(Platform)\$(Configuration)\libhashset-1.dll" "$(TargetDir)libhashset-1.dll" + + + cp "$(SolutionDir)lib\$(PlatformToolset)\$(Platform)\$(Configuration)\libhashset-1.dll" -> "$(TargetDir)libhashset-1.dll" + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)\libhashset\include + + + Console + true + 5.2 + + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)\libhashset\include + + + Console + true + + + + + + Level4 + true + true + false + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + AnySuitable + true + MultiThreaded + false + false + MaxSpeed + Speed + true + $(SolutionDir)\libhashset\include + Fast + + + Console + true + true + false + UseLinkTimeCodeGeneration + 5.2 + + + + + + Level4 + true + true + false + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + AnySuitable + true + MultiThreaded + false + false + MaxSpeed + Speed + true + $(SolutionDir)\libhashset\include + Fast + + + Console + true + true + false + UseLinkTimeCodeGeneration + + + + + + Level4 + true + true + false + NDEBUG;_CONSOLE;HASHSET_DLL;%(PreprocessorDefinitions) + true + AnySuitable + true + MultiThreadedDLL + false + false + MaxSpeed + Speed + true + $(SolutionDir)\libhashset\include + Fast + + + Console + true + true + false + UseLinkTimeCodeGeneration + 5.2 + + + + copy /B /Y /N "$(SolutionDir)lib\$(PlatformToolset)\$(Platform)\$(Configuration)\libhashset-1.dll" "$(TargetDir)libhashset-1.dll" + + + cp "$(SolutionDir)lib\$(PlatformToolset)\$(Platform)\$(Configuration)\libhashset-1.dll" -> "$(TargetDir)libhashset-1.dll" + + + + + Level4 + true + true + false + NDEBUG;_CONSOLE;HASHSET_DLL;%(PreprocessorDefinitions) + true + AnySuitable + true + MultiThreadedDLL + false + false + MaxSpeed + Speed + true + $(SolutionDir)\libhashset\include + Fast + + + Console + true + true + false + UseLinkTimeCodeGeneration + + + + copy /B /Y /N "$(SolutionDir)lib\$(PlatformToolset)\$(Platform)\$(Configuration)\libhashset-1.dll" "$(TargetDir)libhashset-1.dll" + + + cp "$(SolutionDir)lib\$(PlatformToolset)\$(Platform)\$(Configuration)\libhashset-1.dll" -> "$(TargetDir)libhashset-1.dll" + + + + + + \ No newline at end of file diff --git a/example/hash-set/hashset-example.vcxproj.filters b/example/hash-map/hash-map-example.vcxproj.filters similarity index 100% rename from example/hash-set/hashset-example.vcxproj.filters rename to example/hash-map/hash-map-example.vcxproj.filters diff --git a/example/hash-map/src/input.c b/example/hash-map/src/input.c new file mode 100644 index 0000000..3275855 --- /dev/null +++ b/example/hash-map/src/input.c @@ -0,0 +1,76 @@ +/******************************************************************************/ +/* HashSet for C99, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#include "input.h" + +#define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0U])) + +#define MINIMUM(X,Y) (((X) < (Y)) ? (X) : (Y)) + +/* ========================================================================= */ +/* I/O Functions */ +/* ========================================================================= */ + +static const uint64_t INPUT_KEYS[] = +{ + UINT64_C(0xEC7D0A721D57EC50), UINT64_C(0x2D6A7AB6BF627C22), UINT64_C(0x4FDD65C766A875B3), UINT64_C(0x820325C26E55AADE), UINT64_C(0xDAAA501D96EEF99B), + UINT64_C(0x8144E2BD50513672), UINT64_C(0x292CE22A29D5DB2B), UINT64_C(0xBC6CF8B2053A5334), UINT64_C(0x6D1401A1D373E7D1), UINT64_C(0x24ADEDD753FA66AE), + UINT64_C(0xC10245EBF7AA2022), UINT64_C(0x023529AB3C7CD028), UINT64_C(0x36D2B9AC4939E5B6), UINT64_C(0xC03EBAFFF82F66AA), UINT64_C(0xF192D661B62529F8), + UINT64_C(0xB704F3B5D339E013), UINT64_C(0xA9FF1DACC3E51172), UINT64_C(0x56B07D81D1075239), UINT64_C(0xB1871C613F7AABE2), UINT64_C(0xA8EF994549F8FE4A), + UINT64_C(0xA57BB923C5253144), UINT64_C(0x00D86833851697B3), UINT64_C(0xDA9E46FDD32DC65D), UINT64_C(0x7ACB9C0ADDDFAB93), UINT64_C(0xB5B5850E76C843DB), + UINT64_C(0x3B6A7C89C7E81A5D), UINT64_C(0x4EB1262597175FCD), UINT64_C(0xB6FBE2171D1D7EA0), UINT64_C(0x081137EA1D536FC4), UINT64_C(0xE0445CD0FAD5C7DD), + UINT64_C(0x893007682A3BF169), UINT64_C(0xD0C19098FBD42472), UINT64_C(0xEDB93F32F0A49B30), UINT64_C(0x2EA9A358058AB291), UINT64_C(0x8DAEDB071BA37583), + UINT64_C(0x7D82157789453793), UINT64_C(0xBB2773CA12083932), UINT64_C(0x6303F020DEFC11A9), UINT64_C(0xD41F0AD15E0189DC), UINT64_C(0x19B057F06E39C89E), + UINT64_C(0x4253F25E0B30719C), UINT64_C(0x09463D8E5BA23234), UINT64_C(0x34D04E31976724AA), UINT64_C(0x4671121B954F5A79), UINT64_C(0xE92C10C3DB5D8D1B), + UINT64_C(0xE9CBC62D55911AE7), UINT64_C(0x2A1AB1C994DB788E), UINT64_C(0xC8C33ECF2C582D77), UINT64_C(0x5FA82FE6C15B629E), UINT64_C(0x466CEED23C511177), + UINT64_C(0x036D0870BAE19AE1), UINT64_C(0x6606CC0204159016), UINT64_C(0xD6238C8747BC5852), UINT64_C(0x223E4185F09E378F), UINT64_C(0xA9B3F004F76E5444), + UINT64_C(0x23C9471F9E0284DD), UINT64_C(0x8B8FD1A11AA899F1), UINT64_C(0x0AB371E3FC9D3B56), UINT64_C(0xAC2E67112F3A040D), UINT64_C(0x192A053B04315830), + UINT64_C(0x432D3397538C1F92), UINT64_C(0xE959C8CB414D0179), UINT64_C(0x2CBAD874DD3E7ED3), UINT64_C(0x27C1B56C45A07443), UINT64_C(0x985DCE41B814DB32), + UINT64_C(0x7D89DA5C4F0A66B3), UINT64_C(0x8BDD31E4EBE03149), UINT64_C(0xDE099239E444C693), UINT64_C(0x1ADD95B03AF163A0), UINT64_C(0x36CD32A83DE833F2), + UINT64_C(0xDF4A8C3B1DC66B09), UINT64_C(0xC29C92C5A077B9FE), UINT64_C(0x56950ECB370CBC27), UINT64_C(0x7FEAD937FCA475F1), UINT64_C(0x1E4AD443AD9D41EC), + UINT64_C(0x9A265C879D7E5872), UINT64_C(0x455AB983F8BA1441), UINT64_C(0x027C2ED163D11C29), UINT64_C(0xA7A7D23CAA6F4DC6), UINT64_C(0xEE7939920BE1E7BC), + UINT64_C(0x2AE02B16E28F02F6), UINT64_C(0xFE8A0CB4EB33DA1B), UINT64_C(0x8CCBF16F006BE241), UINT64_C(0x3371EF26F2850DAF), UINT64_C(0xC35EA57F39EA9E58), + UINT64_C(0x20977AD263FB0272), UINT64_C(0x29BBDD2B8DFCD944), UINT64_C(0x3EF8BD07A2CA3369), UINT64_C(0x64FC3600F0B37716), UINT64_C(0x53CBD741D2433E51), + UINT64_C(0x69459841B5CEE1AB), UINT64_C(0x170B6EBB26B0102C), UINT64_C(0xAF7D361D649AED05), UINT64_C(0x8843B951E1A79CA4), UINT64_C(0xCC0B139063D3EF0A), + UINT64_C(0x85CB5DBD53986A64), UINT64_C(0x7C66F5CD3E46FDB7), UINT64_C(0xE863F4436E8BD7F8), UINT64_C(0x82496CD067153344), UINT64_C(0x863F1F732CE3751E) +}; + +static const uint64_t INPUT_VALUES[] = +{ + UINT64_C(0xE0359D7D27190470), UINT64_C(0x6A8B8F75240A606C), UINT64_C(0x1E0DB534262646E7), UINT64_C(0xFD79E3AA64A2774E), UINT64_C(0x010324B945C7DA9E), + UINT64_C(0x5248593526FDF636), UINT64_C(0x616C6381A68AD5D2), UINT64_C(0x567760F9D1A41240), UINT64_C(0x107AE9902BD79DF3), UINT64_C(0x180565321506BC12), + UINT64_C(0x93C190B7B4C690B7), UINT64_C(0x021CECCD4CC37F4E), UINT64_C(0xA730203BEBF9E4D5), UINT64_C(0x6843C5BFFF5F0029), UINT64_C(0xA049B07E88876973), + UINT64_C(0xAB835AAB4A21CF62), UINT64_C(0x4A167077B6B1B540), UINT64_C(0xF0513939D4BB09C7), UINT64_C(0x171E212B6BFE3CC6), UINT64_C(0x75391B09F3E8073F), + UINT64_C(0x9EA4B0561CF36216), UINT64_C(0xC2E2EAA1763DB240), UINT64_C(0xBE7329DB422CB870), UINT64_C(0x747A0119733A6678), UINT64_C(0x80D47F93E07AEA14), + UINT64_C(0x7C213D7D6C45692E), UINT64_C(0x78D344C281625298), UINT64_C(0xA3702E83B09D09EC), UINT64_C(0x8A2E4CBCB5854C9A), UINT64_C(0x7C5D14775EA29F19), + UINT64_C(0xF8E8367157E71126), UINT64_C(0x1A23EB25A1EB2F5E), UINT64_C(0x3950A676B29C5168), UINT64_C(0x0D841DE0570B4BCD), UINT64_C(0x9D2EFF1E00F0CAA1), + UINT64_C(0x805A7DB68B0BD1E9), UINT64_C(0x257122C35CCA2C4A), UINT64_C(0xA6014DB72C54C6B3), UINT64_C(0x56381789F58629F7), UINT64_C(0x5B73293145C3ECFD), + UINT64_C(0xE38B60A19F9F66DC), UINT64_C(0xCE2F1F9A4F0A2B9C), UINT64_C(0xD7BEB6E08E3D1CAE), UINT64_C(0x94CCEE57F5E987AD), UINT64_C(0x2AE0D325CA31CE67), + UINT64_C(0x296763D4011FE85E), UINT64_C(0x1758779DA1AD3EE9), UINT64_C(0x6D9E6C88EDE24333), UINT64_C(0xAA4D0B75B95D0B87), UINT64_C(0x6C44E1B8BB601769), + UINT64_C(0xF2BCEBD8E7CBE458), UINT64_C(0xEE8AEF528886EB2B), UINT64_C(0x8468C256C2C1ABE8), UINT64_C(0x15D71B47ADF3A068), UINT64_C(0xB3678DEC66A855B2), + UINT64_C(0x47724D7B0DD806E1), UINT64_C(0xE19BF73125B028FD), UINT64_C(0xF1597C7F2C2ED5FF), UINT64_C(0xB22D9DC7FAD6FE0E), UINT64_C(0x4508DB9A9BDB545B), + UINT64_C(0x6450AFC649347952), UINT64_C(0x58D19E1C39A76284), UINT64_C(0x5A3ED68E33B34DB6), UINT64_C(0x0514B87BA3BC46AD), UINT64_C(0xFFE9C5CC1BC8DC3B), + UINT64_C(0xD4342CA8BA80B88F), UINT64_C(0x1E943E872C760875), UINT64_C(0x74B67F3F010A4EE0), UINT64_C(0x5DC7F60598468608), UINT64_C(0x1A8D0AFC0EDBEEDE), + UINT64_C(0xD1C5038CB00E5797), UINT64_C(0x32F54E47C7CDDFF8), UINT64_C(0x9C3BFD89909638CF), UINT64_C(0xF7CEB89A9A5694A8), UINT64_C(0x0B08DBC73FCDCAA8), + UINT64_C(0x3B0A8A70937F2C83), UINT64_C(0xD9336A333909473D), UINT64_C(0x8981D0E8582A159E), UINT64_C(0x1EDB1D67114D0B60), UINT64_C(0xB34F04B1A91FE2C8), + UINT64_C(0xB165B7EAEA63981E), UINT64_C(0xB60C653D40FAEC9E), UINT64_C(0x6D49BC1AA59D15D3), UINT64_C(0x44829E6CD50292F8), UINT64_C(0xDFF648A8A584435D), + UINT64_C(0x3C285E7E252BB536), UINT64_C(0xE559AEF6AAF055CA), UINT64_C(0xDEA7ED70CF4CB9F0), UINT64_C(0xD99AAB38FC839CA2), UINT64_C(0x446AFF8DD57AE1F6), + UINT64_C(0xE41ECE48B14E57C8), UINT64_C(0x35ED1D10689E16F7), UINT64_C(0x94D200DEBFC9A34D), UINT64_C(0x2EBECCABA278C71F), UINT64_C(0x152AB2E31BE7B630), + UINT64_C(0xC0AA1A102A24DBD9), UINT64_C(0x7CCAAA448391C9B1), UINT64_C(0xC7109A86B8AA7A2C), UINT64_C(0x9C42009B6F56CE0A), UINT64_C(0xA3C3AE98F226B08D) +}; + +int have_more_items(const size_t offset) +{ + return offset < MINIMUM(ARRAY_SIZE(INPUT_KEYS), ARRAY_SIZE(INPUT_VALUES)); +} + +pair_t get_next_item(const size_t offset) +{ + if (offset < MINIMUM(ARRAY_SIZE(INPUT_KEYS), ARRAY_SIZE(INPUT_VALUES))) + { + return (pair_t) { INPUT_KEYS[offset], INPUT_VALUES[offset] }; + } + return (pair_t) { 0U, 0U }; +} diff --git a/example/hash-map/src/input.h b/example/hash-map/src/input.h new file mode 100644 index 0000000..8b6fb45 --- /dev/null +++ b/example/hash-map/src/input.h @@ -0,0 +1,22 @@ +/******************************************************************************/ +/* HashSet for C99, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifndef _EXAMPLE_INPUT_INCLUDED +#define _EXAMPLE_INPUT_INCLUDED + +#include +#include + +typedef struct +{ + uint64_t key; + uint64_t value; +} +pair_t; + +int have_more_items(const size_t offset); +pair_t get_next_item(const size_t offset); + +#endif /*_EXAMPLE_INPUT_INCLUDED*/ diff --git a/example/hash-map/src/main.c b/example/hash-map/src/main.c new file mode 100644 index 0000000..d7065ca --- /dev/null +++ b/example/hash-map/src/main.c @@ -0,0 +1,59 @@ +/******************************************************************************/ +/* HashSet for C99, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#include +#include +#include +#include "input.h" + +/* ========================================================================= */ +/* MAIN */ +/* ========================================================================= */ + +int main(void) +{ + hash_map64_t *hash_map; + uint64_t key, value; + size_t cursor = 0U, offset = 0U; + + /* print logo */ + printf("LibHashSet Hash-Map Example v%" PRIu16 ".%" PRIu16 ".%" PRIu16 " [%s]\n\n", + HASHSET_VERSION_MAJOR, HASHSET_VERSION_MINOR, HASHSET_VERSION_PATCH, HASHSET_BUILD_DATE); + + /* create new hash map instance */ + hash_map = hash_map_create64(0U, -1.0); + if (!hash_map) + { + fputs("Allocation has failed!\n", stderr); + return EXIT_FAILURE; + } + + /* add a number of items to the hash map, the map will grow as needed */ + puts("Inserting key-value pairs into hash map, please wait..."); + while (have_more_items(offset)) + { + const pair_t input = get_next_item(offset++); + const errno_t error = hash_map_insert64(hash_map, input.key, input.value); + if (error) + { + fprintf(stderr, "Insert operation failed! (error: %d)\n", error); + return EXIT_FAILURE; + } + } + puts("Done.\n"); + + /* print total number of items in the hash map */ + printf("Total number of entries in the map: %zu\n\n", hash_map_size64(hash_map)); + + /* print all items in the map */ + while (hash_map_iterate64(hash_map, &cursor, &key, &value) == 0) + { + printf("Entry: 0x%016" PRIX64 " -> 0x%016" PRIX64 "\n", key, value); + } + + /* destroy the hash map, when it is no longer needed! */ + hash_map_destroy64(hash_map); + return EXIT_SUCCESS; +} diff --git a/example/hash-set/Makefile b/example/hash-set/Makefile index 4cd675c..3455e07 100644 --- a/example/hash-set/Makefile +++ b/example/hash-set/Makefile @@ -32,7 +32,7 @@ SRC_PATH := src BIN_PATH := bin ALL_PATH := $(SRC_PATH) $(BIN_PATH) -BIN_FILE := $(BIN_PATH)/hashset-example$(EXE_SUFFIX) +BIN_FILE := $(BIN_PATH)/example-hash-set$(EXE_SUFFIX) SRC_FILE := $(wildcard $(SRC_PATH)/*.c) LIB_FILE := ../../libhashset/lib/libhashset-1.a diff --git a/example/hash-set/hashset-example.vcxproj b/example/hash-set/hash-set-example.vcxproj similarity index 100% rename from example/hash-set/hashset-example.vcxproj rename to example/hash-set/hash-set-example.vcxproj diff --git a/example/hash-set/hash-set-example.vcxproj.filters b/example/hash-set/hash-set-example.vcxproj.filters new file mode 100644 index 0000000..274df3c --- /dev/null +++ b/example/hash-set/hash-set-example.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/example/hash-set/src/input.c b/example/hash-set/src/input.c index 8e38d14..7d35384 100644 --- a/example/hash-set/src/input.c +++ b/example/hash-set/src/input.c @@ -32,7 +32,7 @@ static const uint64_t INPUT_DATA[] = UINT64_C(0x2AE02B16E28F02F6), UINT64_C(0xFE8A0CB4EB33DA1B), UINT64_C(0x8CCBF16F006BE241), UINT64_C(0x3371EF26F2850DAF), UINT64_C(0xC35EA57F39EA9E58), UINT64_C(0x20977AD263FB0272), UINT64_C(0x29BBDD2B8DFCD944), UINT64_C(0x3EF8BD07A2CA3369), UINT64_C(0x64FC3600F0B37716), UINT64_C(0x53CBD741D2433E51), UINT64_C(0x69459841B5CEE1AB), UINT64_C(0x170B6EBB26B0102C), UINT64_C(0xAF7D361D649AED05), UINT64_C(0x8843B951E1A79CA4), UINT64_C(0xCC0B139063D3EF0A), - UINT64_C(0x85CB5DBD53986A64), UINT64_C(0x7C66F5CD3E46FDB7), UINT64_C(0xE863F4436E8BD7F8), UINT64_C(0x82496CD067153344), UINT64_C(0x863F1F732CE3751E), + UINT64_C(0x85CB5DBD53986A64), UINT64_C(0x7C66F5CD3E46FDB7), UINT64_C(0xE863F4436E8BD7F8), UINT64_C(0x82496CD067153344), UINT64_C(0x863F1F732CE3751E) }; int have_more_items(const size_t offset) diff --git a/example/hash-set/src/main.c b/example/hash-set/src/main.c index e0f6802..5885847 100644 --- a/example/hash-set/src/main.c +++ b/example/hash-set/src/main.c @@ -15,11 +15,11 @@ int main(void) { hash_set64_t *hash_set; - uint64_t value; + uint64_t item; size_t cursor = 0U, offset = 0U; /* print logo */ - printf("LibHashSet Example v%" PRIu16 ".%" PRIu16 ".%" PRIu16 " [%s]\n\n", + printf("LibHashSet Hash-Set Example v%" PRIu16 ".%" PRIu16 ".%" PRIu16 " [%s]\n\n", HASHSET_VERSION_MAJOR, HASHSET_VERSION_MINOR, HASHSET_VERSION_PATCH, HASHSET_BUILD_DATE); /* create new hash set instance */ @@ -47,9 +47,9 @@ int main(void) printf("Total number of items in the set: %zu\n\n", hash_set_size64(hash_set)); /* print all items in the set */ - while (hash_set_iterate64(hash_set, &cursor, &value) == 0) + while (hash_set_iterate64(hash_set, &cursor, &item) == 0) { - printf("Item: 0x%016" PRIX64 "\n", value); + printf("Item: 0x%016" PRIX64 "\n", item); } /* destroy the hash set, when it is no longer needed! */ diff --git a/hashset.sln b/hashset.sln index 78b29d6..74bba12 100644 --- a/hashset.sln +++ b/hashset.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.33027.164 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libhashset", "libhashset\libhashset.vcxproj", "{8CF3BD19-28B1-435D-B719-E00B052DFC3A}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-hash-set", "example\hash-set\hashset-example.vcxproj", "{8FB9B9DE-DC49-4224-892B-589422484766}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-hash-set", "example\hash-set\hash-set-example.vcxproj", "{8FB9B9DE-DC49-4224-892B-589422484766}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{1EFCA710-2528-41D7-B757-F4615301DCA2}" EndProject @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{42437750 EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test-hash-set", "test\hash-set\hashset-test.vcxproj", "{0B7ABB95-B60F-418B-8386-930B1629058F}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example-hash-map", "example\hash-map\hash-map-example.vcxproj", "{C703A94D-2755-40AD-A8D4-C169E14DCF5F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ARM64 = Debug|ARM64 @@ -80,6 +82,24 @@ Global {0B7ABB95-B60F-418B-8386-930B1629058F}.Static|x64.Build.0 = Static|x64 {0B7ABB95-B60F-418B-8386-930B1629058F}.Static|x86.ActiveCfg = Static|Win32 {0B7ABB95-B60F-418B-8386-930B1629058F}.Static|x86.Build.0 = Static|Win32 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Debug|ARM64.Build.0 = Debug|ARM64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Debug|x64.ActiveCfg = Debug|x64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Debug|x64.Build.0 = Debug|x64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Debug|x86.ActiveCfg = Debug|Win32 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Debug|x86.Build.0 = Debug|Win32 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Shared|ARM64.ActiveCfg = Shared|ARM64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Shared|ARM64.Build.0 = Shared|ARM64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Shared|x64.ActiveCfg = Shared|x64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Shared|x64.Build.0 = Shared|x64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Shared|x86.ActiveCfg = Shared|Win32 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Shared|x86.Build.0 = Shared|Win32 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Static|ARM64.ActiveCfg = Static|ARM64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Static|ARM64.Build.0 = Static|ARM64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Static|x64.ActiveCfg = Static|x64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Static|x64.Build.0 = Static|x64 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Static|x86.ActiveCfg = Static|Win32 + {C703A94D-2755-40AD-A8D4-C169E14DCF5F}.Static|x86.Build.0 = Static|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -87,6 +107,7 @@ Global GlobalSection(NestedProjects) = preSolution {8FB9B9DE-DC49-4224-892B-589422484766} = {1EFCA710-2528-41D7-B757-F4615301DCA2} {0B7ABB95-B60F-418B-8386-930B1629058F} = {42437750-05E3-4DB6-AADA-FB44E73729B0} + {C703A94D-2755-40AD-A8D4-C169E14DCF5F} = {1EFCA710-2528-41D7-B757-F4615301DCA2} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {DC8E0EA3-7ABA-4BA8-B2E1-D9A43934BA40} diff --git a/libhashset/src/generic_hash_map.h b/libhashset/src/generic_hash_map.h index a9087e8..5d8342b 100644 --- a/libhashset/src/generic_hash_map.h +++ b/libhashset/src/generic_hash_map.h @@ -42,7 +42,7 @@ static INLINE bool_t alloc_data(hash_data_t *const data, const size_t capacity) zero_memory(data, 1U, sizeof(hash_data_t)); data->keys = (value_t*) calloc(capacity, sizeof(value_t)); - if (!data->values) + if (!data->keys) { return FALSE; } @@ -50,12 +50,14 @@ static INLINE bool_t alloc_data(hash_data_t *const data, const size_t capacity) data->values = (value_t*) calloc(capacity, sizeof(value_t)); if (!data->values) { + SAFE_FREE(data->keys); return FALSE; } data->used = (uint8_t*) calloc(div_ceil(capacity, 8U), sizeof(uint8_t)); if (!data->used) { + SAFE_FREE(data->keys); SAFE_FREE(data->values); return FALSE; } @@ -63,8 +65,9 @@ static INLINE bool_t alloc_data(hash_data_t *const data, const size_t capacity) data->deleted = (uint8_t*) calloc(div_ceil(capacity, 8U), sizeof(uint8_t)); if (!data->deleted) { - SAFE_FREE(data->used); + SAFE_FREE(data->keys); SAFE_FREE(data->values); + SAFE_FREE(data->used); return FALSE; }