├── .clang-format ├── .clangd ├── .gitattributes ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── bug_report.md ├── feature_request.md └── workflows │ └── test_suit.yml ├── .gitignore ├── AUTHORS ├── CHANGELOG ├── CMakeLists.txt ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── README.md ├── TODO ├── benchmarks ├── Makefile ├── benchmark.c ├── comparisons │ └── hashtable │ │ ├── Makefile │ │ ├── hashmap.c │ │ ├── hashmultimap.c │ │ ├── murmurhash.h │ │ └── unorderedmap.cpp └── util │ └── twister.c ├── book.toml ├── cmc ├── bitset.h ├── bitset │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── cor │ ├── alloc.h │ ├── bitset.h │ ├── callbacks.h │ ├── core.h │ ├── dev.h │ ├── flags.h │ ├── ftable.h │ ├── hashtable.h │ ├── heap.h │ └── undef.h ├── deque.h ├── deque │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── hashbidimap.h ├── hashbidimap │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── hashmap.h ├── hashmap │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── hashmultimap.h ├── hashmultimap │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── hashmultiset.h ├── hashmultiset │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── hashset.h ├── hashset │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── heap.h ├── heap │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── intervalheap.h ├── intervalheap │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── linkedlist.h ├── linkedlist │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── list.h ├── list │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── queue.h ├── queue │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── sortedlist.h ├── sortedlist │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── stack.h ├── stack │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── treemap.h ├── treemap │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h ├── treeset.h ├── treeset │ ├── code.h │ ├── ext │ │ ├── code.h │ │ ├── header.h │ │ └── struct.h │ ├── header.h │ └── struct.h └── utl │ ├── assert.h │ ├── foreach.h │ ├── futils.h │ ├── log.h │ ├── mutex.h │ ├── test.h │ ├── thread.h │ └── timer.h ├── docs ├── .nojekyll ├── 404.html ├── CNAME ├── Examples │ └── examples.html ├── FontAwesome │ ├── css │ │ └── font-awesome.css │ └── fonts │ │ ├── FontAwesome.ttf │ │ ├── fontawesome-webfont.eot │ │ ├── fontawesome-webfont.svg │ │ ├── fontawesome-webfont.ttf │ │ ├── fontawesome-webfont.woff │ │ └── fontawesome-webfont.woff2 ├── Introduction │ ├── how_to_install.html │ ├── how_to_use_the_library.html │ ├── img │ │ └── logo.png │ ├── introduction.html │ ├── library_structure.html │ ├── understanding_the_library.html │ └── why_this_library.html ├── Overview │ ├── collections_overview.html │ ├── features_overview.html │ ├── files_overview.html │ └── index.html ├── ayu-highlight.css ├── book.js ├── clipboard.min.js ├── cmc │ ├── bitset.h │ │ └── index.html │ ├── deque.h │ │ ├── functions.html │ │ └── index.html │ └── index.html ├── cor │ ├── callbacks │ │ └── index.html │ ├── custom_allocation │ │ └── index.html │ ├── error_codes │ │ └── index.html │ ├── functions_table │ │ └── index.html │ ├── index.html │ └── iterators │ │ └── index.html ├── css │ ├── chrome.css │ ├── general.css │ ├── print.css │ └── variables.css ├── dev │ └── index.html ├── elasticlunr.min.js ├── favicon.png ├── favicon.svg ├── fonts │ ├── OPEN-SANS-LICENSE.txt │ ├── SOURCE-CODE-PRO-LICENSE.txt │ ├── fonts.css │ ├── open-sans-v17-all-charsets-300.woff2 │ ├── open-sans-v17-all-charsets-300italic.woff2 │ ├── open-sans-v17-all-charsets-600.woff2 │ ├── open-sans-v17-all-charsets-600italic.woff2 │ ├── open-sans-v17-all-charsets-700.woff2 │ ├── open-sans-v17-all-charsets-700italic.woff2 │ ├── open-sans-v17-all-charsets-800.woff2 │ ├── open-sans-v17-all-charsets-800italic.woff2 │ ├── open-sans-v17-all-charsets-italic.woff2 │ ├── open-sans-v17-all-charsets-regular.woff2 │ └── source-code-pro-v11-all-charsets-500.woff2 ├── highlight.css ├── highlight.js ├── img │ └── logo.png ├── index.html ├── mark.min.js ├── print.html ├── sac │ └── index.html ├── searcher.js ├── searchindex.js ├── searchindex.json ├── tomorrow-night.css └── utl │ ├── assert.h │ ├── generic_assertions.html │ ├── index.html │ ├── overview.html │ └── valued_assertions.html │ ├── foreach.h │ └── index.html │ ├── futils.h │ └── index.html │ ├── index.html │ ├── log.h │ ├── configuration.html │ ├── index.html │ ├── log_functions.html │ └── meanings.html │ ├── test.h │ └── index.html │ └── timer.h │ └── index.html ├── documentation ├── .gitignore ├── README.md ├── book.toml └── src │ ├── Examples │ └── examples.md │ ├── Introduction │ ├── how_to_install.md │ ├── how_to_use_the_library.md │ ├── img │ │ └── logo.png │ ├── introduction.md │ ├── library_structure.md │ ├── understanding_the_library.md │ └── why_this_library.md │ ├── Overview │ ├── collections_overview.md │ ├── features_overview.md │ ├── files_overview.md │ └── index.md │ ├── SUMMARY.md │ ├── chapter_1.md │ ├── cmc │ ├── bidimap.md │ ├── bitset.h │ │ └── index.md │ ├── deque.h │ │ ├── functions.md │ │ └── index.md │ ├── deque.md │ ├── hashmap.md │ ├── hashset.md │ ├── heap.md │ ├── index.md │ ├── intervalheap.md │ ├── linkedlist.md │ ├── list.md │ ├── multimap.md │ ├── multiset.md │ ├── queue.md │ ├── sortedlist.md │ ├── stack.md │ ├── treemap.md │ └── treeset.md │ ├── cor │ ├── callbacks │ │ └── index.md │ ├── custom_allocation │ │ └── index.md │ ├── error_codes │ │ └── index.md │ ├── functions_table │ │ └── index.md │ ├── index.md │ └── iterators │ │ └── index.md │ ├── dev │ └── index.md │ ├── img │ └── logo.png │ ├── sac │ └── index.md │ └── utl │ ├── assert.h │ ├── generic_assertions.md │ ├── index.md │ ├── overview.md │ └── valued_assertions.md │ ├── foreach.h │ └── index.md │ ├── futils.h │ └── index.md │ ├── index.md │ ├── log.h │ ├── configuration.md │ ├── index.md │ ├── log_functions.md │ └── meanings.md │ ├── test.h │ └── index.md │ └── timer.h │ └── index.md ├── examples ├── Makefile ├── callbacks.c ├── dev_example.c ├── dfs.c ├── example.c ├── hanoi.c ├── heapsort.c ├── ice_cream.c ├── sac_example.c └── sieve.c └── tests ├── Makefile ├── main.c ├── single.c ├── tostring.h ├── types.c ├── unt_bitset.h ├── unt_deque.h ├── unt_foreach.h ├── unt_hashbidimap.h ├── unt_hashmap.h ├── unt_hashmultimap.h ├── unt_hashmultiset.h ├── unt_hashset.h ├── unt_heap.h ├── unt_intervalheap.h ├── unt_linkedlist.h ├── unt_list.h ├── unt_queue.h ├── unt_sortedlist.h ├── unt_stack.h ├── unt_treemap.h ├── unt_treeset.h └── utl.h /.clang-format: -------------------------------------------------------------------------------- 1 | # clang-format 9.0.0 2 | Language: Cpp 3 | BasedOnStyle: LLVM 4 | AlignEscapedNewlines: DontAlign 5 | AllowShortFunctionsOnASingleLine: None 6 | AllowShortIfStatementsOnASingleLine: Never 7 | AllowShortLoopsOnASingleLine: false 8 | BreakBeforeBraces: Allman 9 | BreakStringLiterals: false 10 | ColumnLimit: 120 11 | Cpp11BracedListStyle: false 12 | ForEachMacros: ["CMC_FOREACH", "CMC_FOREACH_REV"] 13 | IndentCaseLabels: true 14 | IndentWidth: 4 15 | KeepEmptyLinesAtTheStartOfBlocks: true 16 | MaxEmptyLinesToKeep: 1 17 | SpacesInContainerLiterals: false 18 | TabWidth: 4 19 | UseTab: Never 20 | -------------------------------------------------------------------------------- /.clangd: -------------------------------------------------------------------------------- 1 | CompileFlags: 2 | CompilationDatabase: "cmake" 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | *.h linguist-language=C 5 | *.c linguist-language=C 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | # Bug Report template 11 | 12 | ### Describe the bug 13 | 14 | A clear and concise description of what the bug is. 15 | 16 | ### How to Reproduce 17 | 18 | Steps to reproduce the behavior: 19 | 20 | 1. Go to '...' 21 | 2. Click on '....' 22 | 3. Scroll down to '....' 23 | 4. See error 24 | 25 | ### Expected behavior 26 | 27 | A clear and concise description of what you expected to happen. 28 | 29 | ### Screenshots 30 | 31 | If applicable, add screenshots to help explain your problem. 32 | 33 | ### Please complete the following information: 34 | 35 | - Type: [e.g. src, tests, examples, benchmarks] 36 | - OS: [e.g. linux, win32] 37 | - Compiler [e.g. gcc, clang] 38 | - Compiler Version [e.g. 8.1.0] 39 | 40 | ### Additional context 41 | 42 | Add any other context about the problem here. 43 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: feature request 6 | assignees: '' 7 | 8 | --- 9 | 10 | # Feature Request Template 11 | 12 | ### What is the nature of this request? 13 | 14 | * New functionalities 15 | * Change in standards 16 | * Change in source code details (names of variables, functions, parameters, etc) 17 | 18 | ### Is your feature request related to a problem? Please describe. 19 | 20 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 21 | 22 | ### Describe the solution you'd like 23 | 24 | A clear and concise description of what you want to happen. 25 | 26 | ### Describe alternatives you've considered 27 | 28 | A clear and concise description of any alternative solutions or features you've considered. 29 | 30 | ### Additional context 31 | 32 | Add any other context or screenshots about the feature request here. 33 | -------------------------------------------------------------------------------- /.github/bug_report.md: -------------------------------------------------------------------------------- 1 | # Bug Report template 2 | 3 | ### Describe the bug 4 | 5 | A clear and concise description of what the bug is. 6 | 7 | ### How to Reproduce 8 | 9 | Steps to reproduce the behavior: 10 | 11 | 1. Go to '...' 12 | 2. Click on '....' 13 | 3. Scroll down to '....' 14 | 4. See error 15 | 16 | ### Expected behavior 17 | 18 | A clear and concise description of what you expected to happen. 19 | 20 | ### Screenshots 21 | 22 | If applicable, add screenshots to help explain your problem. 23 | 24 | ### Please complete the following information: 25 | 26 | - Type: [e.g. src, tests, examples, benchmarks] 27 | - OS: [e.g. linux, win32] 28 | - Compiler [e.g. gcc, clang] 29 | - Compiler Version [e.g. 8.1.0] 30 | 31 | ### Additional context 32 | 33 | Add any other context about the problem here. 34 | -------------------------------------------------------------------------------- /.github/feature_request.md: -------------------------------------------------------------------------------- 1 | # Feature Request Template 2 | 3 | ### What is the nature of this request? 4 | 5 | * New functionalities 6 | * Change in standards 7 | * Change in source code details (names of variables, functions, parameters, etc) 8 | 9 | ### Is your feature request related to a problem? Please describe. 10 | 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | ### Describe the solution you'd like 14 | 15 | A clear and concise description of what you want to happen. 16 | 17 | ### Describe alternatives you've considered 18 | 19 | A clear and concise description of any alternative solutions or features you've considered. 20 | 21 | ### Additional context 22 | 23 | Add any other context or screenshots about the feature request here. 24 | -------------------------------------------------------------------------------- /.github/workflows/test_suit.yml: -------------------------------------------------------------------------------- 1 | name: Test Suit 2 | 3 | on: 4 | push: 5 | branches: [ master, dev ] 6 | 7 | jobs: 8 | main: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v2 12 | - name: Setup Environment 13 | run: sudo apt install make valgrind gcc 14 | - name: Versions 15 | run: | 16 | gcc --version \ 17 | make --version \ 18 | valgrind --version \ 19 | - name: Test Suite 20 | run: make tests --always-make 21 | - name: Upload Coverage 22 | uses: codecov/codecov-action@v3 23 | with: 24 | directory: ./tests 25 | verbose: true 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | # vscode folder 55 | .vscode 56 | 57 | # Experimental folder 58 | experimental/ 59 | 60 | # Test / Code Coverage build folder 61 | build 62 | 63 | tests/coverage 64 | tests/out 65 | 66 | cmake 67 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Leonardo Vencovsky (https://github.com/LeoVen) 2 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.8) 2 | 3 | project(cmc) 4 | 5 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 6 | 7 | file(GLOB_RECURSE sources 8 | "./tests/main.c" 9 | ) 10 | 11 | add_definitions(-DCMC_CALLBACKS) 12 | 13 | add_executable(cmc ${sources}) 14 | 15 | target_include_directories(cmc PUBLIC 16 | "./" 17 | ) 18 | 19 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | Two simple steps: 4 | 5 | * Fork this repository to your github account 6 | * Clone it to your machine 7 | 8 | Now you can start working on the project. 9 | 10 | ## Open Issues 11 | 12 | Check out `bug_report.md` and `feature_request.md` templates in the `.github` folder to know how to make a bug report or feature request. 13 | 14 | ## Here are things you could work on: 15 | 16 | > You can check out the TODO file to see what I've planned on doing, in case you want to work on any of those topics 17 | 18 | 1. New features, new functions, new stuff! 19 | * Before adding anything new, please, first create an issue for it to be discussed; 20 | * Write tests to anything new you make. 21 | 2. Writing tests to try to reproduce those pesky bugs! 22 | * Anything you might think that have not been tested yet. 23 | 3. Writing the documentation. 24 | * Don't write any documentation in the source files, only in `Documentation.md`. 25 | 4. Writing cool examples. 26 | 5. Writing benchmarks 27 | 28 | ## But to do so you must: 29 | 30 | 1. Maintain the code quality 31 | * 4 space indentation; 32 | * Preferably don't clump up the code, try to make it well spaced by using empty lines; 33 | * Opening braces go underneath; 34 | * Give some sense to the variable names. 35 | 2. Maintain the structure of the project 36 | * Separate files into folders when possible; 37 | * Keep the `src` folder clean from anything that is not source code; 38 | * Create a new folder for each example with a meaningful name if it has more than one file; 39 | 3. If something looks complicated, explain it as well as possible 40 | 4. All source files (`.c`, `.h`, etc) must contain a header with the following template: 41 | * Complete file name. 42 | * First commit date in `dd/mm/yyyy`. 43 | * Authors name with (optional) link to github page. 44 | 45 | ``` 46 | /** 47 | * file_name.c 48 | * 49 | * Creation Date: dd/mm/yyyy 50 | * 51 | * Authors: 52 | * Author 1 Name (https://github.com/AuthorGithubPage) 53 | * Author 2 Name 54 | * 55 | */ 56 | ``` 57 | 58 | **Make sure your name is listed in the AUTHORS file** 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Leonardo Vencovsky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | help: 3 | @echo Welcome to the C Macro Collections Library. Targets: 4 | @echo 5 | @echo - tests: run tests 6 | @echo - tests_dev: run tests for dev 7 | @echo - format: formats the entire project 8 | @echo - bench: runs benchmarks 9 | @echo - docbuild: builds documentation to ./docs 10 | @echo - docserve: serves mdbook docs 11 | @echo - install_dev_deps: install dev dependencies 12 | 13 | tests: 14 | make -C ./tests 15 | 16 | tests_dev: 17 | make -C ./tests dev 18 | 19 | format: 20 | - find ./ -name *.h -type f | xargs clang-format --style=file --verbose -i 21 | 22 | bench: 23 | make -C ./benchmarks benchmark 24 | 25 | docbuild: 26 | mdbook build -d ../docs documentation 27 | 28 | docserve: 29 | mdbook serve documentation 30 | 31 | install_dev_deps: 32 | sudo apt install gcc make valgrind lcov clang-format hyperfine clangd cmake 33 | cargo install mdbook 34 | 35 | build_clangd: 36 | cmake -S . -G "Unix Makefiles" -B cmake 37 | 38 | .PHONY: tests tests_dev format bench docbuild docserve install_dev_deps 39 | -------------------------------------------------------------------------------- /benchmarks/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | INCLUDE=-I .. 3 | CFLAGS=-Wall -Wextra -Werror 4 | 5 | benchmark: benchmark.c 6 | $(CC) $(CFLAGS) $(INCLUDE) $< -o ./$@.exe -lm 7 | ./$@.exe 8 | 9 | clean: 10 | rm *.exe -f 11 | rm *.txt -f 12 | -------------------------------------------------------------------------------- /benchmarks/comparisons/hashtable/Makefile: -------------------------------------------------------------------------------- 1 | INCLUDE=-I ../../.. 2 | CFLAGS=-O3 -Wall -Wextra -Werror -DCOUNT=20000000 -DCMC_NO_CALLBACKS 3 | 4 | # Install hyperfine 5 | # https://github.com/sharkdp/hyperfine 6 | 7 | run: build 8 | hyperfine --warmup 2 --runs 5 './hashmap.exe' 9 | hyperfine --warmup 2 --runs 5 './hashmultimap.exe' 10 | hyperfine --warmup 2 --runs 5 './unorderedmap.exe' 11 | 12 | build: 13 | gcc hashmap.c -o hashmap.exe $(INCLUDE) $(CFLAGS) 14 | gcc hashmultimap.c -o hashmultimap.exe $(INCLUDE) $(CFLAGS) 15 | g++ unorderedmap.cpp $(INCLUDE) -o unorderedmap.exe $(CFLAGS) 16 | 17 | clean: 18 | rm *.exe -f 19 | rm *.txt -f 20 | -------------------------------------------------------------------------------- /benchmarks/comparisons/hashtable/hashmap.c: -------------------------------------------------------------------------------- 1 | #ifndef COUNT 2 | #error "Please define COUNT" 3 | #endif 4 | 5 | #include "murmurhash.h" 6 | 7 | #include "../../util/twister.c" 8 | 9 | #define K uint64_t 10 | #define V uint64_t 11 | #define PFX hm 12 | #define SNAME hashmap 13 | #include "cmc/hashmap.h" 14 | 15 | int cmp(uint64_t a, uint64_t b) 16 | { 17 | return (a > b) - (a < b); 18 | } 19 | 20 | // size_t hash(uint64_t a) 21 | // { 22 | // size_t x = (size_t)a; 23 | // x = ((x >> 16) ^ x) * 0x45d9f3b; 24 | // x = ((x >> 16) ^ x) * 0x45d9f3b; 25 | // x = (x >> 16) ^ x; 26 | // return x; 27 | // } 28 | 29 | size_t hash(uint64_t k) 30 | { 31 | static size_t seed = 211106232532969; 32 | return MurMurHash(&k, sizeof(k), seed); 33 | } 34 | 35 | struct hashmap_fkey *fkey = &(struct hashmap_fkey){ .cmp = cmp, .hash = hash, NULL }; 36 | 37 | struct hashmap_fval *fval = &(struct hashmap_fval){ .cmp = cmp, .hash = hash, NULL }; 38 | 39 | int main(void) 40 | { 41 | mt_state tw; 42 | twist_init(&tw, 50331653); 43 | 44 | struct hashmap *map = hm_new(COUNT, 0.6, fkey, fval); 45 | 46 | for (uint64_t i = 0; i < COUNT; i++) 47 | { 48 | uint64_t v = twist_uint64(&tw); 49 | hm_insert(map, v, v); 50 | } 51 | 52 | hm_free(map); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /benchmarks/comparisons/hashtable/hashmultimap.c: -------------------------------------------------------------------------------- 1 | #ifndef COUNT 2 | #error "Please define COUNT" 3 | #endif 4 | 5 | #include "murmurhash.h" 6 | 7 | #include "../../util/twister.c" 8 | 9 | #define K uint64_t 10 | #define V uint64_t 11 | #define PFX hmm 12 | #define SNAME hashmultimap 13 | #include "cmc/hashmultimap.h" 14 | 15 | int cmp(uint64_t a, uint64_t b) 16 | { 17 | return (a > b) - (a < b); 18 | } 19 | 20 | // size_t hash(uint64_t a) 21 | // { 22 | // size_t x = (size_t)a; 23 | // x = ((x >> 16) ^ x) * 0x45d9f3b; 24 | // x = ((x >> 16) ^ x) * 0x45d9f3b; 25 | // x = (x >> 16) ^ x; 26 | // return x; 27 | // } 28 | 29 | size_t hash(uint64_t k) 30 | { 31 | static size_t seed = 211106232532969; 32 | return MurMurHash(&k, sizeof(k), seed); 33 | } 34 | 35 | struct hashmultimap_fkey *fkey = &(struct hashmultimap_fkey){ .cmp = cmp, .hash = hash, NULL }; 36 | 37 | struct hashmultimap_fval *fval = &(struct hashmultimap_fval){ .cmp = cmp, .hash = hash, NULL }; 38 | 39 | int main(void) 40 | { 41 | mt_state tw; 42 | twist_init(&tw, 50331653); 43 | 44 | struct hashmultimap *map = hmm_new(COUNT, 0.6, fkey, fval); 45 | 46 | for (uint64_t i = 0; i < COUNT; i++) 47 | { 48 | uint64_t v = twist_uint64(&tw); 49 | hmm_insert(map, v, v); 50 | } 51 | 52 | hmm_free(map); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /benchmarks/comparisons/hashtable/murmurhash.h: -------------------------------------------------------------------------------- 1 | // https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/hash_bytes.cc 2 | 3 | #ifndef MURMURHASH 4 | #define MURMURHASH 5 | 6 | #include 7 | 8 | inline size_t unaligned_load(const char *p) 9 | { 10 | size_t result; 11 | memcpy(&result, p, sizeof(result)); 12 | return result; 13 | } 14 | 15 | inline size_t load_bytes(const char *p, int n) 16 | { 17 | size_t result = 0; 18 | --n; 19 | do 20 | { 21 | result = (result << 8) + (unsigned char)(p[n]); 22 | } while (--n >= 0); 23 | return result; 24 | } 25 | 26 | inline size_t shift_mix(size_t v) 27 | { 28 | return v ^ (v >> 47); 29 | } 30 | 31 | size_t MurMurHash(const void *ptr, size_t len, size_t seed) 32 | { 33 | static const size_t mul = (((size_t)0xc6a4a793UL) << 32UL) + (size_t)0x5bd1e995UL; 34 | const char *const buf = (const char *)(ptr); 35 | 36 | // Remove the bytes not divisible by the sizeof(size_t). This 37 | // allows the main loop to process the data as 64-bit integers. 38 | const size_t len_aligned = len & ~(size_t)0x7; 39 | const char *const end = buf + len_aligned; 40 | size_t hash = seed ^ (len * mul); 41 | for (const char *p = buf; p != end; p += 8) 42 | { 43 | const size_t data = shift_mix(unaligned_load(p) * mul) * mul; 44 | hash ^= data; 45 | hash *= mul; 46 | } 47 | if ((len & 0x7) != 0) 48 | { 49 | const size_t data = load_bytes(end, len & 0x7); 50 | hash ^= data; 51 | hash *= mul; 52 | } 53 | hash = shift_mix(hash) * mul; 54 | hash = shift_mix(hash); 55 | return hash; 56 | } 57 | 58 | #endif /* MURMURHASH */ 59 | -------------------------------------------------------------------------------- /benchmarks/comparisons/hashtable/unorderedmap.cpp: -------------------------------------------------------------------------------- 1 | #ifndef COUNT 2 | #error "Please define COUNT" 3 | #endif 4 | 5 | #include "murmurhash.h" 6 | 7 | #include 8 | 9 | #include "../../util/twister.c" 10 | 11 | struct IntHasher 12 | { 13 | std::size_t operator()(const uint64_t &k) const 14 | { 15 | static size_t seed = 211106232532969; 16 | return MurMurHash(&k, sizeof(k), seed); 17 | } 18 | }; 19 | 20 | int main(void) 21 | { 22 | mt_state tw; 23 | twist_init(&tw, 50331653); 24 | 25 | std::unordered_map map(COUNT); 26 | 27 | for (uint64_t i = 0; i < COUNT; i++) 28 | { 29 | uint64_t v = twist_uint64(&tw); 30 | map.insert({ v, v }); 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /benchmarks/util/twister.c: -------------------------------------------------------------------------------- 1 | /** 2 | * twister.c 3 | * 4 | * Creation Date: 09/05/2019 5 | * 6 | * Authors: 7 | * Leonardo Vencovsky (https://github.com/LeoVen) 8 | * 9 | * Mersenne Twister PRNG utility 10 | * 11 | */ 12 | #include 13 | #include 14 | #include 15 | 16 | #define NN 312 17 | #define MM 156 18 | #define MATRIX_A UINT64_C(0xB5026F5AA96619E9) 19 | #define UM UINT64_C(0xFFFFFFFF80000000) // Most significant 33 bits 20 | #define LM UINT64_C(0x7FFFFFFF) // Least significant 31 bits 21 | 22 | // State vector 23 | typedef struct mt_state_s 24 | { 25 | uint64_t mt[NN]; 26 | int mti; 27 | } mt_state, *mt_state_ptr; 28 | 29 | // Initializes mt[NN] with a seed 30 | void twist_init(mt_state_ptr st, uint64_t seed); 31 | 32 | // Generates a random uint64_t in [0, 2^64-1] interval 33 | uint64_t twist_uint64(mt_state_ptr st); 34 | 35 | // Generates a double in [0, 1) interval 36 | double twist_real(mt_state_ptr st); 37 | 38 | // 39 | // Implementation 40 | // 41 | 42 | // Initialize a state mt[NN] with a seed 43 | void twist_init(mt_state_ptr st, uint64_t seed) 44 | { 45 | st->mti = NN + 1; 46 | 47 | st->mt[0] = seed; 48 | 49 | for (st->mti = 1; st->mti < NN; st->mti++) 50 | { 51 | st->mt[st->mti] = (UINT64_C(6364136223846793005) * (st->mt[st->mti - 1] ^ (st->mt[st->mti - 1] >> 62)) + st->mti); 52 | } 53 | } 54 | 55 | // Generates a random uint64_t in [0, 2^64-1] interval 56 | uint64_t twist_uint64(mt_state_ptr st) 57 | { 58 | int i; 59 | uint64_t x; 60 | static uint64_t mag01[2] = {UINT64_C(0), MATRIX_A}; 61 | 62 | if (st->mti >= NN) 63 | { 64 | // Generate NN words at one time if twist_init() has not been called 65 | // A default initial seed is used 66 | if (st->mti == NN + 1) 67 | twist_init(st, UINT64_C(5489)); 68 | 69 | for (i = 0; i < NN - MM; i++) 70 | { 71 | x = (st->mt[i] & UM) | (st->mt[i + 1] & LM); 72 | st->mt[i] = st->mt[i + MM] ^ (x >> 1) ^ mag01[(int)(x & UINT64_C(1))]; 73 | } 74 | 75 | for (; i < NN - 1; i++) 76 | { 77 | x = (st->mt[i] & UM) | (st->mt[i + 1] & LM); 78 | st->mt[i] = st->mt[i + (MM - NN)] ^ (x >> 1) ^ mag01[(int)(x & UINT64_C(1))]; 79 | } 80 | 81 | x = (st->mt[NN - 1] & UM) | (st->mt[0] & LM); 82 | 83 | st->mt[NN - 1] = st->mt[MM - 1] ^ (x >> 1) ^ mag01[(int)(x & UINT64_C(1))]; 84 | 85 | st->mti = 0; 86 | } 87 | 88 | x = st->mt[st->mti++]; 89 | 90 | x ^= (x >> 29) & UINT64_C(0x5555555555555555); 91 | x ^= (x << 17) & UINT64_C(0x71D67FFFEDA60000); 92 | x ^= (x << 37) & UINT64_C(0xFFF7EEE000000000); 93 | x ^= (x >> 43); 94 | 95 | return x; 96 | } 97 | 98 | // Generates a double in [0, 1) interval 99 | double twist_real(mt_state_ptr st) 100 | { 101 | return (twist_uint64(st) >> 11) * (1.0 / 9007199254740992.0); 102 | } 103 | -------------------------------------------------------------------------------- /book.toml: -------------------------------------------------------------------------------- 1 | [book] 2 | authors = ["Leonardo Vencovsky"] 3 | language = "en" 4 | multilingual = false 5 | src = "documentation/" 6 | title = "C Macro Collections" 7 | 8 | [build] 9 | build-dir = "docs/" 10 | 11 | [preprocessor.index] 12 | -------------------------------------------------------------------------------- /cmc/bitset/ext/code.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * INIT 27 | * 28 | * The part 'INIT' gives a new way of initializing a collection. The collection 29 | * struct is not heap allocated, only its internal structure (nodes, buffers). 30 | */ 31 | #ifdef CMC_EXT_INIT 32 | 33 | #endif /* CMC_EXT_INIT */ 34 | 35 | /** 36 | * ITER 37 | * 38 | * Bitset bi-directional iterator. 39 | */ 40 | #ifdef CMC_EXT_ITER 41 | 42 | /* Iterator Functions */ 43 | /* Iterator Initialization */ 44 | /* Iterator State */ 45 | /* Iterator Movement */ 46 | /* Iterator Access */ 47 | 48 | #endif /* CMC_EXT_ITER */ 49 | 50 | /** 51 | * STR 52 | * 53 | * Print helper functions. 54 | */ 55 | #ifdef CMC_EXT_STR 56 | 57 | #endif /* CMC_EXT_STR */ 58 | -------------------------------------------------------------------------------- /cmc/bitset/ext/header.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * INIT 27 | * 28 | * The part 'INIT' gives a new way of initializing a collection. The collection 29 | * struct is not heap allocated, only its internal structure (nodes, buffers). 30 | */ 31 | #ifdef CMC_EXT_INIT 32 | 33 | #endif /* CMC_EXT_INIT */ 34 | 35 | /** 36 | * ITER 37 | * 38 | * Bitset bi-directional iterator. 39 | */ 40 | #ifdef CMC_EXT_ITER 41 | 42 | /* Iterator Functions */ 43 | /* Iterator Initialization */ 44 | /* Iterator State */ 45 | /* Iterator Movement */ 46 | /* Iterator Access */ 47 | 48 | #endif /* CMC_EXT_ITER */ 49 | 50 | /** 51 | * STR 52 | * 53 | * Print helper functions. 54 | */ 55 | #ifdef CMC_EXT_STR 56 | 57 | #endif /* CMC_EXT_STR */ 58 | -------------------------------------------------------------------------------- /cmc/bitset/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Bitset bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* BitSet Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target bitset */ 36 | struct SNAME *target; 37 | /* Cursor's position (index) */ 38 | size_t cursor; 39 | /* If the iterator has reached the start of the iteration */ 40 | bool start; 41 | /* If the iterator has reached the end of the iteration */ 42 | bool end; 43 | }; 44 | 45 | #endif /* CMC_EXT_ITER */ 46 | -------------------------------------------------------------------------------- /cmc/bitset/header.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* Collection Allocation and Deallocation */ 26 | struct SNAME *CMC_(PFX, _new)(size_t n_bits); 27 | struct SNAME *CMC_(PFX, _new_custom)(size_t n_bits, CMC_ALLOC_TYPE alloc, CMC_CALLBACK_TYPE callbacks); 28 | struct SNAME CMC_(PFX, _init)(size_t n_bits); 29 | struct SNAME CMC_(PFX, _init_custom)(size_t n_bits, CMC_ALLOC_TYPE alloc, CMC_CALLBACK_TYPE callbacks); 30 | void CMC_(PFX, _free)(struct SNAME *_bitset_); 31 | void CMC_(PFX, _release)(struct SNAME _bitset_); 32 | void CMC_(PFX, _customize)(struct SNAME *_bitset_, CMC_ALLOC_TYPE alloc, CMC_CALLBACK_TYPE callbacks); 33 | /* Collection Input and Output */ 34 | bool CMC_(PFX, _set)(struct SNAME *_bitset_, size_t bit_index); 35 | bool CMC_(PFX, _set_range)(struct SNAME *_bitset_, size_t from, size_t to); 36 | bool CMC_(PFX, _clear)(struct SNAME *_bitset_, size_t bit_index); 37 | bool CMC_(PFX, _clear_range)(struct SNAME *_bitset_, size_t from, size_t to); 38 | bool CMC_(PFX, _flip)(struct SNAME *_bitset_, size_t bit_index); 39 | bool CMC_(PFX, _flip_range)(struct SNAME *_bitset_, size_t from, size_t to); 40 | bool CMC_(PFX, _put)(struct SNAME *_bitset_, size_t bit_index, bool state); 41 | bool CMC_(PFX, _put_range)(struct SNAME *_bitset_, size_t from, size_t to, bool state); 42 | bool CMC_(PFX, _set_all)(struct SNAME *_bitset_); 43 | bool CMC_(PFX, _clear_all)(struct SNAME *_bitset_); 44 | bool CMC_(PFX, _flip_all)(struct SNAME *_bitset_); 45 | /* Element Access */ 46 | bool CMC_(PFX, _get)(struct SNAME *_bitset_, size_t bit_index); 47 | /* Collection State */ 48 | /* Collection Utility */ 49 | bool CMC_(PFX, _resize)(struct SNAME *_bitset_, size_t n_bits); 50 | -------------------------------------------------------------------------------- /cmc/bitset/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* BitSet Structure */ 26 | struct SNAME 27 | { 28 | /* Dynamic Array of Bits */ 29 | cmc_bitset_word *buffer; 30 | /* Current array capacity */ 31 | size_t capacity; 32 | /* Currently used bits */ 33 | /* This should always be true: */ 34 | /* capacity - count < sizeof(cmc_bitset_word) * CHAR_BIT */ 35 | size_t count; 36 | /* Flags indicating errors or success */ 37 | int flag; 38 | /* Custom allocation functions */ 39 | CMC_ALLOC_TYPE alloc; 40 | /* Custom callback functions */ 41 | CMC_CALLBACKS_DECL; 42 | }; 43 | -------------------------------------------------------------------------------- /cmc/cor/alloc.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * alloc.h 27 | * 28 | * Creation Date: 24/05/2020 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | #ifndef CMC_COR_ALLOC_H 36 | #define CMC_COR_ALLOC_H 37 | 38 | #ifndef CMC_NO_ALLOC 39 | 40 | #ifdef CMC_CAMEL_CASE 41 | #define CMC_ALLOC_NODE_NAME CMCAllocNode 42 | #else 43 | #define CMC_ALLOC_NODE_NAME cmc_alloc_node 44 | #endif 45 | 46 | #include "core.h" 47 | /** 48 | * struct cmc_alloc_node 49 | * 50 | * Custom allocation node. Allows collections to use custom allocation 51 | * functions. The cmc_alloc_node_default is the default heap allocator provided 52 | * by the standard library. 53 | */ 54 | 55 | #define CMC_ALLOC_TYPE struct CMC_ALLOC_NODE_NAME * 56 | 57 | static struct CMC_ALLOC_NODE_NAME 58 | { 59 | void *(*malloc)(size_t); 60 | void *(*calloc)(size_t, size_t); 61 | void *(*realloc)(void *, size_t); 62 | void (*free)(void *); 63 | } CMC_UNUSED cmc_alloc_node_default = { malloc, calloc, realloc, free }; 64 | 65 | #endif /* CMC_NO_ALLOC */ 66 | 67 | #endif /* CMC_COR_ALLOC_H */ 68 | -------------------------------------------------------------------------------- /cmc/cor/bitset.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * bitset.h 27 | * 28 | * Creation Date: 20/05/2020 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * Things commonly used by bitset collections. 37 | */ 38 | 39 | #ifndef CMC_COR_BITSET_H 40 | #define CMC_COR_BITSET_H 41 | 42 | #include "core.h" 43 | 44 | /* Defines the underlaying data type for the bitset array */ 45 | /* Possible values: uint64_t, uint32_t, uint16_t, uint8_t, size_t */ 46 | #ifndef CMC_BITSET_WORD_TYPE 47 | #define CMC_BITSET_WORD_TYPE uint32_t 48 | #endif /* CMC_BITSET_WORD_TYPE */ 49 | 50 | typedef CMC_BITSET_WORD_TYPE cmc_bitset_word; 51 | 52 | /** 53 | * cmc_bidx_to_widx 54 | * 55 | * Translates a bit index to a word index 56 | */ 57 | static inline size_t cmc_bidx_to_widx(size_t idx) 58 | { 59 | /* Calculate how many shifts based on the size of cmc_bitset_word */ 60 | static const size_t shift = ((sizeof(cmc_bitset_word) * 8) >> 6) > 0 ? 6 61 | : ((sizeof(cmc_bitset_word) * 8) >> 5) > 0 ? 5 62 | : ((sizeof(cmc_bitset_word) * 8) >> 4) > 0 ? 4 63 | : 3; 64 | 65 | return idx >> shift; 66 | } 67 | 68 | #endif /* CMC_COR_BITSET_H */ 69 | -------------------------------------------------------------------------------- /cmc/cor/callbacks.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * callbacks.h 27 | * 28 | * Creation Date: 24/05/2020 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | #ifndef CMC_COR_CALLBACKS_H 36 | #define CMC_COR_CALLBACKS_H 37 | 38 | typedef void (*cmc_callback)(const char func_name[], void *self); 39 | #define CMC_CALLBACK_TYPE cmc_callback 40 | 41 | #ifdef CMC_CALLBACKS 42 | 43 | #define CMC_CALLBACKS_DECL cmc_callback callbacks 44 | #define CMC_CALLBACKS_MAYBE_UNUSED(param) 45 | #define CMC_CALLBACKS_GET(ds) (ds)->callbacks 46 | #define CMC_CALLBACKS_ASSIGN(ds, cb) CMC_CALLBACKS_GET(ds) = cb 47 | #define CMC_CALLBACKS_CALL(ds) \ 48 | if (CMC_CALLBACKS_GET(ds)) \ 49 | CMC_CALLBACKS_GET(ds)(__func__, (void *)ds); 50 | 51 | #else 52 | 53 | #define CMC_CALLBACKS_DECL 54 | #define CMC_CALLBACKS_MAYBE_UNUSED(param) CMC_UNUSED_PARAM(param) /* Prevent -Wunused-parameter */ 55 | #define CMC_CALLBACKS_GET(ds) NULL /* Helps with compatibility */ 56 | #define CMC_CALLBACKS_ASSIGN(ds, cb) 57 | #define CMC_CALLBACKS_CALL(ds) 58 | 59 | #endif 60 | 61 | #endif /* CMC_COR_CALLBACKS_H */ 62 | -------------------------------------------------------------------------------- /cmc/cor/dev.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * dev.h 27 | * 28 | * Creation Date: 01/02/2023 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | #ifndef CMC_COR_DEV_H 36 | #define CMC_COR_DEV_H 37 | 38 | /** 39 | * Macros for tracing and debugging data structures when CMC_DEV is enabled 40 | */ 41 | 42 | #ifndef CMC_DEV_FCALL 43 | #define CMC_DEV_FCALL cmc_log_trace("fcall") 44 | #endif 45 | 46 | #endif /* CMC_COR_DEV_H */ 47 | -------------------------------------------------------------------------------- /cmc/cor/flags.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * flags.h 27 | * 28 | * Creation Date: 14/05/2020 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | #ifndef CMC_COR_FLAGS_H 36 | #define CMC_COR_FLAGS_H 37 | 38 | #include "core.h" 39 | 40 | /** 41 | * enum cmc_flags 42 | * 43 | * Defines common error codes used by the entire library. 44 | */ 45 | // clang-format off 46 | enum cmc_flags 47 | { 48 | CMC_FLAG_OK = 0, // No errors 49 | CMC_FLAG_ALLOC = 1, // Allocation failed 50 | CMC_FLAG_EMPTY = 2, // The collection is empty when it should not 51 | CMC_FLAG_NOT_FOUND = 3, // Key or value not found 52 | CMC_FLAG_INVALID = 4, // Invalid argument or operation 53 | CMC_FLAG_RANGE = 5, // Index out of range 54 | CMC_FLAG_DUPLICATE = 6, // Duplicate key or value 55 | CMC_FLAG_ERROR = 7, // Generic error, usually caused by algorithm error 56 | CMC_FLAG_THREAD = 8, // Generic error regarding threads 57 | CMC_FLAG_MUTEX = 9, // Generic error regarding mutexes 58 | CMC_FLAG_FULL = 10, // When a collection that doesn't resize is full 59 | CMC_FLAG_FTABLE = 11, // When a required ftable function is missing 60 | }; 61 | // clang-format on 62 | 63 | /** 64 | * cmc_flags_to_str 65 | * 66 | * Maps the error codes to their character representation. 67 | */ 68 | CMC_UNUSED static const char *cmc_flags_to_str[12] = { 69 | "OK", "ALLOC", "EMPTY", "NOT_FOUND", "INVALID", "RANGE", "DUPLICATE", "ERROR", "THREAD", "MUTEX", "FULL", "FTABLE" 70 | }; 71 | 72 | #endif /* CMC_COR_FLAGS_H */ 73 | -------------------------------------------------------------------------------- /cmc/cor/ftable.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ftable.h 27 | * 28 | * Creation Date: 27/05/2020 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * Type definitions for Functions Tables. 37 | */ 38 | 39 | #ifndef CMC_COR_FTABLE_H 40 | #define CMC_COR_FTABLE_H 41 | 42 | /** 43 | * Where T can either be K or V 44 | */ 45 | #define CMC_DEF_FTAB_CMP(T) int (*cmp)(T, T) 46 | #define CMC_DEF_FTAB_CPY(T) T (*cpy)(T) 47 | #define CMC_DEF_FTAB_STR(T) bool (*str)(FILE *, T) 48 | #define CMC_DEF_FTAB_FREE(T) void (*free)(T) 49 | #define CMC_DEF_FTAB_HASH(T) size_t (*hash)(T) 50 | #define CMC_DEF_FTAB_PRI(T) int (*pri)(T, T) 51 | 52 | #endif /* CMC_COR_FTABLE_H */ 53 | -------------------------------------------------------------------------------- /cmc/cor/heap.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * heap.h 27 | * 28 | * Creation Date: 01/06/2020 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * Things commonly used by heap collections. 37 | */ 38 | 39 | #ifndef CMC_COR_HEAP_H 40 | #define CMC_COR_HEAP_H 41 | 42 | /** 43 | * enum cmc_heap_order 44 | * 45 | * Defines the two possible heaps: 46 | * - Max Heap has the greatest element at the top 47 | * - Min Heap has the smallest element at the top 48 | */ 49 | enum cmc_heap_order 50 | { 51 | CMC_MAX_HEAP = 1, 52 | CMC_MIN_HEAP = -1 53 | }; 54 | 55 | #endif /* CMC_COR_HEAP_H */ 56 | -------------------------------------------------------------------------------- /cmc/cor/undef.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * undef.h 27 | * 28 | * Creation Date: 12/01/2023 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * Common undefs 37 | */ 38 | 39 | /* You never want these to fallthrough since they should be unique for each collection */ 40 | #undef PFX 41 | #undef SNAME 42 | 43 | #ifndef CMC_ARGS_FALLTHROUGH 44 | 45 | #undef CMC_DEV 46 | #undef CMC_SAC 47 | 48 | #ifndef CMC_ARGS_KEY_FALLTHROUGH 49 | #undef K 50 | #endif 51 | 52 | #ifndef CMC_ARGS_VAL_FALLTHROUGH 53 | #undef V 54 | #endif 55 | 56 | #undef SIZE 57 | 58 | #ifndef CMC_EXT_FALLTHROUGH 59 | #undef CMC_EXT_INIT 60 | #undef CMC_EXT_ITER 61 | #undef CMC_EXT_NODE 62 | #undef CMC_EXT_SEQ 63 | #undef CMC_EXT_SETF 64 | #undef CMC_EXT_STR 65 | #endif 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /cmc/deque/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Deque bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* Deque Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target deque */ 36 | struct SNAME *target; 37 | /* Cursor's position (index) */ 38 | size_t cursor; 39 | /* Keeps track of relative index to the iteration of elements */ 40 | size_t index; 41 | /* If the iterator has reached the start of the iteration */ 42 | bool start; 43 | /* If the iterator has reached the end of the iteration */ 44 | bool end; 45 | }; 46 | 47 | #endif /* CMC_EXT_ITER */ 48 | -------------------------------------------------------------------------------- /cmc/deque/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | struct SNAME 26 | { 27 | /* Dynamic circular array of elements */ 28 | V *buffer; 29 | /* Current circular array capacity */ 30 | size_t capacity; 31 | /* Current amount of elements */ 32 | size_t count; 33 | /* Index representing the front of the deque */ 34 | size_t front; 35 | /* Index representing the back of the deque */ 36 | size_t back; 37 | /* Flags indicating errors or success */ 38 | int flag; 39 | /* Functions Table */ 40 | struct CMC_DEF_FVAL(SNAME) * f_val; 41 | /* Custom allocation functions */ 42 | CMC_ALLOC_TYPE alloc; 43 | /* Custom callback functions */ 44 | CMC_CALLBACKS_DECL; 45 | }; 46 | -------------------------------------------------------------------------------- /cmc/hashbidimap.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * hashbidimap.h 27 | * 28 | * Creation Date: 26/09/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * HashBidiMap 37 | * 38 | * A bidirectional map is a map that allows you to create a bijection in both 39 | * directions between two sets of elements (K <-> V). It is possible to retrieve 40 | * the value using a key or retrieve a key using a value. The naming (key and 41 | * value) is simply used to differentiate between one set of elements and the 42 | * other set of elements. 43 | * 44 | * Implementation 45 | * 46 | * This implementation uses two arrays of pointers to an entry containing both 47 | * the key and the value. 48 | */ 49 | 50 | #include "cor/core.h" 51 | #include "cor/hashtable.h" 52 | 53 | #ifdef CMC_DEV 54 | #include "utl/log.h" 55 | #endif 56 | 57 | /** 58 | * Used values 59 | * K - hashbidimap key data type 60 | * V - hashbidimap value data type 61 | * SNAME - struct name and prefix of other related structs 62 | * PFX - functions prefix 63 | */ 64 | 65 | /* Structs definition */ 66 | #include "cmc/hashbidimap/struct.h" 67 | 68 | /* Function declaration */ 69 | #include "cmc/hashbidimap/header.h" 70 | 71 | /* Function implementation */ 72 | #include "cmc/hashbidimap/code.h" 73 | 74 | /** 75 | * Extensions 76 | * 77 | * INIT - Initializes the struct on the stack 78 | * ITER - Hashbidimap iterator 79 | * STR - Print helper functions 80 | */ 81 | #define CMC_EXT_HASHBIDIMAP_PARTS INIT, ITER, STR 82 | /**/ 83 | #include "cmc/hashbidimap/ext/struct.h" 84 | /**/ 85 | #include "cmc/hashbidimap/ext/header.h" 86 | /**/ 87 | #include "cmc/hashbidimap/ext/code.h" 88 | 89 | #include "cor/undef.h" 90 | -------------------------------------------------------------------------------- /cmc/hashbidimap/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Hashbidimap bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* HashBidiMap Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target hashbidimap */ 36 | struct SNAME *target; 37 | /* Cursor's position (index) */ 38 | size_t cursor; 39 | /* Keeps track of relative index to the iteration of elements */ 40 | size_t index; 41 | /* The index of the first element */ 42 | size_t first; 43 | /* The index of the last element */ 44 | size_t last; 45 | /* If the iterator has reached the start of the iteration */ 46 | bool start; 47 | /* If the iterator has reached the end of the iteration */ 48 | bool end; 49 | }; 50 | 51 | #endif /* CMC_EXT_ITER */ 52 | -------------------------------------------------------------------------------- /cmc/hashbidimap/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | struct SNAME 26 | { 27 | /* Array 0 is K -> V and array 1 is V -> K */ 28 | struct CMC_DEF_ENTRY(SNAME) * (*buffer)[2]; 29 | /* Current arrays capacity */ 30 | size_t capacity; 31 | /* Current amount of keys */ 32 | size_t count; 33 | /* Load factor in range (0.0, 1.0) */ 34 | double load; 35 | /* Flags indicating errors or success */ 36 | int flag; 37 | /* Key function table */ 38 | struct CMC_DEF_FKEY(SNAME) * f_key; 39 | /* Value function table */ 40 | struct CMC_DEF_FVAL(SNAME) * f_val; 41 | /* Custom allocation functions */ 42 | CMC_ALLOC_TYPE alloc; 43 | /* Custom callback functions */ 44 | CMC_CALLBACKS_DECL; 45 | /* Methods */ 46 | /* Returns an iterator to the start of the hashbidimap */ 47 | struct CMC_DEF_ITER(SNAME) (*it_start)(struct SNAME *); 48 | /* Returns an iterator to the end of the hashbidimap */ 49 | struct CMC_DEF_ITER(SNAME) (*it_end)(struct SNAME *); 50 | }; 51 | 52 | /* HashBidiMap Entry */ 53 | struct CMC_DEF_ENTRY(SNAME) 54 | { 55 | /* Entry Key */ 56 | K key; 57 | /* Entry Value */ 58 | V value; 59 | /* The distance of this node to its original position */ 60 | /* dist[0] is relative to K -> V */ 61 | /* dist[1] is relative to V -> K */ 62 | size_t dist[2]; 63 | /* References to this node in the hashbidimap buffer. Used to */ 64 | /* prevent searching for this node twice for update() and */ 65 | /* remove(). Increases memory overhead but reduces execution time */ 66 | /* ref[0] is relative to K -> V */ 67 | /* ref[1] is relative to V -> K */ 68 | struct CMC_DEF_ENTRY(SNAME) * *ref[2]; 69 | }; 70 | -------------------------------------------------------------------------------- /cmc/hashmap.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * hashmap.h 27 | * 28 | * Creation Date: 03/04/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * HashMap 37 | * 38 | * A HashMap is an implementation of a Map with unique keys, where every key is 39 | * mapped to a value. The keys are not sorted. It is implemented as a flat 40 | * hashtable with linear probing and robin hood hashing. 41 | */ 42 | 43 | #include "cor/core.h" 44 | #include "cor/hashtable.h" 45 | 46 | #ifdef CMC_DEV 47 | #include "utl/log.h" 48 | #endif 49 | 50 | /** 51 | * Used values 52 | * K - hashmap key data type 53 | * V - hashmap value data type 54 | * SNAME - struct name and prefix of other related structs 55 | * PFX - functions prefix 56 | */ 57 | 58 | /* Structs definition */ 59 | #include "cmc/hashmap/struct.h" 60 | 61 | /* Function declaration */ 62 | #include "cmc/hashmap/header.h" 63 | 64 | /* Function implementation */ 65 | #include "cmc/hashmap/code.h" 66 | 67 | /** 68 | * Extensions 69 | * 70 | * INIT - Initializes the struct on the stack 71 | * ITER - hashmap iterator 72 | * STR - Print helper functions 73 | */ 74 | #define CMC_EXT_HASHMAP_PARTS INIT, ITER, STR 75 | /**/ 76 | #include "cmc/hashmap/ext/struct.h" 77 | /**/ 78 | #include "cmc/hashmap/ext/header.h" 79 | /**/ 80 | #include "cmc/hashmap/ext/code.h" 81 | 82 | #include "cor/undef.h" 83 | -------------------------------------------------------------------------------- /cmc/hashmap/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Hashmap bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* HashMap Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target hashmap */ 36 | struct SNAME *target; 37 | /* Cursor's position (index) */ 38 | size_t cursor; 39 | /* Keeps track of relative index to the iteration of elements */ 40 | size_t index; 41 | /* The index of the first element */ 42 | size_t first; 43 | /* The index of the last element */ 44 | size_t last; 45 | /* If the iterator has reached the start of the iteration */ 46 | bool start; 47 | /* If the iterator has reached the end of the iteration */ 48 | bool end; 49 | }; 50 | 51 | #endif /* CMC_EXT_ITER */ 52 | -------------------------------------------------------------------------------- /cmc/hashmap/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* HashMap Structure */ 26 | struct SNAME 27 | { 28 | /* Array of Entries */ 29 | struct CMC_DEF_ENTRY(SNAME) * buffer; 30 | /* Current array capacity */ 31 | size_t capacity; 32 | /* Current amount of keys */ 33 | size_t count; 34 | /* Load factor in range (0.0, 1.0) */ 35 | double load; 36 | /* Flags indicating errors or success */ 37 | int flag; 38 | /* Key function table */ 39 | struct CMC_DEF_FKEY(SNAME) * f_key; 40 | /* Value function table */ 41 | struct CMC_DEF_FVAL(SNAME) * f_val; 42 | /* Custom allocation functions */ 43 | CMC_ALLOC_TYPE alloc; 44 | /* Custom callback functions */ 45 | CMC_CALLBACKS_DECL; 46 | }; 47 | 48 | /* HashMap Entry */ 49 | struct CMC_DEF_ENTRY(SNAME) 50 | { 51 | /* Entry Key */ 52 | K key; 53 | /* Entry Value */ 54 | V value; 55 | /* The distance of this node to its original position, used by */ 56 | /* robin-hood hashing */ 57 | size_t dist; 58 | /* The sate of this node (DELETED, EMPTY, FILLED) */ 59 | enum cmc_entry_state state; 60 | }; 61 | -------------------------------------------------------------------------------- /cmc/hashmultimap/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Hashmultimap bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* HashMultiMap Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target hashmultimap */ 36 | struct SNAME *target; 37 | /* Current entry */ 38 | struct CMC_DEF_ENTRY(SNAME) * curr_entry; 39 | /* Cursor`s position (index) */ 40 | size_t cursor; 41 | /* Keeps track of relative index to the iteration of elements */ 42 | size_t index; 43 | /* The index of the first element */ 44 | size_t first; 45 | /* The index of the last element */ 46 | size_t last; 47 | /* If the iterator has reached the start of the iteration */ 48 | bool start; 49 | /* If the iterator has reached the end of the iteration */ 50 | bool end; 51 | }; 52 | 53 | #endif /* CMC_EXT_ITER */ 54 | -------------------------------------------------------------------------------- /cmc/hashmultimap/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* HashMultimap Structure */ 26 | struct SNAME 27 | { 28 | /* Array of linked list to entries */ 29 | struct CMC_DEF_ENTRY(SNAME) * (*buffer)[2]; 30 | /* Current array capacity */ 31 | size_t capacity; 32 | /* Current amount of keys */ 33 | size_t count; 34 | /* Load factor in range (0.0, infinity) */ 35 | double load; 36 | /* Flags indicating errors or success */ 37 | int flag; 38 | /* Key function table */ 39 | struct CMC_DEF_FKEY(SNAME) * f_key; 40 | /* Value function table */ 41 | struct CMC_DEF_FVAL(SNAME) * f_val; 42 | /* Custom allocation functions */ 43 | CMC_ALLOC_TYPE alloc; 44 | /* Custom callback functions */ 45 | CMC_CALLBACKS_DECL; 46 | }; 47 | 48 | /* HashMultimap Entry */ 49 | struct CMC_DEF_ENTRY(SNAME) 50 | { 51 | /* Entry Key */ 52 | K key; 53 | /* Entry Value */ 54 | V value; 55 | /* Next entry on the linked list */ 56 | struct CMC_DEF_ENTRY(SNAME) * next; 57 | /* Previous entry on the linked list */ 58 | struct CMC_DEF_ENTRY(SNAME) * prev; 59 | }; 60 | -------------------------------------------------------------------------------- /cmc/hashmultiset.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * hashmultiset.h 27 | * 28 | * Creation Date: 10/04/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * HashMultiSet 37 | * 38 | * In mathematics, a multiset is a modification of the concept of a set that, 39 | * unlike a set, allows for multiple instances for each of its elements. The 40 | * positive integer number of instances, given for each element is called the 41 | * multiplicity of this element in the multiset. A multiSet also has a 42 | * cardinality which equals the sum of the multiplicities of its elements. 43 | * 44 | * Implementation 45 | * 46 | * In this implementation, the values are stored in a flat hashtable and are 47 | * mapped to their multiplicity. 48 | */ 49 | 50 | #include "cor/core.h" 51 | #include "cor/hashtable.h" 52 | 53 | #ifdef CMC_DEV 54 | #include "utl/log.h" 55 | #endif 56 | 57 | /** 58 | * Used values 59 | * V - hashmultiset value data type 60 | * SNAME - struct name and prefix of other related structs 61 | * PFX - functions prefix 62 | */ 63 | 64 | /* Structs definition */ 65 | #include "cmc/hashmultiset/struct.h" 66 | 67 | /* Function declaration */ 68 | #include "cmc/hashmultiset/header.h" 69 | 70 | /* Function implementation */ 71 | #include "cmc/hashmultiset/code.h" 72 | 73 | /** 74 | * Extensions 75 | * 76 | * INIT - Initializes the struct on the stack 77 | * ITER - hashmultiset iterator 78 | * STR - Print helper functions 79 | */ 80 | #define CMC_EXT_HASHMULTISET_PARTS INIT, ITER, SETF, STR 81 | /**/ 82 | #include "cmc/hashmultiset/ext/struct.h" 83 | /**/ 84 | #include "cmc/hashmultiset/ext/header.h" 85 | /**/ 86 | #include "cmc/hashmultiset/ext/code.h" 87 | 88 | #include "cor/undef.h" 89 | -------------------------------------------------------------------------------- /cmc/hashmultiset/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Hashmultiset bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* HashMultiSet Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target HashMultiset */ 36 | struct SNAME *target; 37 | 38 | /* Cursor's position (index) */ 39 | size_t cursor; 40 | 41 | /* Keeps track of relative index to the iteration of elements */ 42 | size_t index; 43 | 44 | /* The index of the first element */ 45 | size_t first; 46 | 47 | /* The index of the last element */ 48 | size_t last; 49 | 50 | /* If the iterator has reached the start of the iteration */ 51 | bool start; 52 | 53 | /* If the iterator has reached the end of the iteration */ 54 | bool end; 55 | }; 56 | 57 | #endif /* CMC_EXT_ITER */ 58 | -------------------------------------------------------------------------------- /cmc/hashmultiset/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | struct SNAME 26 | { 27 | /* Array of Entries */ 28 | struct CMC_DEF_ENTRY(SNAME) * buffer; 29 | /* Current Array Capcity */ 30 | size_t capacity; 31 | /* Current amount of unique elements */ 32 | size_t count; 33 | /* Total amount of elements taking into account their multiplicity */ 34 | size_t cardinality; 35 | /* Load factor in range (0.0, 1.0) */ 36 | double load; 37 | /* Flags indicating errors or success */ 38 | int flag; 39 | /* Key function table */ 40 | struct CMC_DEF_FVAL(SNAME) * f_val; 41 | /* Custom allocation functions */ 42 | CMC_ALLOC_TYPE alloc; 43 | /* Custom callback functions */ 44 | CMC_CALLBACKS_DECL; 45 | }; 46 | 47 | struct CMC_DEF_ENTRY(SNAME) 48 | { 49 | /* Entry element */ 50 | V value; 51 | /* The element's multiplicity */ 52 | size_t multiplicity; 53 | /* The distance of this node to its original position, used by */ 54 | /* robin-hood hashing */ 55 | size_t dist; 56 | /* The sate of this node (DELETED, EMPTY, FILLED) */ 57 | enum cmc_entry_state state; 58 | }; 59 | -------------------------------------------------------------------------------- /cmc/hashset.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * hashset.h 27 | * 28 | * Creation Date: 01/04/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * HashSet 37 | * 38 | * A HashSet is an implementation of a Set with unique keys. The keys are not 39 | * sorted. It is implemented as a flat hashtable with linear probing and robin 40 | * hood hashing. 41 | */ 42 | 43 | #include "cor/core.h" 44 | #include "cor/hashtable.h" 45 | 46 | #ifdef CMC_DEV 47 | #include "utl/log.h" 48 | #endif 49 | 50 | /** 51 | * Used values 52 | * V - hashset value data type 53 | * SNAME - struct name and prefix of other related structs 54 | * PFX - functions prefix 55 | */ 56 | 57 | /* Structs definition */ 58 | #include "cmc/hashset/struct.h" 59 | 60 | /* Function declaration */ 61 | #include "cmc/hashset/header.h" 62 | 63 | /* Function implementation */ 64 | #include "cmc/hashset/code.h" 65 | 66 | /** 67 | * Extensions 68 | * 69 | * INIT - Initializes the struct on the stack 70 | * ITER - hashset iterator 71 | * STR - Print helper functions 72 | */ 73 | #define CMC_EXT_HASHSET_PARTS ITER, SETF, STR 74 | /**/ 75 | #include "cmc/hashset/ext/struct.h" 76 | /**/ 77 | #include "cmc/hashset/ext/header.h" 78 | /**/ 79 | #include "cmc/hashset/ext/code.h" 80 | 81 | #include "cor/undef.h" 82 | -------------------------------------------------------------------------------- /cmc/hashset/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Hashset bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* HashSet Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target HashSet */ 36 | struct SNAME *target; 37 | /* Cursor's position (index) */ 38 | size_t cursor; 39 | /* Keeps track of relative index to the iteration of elements */ 40 | size_t index; 41 | /* The index of the first element */ 42 | size_t first; 43 | /* The index of the last element */ 44 | size_t last; 45 | /* If the iterator has reached the start of the iteration */ 46 | bool start; 47 | /* If the iterator has reached the end of the iteration */ 48 | bool end; 49 | }; 50 | 51 | #endif /* CMC_EXT_ITER */ 52 | -------------------------------------------------------------------------------- /cmc/hashset/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* Hashset Structure */ 26 | struct SNAME 27 | { 28 | /* Array of Entries */ 29 | struct CMC_DEF_ENTRY(SNAME) * buffer; 30 | /* Current Array Capcity */ 31 | size_t capacity; 32 | /* Current amount of elements */ 33 | size_t count; 34 | /* Load factor in range (0.0, 1.0) */ 35 | double load; 36 | /* Flags indicating errors or success */ 37 | int flag; 38 | /* Value function table */ 39 | struct CMC_DEF_FVAL(SNAME) * f_val; 40 | /* Custom allocation functions */ 41 | CMC_ALLOC_TYPE alloc; 42 | /* Custom callback functions */ 43 | CMC_CALLBACKS_DECL; 44 | }; 45 | 46 | struct CMC_DEF_ENTRY(SNAME) 47 | { 48 | /* Entry value */ 49 | V value; 50 | /* The distance of this node to its original position, used by */ 51 | /* robin-hood hashing */ 52 | size_t dist; 53 | /* The sate of this node (DELETED, EMPTY, FILLED) */ 54 | enum cmc_entry_state state; 55 | }; 56 | -------------------------------------------------------------------------------- /cmc/heap/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Heap bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* Heap Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target heap */ 36 | struct SNAME *target; 37 | /* Cursor's position (index) */ 38 | size_t cursor; 39 | /* If the iterator has reached the start of the iteration */ 40 | bool start; 41 | /* If the iterator has reached the end of the iteration */ 42 | bool end; 43 | }; 44 | 45 | #endif /* CMC_EXT_ITER */ 46 | -------------------------------------------------------------------------------- /cmc/heap/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | struct SNAME 26 | { 27 | /* Dynamic array of elements */ 28 | V *buffer; 29 | /* Current array capacity */ 30 | size_t capacity; 31 | /* Current amount of elements in the heap */ 32 | size_t count; 33 | /* Heap order (MaxHeap or MinHeap) */ 34 | enum cmc_heap_order HO; 35 | /* Flags indicating errors or success */ 36 | int flag; 37 | /* Value function table */ 38 | struct CMC_DEF_FVAL(SNAME) * f_val; 39 | /* Custom allocation functions */ 40 | CMC_ALLOC_TYPE alloc; 41 | /* Custom callback functions */ 42 | CMC_CALLBACKS_DECL; 43 | }; 44 | -------------------------------------------------------------------------------- /cmc/intervalheap.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * intervalheap.h 27 | * 28 | * Creation Date: 06/07/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * Interval Heap 37 | * 38 | * An interval heap is a specialized heap with: 39 | * 40 | * - O(1) - Find Min 41 | * - O(1) - Find Max 42 | * - O(log n) - Insert 43 | * - O(log n) - Remove Min 44 | * - O(log n) - Remove Max 45 | */ 46 | 47 | #include "cor/core.h" 48 | 49 | #ifdef CMC_DEV 50 | #include "utl/log.h" 51 | #endif 52 | 53 | /** 54 | * Used values 55 | * V - intervalheap value data type 56 | * SNAME - struct name and prefix of other related structs 57 | * PFX - functions prefix 58 | */ 59 | 60 | /* Structs definition */ 61 | #include "cmc/intervalheap/struct.h" 62 | 63 | /* Function declaration */ 64 | #include "cmc/intervalheap/header.h" 65 | 66 | /* Function implementation */ 67 | #include "cmc/intervalheap/code.h" 68 | 69 | /** 70 | * Extensions 71 | * 72 | * INIT - Initializes the struct on the stack 73 | * ITER - intervalheap iterator 74 | * STR - Print helper functions 75 | */ 76 | #define CMC_EXT_HEAP_PARTS ITER, SETF, STR 77 | /**/ 78 | #include "cmc/intervalheap/ext/struct.h" 79 | /**/ 80 | #include "cmc/intervalheap/ext/header.h" 81 | /**/ 82 | #include "cmc/intervalheap/ext/code.h" 83 | 84 | #include "cor/undef.h" 85 | -------------------------------------------------------------------------------- /cmc/intervalheap/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | -------------------------------------------------------------------------------- /cmc/intervalheap/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | struct SNAME 26 | { 27 | /* Dynamic array of elements */ 28 | /* buffer[n][0] is MinHeap and buffer[n][1] is MaxHeap */ 29 | V (*buffer)[2]; 30 | /* Current array capacity */ 31 | size_t capacity; 32 | /* Current amount of pairs of values in the array */ 33 | size_t size; 34 | /* Current amount of elements in the heap */ 35 | size_t count; 36 | /* Flags indicating errors or success */ 37 | int flag; 38 | /* Value function table */ 39 | struct CMC_DEF_FVAL(SNAME) * f_val; 40 | /* Custom allocation functions */ 41 | CMC_ALLOC_TYPE alloc; 42 | /* Custom callback functions */ 43 | CMC_CALLBACKS_DECL; 44 | }; 45 | -------------------------------------------------------------------------------- /cmc/linkedlist/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * List bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | #endif /* CMC_EXT_ITER */ 33 | -------------------------------------------------------------------------------- /cmc/linkedlist/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* Linked List Structure */ 26 | struct SNAME 27 | { 28 | /* First node in the list */ 29 | struct CMC_DEF_NODE(SNAME) * head; 30 | 31 | /* Last node in the list */ 32 | struct CMC_DEF_NODE(SNAME) * tail; 33 | 34 | /* Current amount of elements in the list */ 35 | size_t count; 36 | 37 | /* Flags indicating errors or success */ 38 | int flag; 39 | 40 | /* Value function table */ 41 | struct CMC_DEF_FVAL(SNAME) * f_val; 42 | 43 | /* Custom allocation functions */ 44 | CMC_ALLOC_TYPE alloc; 45 | 46 | /* Custom callback functions */ 47 | CMC_CALLBACKS_DECL; 48 | }; 49 | 50 | /* Doubly-linked list node */ 51 | struct CMC_DEF_NODE(SNAME) 52 | { 53 | /* Node's value */ 54 | V value; 55 | 56 | /* Pointer to the next node on the linked list */ 57 | struct CMC_DEF_NODE(SNAME) * next; 58 | 59 | /* Pointer to the previous node on the linked list */ 60 | struct CMC_DEF_NODE(SNAME) * prev; 61 | }; 62 | -------------------------------------------------------------------------------- /cmc/list/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * List bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* List Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target List */ 36 | struct SNAME *target; 37 | /* Cursor's position (index) */ 38 | size_t cursor; 39 | /* If the iterator has reached the start of the iteration */ 40 | bool start; 41 | /* If the iterator has reached the end of the iteration */ 42 | bool end; 43 | }; 44 | 45 | #endif /* CMC_EXT_ITER */ 46 | -------------------------------------------------------------------------------- /cmc/list/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | struct SNAME 26 | { 27 | #ifdef CMC_SAC 28 | V buffer[SIZE]; 29 | #else 30 | /* Dynamic array of elements */ 31 | V *buffer; 32 | /* Current array capacity */ 33 | size_t capacity; 34 | #endif 35 | /* Current amount of elements */ 36 | size_t count; 37 | /* Flags indicating errors or success */ 38 | int flag; 39 | /* Value function table */ 40 | struct CMC_DEF_FVAL(SNAME) * f_val; 41 | /* Custom allocation functions */ 42 | CMC_ALLOC_TYPE alloc; 43 | /* Custom callback functions */ 44 | CMC_CALLBACKS_DECL; 45 | }; 46 | -------------------------------------------------------------------------------- /cmc/queue/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Queue bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* Queue Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target queue */ 36 | struct SNAME *target; 37 | /* Cursor's position (index) */ 38 | size_t cursor; 39 | /* Keeps track of relative index to the iteration of elements */ 40 | size_t index; 41 | /* If the iterator has reached the start of the iteration */ 42 | bool start; 43 | /* If the iterator has reached the end of the iteration */ 44 | bool end; 45 | }; 46 | 47 | #endif /* CMC_EXT_ITER */ 48 | -------------------------------------------------------------------------------- /cmc/queue/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | struct SNAME 26 | { 27 | /* Dynamic circular array of elements */ 28 | V *buffer; 29 | /* Current circular array capacity */ 30 | size_t capacity; 31 | /* Current amount of elements */ 32 | size_t count; 33 | /* Index representing the front of the queue */ 34 | size_t front; 35 | /* Index representing the back of the queue */ 36 | size_t back; 37 | /* Flags indicating errors or success */ 38 | int flag; 39 | /* Value function table */ 40 | struct CMC_DEF_FVAL(SNAME) * f_val; 41 | /* Custom allocation functions */ 42 | CMC_ALLOC_TYPE alloc; 43 | /* Custom callback functions */ 44 | CMC_CALLBACKS_DECL; 45 | }; 46 | -------------------------------------------------------------------------------- /cmc/sortedlist.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * sortedlist.h 27 | * 28 | * Creation Date: 17/09/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * SortedList 37 | * 38 | * A sorted list is a dynamic array, meaning that you can store as many elements 39 | * as you like and when its capacity is full, the buffer is reallocated. The 40 | * elements are only sorted when a certain action requires that the array is 41 | * sorted like accessing min() or max(). This prevents the array from being 42 | * sorted after every insertion or removal. The array is sorted using a 43 | * variation of quick sort that uses insertion sort for small partitions. 44 | */ 45 | 46 | #include "cor/core.h" 47 | 48 | #ifdef CMC_DEV 49 | #include "utl/log.h" 50 | #endif 51 | 52 | /** 53 | * Used values 54 | * V - sortedlist data type 55 | * SNAME - struct name and prefix of other related structs 56 | * PFX - functions prefix 57 | */ 58 | 59 | /* Structs definition */ 60 | #include "cmc/sortedlist/struct.h" 61 | 62 | /* Function declaration */ 63 | #include "cmc/sortedlist/header.h" 64 | 65 | /* Function implementation */ 66 | #include "cmc/sortedlist/code.h" 67 | 68 | /** 69 | * Extensions 70 | * 71 | * INIT - Initializes the struct on the stack 72 | * ITER - List iterator 73 | * STR - Print helper functions 74 | */ 75 | #define CMC_EXT_SORTEDLIST_PARTS INIT, ITER, STR 76 | /**/ 77 | #include "cmc/sortedlist/ext/struct.h" 78 | /**/ 79 | #include "cmc/sortedlist/ext/header.h" 80 | /**/ 81 | #include "cmc/sortedlist/ext/code.h" 82 | 83 | #include "cor/undef.h" 84 | -------------------------------------------------------------------------------- /cmc/sortedlist/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | -------------------------------------------------------------------------------- /cmc/sortedlist/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | struct SNAME 26 | { 27 | /* Dynamic array of elements */ 28 | V *buffer; 29 | /* Current array capacity */ 30 | size_t capacity; 31 | /* Current amount of elements */ 32 | size_t count; 33 | /* Flag if the list is sorted or not, used by lazy evaluation */ 34 | bool is_sorted; 35 | /* Flags indicating errors or success */ 36 | int flag; 37 | /* Value function table */ 38 | struct CMC_DEF_FVAL(SNAME) * f_val; 39 | /* Custom allocation functions */ 40 | CMC_ALLOC_TYPE alloc; 41 | /* Custom callback functions */ 42 | CMC_CALLBACKS_DECL; 43 | }; 44 | -------------------------------------------------------------------------------- /cmc/stack.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * stack.h 27 | * 28 | * Creation Date: 14/02/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * Stack 37 | * 38 | * A Stack is a Last-in First-out (or First-in Last-out) data structure used in 39 | * a variety of algorithms. It is a Dynamic Array that can only add or remove 40 | * its elements at the end of the buffer, in this case, it represents the top of 41 | * the stack. 42 | * 43 | * It has three main functions: `push` which adds an element at the top of the 44 | * stack; `pop` which removes the top element from the stack; and `top` which 45 | * returns the top element without removing it (it is also sometimes called 46 | * `peek`). 47 | * 48 | * A Stack is used in algorithms like backtracking, depth-first search, 49 | * expression evaluation, syntax parsing and many more. 50 | */ 51 | 52 | #include "cor/core.h" 53 | 54 | #ifdef CMC_DEV 55 | #include "utl/log.h" 56 | #endif 57 | 58 | /** 59 | * Used values 60 | * V - stack data type 61 | * SNAME - struct name and prefix of other related structs 62 | * PFX - functions prefix 63 | */ 64 | 65 | /* Structs definition */ 66 | #include "cmc/stack/struct.h" 67 | 68 | /* Function declaration */ 69 | #include "cmc/stack/header.h" 70 | 71 | /* Function implementation */ 72 | #include "cmc/stack/code.h" 73 | 74 | /** 75 | * Extensions 76 | * 77 | * ITER - Stack iterator 78 | * STR - Print helper functions 79 | */ 80 | #define CMC_EXT_STACK_PARTS INIT, ITER, STR 81 | /**/ 82 | #include "cmc/stack/ext/struct.h" 83 | /**/ 84 | #include "cmc/stack/ext/header.h" 85 | /**/ 86 | #include "cmc/stack/ext/code.h" 87 | 88 | #include "cor/undef.h" 89 | -------------------------------------------------------------------------------- /cmc/stack/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * ITER 27 | * 28 | * Stack bi-directional iterator. 29 | */ 30 | #ifdef CMC_EXT_ITER 31 | 32 | /* Stack Iterator */ 33 | struct CMC_DEF_ITER(SNAME) 34 | { 35 | /* Target stack */ 36 | struct SNAME *target; 37 | /* Cursor's position (index) */ 38 | size_t cursor; 39 | /* If the iterator has reached the start of the iteration */ 40 | bool start; 41 | /* If the iterator has reached the end of the iteration */ 42 | bool end; 43 | }; 44 | 45 | #endif /* CMC_EXT_ITER */ 46 | -------------------------------------------------------------------------------- /cmc/stack/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* Stack Structure */ 26 | struct SNAME 27 | { 28 | /* Dynamic array of elements */ 29 | V *buffer; 30 | /* Current array capacity */ 31 | size_t capacity; 32 | /* Current amount of elements */ 33 | size_t count; 34 | /* Flags indicating errors or success */ 35 | int flag; 36 | /* Value function table */ 37 | struct CMC_DEF_FVAL(SNAME) * f_val; 38 | /* Custom allocation functions */ 39 | CMC_ALLOC_TYPE alloc; 40 | /* Custom callback functions */ 41 | CMC_CALLBACKS_DECL; 42 | }; 43 | -------------------------------------------------------------------------------- /cmc/treemap.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * treemap.h 27 | * 28 | * Creation Date: 28/03/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * TreeMap 37 | * 38 | * A TreeMap is an implementation of a Map that keeps its keys sorted. Like a 39 | * Map, it has only unique keys. This implementation uses a balanced binary 40 | * tree called AVL Tree that uses the height of nodes to keep its keys balanced. 41 | */ 42 | 43 | #include "cor/core.h" 44 | 45 | #ifdef CMC_DEV 46 | #include "utl/log.h" 47 | #endif 48 | 49 | /** 50 | * Used values 51 | * K - treemap key data type 52 | * V - treemap value data type 53 | * SNAME - struct name and prefix of other related structs 54 | * PFX - functions prefix 55 | */ 56 | 57 | /* Structs definition */ 58 | #include "cmc/treemap/struct.h" 59 | 60 | /* Function declaration */ 61 | #include "cmc/treemap/header.h" 62 | 63 | /* Function implementation */ 64 | #include "cmc/treemap/code.h" 65 | 66 | /** 67 | * Extensions 68 | * 69 | * INIT - Initializes the struct on the stack 70 | * ITER - treemap iterator 71 | * STR - Print helper functions 72 | */ 73 | #define CMC_EXT_TREEMAP_PARTS INIT, ITER, STR 74 | /**/ 75 | #include "cmc/treemap/ext/struct.h" 76 | /**/ 77 | #include "cmc/treemap/ext/header.h" 78 | /**/ 79 | #include "cmc/treemap/ext/code.h" 80 | 81 | #include "cor/undef.h" 82 | -------------------------------------------------------------------------------- /cmc/treemap/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | -------------------------------------------------------------------------------- /cmc/treemap/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* Treemap Structure */ 26 | struct SNAME 27 | { 28 | /* Root node */ 29 | struct CMC_DEF_NODE(SNAME) * root; 30 | 31 | /* Current amount of keys */ 32 | size_t count; 33 | 34 | /* Flags indicating errors or success */ 35 | int flag; 36 | 37 | /* Key function table */ 38 | struct CMC_DEF_FKEY(SNAME) * f_key; 39 | 40 | /* Value function table */ 41 | struct CMC_DEF_FVAL(SNAME) * f_val; 42 | 43 | /* Custom allocation functions */ 44 | CMC_ALLOC_TYPE alloc; 45 | 46 | /* Custom callback functions */ 47 | CMC_CALLBACKS_DECL; 48 | }; 49 | 50 | /* Treemap Node */ 51 | struct CMC_DEF_NODE(SNAME) 52 | { 53 | /* Node Key */ 54 | K key; 55 | 56 | /* Node Value */ 57 | V value; 58 | 59 | /* Node height used by the AVL tree to keep it strictly balanced */ 60 | unsigned char height; 61 | 62 | /* Right child node or subtree */ 63 | struct CMC_DEF_NODE(SNAME) * right; 64 | 65 | /* Left child node or subtree */ 66 | struct CMC_DEF_NODE(SNAME) * left; 67 | 68 | /* Parent node */ 69 | struct CMC_DEF_NODE(SNAME) * parent; 70 | }; 71 | -------------------------------------------------------------------------------- /cmc/treeset.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * treeset.h 27 | * 28 | * Creation Date: 27/03/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * TreeSet 37 | * 38 | * A TreeSet is an implementation of a Set that keeps its elements sorted. Like 39 | * a Set it has only unique keys. This implementation uses a balanced binary 40 | * tree called AVL Tree that uses the height of nodes to keep its keys balanced. 41 | */ 42 | 43 | #include "cor/core.h" 44 | 45 | #ifdef CMC_DEV 46 | #include "utl/log.h" 47 | #endif 48 | 49 | /** 50 | * Used values 51 | * V - treeset value data type 52 | * SNAME - struct name and prefix of other related structs 53 | * PFX - functions prefix 54 | */ 55 | 56 | /* Structs definition */ 57 | #include "cmc/treeset/struct.h" 58 | 59 | /* Function declaration */ 60 | #include "cmc/treeset/header.h" 61 | 62 | /* Function implementation */ 63 | #include "cmc/treeset/code.h" 64 | 65 | /** 66 | * Extensions 67 | * 68 | * INIT - Initializes the struct on the stack 69 | * ITER - treeset iterator 70 | * SETF - Set functions 71 | * STR - Print helper functions 72 | */ 73 | #define CMC_EXT_TREESET_PARTS INIT, ITER, SETF, STR 74 | /**/ 75 | #include "cmc/treeset/ext/struct.h" 76 | /**/ 77 | #include "cmc/treeset/ext/header.h" 78 | /**/ 79 | #include "cmc/treeset/ext/code.h" 80 | 81 | #include "cor/undef.h" 82 | -------------------------------------------------------------------------------- /cmc/treeset/ext/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | -------------------------------------------------------------------------------- /cmc/treeset/header.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* Value struct function table */ 26 | struct CMC_DEF_FVAL(SNAME) 27 | { 28 | /* Comparator function */ 29 | CMC_DEF_FTAB_CMP(V); 30 | /* Copy function */ 31 | CMC_DEF_FTAB_CPY(V); 32 | /* To string function */ 33 | CMC_DEF_FTAB_STR(V); 34 | /* Free from memory function */ 35 | CMC_DEF_FTAB_FREE(V); 36 | /* Hash function */ 37 | CMC_DEF_FTAB_HASH(V); 38 | /* Priority function */ 39 | CMC_DEF_FTAB_PRI(V); 40 | }; 41 | 42 | /* Collection Functions */ 43 | /* Collection Allocation and Deallocation */ 44 | struct SNAME *CMC_(PFX, _new)(struct CMC_DEF_FVAL(SNAME) * f_val); 45 | struct SNAME *CMC_(PFX, _new_custom)(struct CMC_DEF_FVAL(SNAME) * f_val, CMC_ALLOC_TYPE alloc, 46 | CMC_CALLBACK_TYPE callbacks); 47 | void CMC_(PFX, _clear)(struct SNAME *_set_); 48 | void CMC_(PFX, _free)(struct SNAME *_set_); 49 | /* Customization of Allocation and Callbacks */ 50 | void CMC_(PFX, _customize)(struct SNAME *_set_, CMC_ALLOC_TYPE alloc, CMC_CALLBACK_TYPE callbacks); 51 | /* Collection Input and Output */ 52 | bool CMC_(PFX, _insert)(struct SNAME *_set_, V value); 53 | bool CMC_(PFX, _remove)(struct SNAME *_set_, V value); 54 | /* Element Access */ 55 | bool CMC_(PFX, _max)(struct SNAME *_set_, V *value); 56 | bool CMC_(PFX, _min)(struct SNAME *_set_, V *value); 57 | /* Collection State */ 58 | bool CMC_(PFX, _contains)(struct SNAME *_set_, V value); 59 | bool CMC_(PFX, _empty)(struct SNAME *_set_); 60 | size_t CMC_(PFX, _count)(struct SNAME *_set_); 61 | int CMC_(PFX, _flag)(struct SNAME *_set_); 62 | /* Collection Utility */ 63 | struct SNAME *CMC_(PFX, _copy_of)(struct SNAME *_set_); 64 | bool CMC_(PFX, _equals)(struct SNAME *_set1_, struct SNAME *_set2_); 65 | -------------------------------------------------------------------------------- /cmc/treeset/struct.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /* Treeset Structure */ 26 | struct SNAME 27 | { 28 | /* Root node */ 29 | struct CMC_DEF_NODE(SNAME) * root; 30 | 31 | /* Current amount of elements */ 32 | size_t count; 33 | 34 | /* Flags indicating errors or success */ 35 | int flag; 36 | 37 | /* Value function table */ 38 | struct CMC_DEF_FVAL(SNAME) * f_val; 39 | 40 | /* Custom allocation functions */ 41 | CMC_ALLOC_TYPE alloc; 42 | 43 | /* Custom callback functions */ 44 | CMC_CALLBACKS_DECL; 45 | }; 46 | 47 | /* Treeset Node */ 48 | struct CMC_DEF_NODE(SNAME) 49 | { 50 | /* Node element */ 51 | V value; 52 | 53 | /* Node height used by the AVL tree to keep it strictly balanced */ 54 | unsigned char height; 55 | 56 | /* Right child node or subtree */ 57 | struct CMC_DEF_NODE(SNAME) * right; 58 | 59 | /* Left child node or subtree */ 60 | struct CMC_DEF_NODE(SNAME) * left; 61 | 62 | /* Parent node */ 63 | struct CMC_DEF_NODE(SNAME) * parent; 64 | }; 65 | -------------------------------------------------------------------------------- /cmc/utl/foreach.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * utl_foreach.h 27 | * 28 | * Creation Date: 25/02/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * For-Each macros that work with all Collections within the library. 37 | * 38 | * PFX - Functions prefix 39 | * SNAME - Struct name 40 | * ITERNAME - Iterator variable name 41 | * TARGET - Target data structure variable name 42 | */ 43 | 44 | #ifndef CMC_UTL_FOREACH_H 45 | #define CMC_UTL_FOREACH_H 46 | 47 | #include "../cor/core.h" 48 | 49 | #define CMC_FOREACH(PFX, SNAME, ITERNAME, TARGET) \ 50 | for (struct CMC_DEF_ITER(SNAME) ITERNAME = CMC_(PFX, _iter_start)(TARGET); !CMC_(PFX, _iter_at_end)(&ITERNAME); \ 51 | CMC_(PFX, _iter_next)(&ITERNAME)) 52 | 53 | #define CMC_FOREACH_REV(PFX, SNAME, ITERNAME, TARGET) \ 54 | for (struct CMC_DEF_ITER(SNAME) ITERNAME = CMC_(PFX, _iter_end)(TARGET); !CMC_(PFX, _iter_at_start)(&ITERNAME); \ 55 | CMC_(PFX, _iter_prev)(&ITERNAME)) 56 | 57 | #endif /* CMC_UTL_FOREACH_H */ 58 | -------------------------------------------------------------------------------- /cmc/utl/timer.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2019 Leonardo Vencovsky 3 | * 4 | * This file is part of the C Macro Collections Library. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | /** 26 | * utl_timer.h 27 | * 28 | * Creation Date: 12/04/2019 29 | * 30 | * Authors: 31 | * Leonardo Vencovsky (https://github.com/LeoVen) 32 | * 33 | */ 34 | 35 | /** 36 | * Simple timer macros utility for timing code execution. 37 | */ 38 | 39 | #ifndef CMC_UTL_TIMER_H 40 | #define CMC_UTL_TIMER_H 41 | 42 | #include 43 | 44 | struct cmc_timer 45 | { 46 | clock_t start; 47 | clock_t stop; 48 | double result; /* The result is given in milliseconds */ 49 | }; 50 | 51 | #define cmc_timer_start(timer) \ 52 | do \ 53 | { \ 54 | struct cmc_timer *t__ = &(timer); \ 55 | t__->start = clock(); \ 56 | } while (0) 57 | 58 | #define cmc_timer_stop(timer) \ 59 | do \ 60 | { \ 61 | struct cmc_timer *t__ = &(timer); \ 62 | t__->stop = clock(); \ 63 | t__->result = (double)(t__->stop - t__->start) * 1000.0 / CLOCKS_PER_SEC; \ 64 | } while (0) 65 | 66 | #endif /* CMC_UTL_TIMER_H */ 67 | -------------------------------------------------------------------------------- /docs/.nojekyll: -------------------------------------------------------------------------------- 1 | This file makes sure that Github Pages doesn't process mdBook's output. 2 | -------------------------------------------------------------------------------- /docs/CNAME: -------------------------------------------------------------------------------- 1 | cmc.leoven.dev -------------------------------------------------------------------------------- /docs/FontAwesome/fonts/FontAwesome.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/FontAwesome/fonts/FontAwesome.ttf -------------------------------------------------------------------------------- /docs/FontAwesome/fonts/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/FontAwesome/fonts/fontawesome-webfont.eot -------------------------------------------------------------------------------- /docs/FontAwesome/fonts/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/FontAwesome/fonts/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /docs/FontAwesome/fonts/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/FontAwesome/fonts/fontawesome-webfont.woff -------------------------------------------------------------------------------- /docs/FontAwesome/fonts/fontawesome-webfont.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/FontAwesome/fonts/fontawesome-webfont.woff2 -------------------------------------------------------------------------------- /docs/Introduction/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/Introduction/img/logo.png -------------------------------------------------------------------------------- /docs/ayu-highlight.css: -------------------------------------------------------------------------------- 1 | /* 2 | Based off of the Ayu theme 3 | Original by Dempfi (https://github.com/dempfi/ayu) 4 | */ 5 | 6 | .hljs { 7 | display: block; 8 | overflow-x: auto; 9 | background: #191f26; 10 | color: #e6e1cf; 11 | } 12 | 13 | .hljs-comment, 14 | .hljs-quote { 15 | color: #5c6773; 16 | font-style: italic; 17 | } 18 | 19 | .hljs-variable, 20 | .hljs-template-variable, 21 | .hljs-attribute, 22 | .hljs-attr, 23 | .hljs-regexp, 24 | .hljs-link, 25 | .hljs-selector-id, 26 | .hljs-selector-class { 27 | color: #ff7733; 28 | } 29 | 30 | .hljs-number, 31 | .hljs-meta, 32 | .hljs-builtin-name, 33 | .hljs-literal, 34 | .hljs-type, 35 | .hljs-params { 36 | color: #ffee99; 37 | } 38 | 39 | .hljs-string, 40 | .hljs-bullet { 41 | color: #b8cc52; 42 | } 43 | 44 | .hljs-title, 45 | .hljs-built_in, 46 | .hljs-section { 47 | color: #ffb454; 48 | } 49 | 50 | .hljs-keyword, 51 | .hljs-selector-tag, 52 | .hljs-symbol { 53 | color: #ff7733; 54 | } 55 | 56 | .hljs-name { 57 | color: #36a3d9; 58 | } 59 | 60 | .hljs-tag { 61 | color: #00568d; 62 | } 63 | 64 | .hljs-emphasis { 65 | font-style: italic; 66 | } 67 | 68 | .hljs-strong { 69 | font-weight: bold; 70 | } 71 | 72 | .hljs-addition { 73 | color: #91b362; 74 | } 75 | 76 | .hljs-deletion { 77 | color: #d96c75; 78 | } 79 | -------------------------------------------------------------------------------- /docs/css/print.css: -------------------------------------------------------------------------------- 1 | 2 | #sidebar, 3 | #menu-bar, 4 | .nav-chapters, 5 | .mobile-nav-chapters { 6 | display: none; 7 | } 8 | 9 | #page-wrapper.page-wrapper { 10 | transform: none; 11 | margin-left: 0px; 12 | overflow-y: initial; 13 | } 14 | 15 | #content { 16 | max-width: none; 17 | margin: 0; 18 | padding: 0; 19 | } 20 | 21 | .page { 22 | overflow-y: initial; 23 | } 24 | 25 | code { 26 | background-color: #666666; 27 | border-radius: 5px; 28 | 29 | /* Force background to be printed in Chrome */ 30 | -webkit-print-color-adjust: exact; 31 | } 32 | 33 | pre > .buttons { 34 | z-index: 2; 35 | } 36 | 37 | a, a:visited, a:active, a:hover { 38 | color: #4183c4; 39 | text-decoration: none; 40 | } 41 | 42 | h1, h2, h3, h4, h5, h6 { 43 | page-break-inside: avoid; 44 | page-break-after: avoid; 45 | } 46 | 47 | pre, code { 48 | page-break-inside: avoid; 49 | white-space: pre-wrap; 50 | } 51 | 52 | .fa { 53 | display: none !important; 54 | } 55 | -------------------------------------------------------------------------------- /docs/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/favicon.png -------------------------------------------------------------------------------- /docs/favicon.svg: -------------------------------------------------------------------------------- 1 | 2 | 7 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-300.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-300.woff2 -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-300italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-300italic.woff2 -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-600.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-600.woff2 -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-600italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-600italic.woff2 -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-700.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-700.woff2 -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-700italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-700italic.woff2 -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-800.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-800.woff2 -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-800italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-800italic.woff2 -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-italic.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-italic.woff2 -------------------------------------------------------------------------------- /docs/fonts/open-sans-v17-all-charsets-regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/open-sans-v17-all-charsets-regular.woff2 -------------------------------------------------------------------------------- /docs/fonts/source-code-pro-v11-all-charsets-500.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/fonts/source-code-pro-v11-all-charsets-500.woff2 -------------------------------------------------------------------------------- /docs/highlight.css: -------------------------------------------------------------------------------- 1 | /* 2 | * An increased contrast highlighting scheme loosely based on the 3 | * "Base16 Atelier Dune Light" theme by Bram de Haan 4 | * (http://atelierbram.github.io/syntax-highlighting/atelier-schemes/dune) 5 | * Original Base16 color scheme by Chris Kempson 6 | * (https://github.com/chriskempson/base16) 7 | */ 8 | 9 | /* Comment */ 10 | .hljs-comment, 11 | .hljs-quote { 12 | color: #575757; 13 | } 14 | 15 | /* Red */ 16 | .hljs-variable, 17 | .hljs-template-variable, 18 | .hljs-attribute, 19 | .hljs-tag, 20 | .hljs-name, 21 | .hljs-regexp, 22 | .hljs-link, 23 | .hljs-name, 24 | .hljs-selector-id, 25 | .hljs-selector-class { 26 | color: #d70025; 27 | } 28 | 29 | /* Orange */ 30 | .hljs-number, 31 | .hljs-meta, 32 | .hljs-built_in, 33 | .hljs-builtin-name, 34 | .hljs-literal, 35 | .hljs-type, 36 | .hljs-params { 37 | color: #b21e00; 38 | } 39 | 40 | /* Green */ 41 | .hljs-string, 42 | .hljs-symbol, 43 | .hljs-bullet { 44 | color: #008200; 45 | } 46 | 47 | /* Blue */ 48 | .hljs-title, 49 | .hljs-section { 50 | color: #0030f2; 51 | } 52 | 53 | /* Purple */ 54 | .hljs-keyword, 55 | .hljs-selector-tag { 56 | color: #9d00ec; 57 | } 58 | 59 | .hljs { 60 | display: block; 61 | overflow-x: auto; 62 | background: #f6f7f6; 63 | color: #000; 64 | } 65 | 66 | .hljs-emphasis { 67 | font-style: italic; 68 | } 69 | 70 | .hljs-strong { 71 | font-weight: bold; 72 | } 73 | 74 | .hljs-addition { 75 | color: #22863a; 76 | background-color: #f0fff4; 77 | } 78 | 79 | .hljs-deletion { 80 | color: #b31d28; 81 | background-color: #ffeef0; 82 | } 83 | -------------------------------------------------------------------------------- /docs/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/docs/img/logo.png -------------------------------------------------------------------------------- /docs/tomorrow-night.css: -------------------------------------------------------------------------------- 1 | /* Tomorrow Night Theme */ 2 | /* http://jmblog.github.com/color-themes-for-google-code-highlightjs */ 3 | /* Original theme - https://github.com/chriskempson/tomorrow-theme */ 4 | /* http://jmblog.github.com/color-themes-for-google-code-highlightjs */ 5 | 6 | /* Tomorrow Comment */ 7 | .hljs-comment { 8 | color: #969896; 9 | } 10 | 11 | /* Tomorrow Red */ 12 | .hljs-variable, 13 | .hljs-attribute, 14 | .hljs-tag, 15 | .hljs-regexp, 16 | .ruby .hljs-constant, 17 | .xml .hljs-tag .hljs-title, 18 | .xml .hljs-pi, 19 | .xml .hljs-doctype, 20 | .html .hljs-doctype, 21 | .css .hljs-id, 22 | .css .hljs-class, 23 | .css .hljs-pseudo { 24 | color: #cc6666; 25 | } 26 | 27 | /* Tomorrow Orange */ 28 | .hljs-number, 29 | .hljs-preprocessor, 30 | .hljs-pragma, 31 | .hljs-built_in, 32 | .hljs-literal, 33 | .hljs-params, 34 | .hljs-constant { 35 | color: #de935f; 36 | } 37 | 38 | /* Tomorrow Yellow */ 39 | .ruby .hljs-class .hljs-title, 40 | .css .hljs-rule .hljs-attribute { 41 | color: #f0c674; 42 | } 43 | 44 | /* Tomorrow Green */ 45 | .hljs-string, 46 | .hljs-value, 47 | .hljs-inheritance, 48 | .hljs-header, 49 | .hljs-name, 50 | .ruby .hljs-symbol, 51 | .xml .hljs-cdata { 52 | color: #b5bd68; 53 | } 54 | 55 | /* Tomorrow Aqua */ 56 | .hljs-title, 57 | .css .hljs-hexcolor { 58 | color: #8abeb7; 59 | } 60 | 61 | /* Tomorrow Blue */ 62 | .hljs-function, 63 | .python .hljs-decorator, 64 | .python .hljs-title, 65 | .ruby .hljs-function .hljs-title, 66 | .ruby .hljs-title .hljs-keyword, 67 | .perl .hljs-sub, 68 | .javascript .hljs-title, 69 | .coffeescript .hljs-title { 70 | color: #81a2be; 71 | } 72 | 73 | /* Tomorrow Purple */ 74 | .hljs-keyword, 75 | .javascript .hljs-function { 76 | color: #b294bb; 77 | } 78 | 79 | .hljs { 80 | display: block; 81 | overflow-x: auto; 82 | background: #1d1f21; 83 | color: #c5c8c6; 84 | } 85 | 86 | .coffeescript .javascript, 87 | .javascript .xml, 88 | .tex .hljs-formula, 89 | .xml .javascript, 90 | .xml .vbscript, 91 | .xml .css, 92 | .xml .hljs-cdata { 93 | opacity: 0.5; 94 | } 95 | 96 | .hljs-addition { 97 | color: #718c00; 98 | } 99 | 100 | .hljs-deletion { 101 | color: #c82829; 102 | } 103 | -------------------------------------------------------------------------------- /documentation/.gitignore: -------------------------------------------------------------------------------- 1 | book 2 | -------------------------------------------------------------------------------- /documentation/README.md: -------------------------------------------------------------------------------- 1 | # C Macro Collections Documentation 2 | 3 | The documentation is currently under construction. 4 | -------------------------------------------------------------------------------- /documentation/book.toml: -------------------------------------------------------------------------------- 1 | [book] 2 | authors = ["Leonardo Vencovsky"] 3 | language = "en" 4 | multilingual = false 5 | src = "src" 6 | title = "CMC Docs" 7 | -------------------------------------------------------------------------------- /documentation/src/Introduction/how_to_install.md: -------------------------------------------------------------------------------- 1 | # How to install 2 | 3 | To start using the library you need first to download the source code. You can either fork the repository or download a zip file containing everything. Either way, after you unzip everything you will end up with the root folder (probably something called `C-Macro-Collections`) and inside it is the `src` folder. 4 | 5 | With the `src` folder you can start including the library headers to your project with `-I path/to/library/src`. 6 | 7 | The library has no external dependencies other than the C standard library. 8 | 9 | ```c 10 | // Include header files like this 11 | #include 12 | #include 13 | #include 14 | ``` 15 | 16 | ```c 17 | // Not like this 18 | #include 19 | #include // dev or cmc? 20 | #include // this will import from the standard library 21 | ``` 22 | -------------------------------------------------------------------------------- /documentation/src/Introduction/how_to_use_the_library.md: -------------------------------------------------------------------------------- 1 | # How to Use the Library 2 | 3 | The C Macro Collections Library comes with very powerful data structures and they are only one macro away from you. 4 | 5 | TODO 6 | -------------------------------------------------------------------------------- /documentation/src/Introduction/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/documentation/src/Introduction/img/logo.png -------------------------------------------------------------------------------- /documentation/src/Introduction/library_structure.md: -------------------------------------------------------------------------------- 1 | # Library Structure 2 | 3 | The `src` folder is subdivided in 5 other folders and one file: 4 | 5 | * __cmc__ - The main C Macro Collections Library 6 | * __dev__ - The main C Macro Collections Library for development 7 | * __sac__ - Statically Allocated Collections 8 | * __utl__ - Utility like ForEach macros, logging, etc 9 | * __macro\_collections.h__ - Master header containing all collections and utilities 10 | 11 | ## cmc 12 | 13 | This is where the C Macro Collections are hosted. 14 | 15 | ## dev 16 | 17 | In this folder is an exact copy of the `cmc` Collections with the added logging utility (`./utl/log.h`). These are useful to debug your code because everything that is happening inside the data structure can be seen. 18 | 19 | ## sac 20 | 21 | This is where the Statically Allocated Collections are hosted. These collections are just like the `cmc` Collections but they have a constant size, a C array, instead of a dynamically allocated array (yes, even for Linked List). 22 | 23 | ## utl 24 | 25 | Utility. Here you will find things like assert macros, foreach macros, logging utility, unit test and timer. 26 | 27 | ## macro_collections.h 28 | 29 | This is the master header. Include this big boy and all functionalities of the library will be in your hands. 30 | -------------------------------------------------------------------------------- /documentation/src/Introduction/understanding_the_library.md: -------------------------------------------------------------------------------- 1 | # Understanding the Library 2 | 3 | Every macro within the library is prefixed by `CMC` or `cmc`. If you have [intellisense](https://code.visualstudio.com/docs/editor/intellisense) you can easily check all the features of the library once you include the necessary headers. 4 | 5 | When you generate code for a certain collection you will need to pass four very important parameters: `SNAME`, `PFX`, `K` and `V`. 6 | 7 | ## SNAME 8 | 9 | `SNAME` is short for struct name. This parameter will define your collection so that its type name becomes: 10 | 11 | ```c 12 | struct SNAME; 13 | ``` 14 | 15 | No `typedef`s are used within the code that is generated in order to not pollute the global namespace. If you wish to `typedef` feel free to give your own naming conventions. 16 | __Every other `struct` that is generated as part of the implementation will be prefixed by `SNAME`__. So iterators and nodes will become: 17 | 18 | ```c 19 | struct SNAME##_iter; 20 | struct SNAME##_node; 21 | ``` 22 | 23 | Note that the `##` is a [Token Pasting Operator](https://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation). 24 | 25 | ## PFX 26 | 27 | `PFX` is a short for prefix. __Every function generated will be within this namespace__. When using [Intellisense](https://code.visualstudio.com/docs/editor/intellisense), write whichever prefix you gave to your functions and everything will be in there. 28 | 29 | Functions that are part of the implementation and shouldn't be called directly are prefixed by `PFX` and then by `_impl_`. Every iterator function will be prefixed by `PFX` and then by `_iter_`. For example: 30 | 31 | ```c 32 | // A function that you shouldn't use directly 33 | size_t PFX##_impl_quick_sort(struct SNAME *list); 34 | ``` 35 | 36 | ```c 37 | // A function that is associated with the iterator 38 | struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); 39 | ``` 40 | 41 | ## K 42 | 43 | `K`, short for Key, is the data type that is only used by associative collections to map a key to a value. 44 | 45 | ## V 46 | 47 | `V`, short for Value, is the primary data type for most collections. 48 | 49 | ## Summarizing 50 | 51 | With `SNAME` and `PFX` a common function definition looks like: 52 | 53 | `size_t PFX##_count(struct SNAME *list);` 54 | 55 | So if input `SNAME` as `i32list` and `PFX` as `i32l` you will have the following function definition: 56 | 57 | `size_t i32l_count(struct i32list *list);` 58 | -------------------------------------------------------------------------------- /documentation/src/Introduction/why_this_library.md: -------------------------------------------------------------------------------- 1 | # Why this library 2 | 3 | C is a low level language and lacks all the data structures that we commonly use. Many high level languages already come with these collections so that we don't have to implement everything ourselves. A lot of third party libraries that implement these missing data structures for the C language usually make use of a void pointers and a lot of macros. __This is why the C Macro Collections Library was created.__ All you need to do is to write down one macro and which data type you wish to work with. The library currently provides many data structures, such as: 4 | 5 | * __Linear Collections__ : List, LinkedList, Deque, Stack, Queue, SortedList 6 | * __Sets__ : HashSet, TreeSet, MultiSet 7 | * __Maps__ : HashMap, TreeMap, MultiMap, BidiMap 8 | * __Heaps__ : Heap, IntervalHeap 9 | 10 | All with the following features: 11 | 12 | * __Type-safe__ - No `void *` pointers are used. A collection of type `int` will only accept integers; 13 | * __Customization__ - Custom struct name and function namespace; 14 | * __Bidirectional Iterators__ - Full support for iterators; 15 | * __Nesting__ - Collections can be nested (List of Lists, HashSet of Stacks, etc); 16 | * __One macro to rule them all__ - Only one macro to generate everything and that's it. 17 | 18 | and many other powerful features (see [Features](../Overview/features_overview.html)). 19 | -------------------------------------------------------------------------------- /documentation/src/Overview/features_overview.md: -------------------------------------------------------------------------------- 1 | # Features Overview 2 | 3 | The C Macro Collections library comes with some core features available to all collections. 4 | 5 | | Feature | Description | 6 | | ------- | ----------- | 7 | | [Callbacks](../cor/callbacks/index.html) | Callbacks are functions that are called when an operation is successful. These operations are divided in 5 categories (create, read, update, delete, resize). | 8 | | [Custom Allocation](../cor/custom_allocation/index.html) | Allows you to use your own custom dynamic memory allocation functions inside the collections. | 9 | | [Error Codes](../cor/error_codes/index.md) | Error codes that can be used to treat certain common errors when operating on a collection. | 10 | | [Functions Table](../cor/functions_table/index.html) | A standard way to access required behaviors from the custom data types. Things like hash, comparison, freeing from memory if needed, etc. | 11 | | [Iterators](../cor/iterators/index.html) | Iterators are a simplified access to the values of a collection. | 12 | -------------------------------------------------------------------------------- /documentation/src/Overview/files_overview.md: -------------------------------------------------------------------------------- 1 | # Files Overview 2 | 3 | An overview of the source files (all `.h`) of the C Macro Collections library. 4 | 5 | * `cmc` - The main C Macro Collections library 6 | * `cor` - Core functionalities of the C Macro Collections libraries 7 | * `core.h` - Core functionalities of the library 8 | * `hashtable.h` - Common things used by hash table based collections 9 | * `utl` - Utilities 10 | * `assert.h` - Non-abortive assert macros 11 | * `foreach.h` - For Each macros 12 | * `futils.h` - Common functions used by Functions Table 13 | * `log.h` - Logging utility with levels of severity 14 | * `test.h` - Simple Unit Test building with macros 15 | * `test.h` - Timing code execution utility 16 | * `bidimap.h` - A bi-directional map based on a hash table 17 | * `deque.h` - A double-ended queue 18 | * `hashmap.h` - A map based on a hash table 19 | * `hashset.h` - A set based on a hash table 20 | * `heap.h` - A binary heap based on a dynamic array 21 | * `intervalheap.h` - A heap that is both Min and Max based on a dynamic array 22 | * `linkedlist.h` - A doubly-linked list 23 | * `list.h` - A dynamic array 24 | * `multimap.h` - A map that accepts multiple keys based on a hash table 25 | * `multiset.h` - A multiset based on a hash table 26 | * `queue.h` - A FIFO based on a circular dynamic array 27 | * `sortedlist.h` - A sorted list based on a dynamic array 28 | * `stack.h` - A LIFO based on a dynamic array 29 | * `treemap.h` - A sorted map based on an AVL tree 30 | * `treeset.h` - A sorted set based on an AVL tree 31 | -------------------------------------------------------------------------------- /documentation/src/Overview/index.md: -------------------------------------------------------------------------------- 1 | # Overview 2 | 3 | In this section you will be able to quickly glance at what are the core functionalities that the C Macro Collections library can bring you. 4 | -------------------------------------------------------------------------------- /documentation/src/SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | - [Introduction](./Introduction/introduction.md) 4 | - [Why this library](./Introduction/why_this_library.md) 5 | - [How to install](./Introduction/how_to_install.md) 6 | - [Library structure](./Introduction/library_structure.md) 7 | - [Understanding the library](./Introduction/understanding_the_library.md) 8 | - [How to use the library](./Introduction/how_to_use_the_library.md) 9 | - [Overview](./Overview/index.md) 10 | - [Collections Overview](./Overview/collections_overview.md) 11 | - [Features Overview](./Overview/features_overview.md) 12 | - [Files Overview](./Overview/files_overview.md) 13 | - [cor](./cor/index.md) 14 | - [Callbacks](./cor/callbacks/index.md) 15 | - [Custom allocation](./cor/custom_allocation/index.md) 16 | - [Error Codes](./cor/error_codes/index.md) 17 | - [Functions Table](./cor/functions_table/index.md) 18 | - [Iterators](./cor/iterators/index.md) 19 | - [cmc](./cmc/index.md) 20 | - [bitset.h](./cmc/bitset.h/index.md) 21 | - [deque.h](./cmc/deque.h/index.md) 22 | - [Functions.h](./cmc/deque.h/functions.md) 23 | - [dev](./dev/index.md) 24 | - [sac](./sac/index.md) 25 | - [utl](./utl/index.md) 26 | - [assert.h](./utl/assert.h/index.md) 27 | - [Overview](./utl/assert.h/overview.md) 28 | - [Valued Assertions](./utl/assert.h/valued_assertions.md) 29 | - [Generic Assertions](./utl/assert.h/generic_assertions.md) 30 | - [foreach.h](./utl/foreach.h/index.md) 31 | - [futils.h](./utl/futils.h/index.md) 32 | - [log.h](./utl/log.h/index.md) 33 | - [Log Functions](./utl/log.h/log_functions.md) 34 | - [Configuration](./utl/log.h/configuration.md) 35 | - [Meanings](./utl/log.h/meanings.md) 36 | - [test.h](./utl/test.h/index.md) 37 | - [timer.h](./utl/timer.h/index.md) 38 | - [Examples](./Examples/examples.md) 39 | -------------------------------------------------------------------------------- /documentation/src/chapter_1.md: -------------------------------------------------------------------------------- 1 | # Chapter 1 2 | -------------------------------------------------------------------------------- /documentation/src/cmc/bidimap.md: -------------------------------------------------------------------------------- 1 | # bidimap.h 2 | 3 | A bidirectional map is a map that allows you to create a bijection in both directions between two sets of elements (K <-> V). It is possible to retrieve the value using a key or retrieve a key using a value. The naming is simply used to differentiate between one set of elements and the other set of elements. There is no difference between key and value since both can be keys and/or values. 4 | 5 | ## BidiMap Implementation 6 | 7 | This implementation uses two arrays of pointers to an entry containing both the key and the value. Robin Hood hashing is used to minimize worst case scenarios. 8 | 9 | ## BidiMap Generation Macro 10 | 11 | ## BidiMap Structures 12 | 13 | ### Members 14 | 15 | ## BidiMap Functions 16 | 17 | ## BidiMap Callback Table 18 | -------------------------------------------------------------------------------- /documentation/src/cmc/hashmap.md: -------------------------------------------------------------------------------- 1 | # hashmap.h 2 | 3 | A HashMap is an implementation of a Map with unique keys, where every key is mapped to a value (K -> V). The keys are not sorted. To access a value you must specify a key. This key is hashed to a number that uniquely represents it and through this it is possible to find in which position of an array of buckets the value is. 4 | 5 | ## HashMap Implementation 6 | 7 | The HashMap is implemented as a flat HashTable meaning that every entry is allocated when the collection is initialized, but they are all empty. 8 | 9 | The HashTable uses [Open Addressing](https://en.wikipedia.org/wiki/Open_addressing) and [Linear Probing](https://en.wikipedia.org/wiki/Linear_probing) to resolve collisions along with [Robin Hood Hashing](https://en.wikipedia.org/wiki/Hash_table) to minimize the worst case scenarios. 10 | -------------------------------------------------------------------------------- /documentation/src/cmc/hashset.md: -------------------------------------------------------------------------------- 1 | # hashset.h 2 | 3 | A HashSet is an implementation of a Set with unique keys. The keys are not sorted. It is implemented as a flat hashtable with linear probing and robin hood hashing. 4 | 5 | ## HashSet Implementation 6 | 7 | The HashSet is implemented as a flat HashTable meaning that every entry is allocated when the collection is initialized, but they are all empty. 8 | 9 | The HashTable uses [Open Addressing](https://en.wikipedia.org/wiki/Open_addressing) and [Linear Probing](https://en.wikipedia.org/wiki/Linear_probing) to resolve collisions along with [Robin Hood Hashing](https://en.wikipedia.org/wiki/Hash_table) to minimize the worst case scenarios. 10 | -------------------------------------------------------------------------------- /documentation/src/cmc/heap.md: -------------------------------------------------------------------------------- 1 | # heap.h 2 | 3 | The Heap is a specialized tree-based data structure which is essentially a complete tree that satisfies the heap property: 4 | 5 | __MaxHeap__ 6 | 7 | - \- The root node is the greatest element of the unique set of keys. 8 | - \- A node key is always greater than or equal to its children keys. 9 | 10 | __Minheap__ 11 | 12 | - \- The root node is the smallest element of the unique set of keys. 13 | - \- A node Key is always greater than or equal to its children keys. 14 | 15 | The heap is mostly used to implement priority queues. Sometimes it can also be used to sort elements. There are three main functions: 16 | 17 | - insert : Adds an element to the heap 18 | - remove_(min/max): Removes the min/max element from the heap 19 | - min/max : Accesses the min/max element from the heap 20 | -------------------------------------------------------------------------------- /documentation/src/cmc/index.md: -------------------------------------------------------------------------------- 1 | # CMC 2 | 3 | The main C Macro Collections. 4 | 5 | The C Macro Collections library was created out of necessity. To create powerful data structures of any type without the constant use of macros and `void *` pointers, but at the same time being [type-safe](https://en.wikipedia.org/wiki/Type_safety). These collections are generated by macros containing a template. These templates allow for some much needed customizations including the data type you wish to handle. 6 | 7 | In the `cmc` folder is the default implementation of all collections. There is also a `dev` folder that pretty much has the same macros that expand to the same code, except that they have an extensive use of logging that might be useful for debugging or getting to know what is happening in your program. There is also the `sac` collections that are statically allocated (have a fixed sized buffer). 8 | 9 | ## Quick Example 10 | 11 | Let's say you need a HashMap mapping keys of type `char *` to `int`. All you need to do is: 12 | 13 | ```c 14 | #include "cmc/hashmap" 15 | 16 | // CMC_GENERATE_HASHMAP(PFX, SNAME, K, V) 17 | CMC_GENERATE_HASHMAP(map, hashmap, char *, int) 18 | 19 | int main(void) 20 | { 21 | // Here you have full functionalities of a HashMap 22 | // No more macros needed! 23 | // Every function can be found in the 'map_' prefix and are all type-safe 24 | } 25 | ``` 26 | 27 | You can customize the functions prefix (`PFX`) and the `struct` name (`SNAME`). The macro `CMC_GENERATE_HASHMAP` will expand to all the code that you will need to operate on a HashMap with `char *` keys and `int` values. The only thing left to do is to define a hash function and a comparator functions for the keys of this map. In case of `char *` you can find these functions in `utl/futils.h` or make your own. To know which collections require which functions check out [Functions Table](../cor/functions_table/index.html) or the documentation for each collection. 28 | -------------------------------------------------------------------------------- /documentation/src/cmc/intervalheap.md: -------------------------------------------------------------------------------- 1 | # intervalheap.h 2 | 3 | An interval heap is a specialized heap with: 4 | 5 | - O(1) - Find Min 6 | - O(1) - Find Max 7 | - O(log n) - Insert 8 | - O(log n) - Remove Min 9 | - O(log n) - Remove Max 10 | -------------------------------------------------------------------------------- /documentation/src/cmc/linkedlist.md: -------------------------------------------------------------------------------- 1 | # linkedlist.h 2 | 3 | A LinkedList is a linear collection of elements, whose order is not given by their physical placement in memory. Instead, each element points to the next (singly-linked list) and to a previous one (doubly-linked list). It consists of nodes connected by these next or previous pointers. Unlike an array, the LinkedList can indefinitely grow. 4 | 5 | ## LinkedList Implementation 6 | 7 | The LinkedList is implemented as a doubly-linked list and allows insertions and removals at both ends in O(1) and in a given index in O(N). The list has a head and tail pointer. The head points to the first element in the sequence and tail points to the last. 8 | -------------------------------------------------------------------------------- /documentation/src/cmc/list.md: -------------------------------------------------------------------------------- 1 | # list.h 2 | 3 | A List is a Dynamic Array that store its elements contiguously. It has an internal buffer that is pre-allocated with a greater capacity than necessary in order to foresee new elements being added. The items can be added at both ends of the list and at the middle (with a given index). 4 | 5 | ## List Implementation 6 | 7 | Insertions at the start are more costly as it is necessary to shift all elements one position to the right to give space to a new element being added at index 0. Insertions at the end are instantaneous as long as there is enough space in the buffer. Insertions at the middle will be based on an index. The index represents where the element being added will be located once the operation ends. This operation might also need to shift elements. It is also possible to add arrays of elements in the list. When the buffer is filled, it is reallocated with a greater capacity, usually being doubled. 8 | 9 | Removing elements follows the same principle. Removing the front element will require to shift all other elements one position to the left, thus being slower than removing from the end in which is done in constant time. Removing elements in the middle of the list will also require shifting elements to the left. Is is also possible to remove a range of elements or extract them, creating a new list with the removed items. 10 | 11 | The iterator is a simple structure that is capable of going back and forwards. Any modifications to the target list during iteration is considered undefined behavior. Its sole purpose is to facilitate navigation through a list. 12 | -------------------------------------------------------------------------------- /documentation/src/cmc/multimap.md: -------------------------------------------------------------------------------- 1 | # multimap.h 2 | 3 | The MultiMap is a Map that allows for multiple keys. This is a data structure that has a very narrow usage. A Map (either TreeMap or HashMap) can also work like a MultiMap if a certain key is mapped to another collection. 4 | 5 | `Map>` maps an integer to a list of integer. 6 | 7 | `MultiMap` maps many integer keys to integer values. 8 | 9 | The difference is that in a MultiMap you can store keys that are the same but might be different instances of the same value. This is also relevant if your data type is more complex like a struct where its ID is the same but some other members of this data type are different. 10 | 11 | ## MultiMap Implementation 12 | 13 | The map uses separate chaining and robin hood hashing. Its internal buffer is made of a pair of pointers to entries that represent a linked list. So accessing map->buffer[0] would give the first pair of pointers. Each entry has both a pointer to a previous entry and a next entry. This design choice was made so that every collection in this library has a two-way iterator with a very few exceptions. 14 | 15 | Each entry is composed of a Key and a Value. Entries with the same key should always hash to the same linked list. Also, keys that hash to the same bucket will also be in the same linked list. 16 | 17 | The order of inserting and removing the same keys will behave like a FIFO. So the first key added will be the first to be removed. 18 | -------------------------------------------------------------------------------- /documentation/src/cmc/multiset.md: -------------------------------------------------------------------------------- 1 | # multiset.h 2 | 3 | In mathematics, a multiset is a modification of the concept of a set that, unlike a set, allows for multiple instances for each of its elements. The positive integer number of instances, given for each element is called the multiplicity of this element in the multiset. A MultiSet also has a cardinality which equals the sum of the multiplicities of its elements. 4 | -------------------------------------------------------------------------------- /documentation/src/cmc/queue.md: -------------------------------------------------------------------------------- 1 | # queue.h 2 | 3 | A Queue is a First-In First-out (or Last-in Last-out) data structure. It is a Dynamic Circular Array where elements are added from one end of the array and removed from the other end. The circular array here (also known as circular buffer or ring buffer) is very important so that both adding and removing elements from the Queue are done instantly. The array is linear but with the modulo operator it is treated as a circular sequence of elements. 4 | 5 | ## Queue Implementation 6 | 7 | If the Queue was implemented as a regular Dynamic Array, when adding or removing an element at the front, it would be necessary to shift all elements currently present in the Queue and this would add up a lot of computing time. Shifting `100000` elements in memory by one position every time an element is added to the Queue is simply not efficient. 8 | 9 | The Queue has two ends. The `front` and `back`. In this implementation all elements are added to the back of the Queue and removed from the front, which is more or less how queues work in real life. Unlike a Stack that only has operations at one end of the buffer, the Queue needs to be implemented as a circular array in order to quickly add or remove elements. 10 | 11 | The Queue has three main functions: `enqueue` which adds an element to the Queue; `dequeue` which removes an element from the Queue; and `peek` which return the element at the front of the Queue, that is, the next element to be removed from it. 12 | 13 | The Queue is used in many applications where a resource is shared among multiple consumers and the Queue is responsible for scheduling the access to the resource. 14 | -------------------------------------------------------------------------------- /documentation/src/cmc/sortedlist.md: -------------------------------------------------------------------------------- 1 | # sortedlist.h 2 | 3 | A SortedList is a dynamic array, meaning that you can store as many elements as you like and when its capacity is full, the buffer is reallocated. The elements are only sorted when a certain action requires that the array is sorted like accessing min() or max(). This prevents the array from being sorted after every insertion or removal. The array is sorted using a variation of quick sort that uses insertion sort for small partitions. 4 | -------------------------------------------------------------------------------- /documentation/src/cmc/stack.md: -------------------------------------------------------------------------------- 1 | # stack.h 2 | 3 | A Stack is a Last-in First-out (or First-in Last-out) data structure used in a variety of algorithms. It is a Dynamic Array that can only add or remove its elements at the end of the buffer, in this case, it represents the top of the stack. 4 | 5 | ## Stack Implementation 6 | 7 | It has three main functions: `push` which adds an element at the top of the stack; `pop` which removes the top element from the stack; and `top` which returns the top element without removing it (it is also sometimes called `peek`). 8 | 9 | A Stack is used in algorithms like backtracking, depth-first search, expression evaluation, syntax parsing and many more. 10 | -------------------------------------------------------------------------------- /documentation/src/cmc/treemap.md: -------------------------------------------------------------------------------- 1 | # treemap.h 2 | 3 | A TreeMap is an implementation of a Map that keeps its keys sorted. Like a Map, it has only unique keys. This implementation uses a balanced binary tree called AVL Tree that uses the height of nodes to keep its keys balanced. 4 | -------------------------------------------------------------------------------- /documentation/src/cmc/treeset.md: -------------------------------------------------------------------------------- 1 | # treeset.h 2 | 3 | A TreeSet is an implementation of a Set that keeps its elements sorted. Like a Set it has only unique keys. This implementation uses a balanced binary tree called AVL Tree that uses the height of nodes to keep its keys balanced. 4 | -------------------------------------------------------------------------------- /documentation/src/cor/callbacks/index.md: -------------------------------------------------------------------------------- 1 | # Callbacks 2 | 3 | Every collection can have an optional callback node. In this node there are five functions: 4 | 5 | | Callback | Description | 6 | | -------- | ----------------------------------------------------------------------- | 7 | | `create` | Is called when an element was successfully added to the collection | 8 | | `read` | Is called when the collection was successfully queried about an element | 9 | | `update` | Is called when an element in the collection was successfully updated | 10 | | `delete` | Is called when an element was successfully removed from the collection | 11 | | `resize` | Is called when the collection was full and successfully resized | 12 | 13 | Check the documentation for each collection to see which functions call which callbacks. 14 | 15 | ## cmc_callbacks 16 | 17 | Every collection has a pointer to this `struct`. If it is `NULL` then the collection has no callbacks. Also, individual callbacks can be optional if set to `NULL`. 18 | 19 | ```c 20 | struct cmc_callbacks 21 | { 22 | void (*create)(void); 23 | void (*read)(void); 24 | void (*update)(void); 25 | void (*delete)(void); 26 | void (*resize)(void); 27 | }; 28 | ``` 29 | 30 | ## Example 31 | 32 | ```c 33 | #include "cmc/heap.h" 34 | #include "utl/futils.h" // cmc_i32_cmp 35 | 36 | // Generate a heap of integers 37 | CMC_GENERATE_HEAP(i32h, i32heap, int) 38 | 39 | void heap_on_create(void) 40 | { 41 | printf("An element was added to the heap\n"); 42 | } 43 | 44 | int main(void) 45 | { 46 | // create() is set but the other callbacks are not 47 | struct cmc_callbacks my_callbacks = { .create = heap_on_create, NULL }; 48 | 49 | // Create a max heap with the callbacks 50 | struct i32heap *heap = i32h_new_custom( 51 | 100, cmc_max_heap, &(struct i32heap_fval){ .cmp = cmc_i32_cmp }, NULL, 52 | &my_callbacks); 53 | 54 | i32h_insert(heap, 10); 55 | i32h_insert(heap, 11); 56 | i32h_insert(heap, 12); 57 | 58 | i32h_free(heap); 59 | } 60 | ``` 61 | -------------------------------------------------------------------------------- /documentation/src/cor/custom_allocation/index.md: -------------------------------------------------------------------------------- 1 | # Custom Allocation 2 | 3 | All collections have an allocation node. This node can be modified so that every allocation and de-allocation can be done with custom functions. Every custom allocation function must follow the same prototype of the `stdlib.h` functions. 4 | 5 | ## cmc_alloc_node 6 | 7 | ```c 8 | struct cmc_alloc_node 9 | { 10 | void *(*malloc)(size_t); 11 | void *(*calloc)(size_t, size_t); 12 | void *(*realloc)(void *, size_t); 13 | void (*free)(void *); 14 | }; 15 | ``` 16 | 17 | ## cmc_alloc_node_default 18 | 19 | The default allocation node. If a collections is not initialized with a custom allocation node, this will be the default one using the functions from the C standard library. 20 | 21 | ```c 22 | static struct cmc_alloc_node cmc_alloc_node_default = { malloc, calloc, 23 | realloc, free }; 24 | ``` 25 | 26 | ## Example 27 | 28 | ```c 29 | #include "cmc/heap.h" 30 | #include "utl/futils.h" // cmc_i32_cmp 31 | 32 | // Total bytes allocated 33 | size_t total = 0; 34 | 35 | void *my_malloc(size_t size) 36 | { 37 | void *result = malloc(size); 38 | 39 | if (!result) 40 | return result; 41 | 42 | total += size; 43 | return result; 44 | } 45 | 46 | void *my_calloc(size_t count, size_t size) 47 | { 48 | void *result = calloc(count, size); 49 | 50 | if (!result) 51 | return result; 52 | 53 | total += count * size; 54 | return result; 55 | } 56 | 57 | void *my_realloc(void *block, size_t size) 58 | { 59 | void *result = realloc(block, size); 60 | 61 | if (!result) 62 | return result; 63 | 64 | total += size; 65 | return result; 66 | } 67 | 68 | // Generate a heap of integers 69 | CMC_GENERATE_HEAP(i32h, i32heap, int) 70 | 71 | int main(void) 72 | { 73 | // My custom allocation node 74 | struct cmc_alloc_node node = { .malloc = my_malloc, 75 | .calloc = my_calloc, 76 | .realloc = my_realloc, 77 | .free = free }; 78 | 79 | // Create a max heap with the custom allocation node 80 | struct i32heap *heap = i32h_new_custom( 81 | 100, cmc_max_heap, &(struct i32heap_fval){ .cmp = cmc_i32_cmp }, &node, 82 | NULL); 83 | 84 | for (int i = 0; i < 100000; i++) 85 | i32h_insert(heap, i); 86 | 87 | i32h_free(heap); 88 | 89 | printf("Total bytes allocated : %" PRIuMAX "\n", total); 90 | } 91 | ``` 92 | -------------------------------------------------------------------------------- /documentation/src/cor/error_codes/index.md: -------------------------------------------------------------------------------- 1 | # Error Codes 2 | 3 | Error codes are ways to signal errors or success. It is accessed through the `flag` component of a `struct` or through a function that is present in every collection: 4 | 5 | ```c 6 | int PFX##_flag(struct SNAME *_collection_); 7 | ``` 8 | 9 | Some functions have a return type of `bool` indicating if it exited successfully or not. These error codes can be used for further error handling. 10 | 11 | ## cmc_flags 12 | 13 | It is a global `struct` with default values for error codes. 14 | 15 | ```c 16 | static struct 17 | { 18 | int OK; 19 | int ALLOC; 20 | int EMPTY; 21 | int NOT_FOUND; 22 | int INVALID; 23 | int RANGE; 24 | int DUPLICATE; 25 | int ERROR; 26 | } cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 }; 27 | ``` 28 | 29 | * `OK` - No errors. The operation exited successfully. 30 | * `ALLOC` - Allocation failed. 31 | * `EMPTY` - The collection is empty when it shouldn't. 32 | * `NOT_FOUND` - Key or value not found. 33 | * `INVALID` - Invalid argument or operation given the collection's state 34 | * `RANGE` - Index out of range. 35 | * `DUPLICATE` - Duplicate key or value. 36 | * `ERROR` - Generic error. Usually caused by algorithm errors. 37 | 38 | ## cmc_flags_to_str 39 | 40 | Maps the integer representation of error codes to their character representation. 41 | 42 | ```c 43 | const char *cmc_flags_to_str[8] = { "OK", "ALLOC", "EMPTY", 44 | "NOT_FOUND", "INVALID", "RANGE", 45 | "DUPLICATE", "ERROR" }; 46 | ``` 47 | 48 | ## Example 49 | 50 | ```c 51 | #include "cmc/treeset.h" 52 | #include "utl/futils.h" // cmc_i32_cmp 53 | 54 | // Generate a sorted set of integers 55 | CMC_GENERATE_TREESET(tset, sortedset, int) 56 | 57 | int main(void) 58 | { 59 | struct sortedset *set = 60 | tset_new(&(struct sortedset_fval){ .cmp = cmc_i32_cmp }); 61 | 62 | if (!tset_insert(set, 10)) 63 | printf("Error! %s\n", cmc_flags_to_str[tset_flag(set)]); 64 | 65 | if (!tset_insert(set, 10)) 66 | printf("Error! %s\n", cmc_flags_to_str[tset_flag(set)]); 67 | 68 | if (!tset_remove(set, 10)) 69 | printf("Error! %s\n", cmc_flags_to_str[tset_flag(set)]); 70 | 71 | if (!tset_remove(set, 10)) 72 | printf("Error! %s\n", cmc_flags_to_str[tset_flag(set)]); 73 | 74 | tset_free(set); 75 | } 76 | ``` 77 | -------------------------------------------------------------------------------- /documentation/src/cor/index.md: -------------------------------------------------------------------------------- 1 | # COR 2 | 3 | Core functionalities of the C Macro Collections Library. 4 | 5 | -------------------------------------------------------------------------------- /documentation/src/cor/iterators/index.md: -------------------------------------------------------------------------------- 1 | # Iterators 2 | 3 | Every collection comes with an interface of iterators where you can easily access the elements of a collection. These can move at any direction, can start at any 'end' of a collection and can move any amount of steps per iteration. Every collection generated comes with an iterator. 4 | 5 | By design choice these iterators do not support modifications to the collection. If a collection is modified, all iterators that have been initialized will most likely be invalidated and may cause undefined behavior if used afterwards. 6 | -------------------------------------------------------------------------------- /documentation/src/dev/index.md: -------------------------------------------------------------------------------- 1 | # DEV 2 | 3 | The development collections. These collections are an exact copy of the `cmc` collections but they come with many logging throughout the generated code. Useful for debugging or during development. 4 | -------------------------------------------------------------------------------- /documentation/src/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeoVen/C-Macro-Collections/99f08e2b6e446e03fa3c7321b58e40ff3796d747/documentation/src/img/logo.png -------------------------------------------------------------------------------- /documentation/src/sac/index.md: -------------------------------------------------------------------------------- 1 | # SAC 2 | 3 | The Statically Allocated Collections. These collections have a fixed size. Its size is one of the parameters during macro expansion, producing a C array. 4 | 5 | ## CMC vs. SAC 6 | 7 | The `cmc` collections hold their data either with nodes or a buffer that can be reallocated. The `sac` collections only have an array with fixed size and don't perform any allocation on the heap. 8 | 9 | ```c 10 | // A CMC List 11 | struct List 12 | { 13 | T *buffer; 14 | /* Other fields */ 15 | }; 16 | 17 | // A SAC List 18 | struct List 19 | { 20 | T buffer[SIZE]; 21 | /* Other fields */ 22 | }; 23 | ``` 24 | -------------------------------------------------------------------------------- /documentation/src/utl/assert.h/overview.md: -------------------------------------------------------------------------------- 1 | # Overview 2 | 3 | ## Single Value Assertion Macros 4 | 5 | * __cmc_assert__ - Asserts that an expression evaluates to true; 6 | * __cmc_assert_equals__ - Asserts that a value from type `dtype` equals another; 7 | * __cmc_assert_not_equals__ - Asserts that value from type `dtype` does not equals another; 8 | * __cmc_assert_greater__ - Asserts that a value from type `dtype` is greater than a lower bound; 9 | * __cmc_assert_greater_equals__ - Asserts that a value from type `dtpype` is greater than or equal to a lower bound; 10 | * __cmc_assert_lesser__ - Asserts that a value from type `dtpype` is lesser than an upper bound; 11 | * __cmc_assert_lesser_equals__ - Asserts that a value from type `dtpype` is lesser than or equal to an upper bound; 12 | * __cmc_assert_in_range__ - Asserts that a value from type `dtpype` is within a certain range; 13 | * __cmc_assert_not_in_range__ - Asserts that a value from type `dtpype` is outside of a certain range. 14 | 15 | ## Array Assertion Macros 16 | 17 | * __cmc_assert_array_equals_any__ - Assert that two arrays are equal element by element; 18 | * __cmc_assert_array_within_any__ - Assert that each element in the array are within a bounds; 19 | * __cmc_assert_array_outside_any__ - Assert that each element in the array are out of bounds; 20 | * __cmc_assert_array_sorted_any__ - Assert that the array is sorted. 21 | -------------------------------------------------------------------------------- /documentation/src/utl/foreach.h/index.md: -------------------------------------------------------------------------------- 1 | # foreach.h 2 | 3 | For-Each macros. Since doing a for loop with iterators can be a lot to write, these macros solve that problem. There are two of them: 4 | 5 | * `CMC_FOREACH` - Goes from the beginning of a Collection to the end 6 | * `CMC_FOREAC_REV` - Goes from the end of a Collection to the beginning 7 | 8 | ## CMC_FOREACH 9 | 10 | ```c 11 | #define CMC_FOREACH(PFX, SNAME, ITERNAME, TARGET) 12 | ``` 13 | 14 | * `PFX` - Functions prefix 15 | * `SNAME` - Struct name 16 | * `ITERNAME` - Iterator variable name 17 | * `TARGET` - Target collection variable name 18 | 19 | ## CMC_FOREACH_REV 20 | 21 | ```c 22 | #define CMC_FOREACH_REV(PFX, SNAME, ITERNAME, TARGET) 23 | ``` 24 | 25 | * `PFX` - Functions prefix 26 | * `SNAME` - Struct name 27 | * `ITERNAME` - Iterator variable name 28 | * `TARGET` - Target collection variable name 29 | 30 | ## Example 31 | 32 | ```c 33 | #include "cmc/list.h" 34 | #include "utl/foreach.h" 35 | 36 | CMC_GENERATE_LIST(i32l, i32list, int32_t) 37 | 38 | int main(void) 39 | { 40 | struct i32list *list = i32l_new(100, &(struct i32list_fval) { NULL }); 41 | 42 | for (int i = 0; i < 100; i++) 43 | i32l_push_back(list, i); 44 | 45 | CMC_FOREACH(i32l, i32list, it, list) 46 | { 47 | int value = i32l_iter_value(&it); 48 | size_t index = i32l_iter_index(&it); 49 | 50 | if (index == 0) 51 | printf("[ %d, ", value); 52 | else if (index == i32l_count(list) - 1) 53 | printf("%d ]\n", value); 54 | else 55 | printf("%d, ", value); 56 | } 57 | 58 | i32l_free(list); 59 | } 60 | ``` -------------------------------------------------------------------------------- /documentation/src/utl/index.md: -------------------------------------------------------------------------------- 1 | # UTL 2 | 3 | Inside the `./utl/` folder there are many utility functions and macros that can be extensively used with the C Macro Collections library. Most of these utilities can be used without the main library. 4 | 5 | ## Contents 6 | 7 | * [assert.h](assert.h/index.html) - Non-abortive assert macros 8 | * [foreach.h](foreach.h/index.html) - For Each macros 9 | * [futils.h](futils.h/index.html) - Common functions used by Functions Table 10 | * [log.h](log.h/index.html) - Logging utility with levels of severity 11 | * [test.h](test.h/index.html) - Simple Unit Test building with macros 12 | * [timer.h](timer.h/index.html) - Timing code execution utility 13 | -------------------------------------------------------------------------------- /documentation/src/utl/log.h/configuration.md: -------------------------------------------------------------------------------- 1 | # Configuration 2 | 3 | ## cmc_log_config 4 | 5 | This is an anonymous static `struct` that can be directly modified to change logging levels, enable or disable output or change file output. 6 | 7 | ```c 8 | static struct 9 | { 10 | enum cmc_log_type tlevel; 11 | enum cmc_log_type flevel; 12 | bool tenabled; 13 | bool fenabled; 14 | bool enabled; 15 | FILE * file; 16 | } cmc_log_config = { 0, 0, true, true, true, NULL }; 17 | ``` 18 | 19 | __Members__ 20 | 21 | * `tlevel` - Terminal log level (`stderr`) 22 | * `flevel` - File log level 23 | * `tenabled` - Terminal logging enabled if `true` 24 | * `fenabled` - File logging enabled if `true` 25 | * `enabled` - Enables or disables all logs 26 | * `FILE *file` - File for output 27 | 28 | This configuration is global to every log. The `FILE` pointer is not handled by the API and needs to be able to write to the file. 29 | 30 | ## Log Level 31 | 32 | By tuning `tlevel` and `flevel` (both accessed from `cmc_log_config`) you can filter which log messages are printed or not. 33 | 34 | * If the log level is `0`, all logs are enabled 35 | * If the log level `X` is positive: 36 | * All levels less than `X` will be disabled 37 | * If `X` is greater than the level of `FATAL`, all logs are disabled 38 | * If the log level `X` is negative: 39 | * All levels greater than `abs(X)` are enabled 40 | * If `abs(X)` is greater than the level of `FATAL`, all logs are enabled 41 | 42 | __Examples__ 43 | 44 | If you want to output only `TRACE` logs to a file and the rest to `stderr`: 45 | 46 | * `cmc_log_config.tlevel = CMC_LOG_DEBUG` 47 | * `cmc_log_config.flevel = -CMC_LOG_TRACE` 48 | 49 | If you want to output only `FATAL` to `stderr` and the rest to a file: 50 | 51 | * `cmc_log_config.tlevel = CMC_LOG_FATAL` 52 | * `cmc_log_config.flevel = -CMC_LOG_ERROR` 53 | 54 | If you want to output only `FATAL` and `ERROR` to a file an nothing to `stderr`: 55 | 56 | * `cmc_log_config.tlevel = CMC_LOG_FATAL + 1` 57 | * `cmc_log_config.flevel = CMC_LOG_ERROR` 58 | 59 | __Summary__ 60 | 61 | * Log Level > 0: "Allow all log levels that are greater than this, including it" 62 | * Log Level < 0: "Allow all log levels that are smaller than this, including it" 63 | -------------------------------------------------------------------------------- /documentation/src/utl/log.h/index.md: -------------------------------------------------------------------------------- 1 | # log.h 2 | 3 | Simple and customizable logging macros. Messages can have a custom logging level and a custom file output. This customization can be done via the global variable [`cmc_log_config`](configuration.html#cmc_log_config). Logs are outputted to `stderr`. 4 | 5 | ## cmc_log_type 6 | 7 | Defines the possible Log Levels. These are used to categorize log messages. The higher the number, the more severe or important that message is. Check out [here](meanings.html) 8 | 9 | ```c 10 | enum cmc_log_type 11 | { 12 | CMC_LOG_TRACE = 1, 13 | CMC_LOG_DEBUG = 2, 14 | CMC_LOG_INFO = 3, 15 | CMC_LOG_WARN = 4, 16 | CMC_LOG_ERROR = 5, 17 | CMC_LOG_FATAL = 6 18 | }; 19 | ``` 20 | 21 | ## CMC_LOG_COLOR 22 | 23 | If this macro is defined, logging will be outputted with [ANSI color escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors). If you are logging into a file, these escape codes are not printed. 24 | -------------------------------------------------------------------------------- /documentation/src/utl/log.h/log_functions.md: -------------------------------------------------------------------------------- 1 | # Log Functions 2 | 3 | ## cmc_log_trace 4 | 5 | Defines a message with a Log Level of `CMC_LOG_TRACE`. 6 | 7 | ```c 8 | #define cmc_log_trace(fmt, ...) 9 | ``` 10 | 11 | * `fmt` - A string that contains a text to be written that may contain format specifiers 12 | * `...` - Optional. Values to be formatted into the string that will be printed 13 | 14 | ## cmc_log_debug 15 | 16 | Defines a message with a Log Level of `CMC_LOG_DEBUG`. 17 | 18 | ```c 19 | #define cmc_log_debug(fmt, ...) 20 | ``` 21 | 22 | * `fmt` - A string that contains a text to be written that may contain format specifiers 23 | * `...` - Optional. Values to be formatted into the string that will be printed 24 | 25 | ## cmc_log_info 26 | 27 | Defines a message with a Log Level of `CMC_LOG_INFO`. 28 | 29 | ```c 30 | #define cmc_log_info(fmt, ...) 31 | ``` 32 | 33 | * `fmt` - A string that contains a text to be written that may contain format specifiers 34 | * `...` - Optional. Values to be formatted into the string that will be printed 35 | 36 | ## cmc_log_warn 37 | 38 | Defines a message with a Log Level of `CMC_LOG_WARN`. 39 | 40 | ```c 41 | #define cmc_log_warn(fmt, ...) 42 | ``` 43 | 44 | * `fmt` - A string that contains a text to be written that may contain format specifiers 45 | * `...` - Optional. Values to be formatted into the string that will be printed 46 | 47 | ## cmc_log_error 48 | 49 | Defines a message with a Log Level of `CMC_LOG_ERROR`. 50 | 51 | ```c 52 | #define cmc_log_error(fmt, ...) 53 | ``` 54 | 55 | * `fmt` - A string that contains a text to be written that may contain format specifiers 56 | * `...` - Optional. Values to be formatted into the string that will be printed 57 | 58 | ## cmc_log_fatal 59 | 60 | Defines a message with a Log Level of `CMC_LOG_FATAL`. 61 | 62 | ```c 63 | #define cmc_log_fatal(fmt, ...) 64 | ``` 65 | 66 | * `fmt` - A string that contains a text to be written that may contain format specifiers 67 | * `...` - Optional. Values to be formatted into the string that will be printed 68 | -------------------------------------------------------------------------------- /documentation/src/utl/log.h/meanings.md: -------------------------------------------------------------------------------- 1 | # Meanings 2 | 3 | What does each Log Level means? It depends on the application. In here you will read more about how the C Macro Collections library uses each of these Log Levels, but this does not mean you should use them like described. 4 | 5 | * __TRACE__ - Trace is used to describe the inner workings of a function or an algorithm. These messages can be used extensively, meaning that their content is probably not very useful, even for debugging. 6 | * __DEBUG__ - Mainly used for debugging, tracking certain variables, checking if pointers were deallocated or anything useful to help visualizing what your program is doing in general. 7 | * __INFO__ - Info can be used as a heads up. Something that is important to note, but is not quite a warning or an error. 8 | * __WARN__ - Warnings are attributed to all cases where the function can recover from and usually treated by it. In most cases the user can treat these warnings himself. 9 | * __ERROR__ - Something really bad happened. Your program will not crash yet but depending on what comes next, it will. This might be attributed to pointers that shouldn't be null because later functions might use it. 10 | * __FATAL__ - This will probably be the last logging message before your program crashes or goes into madness. Fatal errors are commonly attributed to dereferencing NULL pointers. 11 | -------------------------------------------------------------------------------- /documentation/src/utl/timer.h/index.md: -------------------------------------------------------------------------------- 1 | # timer.h 2 | 3 | A very simple library that encapsulates `clock()` from ``, making life easier. 4 | 5 | ## cmc_timer 6 | 7 | ```c 8 | struct cmc_timer 9 | { 10 | clock_t start; 11 | clock_t stop; 12 | double result; 13 | }; 14 | ``` 15 | 16 | * `clock_t start` - Start of timing 17 | * `clock_t stop` - End of timing 18 | * `double result` - Result in milliseconds 19 | 20 | ## cmc_timer_start 21 | 22 | Starts the timer by setting `start` with `clock()`. 23 | 24 | ```c 25 | #define cmc_timer_start(timer) 26 | ``` 27 | 28 | * `timer` - A `struct cmc_timer` variable 29 | 30 | ## cmc_timer_stop 31 | 32 | Sets the `stop` variable with `clock()` and calculates the `result` in milliseconds. 33 | 34 | ```c 35 | #define cmc_timer_start(timer) 36 | ``` 37 | 38 | * `timer` - A `struct cmc_timer` variable 39 | 40 | ## Example 41 | 42 | ```c 43 | #include 44 | 45 | #include "utl/timer.h" 46 | 47 | int main(void) 48 | { 49 | struct cmc_timer t; 50 | 51 | cmc_timer_start(t); 52 | 53 | size_t total = 0; 54 | 55 | for (size_t i = 0; i < 1000000000; i++) 56 | total += i; 57 | 58 | cmc_timer_stop(t); 59 | 60 | printf("Sum took %.0lf milliseconds to calculate\n", t.result); 61 | } 62 | ``` 63 | 64 | -------------------------------------------------------------------------------- /examples/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -O3 3 | INCLUDE=-I .. 4 | 5 | ifndef FILE 6 | main: 7 | @echo Please define a FILE parameter to run. Examples: 8 | @echo " dfs" 9 | @echo " example" 10 | @echo " hanoi" 11 | @echo " heapsort" 12 | @echo " ice_cream" 13 | @echo " sieve" 14 | else 15 | $(FILE): $(FILE).c 16 | $(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) 17 | ./$@.exe 18 | endif 19 | 20 | 21 | clean: 22 | rm *.exe -f 23 | rm *.txt -f 24 | 25 | 26 | memcheck: 27 | ifndef FILE 28 | $(error FILE parameter must be set first to check for leaks) 29 | else 30 | $(CC) $(FILE).c -o mem.exe $(CFLAGS) $(INCLUDE) -g 31 | valgrind --leak-check=yes ./mem.exe 32 | endif 33 | -------------------------------------------------------------------------------- /examples/callbacks.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define CMC_CALLBACKS 4 | 5 | #define V int 6 | #define PFX l 7 | #define SNAME list 8 | #include "cmc/list.h" 9 | 10 | #define V int 11 | #define PFX s 12 | #define SNAME stack 13 | #include "cmc/stack.h" 14 | 15 | #define V int 16 | #define PFX b 17 | #define SNAME buffer 18 | #define SIZE 100 19 | #define CMC_SAC 20 | #include "cmc/list.h" 21 | 22 | void callback(const char fname[], void *self) 23 | { 24 | if (fname[0] == 'l') 25 | { 26 | struct list *l = (struct list *)self; 27 | if (l_flag(l) != CMC_FLAG_OK) 28 | { 29 | printf("Error at %s: %s\n", fname, cmc_flags_to_str[l_flag(l)]); 30 | } 31 | } 32 | printf("Function called: %s for collection at %p\n", fname, self); 33 | } 34 | 35 | int main() 36 | { 37 | struct list *l = l_new_custom(100, &(struct list_fval){}, NULL, callback); 38 | 39 | l_get(l, 0); /* Should error */ 40 | 41 | l_push_back(l, 10); 42 | l_push_front(l, 5); 43 | 44 | printf("Got: %d\n", l_get(l, 1)); 45 | 46 | l_pop_back(l); 47 | l_pop_back(l); 48 | l_pop_back(l); /* Error */ 49 | 50 | struct stack *s = s_new_custom(100, &(struct stack_fval){}, NULL, callback); 51 | 52 | s_push(s, 10); 53 | s_push(s, 5); 54 | 55 | printf("Got: %d\n", s_top(s)); 56 | 57 | struct buffer *b = b_new_custom(&(struct buffer_fval){}, NULL, callback); 58 | 59 | b_push_back(b, 10); 60 | b_push_front(b, 5); 61 | 62 | printf("Got: %d\n", b_get(b, 1)); 63 | 64 | l_free(l); 65 | s_free(s); 66 | b_free(b); 67 | } 68 | -------------------------------------------------------------------------------- /examples/dev_example.c: -------------------------------------------------------------------------------- 1 | /* Shows an example of how to use CMC_DEV */ 2 | 3 | /** 4 | * CMC_DEV is a macro that enables DEV mode. It comes with tracing of functions 5 | * and other things that are printed in the terminal in order to debug the code. 6 | * 7 | * You can always compile this with -DCMC_DEV, but in this example we explicitly 8 | * define it in the file. 9 | * 10 | * CMC_DEV uses the cmc/utl/log.h utility. Through it, you can customize the log 11 | * levels and when to enable it. 12 | */ 13 | #define CMC_DEV 14 | #define CMC_LOG_COLOR /* Optional */ 15 | 16 | #define V int 17 | #define SNAME list 18 | #define PFX l 19 | #define CMC_EXT_ITER 20 | #define CMC_EXT_STR 21 | #include "cmc/list.h" 22 | 23 | #include "cmc/utl/futils.h" 24 | 25 | int main() 26 | { 27 | struct list *list = l_new(32, &(struct list_fval){ .str = cmc_i32_str }); 28 | 29 | if (!list || l_flag(list) != CMC_FLAG_OK) 30 | { 31 | #ifdef CMC_DEV 32 | cmc_log_error("error: %s", cmc_flags_to_str[l_flag(list)]); 33 | #endif 34 | return 1; 35 | } 36 | 37 | #ifdef CMC_DEV 38 | /* We don't want to debug this, so disable it for now */ 39 | cmc_log_config.enabled = false; 40 | #endif 41 | 42 | for (int i = 0; i < 100; i++) 43 | { 44 | if (!l_push_back(list, i)) 45 | { 46 | enum cmc_flags flag = l_flag(list); 47 | fprintf(stderr, "%s : push_back failed\n", cmc_flags_to_str[flag]); 48 | } 49 | } 50 | 51 | /* Turn it back on on the operations that we actually care to debug */ 52 | #ifdef CMC_DEV 53 | cmc_log_config.enabled = true; 54 | #endif 55 | 56 | for (struct list_iter it = l_iter_start(list); !l_iter_at_end(&it); l_iter_next(&it)) 57 | { 58 | int *val = l_iter_rvalue(&it); 59 | 60 | if (val != NULL) 61 | *val *= 2; 62 | } 63 | 64 | if (!l_print(list, stdout, "[", ", ", "]\n")) 65 | return 2; 66 | 67 | // Free all of its resources 68 | l_free(list); 69 | } 70 | -------------------------------------------------------------------------------- /examples/dfs.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Example of In-Order Depth-First Search using: 3 | * - A Stack 4 | * - Recursion 5 | * - Without both 6 | */ 7 | 8 | #define CMC_CAMEL_CASE 9 | 10 | #include "cmc/cor/core.h" 11 | #include "cmc/utl/assert.h" 12 | #include "cmc/utl/futils.h" 13 | #include "cmc/utl/log.h" 14 | 15 | #define PFX tree 16 | #define SNAME BinaryTree 17 | #define V int32_t 18 | #define CMC_EXT_ITER 19 | #define CMC_EXT_STR 20 | #include "cmc/treeset.h" 21 | 22 | #define PFX stk 23 | #define SNAME Stack 24 | #define V struct BinaryTreeNode * 25 | #include "cmc/stack.h" 26 | 27 | void DFS(struct BinaryTreeNode *root) 28 | { 29 | if (!root) 30 | return; 31 | 32 | DFS(root->left); 33 | 34 | fprintf(stdout, "%d ", root->value); 35 | 36 | DFS(root->right); 37 | } 38 | 39 | int main(void) 40 | { 41 | struct BinaryTree *tree = tree_new(&(struct BinaryTreeFVal){ .cmp = cmc_i32_cmp, .str = cmc_i32_str, NULL }); 42 | 43 | for (int i = 1; i <= 10; i++) 44 | tree_insert(tree, i); 45 | 46 | int32_t result = 1; 47 | 48 | cmc_assert(tree_min(tree, &result)); 49 | cmc_assert_equals(int32_t, 1, result); 50 | 51 | cmc_assert(tree_max(tree, &result)); 52 | cmc_assert_equals(int32_t, 10, result); 53 | 54 | printf("Without Recursion or Stack:\n"); 55 | tree_print(tree, stdout, "[ ", " ", " ]\n"); 56 | 57 | printf("Recursion:\n"); 58 | fprintf(stdout, "[ "); 59 | DFS(tree->root); 60 | fprintf(stdout, "]\n"); 61 | 62 | printf("Stack:\n"); 63 | fprintf(stdout, "[ "); 64 | struct Stack *stack = stk_new(100, &(struct StackFVal){ 0 }); 65 | 66 | struct BinaryTreeNode *node = tree->root; 67 | 68 | while (node || !stk_empty(stack)) 69 | { 70 | if (node) 71 | { 72 | if (!stk_push(stack, node)) 73 | cmc_log_error("Could not push to stack %d\n", cmc_flags_to_str[stk_flag(stack)]); 74 | 75 | node = node->left; 76 | } 77 | else 78 | { 79 | node = stk_top(stack); 80 | 81 | if (!stk_pop(stack)) 82 | cmc_log_error("Could not pop from the stack %s\n", cmc_flags_to_str[stk_flag(stack)]); 83 | 84 | fprintf(stdout, "%d ", node->value); 85 | 86 | node = node->right; 87 | } 88 | } 89 | 90 | stk_free(stack); 91 | fprintf(stdout, "]\n"); 92 | 93 | tree_free(tree); 94 | } 95 | -------------------------------------------------------------------------------- /examples/hanoi.c: -------------------------------------------------------------------------------- 1 | #define V int 2 | #define PFX stk 3 | #define SNAME stack 4 | #define CMC_EXT_INIT 5 | #include "cmc/stack.h" 6 | 7 | const int disks = 6; 8 | 9 | struct 10 | { 11 | struct stack *from; 12 | struct stack *aux; 13 | struct stack *to; 14 | } print_aux = { 0 }; 15 | 16 | void print_stacks() 17 | { 18 | for (int i = 9; i >= 0; i--) 19 | { 20 | int ia = print_aux.from->buffer[i]; 21 | int ib = print_aux.aux->buffer[i]; 22 | int ic = print_aux.to->buffer[i]; 23 | 24 | printf("| %c | %c | %c |\n", ia == 0 ? 32 : ia + 48, ib == 0 ? 32 : ib + 48, ic == 0 ? 32 : ic + 48); 25 | } 26 | 27 | printf("+---+---+---+\n\n"); 28 | } 29 | 30 | void move(struct stack *from, struct stack *to) 31 | { 32 | stk_push(to, stk_top(from)); 33 | 34 | stk_pop(from); 35 | } 36 | 37 | void hanoi(int n, struct stack *from, struct stack *aux, struct stack *to) 38 | { 39 | if (n == 0) 40 | return; 41 | 42 | hanoi(n - 1, from, to, aux); 43 | 44 | move(from, to); 45 | 46 | print_stacks(); 47 | 48 | hanoi(n - 1, aux, from, to); 49 | } 50 | 51 | int main() 52 | { 53 | struct stack from = stk_init(10, &(struct stack_fval){ 0 }); 54 | struct stack to = stk_init(10, &(struct stack_fval){ 0 }); 55 | struct stack aux = stk_init(10, &(struct stack_fval){ 0 }); 56 | 57 | for (int i = disks; i >= 1; i--) 58 | stk_push(&from, i); 59 | 60 | print_aux.from = &from; 61 | print_aux.aux = &aux; 62 | print_aux.to = &to; 63 | 64 | print_stacks(); 65 | 66 | hanoi(stk_count(&from), &from, &aux, &to); 67 | 68 | stk_release(from); 69 | stk_release(to); 70 | stk_release(aux); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /examples/heapsort.c: -------------------------------------------------------------------------------- 1 | /** 2 | * heapsort.c 3 | * 4 | * Creation Date: 25/03/2019 5 | * 6 | * Authors: 7 | * Leonardo Vencovsky (https://github.com/LeoVen) 8 | * 9 | */ 10 | #include 11 | 12 | #include "cmc/cor/core.h" 13 | #include "cmc/utl/futils.h" 14 | 15 | #define PFX h 16 | #define SNAME heap 17 | #define V int 18 | #define CMC_EXT_ITER 19 | #include "cmc/heap.h" 20 | 21 | int main() 22 | { 23 | struct heap *h = h_new(32, CMC_MIN_HEAP, &(struct heap_fval){ .cmp = cmc_i32_cmp }); 24 | 25 | for (int i = 1000; i > 0; i--) 26 | { 27 | if (!h_insert(h, i)) 28 | { 29 | printf("Failed to insert: %s", cmc_flags_to_str[h_flag(h)]); 30 | return h_flag(h); 31 | } 32 | } 33 | 34 | size_t S = h_count(h); 35 | 36 | int *buff = malloc(sizeof(int) * S); 37 | 38 | int index = 0; 39 | while (!h_empty(h)) 40 | { 41 | int tmp = h_peek(h); 42 | 43 | if (h_flag(h) == CMC_FLAG_OK) 44 | { 45 | buff[index++] = tmp; 46 | h_remove(h); 47 | } 48 | else 49 | { 50 | printf("Heap peek failed: %s", cmc_flags_to_str[h_flag(h)]); 51 | return h_flag(h); 52 | } 53 | } 54 | 55 | for (size_t i = 0; i < S; i++) 56 | { 57 | if (i == 0) 58 | printf("[ %d, ", buff[i]); 59 | else if (i == S - 1) 60 | printf("%d ]\n", buff[i]); 61 | else 62 | printf("%d, ", buff[i]); 63 | } 64 | 65 | free(buff); 66 | h_free(h); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /examples/ice_cream.c: -------------------------------------------------------------------------------- 1 | /** 2 | * ice_cream.c 3 | * 4 | * Creation Date: 03/04/2019 5 | * 6 | * Authors: 7 | * Leonardo Vencovsky (https://github.com/LeoVen) 8 | * 9 | */ 10 | #include 11 | #include 12 | #include 13 | 14 | #define PFX q 15 | #define SNAME queue 16 | #define V const char * 17 | #define CMC_EXT_ITER 18 | #include "cmc/queue.h" 19 | 20 | int main() 21 | { 22 | srand((unsigned)time(NULL)); 23 | 24 | // 1- People are lining up to get some ice cream but there is not enough 25 | // for everyone! Hurry! Get in the queue! 26 | 27 | size_t total_icecreams = 20; 28 | 29 | struct queue *lineup = q_new(10, &(struct queue_fval){ 0 }); 30 | 31 | bool ok = true; 32 | 33 | ok = ok && q_enqueue(lineup, "Albert"); 34 | ok = ok && q_enqueue(lineup, "Newton"); 35 | ok = ok && q_enqueue(lineup, "Marie"); 36 | ok = ok && q_enqueue(lineup, "Charles"); 37 | ok = ok && q_enqueue(lineup, "Nikola"); 38 | ok = ok && q_enqueue(lineup, "Thomas"); 39 | ok = ok && q_enqueue(lineup, "Niels"); 40 | ok = ok && q_enqueue(lineup, "Galileo"); 41 | ok = ok && q_enqueue(lineup, "Stephen"); 42 | ok = ok && q_enqueue(lineup, "Leonardo"); 43 | ok = ok && q_enqueue(lineup, "Max"); 44 | ok = ok && q_enqueue(lineup, "Dmitri"); 45 | 46 | if (!ok) 47 | { 48 | fprintf(stderr, "Something went wrong!"); 49 | return 1; 50 | } 51 | 52 | while (total_icecreams > 0) 53 | { 54 | size_t catch = rand() % 5; 55 | 56 | const char *name = q_peek(lineup); 57 | 58 | if (!q_dequeue(lineup)) 59 | break; 60 | 61 | printf("%-8s got %zu ice cream(s) from the ice cream truck\n", name, catch); 62 | 63 | total_icecreams = catch > total_icecreams ? 0 : total_icecreams - catch; 64 | } 65 | 66 | if (!q_empty(lineup)) 67 | { 68 | printf("\nOh no! Someone didn't get their ice cream!\n\n"); 69 | 70 | while (!q_empty(lineup)) 71 | { 72 | const char *name = q_peek(lineup); 73 | 74 | q_dequeue(lineup); 75 | 76 | printf("%-8s got no ice cream and he is sad\n", name); 77 | } 78 | } 79 | else if (total_icecreams > 0) 80 | { 81 | printf("\n%zu ice creams remaining to be sold on the next day.\n", total_icecreams); 82 | } 83 | 84 | q_free(lineup); 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /examples/sac_example.c: -------------------------------------------------------------------------------- 1 | /* Shows an example of how to use CMC_SAC */ 2 | 3 | /** 4 | * CMC_SAC makes a data structure have a fixed size buffer to store data. 5 | */ 6 | #define CMC_SAC 7 | 8 | #define V int 9 | #define SNAME list 10 | #define PFX l 11 | #define SIZE 100 /* This sets the buffer size */ 12 | #define CMC_EXT_ITER 13 | #define CMC_EXT_STR 14 | #define CMC_EXT_INIT 15 | #define CMC_EXT_SEQ 16 | #include "cmc/list.h" 17 | 18 | #include "cmc/utl/futils.h" 19 | 20 | int main() 21 | { 22 | struct list list = l_init(&(struct list_fval){ .str = cmc_i32_str }); 23 | 24 | if (l_flag(&list) != CMC_FLAG_OK) 25 | return 1; 26 | 27 | for (int i = 1; i < 1000; i++) 28 | { 29 | if (!l_push_back(&list, i)) 30 | { 31 | if (l_flag(&list) == CMC_FLAG_FULL) 32 | { 33 | printf("List full, stopping...\n"); 34 | break; 35 | } 36 | else 37 | { 38 | enum cmc_flags flag = l_flag(&list); 39 | fprintf(stderr, "%s : push_back failed\n", cmc_flags_to_str[flag]); 40 | } 41 | } 42 | } 43 | 44 | for (struct list_iter it = l_iter_start(&list); !l_iter_at_end(&it); l_iter_next(&it)) 45 | { 46 | int *val = l_iter_rvalue(&it); 47 | 48 | if (val != NULL) 49 | *val *= 2; 50 | } 51 | 52 | if (!l_print(&list, stdout, "[", ", ", "]\n")) 53 | return 2; 54 | 55 | // Free all of its resources 56 | l_release(list); 57 | } 58 | -------------------------------------------------------------------------------- /examples/sieve.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PFX bs 4 | #define SNAME bitset 5 | #include "cmc/bitset.h" 6 | 7 | // All the prime numbers up to 1 billion 8 | int main(void) 9 | { 10 | const size_t n = 1000000000; 11 | 12 | struct bitset *set = bs_new(n); 13 | 14 | if (!set) 15 | return 1; 16 | 17 | bs_set_range(set, 0, n); 18 | 19 | size_t up_to = sqrt((double)n) + 1; 20 | for (size_t i = 2; i < up_to; ++i) 21 | { 22 | if (bs_get(set, i)) 23 | { 24 | for (size_t j = 2 * i; j < n; j += i) 25 | bs_clear(set, j); 26 | } 27 | } 28 | 29 | FILE *file = fopen("primes.txt", "w"); 30 | 31 | for (size_t i = 2; i < n; i++) 32 | { 33 | if (bs_get(set, i)) 34 | fprintf(file, "%" PRIuMAX "\n", i); 35 | } 36 | 37 | fclose(file); 38 | } 39 | -------------------------------------------------------------------------------- /tests/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -fprofile-arcs -ftest-coverage -ftime-report -g -O0 -DCMC_CALLBACKS 3 | INCLUDE=-I .. 4 | OUTDIR=out 5 | 6 | main: 7 | mkdir -p ./$(OUTDIR) 8 | $(CC) ./main.c $(INCLUDE) $(CFLAGS) -o ./$(OUTDIR)/$@.exe 9 | valgrind --leak-check=yes ./$(OUTDIR)/$@.exe 10 | gcov -p ./$(OUTDIR)/$@.gcda 11 | 12 | dev: main 13 | mkdir -p ./coverage 14 | mv *.gcov ./coverage 15 | 16 | single: 17 | $(CC) ./single.c $(INCLUDE) $(CFLAGS) -o ./$(OUTDIR)/$@.exe -DTEST=$(TEST) 18 | ./$(OUTDIR)/$@.exe 19 | 20 | clean: 21 | rm ./*.gcov ./*.gcda ./*.gcno ./*.exe -f 22 | rm ./coverage ./$(OUTDIR) -rf 23 | 24 | lcov: dev 25 | lcov --capture --directory ./$(OUTDIR) --output-file ./$(OUTDIR)/coverage.info 26 | genhtml ./$(OUTDIR)/coverage.info --output-directory out 27 | 28 | types: 29 | $(CC) ./types.c $(INCLUDE) -Wall -Wextra -O3 -ftime-report -o ./$(OUTDIR)/$@.exe 30 | ./$(OUTDIR)/$@.exe 31 | 32 | .PHONY: main dev single clean lcov types 33 | -------------------------------------------------------------------------------- /tests/single.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "cmc/utl/log.h" 5 | 6 | #define CMC_ARGS_FALLTHROUGH 7 | 8 | #define CMC_EXT_INIT 9 | #define CMC_EXT_ITER 10 | #define CMC_EXT_NODE 11 | #define CMC_EXT_SEQ 12 | #define CMC_EXT_SETF 13 | #define CMC_EXT_STR 14 | 15 | #include "unt_bitset.h" 16 | #include "unt_deque.h" 17 | #include "unt_hashbidimap.h" 18 | #include "unt_hashmap.h" 19 | #include "unt_hashmultimap.h" 20 | #include "unt_hashmultiset.h" 21 | #include "unt_hashset.h" 22 | #include "unt_heap.h" 23 | #include "unt_intervalheap.h" 24 | #include "unt_linkedlist.h" 25 | #include "unt_list.h" 26 | #include "unt_queue.h" 27 | #include "unt_sortedlist.h" 28 | #include "unt_stack.h" 29 | #include "unt_treemap.h" 30 | #include "unt_treeset.h" 31 | 32 | #define cmc_run(unit, unit_fails, test_fails) \ 33 | do \ 34 | { \ 35 | uintmax_t f = unit(); \ 36 | if (f != 0) \ 37 | unit_fails += 1; \ 38 | test_fails += f; \ 39 | } while (0) 40 | 41 | int main(void) 42 | { 43 | cmc_log_config.tlevel = CMC_LOG_TRACE; 44 | 45 | struct cmc_timer timer; 46 | 47 | cmc_timer_start(timer); 48 | uintmax_t tests = 0, units = 0; 49 | 50 | cmc_run(CMC_(CMC, TEST), units, tests); 51 | cmc_run(CMC_(CMC, CMC_(TEST, Iter)), units, tests); 52 | 53 | printf(" +---------------------------------------------------------------+\n"); 54 | printf(" | SUMMARY |\n"); 55 | printf(" +---------------------------------------------------------------+\n"); 56 | printf(" | Total Units Failed : %25" PRIuMAX " units |\n", units); 57 | printf(" | Total Tests Failed : %25" PRIuMAX " tests |\n", tests); 58 | printf(" | Total Asserts Failed : %25" PRIuMAX " asserts |\n", cmc_assert_failed); 59 | printf(" | Total Runtime : %25.0lf milliseconds |\n", timer.result); 60 | printf(" +---------------------------------------------------------------+\n"); 61 | 62 | return tests; 63 | } 64 | --------------------------------------------------------------------------------