├── .gitignore ├── .gitattributes ├── tests ├── test_release_null.c ├── test_clear_invalid_input.c ├── test_remove_invalid_input.c ├── test_get_invalid_input.c ├── test_insert_invalid_input.c ├── test.h ├── test_release_with_data.c ├── test_insert_remove.c ├── test_clear_with_data.c ├── test_insert_get.c ├── test.c ├── test_insert_with_hash_collision.c ├── test_insert_over_chain_max_size.c ├── test_keys.c └── test_entries.c ├── .editorconfig ├── post-example-build.cmake ├── CHANGELOG.md ├── .github ├── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── CONTRIBUTING.md └── workflows │ ├── ci.yml │ └── codeql-analysis.yml ├── examples └── example.c ├── include └── hashtable.h ├── CMakeLists.txt ├── README.md ├── uncrustify.cfg ├── LICENSE └── src └── hashtable.c /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | temp/ 3 | **/*.log 4 | /core 5 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text eol=lf 2 | *.ico binary 3 | *.woff binary 4 | -------------------------------------------------------------------------------- /tests/test_release_null.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | void test_impl() 5 | { 6 | hashtable_release(NULL); 7 | } 8 | 9 | 10 | int main() 11 | { 12 | test_run(test_impl); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /tests/test_clear_invalid_input.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | void test_impl() 5 | { 6 | hashtable_clear(NULL); 7 | } 8 | 9 | 10 | int main() 11 | { 12 | test_run(test_impl); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | insert_final_newline = true 8 | trim_trailing_whitespace = true 9 | indent_style = space 10 | indent_size = 2 11 | 12 | -------------------------------------------------------------------------------- /post-example-build.cmake: -------------------------------------------------------------------------------- 1 | 2 | include("./cmake-modules/src/utils.cmake") 3 | 4 | utils_embed_example_source_in_readme( 5 | EXAMPLE_FILE "../examples/example.c" 6 | DOCUMENT_FILE "../README.md" 7 | SOURCE_TYPE "c" 8 | ) 9 | 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## CHANGELOG 2 | 3 | ### v0.1.2 (2023-02-19) 4 | 5 | * Fix: bug in hashtable_remove can shift internal indexes 6 | 7 | ### v0.1.1 (2023-01-06) 8 | 9 | * New hashtable_keys function. 10 | * New hashtable_entries function. 11 | 12 | ### v0.1.0 (2023-01-04) 13 | 14 | * Initial Release 15 | -------------------------------------------------------------------------------- /tests/test_remove_invalid_input.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | void test_impl() 5 | { 6 | bool done = hashtable_remove(NULL, "key"); 7 | 8 | assert_true(!done); 9 | 10 | struct HashTable *table = hashtable_new(); 11 | 12 | done = hashtable_remove(table, NULL); 13 | assert_true(!done); 14 | 15 | hashtable_release(table); 16 | } 17 | 18 | 19 | int main() 20 | { 21 | test_run(test_impl); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /tests/test_get_invalid_input.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | void test_impl() 5 | { 6 | void *data = hashtable_get(NULL, "key"); 7 | 8 | assert_true(data == NULL); 9 | 10 | struct HashTable *table = hashtable_new(); 11 | 12 | data = hashtable_get(table, NULL); 13 | assert_true(data == NULL); 14 | 15 | hashtable_release(table); 16 | } 17 | 18 | 19 | int main() 20 | { 21 | test_run(test_impl); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature Request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: sagiegurari 7 | 8 | --- 9 | 10 | ### Feature Description 11 | 12 | 13 | ### Describe The Solution You'd Like 14 | 15 | 16 | ### Code Sample 17 | 18 | ```c 19 | // paste code here 20 | ``` 21 | -------------------------------------------------------------------------------- /tests/test_insert_invalid_input.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | void test_impl() 5 | { 6 | bool done = hashtable_insert(NULL, "key", "value", NULL); 7 | 8 | assert_true(!done); 9 | 10 | struct HashTable *table = hashtable_new(); 11 | 12 | done = hashtable_insert(table, NULL, "value", NULL); 13 | assert_true(!done); 14 | assert_true(hashtable_is_empty(table)); 15 | 16 | hashtable_release(table); 17 | } 18 | 19 | 20 | int main() 21 | { 22 | test_run(test_impl); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug Report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: sagiegurari 7 | 8 | --- 9 | 10 | ### Describe The Bug 11 | 12 | 13 | ### To Reproduce 14 | 15 | 16 | ### Error Stack 17 | 18 | ```console 19 | The error stack trace 20 | ``` 21 | 22 | ### Code Sample 23 | 24 | ```c 25 | // paste code here 26 | ``` 27 | -------------------------------------------------------------------------------- /tests/test.h: -------------------------------------------------------------------------------- 1 | #ifndef __TEST_H__ 2 | #define __TEST_H__ 3 | 4 | #include "hashtable.h" 5 | #include 6 | #include 7 | 8 | void test_run(void (*fn)()); 9 | void test_fail(); 10 | void assert_true(bool); 11 | 12 | void assert_num_equal(size_t, size_t); 13 | void assert_string_equal(char *, char *); 14 | 15 | void release_only_key(char *, void *); 16 | void release_only_value(char *, void *); 17 | void release_all(char *, void *); 18 | void release_none(char *, void *); 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /tests/test_release_with_data.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | void test_impl() 5 | { 6 | struct HashTable *table = hashtable_new(); 7 | 8 | hashtable_insert(table, "constkey", "constvalue", release_none); 9 | hashtable_insert(table, "constkey1", "constvalue1", NULL); 10 | hashtable_insert(table, "constkey2", strdup("value"), release_only_value); 11 | hashtable_insert(table, strdup("key1"), "constvalue", release_only_key); 12 | hashtable_insert(table, strdup("key2"), strdup("value"), release_all); 13 | 14 | hashtable_release(table); 15 | } 16 | 17 | 18 | int main() 19 | { 20 | test_run(test_impl); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guidelines 2 | 3 | ## Issues 4 | 5 | Found a bug? Got a question? Want some enhancement?
6 | First place to go is the repository issues section, and I'll try to help as much as possible. 7 | 8 | ## Pull Requests 9 | 10 | Fixed a bug or just want to provided additional functionality?
11 | Simply fork this repository, implement your changes and create a pull request.
12 | Few guidelines regarding pull requests: 13 | 14 | * This repository is integrated with github actions for continuous integration.
15 | 16 | Your pull request build must pass (the build will run automatically).
17 | You can run the following command locally to ensure the build will pass: 18 | 19 | ```sh 20 | mkdir ./target 21 | cd ./target 22 | cmake .. 23 | make 24 | ctest -C Release --output-on-failure 25 | ``` 26 | 27 | * There are many automatic unit tests as part of the library which provide full coverage of the functionality.
Any fix/enhancement must come with a set of tests to ensure it's working well. 28 | -------------------------------------------------------------------------------- /tests/test_insert_remove.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | void test_impl() 5 | { 6 | struct HashTable *table = hashtable_new(); 7 | 8 | bool done = false; 9 | char *value = NULL; 10 | 11 | assert_true(hashtable_is_empty(table)); 12 | assert_num_equal(hashtable_size(table), 0); 13 | done = hashtable_remove(table, "badkey"); 14 | assert_true(!done); 15 | assert_true(hashtable_is_empty(table)); 16 | assert_num_equal(hashtable_size(table), 0); 17 | 18 | done = hashtable_insert(table, "key", strdup("test1"), release_only_value); 19 | assert_true(done); 20 | assert_true(!hashtable_is_empty(table)); 21 | assert_num_equal(hashtable_size(table), 1); 22 | done = hashtable_remove(table, "key"); 23 | assert_true(done); 24 | assert_true(hashtable_is_empty(table)); 25 | assert_num_equal(hashtable_size(table), 0); 26 | value = hashtable_get(table, "key"); 27 | assert_true(value == NULL); 28 | 29 | hashtable_release(table); 30 | } 31 | 32 | 33 | int main() 34 | { 35 | test_run(test_impl); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push, pull_request] 3 | env: 4 | BUILD_TYPE: Release 5 | jobs: 6 | ci: 7 | name: CI 8 | runs-on: ${{ matrix.os }} 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | os: [ubuntu-latest, windows-latest, macOS-latest] 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@v2 16 | - name: Setup Env 17 | run: cmake -E make_directory target 18 | - name: Configure 19 | shell: bash 20 | working-directory: target 21 | run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE 22 | - name: Build 23 | shell: bash 24 | working-directory: target 25 | run: cmake --build . --config $BUILD_TYPE 26 | - name: Test 27 | shell: bash 28 | working-directory: target 29 | run: ctest -C $BUILD_TYPE --output-on-failure 30 | - name: Memory Leak Test 31 | if: matrix.os == 'ubuntu-latest' 32 | shell: bash 33 | working-directory: target 34 | run: | 35 | sudo apt update 36 | sudo apt install -y valgrind --fix-missing 37 | for testfile in ./bin/test_*; do echo "Testing ${testfile}" && valgrind --leak-check=full --show-leak-kinds=definite,indirect,possible --error-exitcode=1 "${testfile}"; done 38 | 39 | -------------------------------------------------------------------------------- /tests/test_clear_with_data.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | void test_impl() 5 | { 6 | struct HashTable *table = hashtable_new(); 7 | 8 | hashtable_insert(table, "constkey", "constvalue", release_none); 9 | hashtable_insert(table, "constkey1", "constvalue1", NULL); 10 | hashtable_insert(table, "constkey2", strdup("value"), release_only_value); 11 | hashtable_insert(table, strdup("key1"), "constvalue", release_only_key); 12 | hashtable_insert(table, strdup("key2"), strdup("value"), release_all); 13 | 14 | char *value = hashtable_get(table, "constkey"); 15 | assert_string_equal(value, "constvalue"); 16 | assert_true(!hashtable_is_empty(table)); 17 | assert_num_equal(hashtable_size(table), 5); 18 | 19 | hashtable_clear(table); 20 | 21 | assert_true(hashtable_is_empty(table)); 22 | assert_num_equal(hashtable_size(table), 0); 23 | value = hashtable_get(table, "constkey"); 24 | assert_true(value == NULL); 25 | 26 | // just make sure nothing happens if called over and over again 27 | hashtable_clear(table); 28 | hashtable_clear(table); 29 | hashtable_clear(table); 30 | hashtable_clear(table); 31 | 32 | assert_true(hashtable_is_empty(table)); 33 | assert_num_equal(hashtable_size(table), 0); 34 | 35 | hashtable_release(table); 36 | } 37 | 38 | 39 | int main() 40 | { 41 | test_run(test_impl); 42 | } 43 | 44 | -------------------------------------------------------------------------------- /tests/test_insert_get.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | void test_impl() 5 | { 6 | struct HashTable *table = hashtable_new(); 7 | 8 | bool done = false; 9 | char *value = NULL; 10 | 11 | assert_true(hashtable_is_empty(table)); 12 | assert_num_equal(hashtable_size(table), 0); 13 | done = hashtable_insert(table, "key", strdup("test1"), release_only_value); 14 | assert_true(done); 15 | assert_true(!hashtable_is_empty(table)); 16 | assert_num_equal(hashtable_size(table), 1); 17 | value = hashtable_get(table, "key"); 18 | assert_string_equal(value, "test1"); 19 | done = hashtable_insert(table, "key", "test2", release_none); 20 | assert_true(done); 21 | assert_true(!hashtable_is_empty(table)); 22 | assert_num_equal(hashtable_size(table), 1); 23 | value = hashtable_get(table, "key"); 24 | assert_string_equal(value, "test2"); 25 | done = hashtable_insert(table, strdup("key2"), strdup("value"), release_all); 26 | assert_true(done); 27 | assert_true(!hashtable_is_empty(table)); 28 | assert_num_equal(hashtable_size(table), 2); 29 | value = hashtable_get(table, "key"); 30 | assert_string_equal(value, "test2"); 31 | value = hashtable_get(table, "key2"); 32 | assert_string_equal(value, "value"); 33 | 34 | hashtable_release(table); 35 | } 36 | 37 | 38 | int main() 39 | { 40 | test_run(test_impl); 41 | } 42 | 43 | -------------------------------------------------------------------------------- /tests/test.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | #include 3 | #include 4 | 5 | static void _test_noop(void *); 6 | 7 | 8 | void test_run(void (*fn)(void)) 9 | { 10 | printf("Test ... "); 11 | fn(); 12 | printf("Done\n"); 13 | } 14 | 15 | 16 | void test_fail() 17 | { 18 | printf(" Error\n"); 19 | exit(1); 20 | } 21 | 22 | 23 | void assert_true(bool value) 24 | { 25 | if (!value) 26 | { 27 | test_fail(); 28 | } 29 | } 30 | 31 | 32 | void assert_num_equal(size_t value1, size_t value2) 33 | { 34 | if (value1 != value2) 35 | { 36 | #ifdef linux 37 | printf("Assert Failed, value: %zu not equals to value: %zu", value1, value2); 38 | #endif 39 | test_fail(); 40 | } 41 | } 42 | 43 | 44 | void assert_string_equal(char *value1, char *value2) 45 | { 46 | if (strcmp(value1, value2) != 0) 47 | { 48 | printf("assert failed, value: %s not equals to value: %s", value1, value2); 49 | test_fail(); 50 | } 51 | } 52 | 53 | 54 | void release_only_key(char *key, void *data) 55 | { 56 | free(key); 57 | _test_noop(data); 58 | } 59 | 60 | 61 | void release_only_value(char *key, void *data) 62 | { 63 | free(data); 64 | _test_noop(key); 65 | } 66 | 67 | 68 | void release_all(char *key, void *data) 69 | { 70 | free(key); 71 | free(data); 72 | } 73 | 74 | 75 | void release_none(char *key, void *data) 76 | { 77 | _test_noop(key); 78 | _test_noop(data); 79 | } 80 | 81 | 82 | static void _test_noop(void *data) 83 | { 84 | if (data == NULL) 85 | { 86 | return; 87 | } 88 | } 89 | 90 | -------------------------------------------------------------------------------- /tests/test_insert_with_hash_collision.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | static size_t _test_hash_function(char *key) 5 | { 6 | assert_true(key != NULL); 7 | return(10000); 8 | } 9 | 10 | 11 | void test_impl() 12 | { 13 | struct HashTableOptions options = { .table_size = 5, .chain_max_size = 0, .hash_function = _test_hash_function }; 14 | struct HashTable *table = hashtable_new_with_options(options); 15 | 16 | bool done = false; 17 | char *value = NULL; 18 | 19 | assert_true(hashtable_is_empty(table)); 20 | assert_num_equal(hashtable_size(table), 0); 21 | done = hashtable_insert(table, "key", strdup("test1"), release_only_value); 22 | assert_true(done); 23 | assert_true(!hashtable_is_empty(table)); 24 | assert_num_equal(hashtable_size(table), 1); 25 | value = hashtable_get(table, "key"); 26 | assert_string_equal(value, "test1"); 27 | done = hashtable_insert(table, "key", "test2", release_none); 28 | assert_true(done); 29 | assert_true(!hashtable_is_empty(table)); 30 | assert_num_equal(hashtable_size(table), 1); 31 | value = hashtable_get(table, "key"); 32 | assert_string_equal(value, "test2"); 33 | done = hashtable_insert(table, strdup("key2"), strdup("value"), release_all); 34 | assert_true(done); 35 | assert_true(!hashtable_is_empty(table)); 36 | assert_num_equal(hashtable_size(table), 2); 37 | value = hashtable_get(table, "key"); 38 | assert_string_equal(value, "test2"); 39 | value = hashtable_get(table, "key2"); 40 | assert_string_equal(value, "value"); 41 | 42 | hashtable_release(table); 43 | } 44 | 45 | 46 | int main() 47 | { 48 | test_run(test_impl); 49 | } 50 | 51 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | pull_request: 6 | schedule: 7 | - cron: '0 23 * * 5' 8 | 9 | jobs: 10 | analyse: 11 | name: Analyse 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Checkout repository 16 | uses: actions/checkout@v2 17 | with: 18 | # We must fetch at least the immediate parents so that if this is 19 | # a pull request then we can checkout the head. 20 | fetch-depth: 2 21 | 22 | # If this run was triggered by a pull request event, then checkout 23 | # the head of the pull request instead of the merge commit. 24 | - run: git checkout HEAD^2 25 | if: ${{ github.event_name == 'pull_request' }} 26 | 27 | # Initializes the CodeQL tools for scanning. 28 | - name: Initialize CodeQL 29 | uses: github/codeql-action/init@v1 30 | with: 31 | languages: cpp 32 | # Override language selection by uncommenting this and choosing your languages 33 | # with: 34 | # languages: go, javascript, csharp, python, cpp, java 35 | 36 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 37 | # If this step fails, then you should remove it and run the build manually (see below) 38 | - name: Autobuild 39 | uses: github/codeql-action/autobuild@v1 40 | 41 | # ℹ️ Command-line programs to run using the OS shell. 42 | # 📚 https://git.io/JvXDl 43 | 44 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 45 | # and modify them (or add more) to build your code if your project 46 | # uses a compiled language 47 | 48 | #- run: | 49 | # make bootstrap 50 | # make release 51 | 52 | - name: Perform CodeQL Analysis 53 | uses: github/codeql-action/analyze@v1 54 | -------------------------------------------------------------------------------- /examples/example.c: -------------------------------------------------------------------------------- 1 | #include "hashtable.h" 2 | #include 3 | #include 4 | #include 5 | 6 | static void release_key_and_value(char *key, void *value); 7 | 8 | 9 | int main() 10 | { 11 | bool done = false; 12 | size_t size = 0; 13 | char *value = NULL; 14 | 15 | // Create new hashtable with default implementation and size. 16 | // You can also provide your own hash implementation using the new with options function as follows: 17 | // struct HashTableOptions options = { .table_size = 5, .chain_max_size = 0, .hash_function = my_hash_function }; 18 | // struct HashTable *table = hashtable_new_with_options(options); 19 | struct HashTable *table = hashtable_new(); 20 | 21 | // add a new entry to the table. 22 | // The value is void* so you can put anything you need 23 | done = hashtable_insert(table, "key", "value", NULL /* release function */); 24 | value = hashtable_get(table, "key"); 25 | size = hashtable_size(table); 26 | printf("Pushed new entry: %d value is: '%s' current size: %zu\n", done, value, size); 27 | 28 | // To release the entry key/value you can provide a release function 29 | done = hashtable_insert(table, strdup("key2"), strdup("value2"), release_key_and_value); 30 | value = hashtable_get(table, "key2"); 31 | size = hashtable_size(table); 32 | printf("Pushed new entry: %d value is: '%s' current size: %zu\n", done, value, size); 33 | 34 | // remove the value (content will be freed via custom release function) 35 | hashtable_remove(table, "key2"); 36 | printf("Removed 1 entry, is empty: %d\n", hashtable_is_empty(table)); 37 | 38 | hashtable_clear(table); 39 | printf("Removed all entries, is empty: %d\n", hashtable_is_empty(table)); 40 | 41 | // once we are done, we need to release the table and its contents 42 | hashtable_release(table); 43 | 44 | return(0); 45 | } /* main */ 46 | 47 | 48 | static void release_key_and_value(char *key, void *value) 49 | { 50 | free(key); 51 | free(value); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /include/hashtable.h: -------------------------------------------------------------------------------- 1 | #ifndef HASHTABLE_H 2 | #define HASHTABLE_H 3 | 4 | #include 5 | #include 6 | 7 | #define HASHTABLE_DEFAULT_SIZE 1000 8 | 9 | struct HashTable; 10 | 11 | struct HashTableOptions 12 | { 13 | // 0 for default size 14 | size_t table_size; 15 | // 0 for unlimited size 16 | size_t chain_max_size; 17 | // NULL for default implementation 18 | size_t (*hash_function)(char *); 19 | }; 20 | 21 | struct HashTableEntries 22 | { 23 | char **keys; 24 | void **values; 25 | }; 26 | 27 | /** 28 | * Creates and returns a new hashtable with default size and implementation. 29 | * Once no longer needed, it must be released. 30 | */ 31 | struct HashTable *hashtable_new(void); 32 | 33 | /** 34 | * Creates and returns a new hashtable. 35 | * Once no longer needed, it must be released. 36 | */ 37 | struct HashTable *hashtable_new_with_options(struct HashTableOptions); 38 | 39 | /** 40 | * Frees the memory of the provided hashtable. 41 | */ 42 | void hashtable_release(struct HashTable *); 43 | 44 | /** 45 | * Removes and frees all entries. 46 | */ 47 | void hashtable_clear(struct HashTable *); 48 | 49 | /** 50 | * Returns the current entries count. 51 | */ 52 | size_t hashtable_size(struct HashTable *); 53 | 54 | /** 55 | * Returns true if the current hashtable is empty. 56 | */ 57 | bool hashtable_is_empty(struct HashTable *); 58 | 59 | /** 60 | * Inserts the provided value for the given key. 61 | * Once the value is removed or the table is released, it will call the optional release functions. 62 | * If the operation failed (invalid input, max size reached, ...), false will be returned. 63 | */ 64 | bool hashtable_insert(struct HashTable *, char * /* key */, void * /* value */, void (*release)(char *, void *)); 65 | 66 | /** 67 | * Returns the value based on the provided key from the hashtable. 68 | */ 69 | void *hashtable_get(struct HashTable *, char * /* key */); 70 | 71 | /** 72 | * Removes and frees the entry for the given key (if found). 73 | * If an entry was found, true will be returned. 74 | */ 75 | bool hashtable_remove(struct HashTable *, char * /* key */); 76 | 77 | /** 78 | * Returns all keys as an array. 79 | * The array (not values) must be released once done. 80 | */ 81 | char **hashtable_keys(struct HashTable *); 82 | 83 | /** 84 | * Returns all key/value pairs as arrays. 85 | * The arrays (not values) must be released once done. 86 | */ 87 | struct HashTableEntries hashtable_entries(struct HashTable *); 88 | 89 | #endif 90 | 91 | -------------------------------------------------------------------------------- /tests/test_insert_over_chain_max_size.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | static size_t _test_hash_function(char *key) 5 | { 6 | assert_true(key != NULL); 7 | return(10000); 8 | } 9 | 10 | 11 | void test_impl() 12 | { 13 | struct HashTableOptions options = { .table_size = 5, .chain_max_size = 3, .hash_function = _test_hash_function }; 14 | struct HashTable *table = hashtable_new_with_options(options); 15 | 16 | bool done = false; 17 | char *value = NULL; 18 | 19 | assert_true(hashtable_is_empty(table)); 20 | assert_num_equal(hashtable_size(table), 0); 21 | done = hashtable_insert(table, "key1", strdup("test1"), release_only_value); 22 | assert_true(done); 23 | assert_true(!hashtable_is_empty(table)); 24 | assert_num_equal(hashtable_size(table), 1); 25 | done = hashtable_insert(table, "key2", "test2", release_none); 26 | assert_true(done); 27 | assert_true(!hashtable_is_empty(table)); 28 | assert_num_equal(hashtable_size(table), 2); 29 | done = hashtable_insert(table, strdup("key3"), strdup("test3"), release_all); 30 | assert_true(done); 31 | assert_true(!hashtable_is_empty(table)); 32 | assert_num_equal(hashtable_size(table), 3); 33 | value = hashtable_get(table, "key1"); 34 | assert_string_equal(value, "test1"); 35 | value = hashtable_get(table, "key2"); 36 | assert_string_equal(value, "test2"); 37 | value = hashtable_get(table, "key3"); 38 | assert_string_equal(value, "test3"); 39 | 40 | done = hashtable_insert(table, "key4", strdup("test4"), release_only_value); 41 | assert_true(!done); 42 | assert_true(!hashtable_is_empty(table)); 43 | assert_num_equal(hashtable_size(table), 3); 44 | 45 | value = hashtable_get(table, "key1"); 46 | assert_string_equal(value, "test1"); 47 | value = hashtable_get(table, "key2"); 48 | assert_string_equal(value, "test2"); 49 | value = hashtable_get(table, "key3"); 50 | assert_string_equal(value, "test3"); 51 | 52 | done = hashtable_remove(table, "key3"); 53 | assert_true(done); 54 | assert_true(!hashtable_is_empty(table)); 55 | assert_num_equal(hashtable_size(table), 2); 56 | 57 | done = hashtable_insert(table, "key4", strdup("test4"), release_only_value); 58 | assert_true(done); 59 | assert_true(!hashtable_is_empty(table)); 60 | assert_num_equal(hashtable_size(table), 3); 61 | 62 | value = hashtable_get(table, "key1"); 63 | assert_string_equal(value, "test1"); 64 | value = hashtable_get(table, "key2"); 65 | assert_string_equal(value, "test2"); 66 | value = hashtable_get(table, "key4"); 67 | assert_string_equal(value, "test4"); 68 | 69 | hashtable_release(table); 70 | } /* test_impl */ 71 | 72 | 73 | int main() 74 | { 75 | test_run(test_impl); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.7) 3 | 4 | project(hashtable C) 5 | 6 | # include shared utilities 7 | if(NOT EXISTS "target/cmake-modules/src/utils.cmake") 8 | execute_process(COMMAND git clone https://github.com/sagiegurari/cmake-modules.git) 9 | endif() 10 | include("target/cmake-modules/src/utils.cmake") 11 | 12 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 13 | 14 | set(CMAKE_BUILD_TYPE Release) 15 | if(NOT WIN32) 16 | set(X_CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Wall -Wextra -Wcast-align -Wunused -Wshadow -Wpedantic") 17 | endif() 18 | 19 | set(X_CMAKE_PROJECT_ROOT_DIR ${CMAKE_BINARY_DIR}/..) 20 | 21 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 22 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 23 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 24 | 25 | macro(add_external_lib) 26 | utils_add_external_github_lib( 27 | REPO_USERNAME sagiegurari 28 | REPO_NAME c_${ARGV0} 29 | TAG_NAME ${ARGV1} 30 | LIBRARY_NAME ${ARGV0} 31 | LIBRARY_PARENT_DIRECTORY target 32 | ) 33 | endmacro(add_external_lib) 34 | add_external_lib("vector") 35 | 36 | include_directories(include "${VECTOR_INCLUDE}") 37 | 38 | # define all sources 39 | file(GLOB SOURCES "src/*.c") 40 | file(GLOB HEADER_SOURCES "include/*.h") 41 | file(GLOB TEST_SOURCES "tests/*") 42 | file(GLOB COMMON_TEST_SOURCES "tests/test.*") 43 | file(GLOB EXAMPLE_SOURCES "examples/*.c") 44 | 45 | # lint code 46 | utils_cppcheck(INCLUDE_DIRECTORY "./include/" SOURCES "./src/*.c" WORKING_DIRECTORY "${X_CMAKE_PROJECT_ROOT_DIR}") 47 | 48 | # format code 49 | utils_uncrustify( 50 | CONFIG_FILE "${X_CMAKE_PROJECT_ROOT_DIR}/uncrustify.cfg" 51 | SOURCES ${SOURCES} ${HEADER_SOURCES} ${TEST_SOURCES} ${EXAMPLE_SOURCES} 52 | ) 53 | 54 | # create static library 55 | add_library(${CMAKE_PROJECT_NAME} STATIC ${SOURCES} ${VECTOR_SOURCES}) 56 | 57 | if(NOT WIN32) 58 | set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES COMPILE_FLAGS "${X_CMAKE_C_FLAGS} -Wconversion") 59 | endif() 60 | 61 | # example 62 | add_executable(example examples/example.c) 63 | target_link_libraries(example ${CMAKE_PROJECT_NAME}) 64 | set_target_properties(example PROPERTIES COMPILE_FLAGS "${X_CMAKE_C_FLAGS}") 65 | 66 | # tests 67 | include(CTest) 68 | 69 | utils_setup_test_lib( 70 | SOURCES "${COMMON_TEST_SOURCES}" 71 | COMPILATION_FLAGS "${X_CMAKE_C_FLAGS}" 72 | ) 73 | utils_setup_c_all_tests( 74 | COMPILATION_FLAGS "${X_CMAKE_C_FLAGS}" 75 | BINARY_DIRECTORY "${CMAKE_BINARY_DIR}/bin" 76 | LIBRARIES "Test" 77 | ) 78 | 79 | if("$ENV{X_CMAKE_DOC_STEPS}" STREQUAL "true") 80 | # post build steps 81 | add_custom_command( 82 | TARGET example 83 | POST_BUILD 84 | COMMENT "Post Build Steps" 85 | COMMAND ${CMAKE_COMMAND} -P "../post-example-build.cmake" 86 | ) 87 | endif() 88 | 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hashtable 2 | 3 | [![CI](https://github.com/sagiegurari/c_hashtable/workflows/CI/badge.svg?branch=master)](https://github.com/sagiegurari/c_hashtable/actions) 4 | [![Release](https://img.shields.io/github/v/release/sagiegurari/c_hashtable)](https://github.com/sagiegurari/c_hashtable/releases) 5 | [![license](https://img.shields.io/github/license/sagiegurari/c_hashtable)](https://github.com/sagiegurari/c_hashtable/blob/master/LICENSE) 6 | 7 | > C Hash Table 8 | 9 | * [Overview](#overview) 10 | * [Usage](#usage) 11 | * [Contributing](.github/CONTRIBUTING.md) 12 | * [Release History](CHANGELOG.md) 13 | * [License](#license) 14 | 15 | 16 | ## Overview 17 | This library provides a simple basic implementation of hashtables for C. 18 | 19 | 20 | ## Usage 21 | 22 | 23 | ```c 24 | #include "hashtable.h" 25 | #include 26 | #include 27 | #include 28 | 29 | static void release_key_and_value(char *key, void *value); 30 | 31 | 32 | int main() 33 | { 34 | bool done = false; 35 | size_t size = 0; 36 | char *value = NULL; 37 | 38 | // Create new hashtable with default implementation and size. 39 | // You can also provide your own hash implementation using the new with options function as follows: 40 | // struct HashTableOptions options = { .table_size = 5, .chain_max_size = 0, .hash_function = my_hash_function }; 41 | // struct HashTable *table = hashtable_new_with_options(options); 42 | struct HashTable *table = hashtable_new(); 43 | 44 | // add a new entry to the table. 45 | // The value is void* so you can put anything you need 46 | done = hashtable_insert(table, "key", "value", NULL /* release function */); 47 | value = hashtable_get(table, "key"); 48 | size = hashtable_size(table); 49 | printf("Pushed new entry: %d value is: '%s' current size: %zu\n", done, value, size); 50 | 51 | // To release the entry key/value you can provide a release function 52 | done = hashtable_insert(table, strdup("key2"), strdup("value2"), release_key_and_value); 53 | value = hashtable_get(table, "key2"); 54 | size = hashtable_size(table); 55 | printf("Pushed new entry: %d value is: '%s' current size: %zu\n", done, value, size); 56 | 57 | // remove the value (content will be freed via custom release function) 58 | hashtable_remove(table, "key2"); 59 | printf("Removed 1 entry, is empty: %d\n", hashtable_is_empty(table)); 60 | 61 | hashtable_clear(table); 62 | printf("Removed all entries, is empty: %d\n", hashtable_is_empty(table)); 63 | 64 | // once we are done, we need to release the table and its contents 65 | hashtable_release(table); 66 | 67 | return(0); 68 | } /* main */ 69 | 70 | 71 | static void release_key_and_value(char *key, void *value) 72 | { 73 | free(key); 74 | free(value); 75 | } 76 | ``` 77 | 78 | 79 | ## Contributing 80 | See [contributing guide](.github/CONTRIBUTING.md) 81 | 82 | 83 | ## Release History 84 | 85 | See [Changelog](CHANGELOG.md) 86 | 87 | 88 | ## License 89 | Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license. 90 | -------------------------------------------------------------------------------- /tests/test_keys.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | static size_t _test_hash_function(char *key) 5 | { 6 | assert_true(key != NULL); 7 | return(10000); 8 | } 9 | 10 | 11 | static void _test_assert_value(char **array, char *value, size_t size) 12 | { 13 | assert_true(size > 0); 14 | 15 | for (size_t index = 0; index < size; index++) 16 | { 17 | if (!strcmp(array[index], value)) 18 | { 19 | return; 20 | } 21 | } 22 | 23 | test_fail(); 24 | } 25 | 26 | 27 | void test_impl() 28 | { 29 | char **keys = hashtable_keys(NULL); 30 | 31 | assert_true(keys == NULL); 32 | 33 | struct HashTableOptions options = { .table_size = 5, .chain_max_size = 3, .hash_function = _test_hash_function }; 34 | struct HashTable *table = hashtable_new_with_options(options); 35 | 36 | bool done = false; 37 | char *value = NULL; 38 | 39 | assert_true(hashtable_is_empty(table)); 40 | assert_num_equal(hashtable_size(table), 0); 41 | done = hashtable_insert(table, "key1", strdup("test1"), release_only_value); 42 | assert_true(done); 43 | assert_true(!hashtable_is_empty(table)); 44 | assert_num_equal(hashtable_size(table), 1); 45 | done = hashtable_insert(table, "key2", "test2", release_none); 46 | assert_true(done); 47 | assert_true(!hashtable_is_empty(table)); 48 | assert_num_equal(hashtable_size(table), 2); 49 | done = hashtable_insert(table, strdup("key3"), strdup("test3"), release_all); 50 | assert_true(done); 51 | assert_true(!hashtable_is_empty(table)); 52 | assert_num_equal(hashtable_size(table), 3); 53 | value = hashtable_get(table, "key1"); 54 | assert_string_equal(value, "test1"); 55 | value = hashtable_get(table, "key2"); 56 | assert_string_equal(value, "test2"); 57 | value = hashtable_get(table, "key3"); 58 | assert_string_equal(value, "test3"); 59 | 60 | keys = hashtable_keys(table); 61 | assert_true(keys != NULL); 62 | _test_assert_value(keys, "key1", hashtable_size(table)); 63 | _test_assert_value(keys, "key2", hashtable_size(table)); 64 | _test_assert_value(keys, "key3", hashtable_size(table)); 65 | free(keys); 66 | 67 | done = hashtable_insert(table, "key4", strdup("test4"), release_only_value); 68 | assert_true(!done); 69 | assert_true(!hashtable_is_empty(table)); 70 | assert_num_equal(hashtable_size(table), 3); 71 | 72 | value = hashtable_get(table, "key1"); 73 | assert_string_equal(value, "test1"); 74 | value = hashtable_get(table, "key2"); 75 | assert_string_equal(value, "test2"); 76 | value = hashtable_get(table, "key3"); 77 | assert_string_equal(value, "test3"); 78 | 79 | keys = hashtable_keys(table); 80 | assert_true(keys != NULL); 81 | _test_assert_value(keys, "key1", hashtable_size(table)); 82 | _test_assert_value(keys, "key2", hashtable_size(table)); 83 | _test_assert_value(keys, "key3", hashtable_size(table)); 84 | free(keys); 85 | 86 | done = hashtable_remove(table, "key3"); 87 | assert_true(done); 88 | assert_true(!hashtable_is_empty(table)); 89 | assert_num_equal(hashtable_size(table), 2); 90 | 91 | keys = hashtable_keys(table); 92 | assert_true(keys != NULL); 93 | _test_assert_value(keys, "key1", hashtable_size(table)); 94 | _test_assert_value(keys, "key2", hashtable_size(table)); 95 | free(keys); 96 | 97 | done = hashtable_insert(table, "key4", strdup("test4"), release_only_value); 98 | assert_true(done); 99 | assert_true(!hashtable_is_empty(table)); 100 | assert_num_equal(hashtable_size(table), 3); 101 | 102 | value = hashtable_get(table, "key1"); 103 | assert_string_equal(value, "test1"); 104 | value = hashtable_get(table, "key2"); 105 | assert_string_equal(value, "test2"); 106 | value = hashtable_get(table, "key4"); 107 | assert_string_equal(value, "test4"); 108 | 109 | keys = hashtable_keys(table); 110 | assert_true(keys != NULL); 111 | _test_assert_value(keys, "key1", hashtable_size(table)); 112 | _test_assert_value(keys, "key2", hashtable_size(table)); 113 | _test_assert_value(keys, "key4", hashtable_size(table)); 114 | free(keys); 115 | 116 | hashtable_release(table); 117 | } /* test_impl */ 118 | 119 | 120 | int main() 121 | { 122 | test_run(test_impl); 123 | } 124 | 125 | -------------------------------------------------------------------------------- /tests/test_entries.c: -------------------------------------------------------------------------------- 1 | #include "test.h" 2 | 3 | 4 | static size_t _test_hash_function(char *key) 5 | { 6 | assert_true(key != NULL); 7 | return(10000); 8 | } 9 | 10 | 11 | static void _test_assert_value(struct HashTableEntries entries, char *key, void *value, size_t size) 12 | { 13 | assert_true(size > 0); 14 | 15 | for (size_t index = 0; index < size; index++) 16 | { 17 | if (!strcmp(entries.keys[index], key)) 18 | { 19 | assert_string_equal(entries.values[index], (char *)value); 20 | return; 21 | } 22 | } 23 | 24 | test_fail(); 25 | } 26 | 27 | 28 | void test_impl() 29 | { 30 | struct HashTableEntries entries = hashtable_entries(NULL); 31 | 32 | assert_true(entries.keys == NULL); 33 | assert_true(entries.values == NULL); 34 | 35 | struct HashTableOptions options = { .table_size = 5, .chain_max_size = 3, .hash_function = _test_hash_function }; 36 | struct HashTable *table = hashtable_new_with_options(options); 37 | 38 | bool done = false; 39 | char *value = NULL; 40 | 41 | assert_true(hashtable_is_empty(table)); 42 | assert_num_equal(hashtable_size(table), 0); 43 | done = hashtable_insert(table, "key1", strdup("test1"), release_only_value); 44 | assert_true(done); 45 | assert_true(!hashtable_is_empty(table)); 46 | assert_num_equal(hashtable_size(table), 1); 47 | done = hashtable_insert(table, "key2", "test2", release_none); 48 | assert_true(done); 49 | assert_true(!hashtable_is_empty(table)); 50 | assert_num_equal(hashtable_size(table), 2); 51 | done = hashtable_insert(table, strdup("key3"), strdup("test3"), release_all); 52 | assert_true(done); 53 | assert_true(!hashtable_is_empty(table)); 54 | assert_num_equal(hashtable_size(table), 3); 55 | value = hashtable_get(table, "key1"); 56 | assert_string_equal(value, "test1"); 57 | value = hashtable_get(table, "key2"); 58 | assert_string_equal(value, "test2"); 59 | value = hashtable_get(table, "key3"); 60 | assert_string_equal(value, "test3"); 61 | 62 | entries = hashtable_entries(table); 63 | assert_true(entries.keys != NULL); 64 | assert_true(entries.values != NULL); 65 | _test_assert_value(entries, "key1", "test1", hashtable_size(table)); 66 | _test_assert_value(entries, "key2", "test2", hashtable_size(table)); 67 | _test_assert_value(entries, "key3", "test3", hashtable_size(table)); 68 | free(entries.keys); 69 | free(entries.values); 70 | 71 | done = hashtable_insert(table, "key4", strdup("test4"), release_only_value); 72 | assert_true(!done); 73 | assert_true(!hashtable_is_empty(table)); 74 | assert_num_equal(hashtable_size(table), 3); 75 | 76 | value = hashtable_get(table, "key1"); 77 | assert_string_equal(value, "test1"); 78 | value = hashtable_get(table, "key2"); 79 | assert_string_equal(value, "test2"); 80 | value = hashtable_get(table, "key3"); 81 | assert_string_equal(value, "test3"); 82 | 83 | entries = hashtable_entries(table); 84 | assert_true(entries.keys != NULL); 85 | assert_true(entries.values != NULL); 86 | _test_assert_value(entries, "key1", "test1", hashtable_size(table)); 87 | _test_assert_value(entries, "key2", "test2", hashtable_size(table)); 88 | _test_assert_value(entries, "key3", "test3", hashtable_size(table)); 89 | free(entries.keys); 90 | free(entries.values); 91 | 92 | done = hashtable_remove(table, "key3"); 93 | assert_true(done); 94 | assert_true(!hashtable_is_empty(table)); 95 | assert_num_equal(hashtable_size(table), 2); 96 | 97 | entries = hashtable_entries(table); 98 | assert_true(entries.keys != NULL); 99 | assert_true(entries.values != NULL); 100 | _test_assert_value(entries, "key1", "test1", hashtable_size(table)); 101 | _test_assert_value(entries, "key2", "test2", hashtable_size(table)); 102 | free(entries.keys); 103 | free(entries.values); 104 | 105 | done = hashtable_insert(table, "key4", strdup("test4"), release_only_value); 106 | assert_true(done); 107 | assert_true(!hashtable_is_empty(table)); 108 | assert_num_equal(hashtable_size(table), 3); 109 | 110 | value = hashtable_get(table, "key1"); 111 | assert_string_equal(value, "test1"); 112 | value = hashtable_get(table, "key2"); 113 | assert_string_equal(value, "test2"); 114 | value = hashtable_get(table, "key4"); 115 | assert_string_equal(value, "test4"); 116 | 117 | entries = hashtable_entries(table); 118 | assert_true(entries.keys != NULL); 119 | assert_true(entries.values != NULL); 120 | _test_assert_value(entries, "key1", "test1", hashtable_size(table)); 121 | _test_assert_value(entries, "key2", "test2", hashtable_size(table)); 122 | _test_assert_value(entries, "key4", "test4", hashtable_size(table)); 123 | free(entries.keys); 124 | free(entries.values); 125 | 126 | hashtable_release(table); 127 | } /* test_impl */ 128 | 129 | 130 | int main() 131 | { 132 | test_run(test_impl); 133 | } 134 | 135 | -------------------------------------------------------------------------------- /uncrustify.cfg: -------------------------------------------------------------------------------- 1 | output_tab_size = 2 2 | tok_split_gte = true 3 | indent_columns = 2 4 | indent_with_tabs = 0 5 | indent_class = true 6 | indent_member = 2 7 | indent_bool_paren = true 8 | indent_first_bool_expr = true 9 | sp_arith = force 10 | sp_assign = force 11 | sp_assign_default = force 12 | sp_bool = force 13 | sp_compare = force 14 | sp_inside_paren = remove 15 | sp_paren_paren = remove 16 | sp_before_ptr_star = force 17 | sp_between_ptr_star = remove 18 | sp_after_ptr_star = remove 19 | sp_before_byref = force 20 | sp_after_byref = remove 21 | sp_before_angle = remove 22 | sp_inside_angle = remove 23 | sp_after_angle = force 24 | sp_angle_paren = remove 25 | sp_angle_paren_empty = remove 26 | sp_angle_word = force 27 | sp_before_sparen = force 28 | sp_inside_sparen = remove 29 | sp_after_sparen = force 30 | sp_before_semi_for = remove 31 | sp_before_semi_for_empty = force 32 | sp_before_squares = remove 33 | sp_inside_square = remove 34 | sp_after_comma = force 35 | sp_before_ellipsis = remove 36 | sp_after_cast = remove 37 | sp_sizeof_paren = remove 38 | sp_inside_braces_enum = force 39 | sp_inside_braces_struct = force 40 | sp_inside_braces = force 41 | sp_func_proto_paren = remove 42 | sp_func_def_paren = remove 43 | sp_inside_fparen = remove 44 | sp_after_tparen_close = remove 45 | sp_func_call_paren = remove 46 | sp_return_paren = remove 47 | sp_attribute_paren = remove 48 | sp_defined_paren = force 49 | sp_brace_typedef = force 50 | sp_before_dc = remove 51 | sp_after_dc = remove 52 | sp_enum_assign = force 53 | align_func_params = true 54 | align_var_def_span = 2 55 | align_var_def_star_style = 1 56 | align_var_def_amp_style = 1 57 | align_var_def_colon = true 58 | align_var_def_inline = true 59 | align_assign_span = 1 60 | align_enum_equ_span = 4 61 | align_var_class_span = 2 62 | align_var_struct_span = 3 63 | align_struct_init_span = 3 64 | align_typedef_gap = 3 65 | align_typedef_span = 5 66 | align_typedef_star_style = 1 67 | align_right_cmt_span = 3 68 | align_nl_cont = true 69 | align_pp_define_gap = 4 70 | align_pp_define_span = 3 71 | align_asm_colon = true 72 | align_enum_equ_thresh = 8 73 | nl_assign_leave_one_liners = true 74 | nl_class_leave_one_liners = true 75 | nl_enum_leave_one_liners = true 76 | nl_getset_leave_one_liners = true 77 | nl_assign_brace = add 78 | nl_func_var_def_blk = 1 79 | nl_fcall_brace = add 80 | nl_enum_brace = force 81 | nl_struct_brace = add 82 | nl_union_brace = add 83 | nl_if_brace = add 84 | nl_brace_else = add 85 | nl_elseif_brace = add 86 | nl_else_brace = add 87 | nl_finally_brace = add 88 | nl_try_brace = add 89 | nl_for_brace = add 90 | nl_catch_brace = add 91 | nl_while_brace = add 92 | nl_do_brace = add 93 | nl_brace_while = remove 94 | nl_switch_brace = add 95 | nl_before_case = true 96 | nl_after_case = true 97 | nl_case_colon_brace = force 98 | nl_namespace_brace = force 99 | nl_constr_init_args = force 100 | nl_func_type_name = remove 101 | nl_func_paren = remove 102 | nl_func_def_paren = remove 103 | nl_func_decl_start = remove 104 | nl_func_def_start = remove 105 | nl_func_decl_start_single = remove 106 | nl_func_def_start_single = remove 107 | nl_func_decl_args = remove 108 | nl_func_decl_end = remove 109 | nl_func_def_end = remove 110 | nl_func_decl_end_single = remove 111 | nl_func_def_end_single = remove 112 | nl_fdef_brace = force 113 | nl_return_expr = remove 114 | nl_after_semicolon = true 115 | nl_after_brace_open = true 116 | nl_after_vbrace_open = true 117 | nl_after_brace_close = true 118 | nl_squeeze_ifdef = true 119 | nl_constr_colon = force 120 | pos_constr_comma = lead_force 121 | pos_constr_colon = lead_break 122 | pos_bool = lead 123 | pos_enum_comma = trail_force 124 | nl_max = 3 125 | nl_after_func_proto = 1 126 | nl_after_func_proto_group = 2 127 | nl_before_func_body_def = 3 128 | nl_after_access_spec = 1 129 | eat_blanks_after_open_brace = true 130 | eat_blanks_before_close_brace = true 131 | nl_after_return = true 132 | mod_full_brace_do = add 133 | mod_full_brace_for = add 134 | mod_full_brace_if = add 135 | mod_full_brace_while = add 136 | mod_paren_on_return = add 137 | mod_remove_extra_semicolon = true 138 | mod_add_long_function_closebrace_comment = 40 139 | mod_add_long_namespace_closebrace_comment = 5 140 | mod_add_long_switch_closebrace_comment = 40 141 | mod_remove_empty_return = true 142 | cmt_star_cont = true 143 | pp_indent = remove 144 | pp_space = remove 145 | mod_sort_include = true 146 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /src/hashtable.c: -------------------------------------------------------------------------------- 1 | #include "hashtable.h" 2 | #include "vector.h" 3 | #include 4 | #include 5 | 6 | struct HashTable 7 | { 8 | struct Vector *values; 9 | size_t chain_max_size; 10 | size_t (*hash_function)(char *); 11 | size_t count; 12 | }; 13 | 14 | struct HashTableEntry 15 | { 16 | char *key; 17 | void *value; 18 | void (*release)(char *, void *); 19 | }; 20 | 21 | struct HashTableEntryReplaceContext 22 | { 23 | bool done; 24 | struct HashTableEntry *entry; 25 | }; 26 | 27 | struct HashTableChainAndIndex 28 | { 29 | struct Vector *vector; 30 | size_t index; 31 | }; 32 | 33 | // private functions 34 | static size_t _hashtable_hash_key(struct HashTable *, char *); 35 | static size_t _hashtable_hash_function(char *); 36 | static void _hashtable_entry_release(struct HashTableEntry *); 37 | static struct HashTableChainAndIndex _hashtable_get_chain(struct HashTable *, char *, bool create); 38 | static void *_hashtable_for_entry_in_chain(struct HashTable *, struct Vector *, char *, void *, void * (*callback)(struct HashTable *, struct Vector *, struct HashTableEntry *, size_t index, void *)); 39 | static void *_hashtable_get_entry_callback(struct HashTable *, struct Vector *, struct HashTableEntry *, size_t index, void *); 40 | static void *_hashtable_replace_or_add_entry_callback(struct HashTable *, struct Vector *, struct HashTableEntry *, size_t index, void *); 41 | static void *_hashtable_remove_entry_callback(struct HashTable *, struct Vector *, struct HashTableEntry *, size_t index, void *); 42 | static bool _hashtable_release_entries(struct HashTable *); 43 | struct HashTableEntries _hashtable_entries(struct HashTable *, bool include_values); 44 | static void _hashtable_noop(void *); 45 | 46 | struct HashTable *hashtable_new(void) 47 | { 48 | struct HashTableOptions options = { .table_size = 0, .chain_max_size = 0, .hash_function = NULL }; 49 | 50 | return(hashtable_new_with_options(options)); 51 | } 52 | 53 | struct HashTable *hashtable_new_with_options(struct HashTableOptions options) 54 | { 55 | size_t table_size = options.table_size; 56 | 57 | if (!table_size) 58 | { 59 | table_size = HASHTABLE_DEFAULT_SIZE; 60 | } 61 | 62 | struct HashTable *table = malloc(sizeof(struct HashTable)); 63 | table->values = vector_new_with_options(table_size, false); 64 | table->chain_max_size = options.chain_max_size; 65 | table->hash_function = options.hash_function; 66 | if (table->hash_function == NULL) 67 | { 68 | table->hash_function = _hashtable_hash_function; 69 | } 70 | table->count = 0; 71 | 72 | return(table); 73 | } 74 | 75 | 76 | void hashtable_release(struct HashTable *table) 77 | { 78 | if (table == NULL) 79 | { 80 | return; 81 | } 82 | 83 | _hashtable_release_entries(table); 84 | vector_release(table->values); 85 | free(table); 86 | } 87 | 88 | 89 | void hashtable_clear(struct HashTable *table) 90 | { 91 | if (_hashtable_release_entries(table)) 92 | { 93 | vector_clear(table->values); 94 | table->count = 0; 95 | } 96 | } 97 | 98 | 99 | size_t hashtable_size(struct HashTable *table) 100 | { 101 | if (table == NULL) 102 | { 103 | return(0); 104 | } 105 | 106 | return(table->count); 107 | } 108 | 109 | 110 | bool hashtable_is_empty(struct HashTable *table) 111 | { 112 | size_t size = hashtable_size(table); 113 | 114 | return(!size); 115 | } 116 | 117 | 118 | bool hashtable_insert(struct HashTable *table, char *key, void *value, void (*release)(char *, void *)) 119 | { 120 | struct HashTableChainAndIndex chain = _hashtable_get_chain(table, key, true /* create */); 121 | 122 | if (chain.vector == NULL) 123 | { 124 | return(false); 125 | } 126 | 127 | struct HashTableEntry *entry = malloc(sizeof(struct HashTableEntry)); 128 | entry->key = key; 129 | entry->value = value; 130 | entry->release = release; 131 | 132 | struct HashTableEntryReplaceContext context = { .done = false, .entry = entry }; 133 | _hashtable_for_entry_in_chain(table, chain.vector, key, &context, _hashtable_replace_or_add_entry_callback); 134 | 135 | if (!context.done) 136 | { 137 | _hashtable_entry_release(entry); 138 | } 139 | 140 | return(context.done); 141 | } /* hashtable_insert */ 142 | 143 | char **hashtable_keys(struct HashTable *); 144 | 145 | 146 | void *hashtable_get(struct HashTable *table, char *key) 147 | { 148 | struct HashTableChainAndIndex chain = _hashtable_get_chain(table, key, false /* create */); 149 | 150 | if (chain.vector == NULL) 151 | { 152 | return(NULL); 153 | } 154 | 155 | struct HashTableEntry *entry = _hashtable_for_entry_in_chain(table, chain.vector, key, NULL, _hashtable_get_entry_callback); 156 | if (entry == NULL) 157 | { 158 | return(NULL); 159 | } 160 | 161 | return(entry->value); 162 | } 163 | 164 | 165 | bool hashtable_remove(struct HashTable *table, char *key) 166 | { 167 | struct HashTableChainAndIndex chain = _hashtable_get_chain(table, key, false /* create */); 168 | 169 | if (chain.vector == NULL) 170 | { 171 | return(false); 172 | } 173 | 174 | bool done = false; 175 | _hashtable_for_entry_in_chain(table, chain.vector, key, &done, _hashtable_remove_entry_callback); 176 | 177 | if (!vector_size(chain.vector)) 178 | { 179 | vector_release(chain.vector); 180 | vector_set(table->values, chain.index, NULL); 181 | } 182 | 183 | return(done); 184 | } 185 | 186 | 187 | char **hashtable_keys(struct HashTable *table) 188 | { 189 | if (table == NULL) 190 | { 191 | return(NULL); 192 | } 193 | 194 | struct HashTableEntries entries = _hashtable_entries(table, false /* include value */); 195 | if (entries.values != NULL) 196 | { 197 | free(entries.values); 198 | } 199 | 200 | return(entries.keys); 201 | } 202 | 203 | struct HashTableEntries hashtable_entries(struct HashTable *table) 204 | { 205 | return(_hashtable_entries(table, true /* include values */)); 206 | } 207 | 208 | 209 | static size_t _hashtable_hash_key(struct HashTable *table, char *key) 210 | { 211 | if (table == NULL || table->hash_function == NULL || key == NULL) 212 | { 213 | return(0); 214 | } 215 | 216 | size_t capacity = vector_capacity(table->values); 217 | if (!capacity) 218 | { 219 | return(0); 220 | } 221 | 222 | size_t index = table->hash_function(key); 223 | 224 | return(index % capacity); 225 | } 226 | 227 | 228 | static size_t _hashtable_hash_function(char *key) 229 | { 230 | size_t index = 0; 231 | 232 | for (int char_index = 0; key[char_index]; char_index++) 233 | { 234 | index = 31 * index + (size_t)key[char_index]; 235 | } 236 | 237 | return(index); 238 | } 239 | 240 | 241 | static void _hashtable_entry_release(struct HashTableEntry *entry) 242 | { 243 | if (entry == NULL) 244 | { 245 | return; 246 | } 247 | 248 | if (entry->release != NULL) 249 | { 250 | entry->release(entry->key, entry->value); 251 | } 252 | 253 | free(entry); 254 | } 255 | 256 | static struct HashTableChainAndIndex _hashtable_get_chain(struct HashTable *table, char *key, bool create) 257 | { 258 | struct HashTableChainAndIndex chain_and_index = { .vector = NULL, .index = 0 }; 259 | 260 | if (table == NULL || key == NULL) 261 | { 262 | return(chain_and_index); 263 | } 264 | 265 | chain_and_index.index = _hashtable_hash_key(table, key); 266 | 267 | struct Vector *chain = vector_get(table->values, chain_and_index.index); 268 | 269 | if (chain == NULL && create) 270 | { 271 | if (table->chain_max_size) 272 | { 273 | chain = vector_new_with_options(table->chain_max_size, false); 274 | } 275 | else 276 | { 277 | chain = vector_new(); 278 | } 279 | 280 | vector_set(table->values, chain_and_index.index, chain); 281 | } 282 | 283 | chain_and_index.vector = chain; 284 | 285 | return(chain_and_index); 286 | } 287 | 288 | 289 | static void *_hashtable_for_entry_in_chain(struct HashTable *table, struct Vector *chain, char *key, void *context, void * (*callback)(struct HashTable *, struct Vector *, struct HashTableEntry *, size_t index, void *)) 290 | { 291 | if (table == NULL || chain == NULL || key == NULL) 292 | { 293 | return(NULL); 294 | } 295 | 296 | size_t chain_size = vector_size(chain); 297 | for (size_t index = 0; index < chain_size; index++) 298 | { 299 | struct HashTableEntry *entry = vector_get(chain, index); 300 | if (entry != NULL && !strcmp(entry->key, key)) 301 | { 302 | return(callback(table, chain, entry, index, context)); 303 | } 304 | } 305 | 306 | return(callback(table, chain, NULL, 0, context)); 307 | } 308 | 309 | 310 | static void *_hashtable_get_entry_callback(struct HashTable *table, struct Vector *chain, struct HashTableEntry *entry, size_t index, void *context) 311 | { 312 | _hashtable_noop(table); 313 | _hashtable_noop(chain); 314 | _hashtable_noop(&index); 315 | _hashtable_noop(context); 316 | 317 | return(entry); 318 | } 319 | 320 | 321 | static void *_hashtable_replace_or_add_entry_callback(struct HashTable *table, struct Vector *chain, struct HashTableEntry *entry, size_t index, void *context) 322 | { 323 | if (table == NULL || chain == NULL || context == NULL) 324 | { 325 | return(NULL); 326 | } 327 | 328 | struct HashTableEntryReplaceContext *operation_context = (struct HashTableEntryReplaceContext *)context; 329 | if (entry == NULL) 330 | { 331 | // we hit max chain size 332 | size_t chain_size = vector_size(chain); 333 | if (table->chain_max_size && table->chain_max_size <= chain_size) 334 | { 335 | return(NULL); 336 | } 337 | 338 | operation_context->done = vector_push(chain, operation_context->entry); 339 | if (operation_context->done) 340 | { 341 | table->count = table->count + 1; 342 | } 343 | } 344 | else 345 | { 346 | _hashtable_entry_release(entry); 347 | void *old_entry = vector_set(chain, index, operation_context->entry); 348 | operation_context->done = old_entry != NULL; 349 | } 350 | 351 | return(NULL); 352 | } 353 | 354 | 355 | static void *_hashtable_remove_entry_callback(struct HashTable *table, struct Vector *chain, struct HashTableEntry *entry, size_t index, void *context) 356 | { 357 | _hashtable_noop(table); 358 | 359 | bool *bool_ptr = (bool *)context; 360 | if (entry == NULL) 361 | { 362 | *bool_ptr = false; 363 | } 364 | else 365 | { 366 | _hashtable_entry_release(entry); 367 | vector_remove(chain, index); 368 | table->count = table->count - 1; 369 | *bool_ptr = true; 370 | } 371 | 372 | return(NULL); 373 | } 374 | 375 | 376 | static bool _hashtable_release_entries(struct HashTable *table) 377 | { 378 | if (table == NULL) 379 | { 380 | return(false); 381 | } 382 | 383 | if (table->values == NULL) 384 | { 385 | return(false); 386 | } 387 | 388 | size_t table_size = vector_size(table->values); 389 | for (size_t table_index = 0; table_index < table_size; table_index++) 390 | { 391 | struct Vector *chain = vector_get(table->values, table_index); 392 | 393 | if (chain != NULL) 394 | { 395 | size_t chain_size = vector_size(chain); 396 | for (size_t chain_index = 0; chain_index < chain_size; chain_index++) 397 | { 398 | struct HashTableEntry *entry = vector_get(chain, chain_index); 399 | _hashtable_entry_release(entry); 400 | } 401 | 402 | vector_release(chain); 403 | } 404 | } 405 | 406 | table->count = 0; 407 | 408 | return(true); 409 | } 410 | 411 | struct HashTableEntries _hashtable_entries(struct HashTable *table, bool include_values) 412 | { 413 | struct HashTableEntries entries = { .keys = NULL, .values = NULL }; 414 | 415 | if (table == NULL) 416 | { 417 | return(entries); 418 | } 419 | 420 | entries.keys = malloc(sizeof(char *) * table->count); 421 | if (include_values) 422 | { 423 | entries.values = malloc(sizeof(void *) * table->count); 424 | } 425 | 426 | size_t table_size = vector_size(table->values); 427 | size_t entry_index = 0; 428 | for (size_t table_index = 0; table_index < table_size; table_index++) 429 | { 430 | struct Vector *chain = vector_get(table->values, table_index); 431 | 432 | if (chain != NULL) 433 | { 434 | size_t chain_size = vector_size(chain); 435 | for (size_t chain_index = 0; chain_index < chain_size; chain_index++) 436 | { 437 | struct HashTableEntry *entry = vector_get(chain, chain_index); 438 | entries.keys[entry_index] = entry->key; 439 | if (include_values) 440 | { 441 | entries.values[entry_index] = entry->value; 442 | } 443 | entry_index = entry_index + 1; 444 | } 445 | } 446 | } 447 | 448 | return(entries); 449 | } 450 | 451 | 452 | static void _hashtable_noop(void *ignored) 453 | { 454 | if (ignored == NULL) 455 | { 456 | return; 457 | } 458 | } 459 | 460 | --------------------------------------------------------------------------------