Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
191c47fc7c | ||
|
|
f2f806612f | ||
|
|
5e2e001265 | ||
|
|
2ca2210a77 | ||
|
|
747aa8cb4e | ||
|
|
4fe4e96811 | ||
|
|
0df0ebbdeb | ||
|
|
e834aaee81 | ||
|
|
1928fdd6d5 | ||
|
|
abdbfa2018 | ||
|
|
a1a940399a | ||
|
|
855e085996 | ||
|
|
e95379bdd7 | ||
|
|
762b95db26 | ||
|
|
396066f289 | ||
|
|
f070d274f4 | ||
|
|
1f50add1bb | ||
|
|
e68b8a8e0e | ||
|
|
5ee7bfa23a | ||
|
|
8ec768d30e | ||
|
|
35d27ffb39 | ||
|
|
613392ae76 | ||
|
|
f74ce7519a | ||
|
|
01d0b42e1a | ||
|
|
e470021d13 | ||
|
|
7de3ce5c0c | ||
|
|
976965bcfa | ||
|
|
1254e657c5 | ||
|
|
90ee6a424d | ||
|
|
bf79616e7e | ||
|
|
09ebb1eae8 | ||
|
|
ba6d8edde1 | ||
|
|
2f76ad4b6f | ||
|
|
407141661a | ||
|
|
38620ba457 | ||
|
|
f8f704f432 | ||
|
|
8ae83acd1a | ||
|
|
af26f6c081 | ||
|
|
d82be005da | ||
|
|
e37b54fc62 | ||
|
|
9eb8d5f682 | ||
|
|
7ecb2e90fd | ||
|
|
354cd538f8 | ||
|
|
0c883b0a74 | ||
|
|
9c23c3ca77 | ||
|
|
d0244a0513 | ||
|
|
875678d7a9 | ||
|
|
65f2b79157 | ||
|
|
c60ee10185 | ||
|
|
f7a598efc8 | ||
|
|
e8142f96b1 | ||
|
|
97d50bc3d9 | ||
|
|
8c457eeb12 | ||
|
|
846d0bbba6 | ||
|
|
a9314ee8f7 | ||
|
|
9bff706bba | ||
|
|
31c871c4c2 | ||
|
|
82b374ec83 | ||
|
|
940552d6cd | ||
|
|
6a2cb70fb5 | ||
|
|
78cfc0c335 | ||
|
|
f454934b07 | ||
|
|
5504f82052 | ||
|
|
d01997b7cb | ||
|
|
7b349ea426 | ||
|
|
1edb12a83c | ||
|
|
176bbc66ad | ||
|
|
41eebe546a | ||
|
|
afaecb2dde | ||
|
|
9062415322 | ||
|
|
6213b42d5a | ||
|
|
8010cce8c7 | ||
|
|
dad9b7a81f | ||
|
|
21a2bea746 | ||
|
|
b0a2e42cfe | ||
|
|
b6cbadd8e2 | ||
|
|
870cfe302d | ||
|
|
ea04c7ddaa | ||
|
|
a86140094c | ||
|
|
85c49a92fd | ||
|
|
aac07a645c | ||
|
|
efdb1c80a3 | ||
|
|
a9c7439d15 | ||
|
|
26555bc10a | ||
|
|
0f84d65656 | ||
|
|
a9b775419d | ||
|
|
a384a8b180 | ||
|
|
424582f969 | ||
|
|
6044e92262 | ||
|
|
ee96bcbc55 | ||
|
|
37bfcbb9c4 | ||
|
|
d2692a8dbe |
No files matched your search
@@ -0,0 +1,5 @@
|
||||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
|
||||
*.h linguist-language=C
|
||||
*.c linguist-language=C
|
||||
+4
-1
@@ -52,4 +52,7 @@ Mkfile.old
|
||||
dkms.conf
|
||||
|
||||
# vscode folder
|
||||
.vscode
|
||||
.vscode
|
||||
|
||||
# Experimental folder
|
||||
experimental/
|
||||
+6
-8
@@ -8,15 +8,13 @@ addons:
|
||||
- ubuntu-toolchain-r-test
|
||||
packages:
|
||||
- gcc-7
|
||||
- cmake
|
||||
script:
|
||||
- sudo ln -s /usr/bin/gcc-6 /usr/local/bin/gcc
|
||||
- export CC=gcc-7
|
||||
- cd ./tests
|
||||
- gcc foreach.c -Wall -Wextra -o foreach -std=c11
|
||||
- ./foreach
|
||||
- gcc iterators.c -Wall -Wextra -o iterators -std=c11
|
||||
- ./iterators
|
||||
- gcc contains.c -Wall -Wextra -o contains -std=c11
|
||||
- ./contains
|
||||
- gcc pointertype.c -Wall -Wextra -o pointertype -std=c11
|
||||
- ./pointertype
|
||||
- make
|
||||
- cd ./main
|
||||
- make
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
@@ -1,4 +1,158 @@
|
||||
# MAJOR.MINOR.ADJUSTMENTS
|
||||
# RELEASE.MAJOR.MINOR
|
||||
|
||||
1.7.0
|
||||
- Removed FMOD (function modifier)
|
||||
- Added CMC prefix to all macros defined within the library
|
||||
|
||||
1.6.3
|
||||
- Added iterator functions to all collections:
|
||||
- iter_advance
|
||||
- iter_rewind
|
||||
- iter_go_to
|
||||
- Added cmc prefix to the logging utility
|
||||
- Integrated the assert utility with the test utility. Now any assertion that fails inside the unit test will trigger failure and is no longer needed to manually pass or fail tests.
|
||||
|
||||
1.6.2
|
||||
Added new functions to all collections:
|
||||
- copy_of
|
||||
- equals
|
||||
Added the new collections to the tests
|
||||
Fixed some collections and some improvements to the hashtables
|
||||
Unified the functions set and update from the map implementations
|
||||
|
||||
1.6.1
|
||||
Removed ./ext/linkedqueue.h
|
||||
Added a deallocator parameter to all _free and _clear functions
|
||||
|
||||
1.6.0
|
||||
Added ./ext/intervalheap.h
|
||||
Added ./ext/multimap.h
|
||||
Added ./ext/multiset.h
|
||||
Added to_string function to most collections
|
||||
Added the cmc prefix to the enum entry_state used by hashtables and to the enum heap_type used by the heap
|
||||
Fixed return type on some functions from ./cmc/hashset.h and ./cmc/treeset.h
|
||||
Fixed hashtables integer overflow when the capacity is divided by the load factor
|
||||
Changed timer.h struct name from timer_t to cmc_timer to prevent name collisions on linux
|
||||
|
||||
1.5.3
|
||||
Tests completely reorganized and now with full code coverage
|
||||
Some fixes where there was a missing return statement
|
||||
Expanded ./utl/assert.h
|
||||
|
||||
1.5.2
|
||||
Added ./utl/test.h
|
||||
Added ./utl/assert.h
|
||||
Added ./tests/list.c - UnitTest for LIST
|
||||
Small fixes on ./utl/foreach.h, ./utl/test.h and redone ./utl/timer.h
|
||||
Removed ./utl/log.h functions that were unnecessary
|
||||
Added the CMC namespace to CONCATC and CONCATH at macro_collections.h
|
||||
|
||||
1.5.1
|
||||
New queue benchmark
|
||||
Added ./sac/queue.h
|
||||
Added ./utl/timer.h
|
||||
Fixing descriptions and other minor fixes
|
||||
|
||||
1.5.0
|
||||
Changed tests compilation and execution to a single Makefile
|
||||
Changed project structure
|
||||
./src/
|
||||
./cmc/ - Main C Macro Collections Library
|
||||
./dev/ - A copy of ./cmc/ used for development (logging utility)
|
||||
./ext/ - Extra collections
|
||||
./sac/ - Stack Allocated Collections
|
||||
./utl/ - Utility like ForEach, logging, etc
|
||||
Added ./utl/log.h a logging utility
|
||||
Added ./dev/deque.h - A mirror of ./cmc/deque.h but with logging utility
|
||||
Added ./ext/linkedqueue.h - A singly-linked list based queue
|
||||
Added ./sac/stack.h - A Stack purely allocated on the stack
|
||||
|
||||
1.4.3
|
||||
Fixed some functions names
|
||||
Added set functions for both Tree and Hash Set
|
||||
- is_subset
|
||||
- is_proper_subset
|
||||
- is_superset
|
||||
- is_proper_subset
|
||||
- is_disjointset
|
||||
|
||||
1.4.2
|
||||
Adapted code so that plain struct type is accepted
|
||||
Changed TreeSet max and min to be compatible with the TreeMap and the hash collections
|
||||
Changed function modifier of all implementation detail to static
|
||||
Fixed some implementation detail functions that were not prefixed with impl
|
||||
Added types.c to test if it is possible to compile all the collections' generated code accepting all C data types
|
||||
Removed pointertype.c (replaced by types.c)
|
||||
|
||||
1.4.1
|
||||
Removed PUBLIC and PRIVATE from the main macro
|
||||
Corrected examples and benchmark
|
||||
Added new test consisting of compiling the header and source separately
|
||||
|
||||
1.4.0
|
||||
New way on how iterators work to all collections
|
||||
Added iter_init to work with stack allocated iterators
|
||||
Added iter_new and iter_free to work with heap allocated iterators
|
||||
Added two pointers to functions (it_start and it_end) that allow you to access iterators to both start and end of the target collection easily
|
||||
iter_next and iter_prev now only move the iterator
|
||||
To access the key, value and index, use a separate function
|
||||
Removed FOR_EACH_MAP as it is no longer necessary since FOR_EACH also works to associative containers now
|
||||
Updated benchmark, examples and tests to work on these new iterators
|
||||
Changed pointertype test to be a pointer to struct instead of pointer to char
|
||||
Added CHANGELOG and TODO
|
||||
|
||||
1.3.0
|
||||
Changed how the linkedlist functions relative to nodes work
|
||||
Removed reference to owner list on each node
|
||||
Renamed some functions for consistency
|
||||
|
||||
1.2.4
|
||||
Fixing some declarations in hashset and hashmap where a pointer type would cause problems
|
||||
Added a test to make sure all collections support pointer type
|
||||
Tests:
|
||||
- pointertype
|
||||
|
||||
1.2.3
|
||||
Added get_ref functions to all collections that support it (only included in hashmap, linkedlist, list and treemap)
|
||||
|
||||
1.2.2
|
||||
Removed unnecessary modulo operation in hashtable functions
|
||||
|
||||
1.2.1
|
||||
Optimized list - now using 'string.h' functions to shift elements around instead of for-loops:
|
||||
- memset
|
||||
- memcpy
|
||||
- memmove
|
||||
|
||||
1.2.0
|
||||
'contains' functions added to all collections
|
||||
'contains' tests and benchmark
|
||||
Unified Benchmark
|
||||
Updated examples
|
||||
|
||||
1.1.3
|
||||
Renamed deque's 'rear' struct member to 'back' to be more consistent with other collections
|
||||
Better benchmark and foreach tests are alphabetically ordered
|
||||
|
||||
1.1.2
|
||||
Fixing iterators for when they are initialized with an empty target
|
||||
Better iterator tests
|
||||
More examples
|
||||
|
||||
1.1.1
|
||||
Reordering functions (implementation details are now at the end of each file)
|
||||
|
||||
1.1.0
|
||||
Added new list functions:
|
||||
- new_from
|
||||
- prepend
|
||||
- insert
|
||||
- append
|
||||
- remove
|
||||
- fits
|
||||
|
||||
1.0.1
|
||||
Fixed hashset logic in robin-hood hashing
|
||||
|
||||
1.0.0
|
||||
First stable version
|
||||
@@ -18,68 +172,3 @@
|
||||
Tests:
|
||||
- foreach
|
||||
- iterator
|
||||
|
||||
1.0.1
|
||||
Fixed hashset logic in robin-hood hashing
|
||||
|
||||
1.1.0
|
||||
Added new list functions:
|
||||
- new_from
|
||||
- prepend
|
||||
- insert
|
||||
- append
|
||||
- remove
|
||||
- fits
|
||||
|
||||
1.1.1
|
||||
Reordering functions (implementation details are now at the end of each file)
|
||||
|
||||
1.1.2
|
||||
Fixing iterators for when they are initialized with an empty target
|
||||
Better iterator tests
|
||||
More examples
|
||||
|
||||
1.1.3
|
||||
Renamed deque's 'rear' struct member to 'back' to be more consistent with other collections
|
||||
Better benchmark and foreach tests are alphabetically ordered
|
||||
|
||||
1.2.0
|
||||
'contains' functions added to all collections
|
||||
'contains' tests and benchmark
|
||||
Unified Benchmark
|
||||
Updated examples
|
||||
|
||||
1.2.1
|
||||
Optimized list - now using 'string.h' functions to shift elements around instead of for-loops:
|
||||
- memset
|
||||
- memcpy
|
||||
- memmove
|
||||
|
||||
1.2.2
|
||||
Removed unnecessary modulo operation in hashtable functions
|
||||
|
||||
1.2.3
|
||||
Added get_ref functions to all collections that support it (only included in hashmap, linkedlist, list and treemap)
|
||||
|
||||
1.2.4
|
||||
Fixing some declarations in hashset and hashmap where a pointer type would cause problems
|
||||
Added a test to make sure all collections support pointer type
|
||||
Tests:
|
||||
- pointertype
|
||||
|
||||
1.3.0
|
||||
Changed how the linkedlist functions relative to nodes work
|
||||
Removed reference to owner list on each node
|
||||
Renamed some functions for consistency
|
||||
|
||||
1.4.0
|
||||
New way on how iterators work to all collections
|
||||
Added iter_init to work with stack allocated iterators
|
||||
Added iter_new and iter_free to work with heap allocated iterators
|
||||
Added two pointers to functions (it_start and it_end) that allow you to access iterators to both start and end of the target collection easily
|
||||
iter_next and iter_prev now only move the iterator
|
||||
To access the key, value and index, use a separate function
|
||||
Removed FOR_EACH_MAP as it is no longer necessary since FOR_EACH also works to associative containers now
|
||||
Updated benchmark, examples and tests to work on these new iterators
|
||||
Changed pointertype test to be a pointer to struct instead of pointer to char
|
||||
Added CHANGELOG and TODO
|
||||
@@ -13,6 +13,8 @@ Check out `bug_report.md` and `feature_request.md` templates in the `.github` fo
|
||||
|
||||
## Here are things you could work on:
|
||||
|
||||
> 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
|
||||
|
||||
1. New features, new functions, new stuff!
|
||||
* Before adding anything new, please, first create an issue for it to be discussed;
|
||||
* Write tests to anything new you make.
|
||||
|
||||
@@ -1,32 +1,92 @@
|
||||
# C-Macro-Collections
|
||||
|
||||
Generate Simple Data Structures of any type in C for you to use in your projects.
|
||||
Header only, macro generated, generic and type-safe Collections in C.
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
[](https://travis-ci.org/LeoVen/C-Macro-Collections)
|
||||
[](https://codecov.io/gh/LeoVen/C-Macro-Collections)
|
||||
|
||||
## Table of Contents
|
||||
|
||||
* Project Structure
|
||||
* Available Collections
|
||||
* Overall To-Do
|
||||
* Design Decisions
|
||||
* What to Use
|
||||
* How to Use
|
||||
|
||||
## Project Structure
|
||||
|
||||
* __benchmarks__ - Where all benchmarks are hosted
|
||||
* __examples__ - Examples separated by collections
|
||||
* __src__ - All headers part of the C Macro Collections Library
|
||||
* __cmc__ - The main C Macro Collections Library
|
||||
* __dev__ - The main C Macro Collections Library for development (containing logging)
|
||||
* __ext__ - Extra collections
|
||||
* __sac__ - Statically Allocated Collections
|
||||
* __utl__ - Utility like ForEach macros, logging, etc
|
||||
* __macro\_collections.h__ - Master header containing all collections and utilities
|
||||
* __tests__ - Where all tests are hosted
|
||||
|
||||
## Available Collections
|
||||
|
||||
* List
|
||||
* LinkedList
|
||||
* Stack
|
||||
* Queue
|
||||
* Deque
|
||||
* Heap
|
||||
* TreeSet
|
||||
* TreeMap
|
||||
* HashSet
|
||||
* HashMap
|
||||
* Main C Macro Collections Library
|
||||
* Deque
|
||||
* HashMap
|
||||
* HashSet
|
||||
* Heap
|
||||
* LinkedList
|
||||
* List
|
||||
* Queue
|
||||
* Stack
|
||||
* TreeMap
|
||||
* TreeSet
|
||||
* Extra Collections Library
|
||||
* IntervalHeap
|
||||
* MultiMap
|
||||
* MultiSet
|
||||
* Statically Allocated Collections Library
|
||||
* Queue
|
||||
* Stack
|
||||
* Development Collections Library
|
||||
* Deque
|
||||
|
||||
## Available Collections in future updates
|
||||
Check out **Documentation.md** for more information about each collection. The documentation is still being written.
|
||||
|
||||
* Multimap
|
||||
* Multiset
|
||||
* Treehashmap
|
||||
* Heap will be a MinMaxHeap
|
||||
## Overall To-Do
|
||||
|
||||
Check out **Documentation.md** for more information about each collection.
|
||||
In the long term, these are the steps left for the completion of this library:
|
||||
|
||||
* Complete the implementation of all the functions in the scope of the TODO file for the main collections (all those included in `cmc` and `ext`);
|
||||
* Reorganize and complete all tests for `cmc` and `ext`;
|
||||
* Make an exact copy of all collections to `dev` with many logging utility, for them to be used under development;
|
||||
* Port all of these collections to be statically allocated and be part of the `sac` library;
|
||||
* Complete all tests for `sac`.
|
||||
|
||||
## Design Decisions
|
||||
|
||||
### Stack vs Heap Allocation
|
||||
|
||||
Currently all collections need to be allocated on the heap. Iterators have both options but it is encouraged to allocate them on the stack since they don't require dynamic memory.
|
||||
|
||||
### Some collections overlap others in terms of functionality
|
||||
|
||||
Yes, you can use a Deque as a Queue or a List as a Stack without any major cost, but the idea is to have the least amount of code to fulfill the needs of a collection.
|
||||
|
||||
Take for example the Stack. It is simple, small and doesn't have many functions. If you generate a List to be used (only) as a Stack (which is one of the bulkiest collections) you'll end up with a lot of code generated and compiled for nothing.
|
||||
|
||||
The Deque versus Queue situation is a little less problematic, but again, you need to be careful when generating a lot of code as compilation times might go up to 15 seconds even with modern ultra-fast compilers.
|
||||
|
||||
Another example is using a HashMap/TreeMap as a HashSet/TreeSet (with a dummy value that is never used), but I just think that this is a bad thing to do and you would be wasting some memory. Also, the sets generate a lot of code related to set theory, whereas maps don't.
|
||||
|
||||
### But what about the LinkedList ?
|
||||
|
||||
You can use them as Stacks, Queues and Deques, but with modern memory hierarchy models, array-based data structures have a significantly faster runtime due to caching, so I didn't bother to have specific implementations of those aforementioned collections.
|
||||
|
||||
### You can't structurally modify a collection when iterating over it
|
||||
|
||||
Modifying a collection will possibly invalidate all iterators currently initialized by it. Currently, the only collection that allows this is the LinkedList (using the node-based functions, not the iterator).
|
||||
|
||||
## What to use
|
||||
|
||||
@@ -44,15 +104,7 @@ The following table shows how each collection is implemented and how well they d
|
||||
|
||||
## How to use
|
||||
|
||||
To generate the collection, all you need to do is to include the necessary header files. You can include the containers you want to use individually or you can include the master header, `macro_collections.h`, that comes with all collections and other functionalities like the `FOR_EACH` macro.
|
||||
|
||||
There are three C standard headers that all collections use:
|
||||
|
||||
* `<stdlib.h>`
|
||||
* `<stdbool.h>`
|
||||
* `<string.h>`
|
||||
|
||||
With the last one being used only by array based containers. These headers are already included by default.
|
||||
To generate the collection, all you need to do is to include the necessary header files. You can include the containers you want to use individually or you can include the master header, `macro_collections.h`, that comes with the entire C-Macro-Collections library.
|
||||
|
||||
### Macros
|
||||
|
||||
@@ -85,21 +137,13 @@ Check out the **Documentation.md** to know exactly what represents the *end* and
|
||||
|
||||
When including `macro_collections.h` in your source code you gain access to a macro called `COLLECTION_GENERATE` with the following parameters:
|
||||
|
||||
> Note: in future updates, PRIVATE and PUBLIC will no longer exist
|
||||
* __C__ - Container name in uppercase (*LIST*, *LINKEDLIST*, *STACK*, *QUEUE*, *DEQUE*, *HEAP*, *TREESET*, *TREEMAP*, *HASHSET*, *HASHMAP*).
|
||||
* __PFX__ - Functions prefix or namespace.
|
||||
* __SNAME__ - Structure name (`typedef struct SNAME##_s SNAME`).
|
||||
* \*__K__ - Key type. Only used in *HASHMAP* and *TREEMAP*; ignored by others.
|
||||
* __V__ - Value type. Primary type for most collections, or value to be mapped by *HASHMAP* and *TREEMAP*.
|
||||
|
||||
* \*__C__ - Container name in uppercase (*LIST*, *LINKEDLIST*, *STACK*, *QUEUE*, *DEQUE*, *HEAP*, *TREESET*, *TREEMAP*, *HASHSET*, *HASHMAP*).
|
||||
* \*__P__ - Permission (PRIVATE, PUBLIC). Only matters when header and source are declared in separate files.
|
||||
* *PRIVATE* - Members of a struct are hidden in source file and only accessible through functions.
|
||||
* *PUBLIC* - Members of a struct are visible.
|
||||
* \*__PFX__ - Functions prefix or namespace.
|
||||
* \*__SNAME__ - Structure name (`typedef struct SNAME##_s SNAME`).
|
||||
* __FMOD__ - Function modifier (`static` or empty).
|
||||
* \*\*__K__ - Key type. Only used in *HASHMAP* and *TREEMAP*; ignored by others.
|
||||
* \*__V__ - Value type. Primary type for most collections, or value to be mapped by *HASHMAP* and *TREEMAP*.
|
||||
|
||||
\* Required parameters
|
||||
|
||||
\*\* Required only by *HASHMAP* and *TREEMAP*
|
||||
\* Required only by *HASHMAP* and *TREEMAP*
|
||||
|
||||
**In fact, all macros follow this pattern.** So whenever you see a macro with a bunch of parameters and you don't know what they are, you can check out the above list.
|
||||
|
||||
@@ -110,4 +154,16 @@ When including `foreach.h` in your source code you gain access to all for-each m
|
||||
* __TARGET__ - The variable name of the collection you wish to iterate over.
|
||||
* __BODY__ - Block of code.
|
||||
|
||||
Inside body you will have access to the iterator variable. With it you can use functions to access the key, value or index from the iterator.
|
||||
Inside body you will have access to the iterator variable. With it you can use functions to access the key, value or index from the iterator. Checkout the documentation for more details.
|
||||
|
||||
<hr>
|
||||
|
||||
Check out some code reviews that covers some parts the project:
|
||||
|
||||
| About | Link |
|
||||
|-------|--------|
|
||||
| Unit Test *./utl/test.h* | [](http://codereview.stackexchange.com/q/222954/178948) |
|
||||
| Interval Heap *./ext/intervalheap.h* | [](http://codereview.stackexchange.com/q/223595/178948) |
|
||||
| Hash Set *./cmc/hashset.h* | [](http://codereview.stackexchange.com/q/217333/178948) |
|
||||
| Linked List *./cmc/linkedlist.h* | [](http://codereview.stackexchange.com/q/216737/178948) |
|
||||
| Others | [](http://codereview.stackexchange.com/q/213553/178948) |
|
||||
@@ -1,56 +1,65 @@
|
||||
TODO
|
||||
|
||||
[ ] Finish Documentation
|
||||
[ ] Simplify PUBLIC and PRIVATE
|
||||
[ ] Add multiset
|
||||
[ ] Add multimap
|
||||
[ ] Upgrade heap to a min-max heap
|
||||
[ ] Add dispose function
|
||||
[/] - In Progress
|
||||
[X] - Finished
|
||||
[~] - Needs to be reviewed
|
||||
|
||||
[ ] Future possible Collections
|
||||
[ ] Add TreeHashMap
|
||||
[ ] Add SortedList
|
||||
[ ] Add BiDiMap
|
||||
[ ] Add Trie (or any variant)
|
||||
|
||||
[/] Documentation
|
||||
[X] Remove PUBLIC and PRIVATE
|
||||
[X] Extra Collections
|
||||
[X] Add MultiSet
|
||||
[X] Add MultiMap
|
||||
[X] Add IntervalHeap
|
||||
[ ] Add functions
|
||||
[X] update {hashmap, treemap, multimap}
|
||||
[X] set {hashmap, treemap, multimap}
|
||||
[X] to_string {all}
|
||||
[X] iter_advance {all} (iterators)
|
||||
[X] iter_rewind {all} (iterators)
|
||||
[X] iter_go_to {all} (iterators)
|
||||
[ ] from_array {all}
|
||||
[ ] to_array {all}
|
||||
[X] equals {all}
|
||||
[X] copy_of {all}
|
||||
[ ] resize {dynamic arrays and circular buffers}
|
||||
[ ] Serialization
|
||||
[ ] Iterator based functions (like C++ collections)
|
||||
|
||||
|
||||
MAYBE
|
||||
|
||||
[ ] Add conditional code generation based on variadic macros
|
||||
|
||||
|
||||
PLANNED
|
||||
|
||||
Before 2.0
|
||||
Get Documentation fixed and completed
|
||||
|
||||
1.5
|
||||
Simplify PUBLIC and PRIVATE (simply remove them)
|
||||
Conditional code generation
|
||||
Add extra content macro (Insert/Remove/Append/Prepend) and let user decide if he wants to include them or not
|
||||
Separate Implementation Detail into a different MACRO (LIST_GENERATE_DETAIL)
|
||||
|
||||
1.6
|
||||
Iterator move and move_to
|
||||
LinkedList - Insert/Remove/Append/Prepend/Extract
|
||||
to_array - Creates an array with all the elements inside the collection
|
||||
|
||||
1.7
|
||||
Add shrink function that shrinks the internal buffer to current count of elements
|
||||
|
||||
2.0
|
||||
Add multiset, multimap and new heap implementation
|
||||
|
||||
2.1
|
||||
Adapt multiset, multimap, treehashmap and new heap implementation to the current standards
|
||||
Add dispose method to each collection in order to automatically free its data
|
||||
|
||||
2.2
|
||||
Add serialization functions
|
||||
Header:
|
||||
%d - Type specifier (LIST = 0, DEQUE = 1, STACK = 2, etc) for quick check
|
||||
%llu - How many elements there are
|
||||
%d - If data varies in size
|
||||
%llu - Data size
|
||||
|
||||
2.3
|
||||
Iterator functions (functions that operate relative to iterators position) like add or remove ranges
|
||||
|
||||
Sometime
|
||||
Add treehashmap
|
||||
[ ] save {all}
|
||||
[ ] load {all}
|
||||
[ ] serialize {all}
|
||||
[ ] deserialize {all}
|
||||
[ ] Add callback utility
|
||||
[ ] Zip Iterators
|
||||
[ ] Stack Allocated Collections
|
||||
[ ] Deque
|
||||
[ ] HashMap
|
||||
[ ] HashSet
|
||||
[ ] Heap
|
||||
[ ] LinkedList
|
||||
[ ] List
|
||||
[~] Queue
|
||||
[~] Stack
|
||||
[ ] TreeMap
|
||||
[ ] TreeSet
|
||||
[ ] Dev Collections
|
||||
[~] Deque
|
||||
[ ] HashMap
|
||||
[ ] HashSet
|
||||
[ ] Heap
|
||||
[ ] LinkedList
|
||||
[ ] List
|
||||
[ ] Queue
|
||||
[ ] Stack
|
||||
[ ] TreeMap
|
||||
[ ] TreeSet
|
||||
[~] Utility
|
||||
[~] assert.h
|
||||
[X] foreach.h
|
||||
[~] log.h
|
||||
[~] test.h
|
||||
[X] timer.h
|
||||
+42
-31
@@ -13,7 +13,6 @@
|
||||
* - Output
|
||||
*/
|
||||
#include "macro_collections.h"
|
||||
#include "util/timer.h"
|
||||
#include "util/twister.c"
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
@@ -60,39 +59,43 @@ void shuffle(int *array, mt_state_ptr st)
|
||||
}
|
||||
}
|
||||
|
||||
COLLECTION_GENERATE(LIST, PUBLIC, l, list, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(STACK, PUBLIC, s, stack, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(QUEUE, PUBLIC, q, queue, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(DEQUE, PUBLIC, d, deque, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(LINKEDLIST, PUBLIC, ll, linked, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HEAP, PUBLIC, h, heap, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(TREESET, PUBLIC, ts, tset, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(TREEMAP, PUBLIC, tm, tmap, /* FMOD */, int, int)
|
||||
COLLECTION_GENERATE(HASHSET, PUBLIC, hs, hset, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HASHMAP, PUBLIC, hm, hmap, /* FMOD */, int, int)
|
||||
COLLECTION_GENERATE(DEQUE, d, deque, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HASHMAP, hm, hmap, /* FMOD */, int, int)
|
||||
COLLECTION_GENERATE(HASHSET, hs, hset, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HEAP, h, heap, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(LINKEDLIST, ll, linked, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(LIST, l, list, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(QUEUE, q, queue, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(STACK, s, stack, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(TREEMAP, tm, tmap, /* FMOD */, int, int)
|
||||
COLLECTION_GENERATE(TREESET, ts, tset, /* FMOD */, /* K */, int)
|
||||
|
||||
COLLECTION_GENERATE(INTERVALHEAP, ih, iheap, , , int)
|
||||
COLLECTION_GENERATE(MULTIMAP, mm, mmap, /* FMOD */, int, int)
|
||||
COLLECTION_GENERATE(MULTISET, ms, mset, /* FMOD */, /* K */, int)
|
||||
|
||||
#define BENCHMARK(NAME, PFX, sname, initfunc, insertbody, removebody, searchbody) \
|
||||
void NAME##_io_benchmark(int *array, int *sarray, size_t s) \
|
||||
{ \
|
||||
printf("+------------------------------------------------------------ %10s\n", #NAME); \
|
||||
printf("+------------------------------------------------------------ \n"); \
|
||||
timer_t timer, total; \
|
||||
cmc_timer timer, total; \
|
||||
int r, k, v; \
|
||||
size_t j; \
|
||||
\
|
||||
sname *coll = initfunc; \
|
||||
\
|
||||
TIMER_START(total) \
|
||||
TIMER_START(timer) \
|
||||
timer_start(total); \
|
||||
timer_start(timer); \
|
||||
\
|
||||
for (size_t i = 0; i < NTOTAL; i++) \
|
||||
{ \
|
||||
insertbody; \
|
||||
} \
|
||||
\
|
||||
TIMER_STOP(timer) \
|
||||
timer_stop(timer); \
|
||||
\
|
||||
TIMER_CALC(timer) \
|
||||
timer_calc(timer); \
|
||||
\
|
||||
size_t s1 = PFX##_count(coll); \
|
||||
\
|
||||
@@ -100,16 +103,16 @@ COLLECTION_GENERATE(HASHMAP, PUBLIC, hm, hmap, /* FMOD */, int, int)
|
||||
\
|
||||
double input_time = timer.result; \
|
||||
\
|
||||
TIMER_START(timer) \
|
||||
timer_start(timer); \
|
||||
\
|
||||
for (size_t i = 0; i < s; i++) \
|
||||
{ \
|
||||
searchbody; \
|
||||
} \
|
||||
\
|
||||
TIMER_STOP(timer) \
|
||||
timer_stop(timer); \
|
||||
\
|
||||
TIMER_CALC(timer) \
|
||||
timer_calc(timer); \
|
||||
\
|
||||
printf(" SEARCH %10s TOOK %8.0lf milliseconds for %8" PRIuMAX " elements\n", #NAME, timer.result, s); \
|
||||
\
|
||||
@@ -117,30 +120,30 @@ COLLECTION_GENERATE(HASHMAP, PUBLIC, hm, hmap, /* FMOD */, int, int)
|
||||
\
|
||||
sname##_iter iter; \
|
||||
\
|
||||
TIMER_START(timer) \
|
||||
timer_start(timer); \
|
||||
\
|
||||
for (PFX##_iter_init(&iter, coll); !PFX##_iter_end(&iter); PFX##_iter_next(&iter)) \
|
||||
{ \
|
||||
} \
|
||||
\
|
||||
TIMER_STOP(timer) \
|
||||
timer_stop(timer); \
|
||||
\
|
||||
TIMER_CALC(timer) \
|
||||
timer_calc(timer); \
|
||||
\
|
||||
printf("ITERATION %10s TOOK %8.0lf milliseconds for %8" PRIuMAX " elements\n", #NAME, timer.result, s1); \
|
||||
\
|
||||
double iter_time = timer.result; \
|
||||
\
|
||||
TIMER_START(timer); \
|
||||
timer_start(timer); \
|
||||
\
|
||||
for (size_t i = 0; i < NTOTAL; i++) \
|
||||
{ \
|
||||
removebody; \
|
||||
} \
|
||||
\
|
||||
TIMER_STOP(timer) \
|
||||
timer_stop(timer); \
|
||||
\
|
||||
TIMER_CALC(timer) \
|
||||
timer_calc(timer); \
|
||||
\
|
||||
size_t s2 = PFX##_count(coll); \
|
||||
\
|
||||
@@ -148,10 +151,10 @@ COLLECTION_GENERATE(HASHMAP, PUBLIC, hm, hmap, /* FMOD */, int, int)
|
||||
\
|
||||
double output_time = timer.result; \
|
||||
\
|
||||
TIMER_STOP(total) \
|
||||
TIMER_CALC(total) \
|
||||
timer_stop(total); \
|
||||
timer_calc(total); \
|
||||
\
|
||||
PFX##_free(coll); \
|
||||
PFX##_free(coll, NULL); \
|
||||
\
|
||||
double adj_search_time = (total.result - search_time) + (search_time * (double)NTOTAL) / (double)s; \
|
||||
double no_search = input_time + iter_time + output_time; \
|
||||
@@ -173,9 +176,9 @@ COLLECTION_GENERATE(HASHMAP, PUBLIC, hm, hmap, /* FMOD */, int, int)
|
||||
}
|
||||
|
||||
BENCHMARK(DEQUE, d, deque, d_new(NTOTAL), d_push_back(coll, array[i]), d_pop_back(coll), d_contains(coll, sarray[i], intcmp))
|
||||
BENCHMARK(HASHMAP, hm, hmap, hm_new(NTOTAL, 0.8, intcmp, inthash), hm_insert(coll, array[i], array[i]), hm_remove(coll, array[i], &r), hm_contains(coll, sarray[i]))
|
||||
BENCHMARK(HASHSET, hs, hset, hs_new(NTOTAL, 0.8, intcmp, inthash), hs_insert(coll, array[i]), hs_remove(coll, array[i]), hs_contains(coll, sarray[i]))
|
||||
BENCHMARK(HEAP, h, heap, h_new(NTOTAL, MinHeap, intcmp), h_insert(coll, array[i]), h_remove(coll, &r), h_contains(coll, sarray[i]))
|
||||
BENCHMARK(HASHMAP, hm, hmap, hm_new(NTOTAL, 0.6, intcmp, inthash), hm_insert(coll, array[i], array[i]), hm_remove(coll, array[i], &r), hm_contains(coll, sarray[i]))
|
||||
BENCHMARK(HASHSET, hs, hset, hs_new(NTOTAL, 0.6, intcmp, inthash), hs_insert(coll, array[i]), hs_remove(coll, array[i]), hs_contains(coll, sarray[i]))
|
||||
BENCHMARK(HEAP, h, heap, h_new(NTOTAL, cmc_min_heap, intcmp), h_insert(coll, array[i]), h_remove(coll, &r), h_contains(coll, sarray[i]))
|
||||
BENCHMARK(LINKEDLIST, ll, linked, ll_new(), ll_push_back(coll, array[i]), ll_pop_back(coll), ll_contains(coll, sarray[i], intcmp))
|
||||
BENCHMARK(LIST, l, list, l_new(NTOTAL), l_push_back(coll, array[i]), l_pop_back(coll), l_contains(coll, sarray[i], intcmp))
|
||||
BENCHMARK(QUEUE, q, queue, q_new(NTOTAL), q_enqueue(coll, array[i]), q_dequeue(coll), q_contains(coll, sarray[i], intcmp))
|
||||
@@ -183,6 +186,10 @@ BENCHMARK(STACK, s, stack, s_new(NTOTAL), s_push(coll, array[i]), s_pop(coll), s
|
||||
BENCHMARK(TREEMAP, tm, tmap, tm_new(intcmp), tm_insert(coll, array[i], array[i]), tm_remove(coll, array[i], &r), tm_contains(coll, sarray[i]))
|
||||
BENCHMARK(TREESET, ts, tset, ts_new(intcmp), ts_insert(coll, array[i]), ts_remove(coll, array[i]), ts_contains(coll, sarray[i]))
|
||||
|
||||
BENCHMARK(INTERVALHEAP, ih, iheap, ih_new(NTOTAL, intcmp), ih_insert(coll, array[i]), ih_remove_max(coll, &r), ih_contains(coll, sarray[i]))
|
||||
BENCHMARK(MULTIMAP, mm, mmap, mm_new(NTOTAL, 0.8, intcmp, inthash), mm_insert(coll, array[i], array[i]), mm_remove(coll, array[i], &r), mm_contains(coll, sarray[i]))
|
||||
BENCHMARK(MULTISET, ms, mset, ms_new(NTOTAL, 0.6, intcmp, inthash), ms_insert(coll, array[i]), ms_remove(coll, array[i]), ms_contains(coll, sarray[i]))
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Using the Mersenne Twister to generate random numbers. This seed
|
||||
@@ -225,6 +232,10 @@ int main(void)
|
||||
TREEMAP_io_benchmark(array, sarray, NTOTAL);
|
||||
TREESET_io_benchmark(array, sarray, NTOTAL);
|
||||
|
||||
INTERVALHEAP_io_benchmark(array, array, NMIN);
|
||||
MULTIMAP_io_benchmark(array, array, NTOTAL);
|
||||
MULTISET_io_benchmark(array, array, NTOTAL);
|
||||
|
||||
free(array);
|
||||
free(sarray);
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
CFLAGS = -Wall -Wextra
|
||||
INCLUDE = ../../src
|
||||
|
||||
main:
|
||||
gcc queue.c -I $(INCLUDE) $(CFLAGS) -o a.exe -DCMC_QUEUE
|
||||
a.exe
|
||||
gcc queue.c -I $(INCLUDE) $(CFLAGS) -o a.exe -DSAC_QUEUE
|
||||
a.exe
|
||||
gcc queue.c -I $(INCLUDE) $(CFLAGS) -o a.exe -DEXT_QUEUE
|
||||
a.exe
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* queue.c
|
||||
*
|
||||
* Creation Date: 24/06/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/* Showing off how different queue implementations affect runtime cost */
|
||||
|
||||
#include "utl/timer.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#define MAX 200000
|
||||
|
||||
#if defined(CMC_QUEUE)
|
||||
#include "cmc/queue.h"
|
||||
QUEUE_GENERATE(q, queue, , int)
|
||||
#define TARGET "QUEUE"
|
||||
#elif defined(SAC_QUEUE)
|
||||
#include "sac/queue.h"
|
||||
SAC_QUEUE_GENERATE(q, queue, , int, MAX)
|
||||
#define TARGET "SAC QUEUE"
|
||||
#elif defined(EXT_QUEUE)
|
||||
#include "ext/linkedqueue.h"
|
||||
LINKEDQUEUE_GENERATE(q, queue, , int)
|
||||
#define TARGET "LINKED QUEUE"
|
||||
#else
|
||||
#error Please define either CMC_QUEUE, SAC_QUEUE or EXT_QUEUE
|
||||
#endif
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
#if defined(CMC_QUEUE)
|
||||
queue *q = q_new(MAX);
|
||||
#elif defined(SAC_QUEUE)
|
||||
queue _q = q_new();
|
||||
queue_ptr q = &_q;
|
||||
#elif defined(EXT_QUEUE)
|
||||
queue *q = q_new();
|
||||
#endif
|
||||
|
||||
size_t sum = 0;
|
||||
int i = 0;
|
||||
|
||||
timer_t timer;
|
||||
TIMER_START(timer);
|
||||
|
||||
while (q_count(q) != MAX)
|
||||
{
|
||||
q_enqueue(q, i++);
|
||||
|
||||
if (i % 10 == 0)
|
||||
{
|
||||
for (size_t i = 0; i < 9; i++)
|
||||
{
|
||||
sum += q_peek(q);
|
||||
q_dequeue(q);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TIMER_STOP(timer);
|
||||
TIMER_CALC(timer);
|
||||
|
||||
printf("----------------------------------------\n");
|
||||
printf("%s\n", TARGET);
|
||||
printf("Execution time: %.0lf milliseconds\n", timer.result);
|
||||
printf("SUM: %" PRIuMAX "\n", sum);
|
||||
printf("----------------------------------------\n");
|
||||
|
||||
#if defined(CMC_QUEUE) || defined(EXT_QUEUE)
|
||||
q_free(q);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
/**
|
||||
* timer.h
|
||||
*
|
||||
* Creation Date: 12/04/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
* Simple timer macros.
|
||||
*/
|
||||
#include <time.h>
|
||||
|
||||
typedef struct timer_s
|
||||
{
|
||||
clock_t start;
|
||||
clock_t stop;
|
||||
double result;
|
||||
} timer_t;
|
||||
|
||||
#define TIMER_START(timer) \
|
||||
timer.start = clock();
|
||||
|
||||
#define TIMER_STOP(timer) \
|
||||
timer.stop = clock();
|
||||
|
||||
#define TIMER_CALC(timer) \
|
||||
timer.result = (double)(((timer.stop - timer.start) * 1000.0) / CLOCKS_PER_SEC);
|
||||
@@ -7,7 +7,7 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
* Mersenne Twister PRNG utility
|
||||
*
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,247 @@
|
||||
THIS IS A SAFE COPY OF THE MARKDOWN FOR THE DOCUMENTATION LOCATED AT https://leoven.github.io/C-Macro-Collections/
|
||||
|
||||
|
||||
|
||||
|
||||
---
|
||||
title: CMC Documentation
|
||||
|
||||
toc_footers:
|
||||
- <a href='https://github.com/lord/slate'>Documentation Powered by Slate</a>
|
||||
|
||||
language_tabs:
|
||||
- C
|
||||
|
||||
search: true
|
||||
---
|
||||
|
||||
# Introduction
|
||||
|
||||
[](https://github.com/LeoVen/C-Macro-Collections)
|
||||
|
||||

|
||||

|
||||
[](https://travis-ci.org/LeoVen/C-Macro-Collections)
|
||||
[](https://codecov.io/gh/LeoVen/C-Macro-Collections)
|
||||
|
||||
The C Macro Collections Library is a compilation of macros that can be used to generate common data structures for any desired type. These data structures are [type safe](https://en.wikipedia.org/wiki/Type_safety).
|
||||
|
||||
The documentation is currently being written in hopes that in the future it can be used to help you get to know better this amazing C library.
|
||||
|
||||
## Why this library
|
||||
|
||||
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. This is why the C Macro Collections Library was created.
|
||||
|
||||
The library currently provides many data structures, such as:
|
||||
|
||||
* Linear Collections
|
||||
* List, LinkedList, Deque, Stack, Queue
|
||||
* Sets
|
||||
* HashSet, TreeSet, MultiSet
|
||||
* Maps
|
||||
* HashMap, TreeMap, MultiMap
|
||||
* Heaps
|
||||
* Heap, IntervalHeap
|
||||
|
||||
All with the following features:
|
||||
|
||||
* __Type-safe__ - No `void *` pointers are used. A collection of type `int` will only accept integers;
|
||||
* __Customization__ - Custom struct name and function namespace;
|
||||
* __Bidirectional Iterators__ - Full support for iterators;
|
||||
* __Nesting__ - Collections can be nested (List of Lists, HashSet of Stacks, etc).
|
||||
|
||||
## Library Structure
|
||||
|
||||
The `src` folder is subdivided in 5 other folders and one file:
|
||||
|
||||
> The `dev` folder is an exact copy of `cmc` but it is filled with logging information regarding the code execution and debugging.
|
||||
|
||||
> The `sac` library contains fixed-length collections with an internal C array.
|
||||
|
||||
* __cmc__ - The main C Macro Collections Library
|
||||
* __dev__ - The main C Macro Collections Library for development
|
||||
* __ext__ - Extra collections
|
||||
* __sac__ - Statically Allocated Collections
|
||||
* __utl__ - Utility like ForEach macros, logging, etc
|
||||
* __macro\_collections.h__ - Master header containing all collections and utilities
|
||||
|
||||
## How to install
|
||||
|
||||
> Include header files like this
|
||||
|
||||
```c
|
||||
#include <cmc/list.h>
|
||||
#include <dev/deque.h>
|
||||
#include <utl/assert.h>
|
||||
```
|
||||
> Not like this
|
||||
|
||||
```c
|
||||
#include <list.h>
|
||||
#include <deque.h> // dev or cmc?
|
||||
#include <assert.h> // this will import from the standard library
|
||||
```
|
||||
|
||||
No installation is required. All you have to do is include the appropriate header files, generate any collections that you desire and then compile your program. It is recommended that you include the headers relative to the `src` folder and include it with `-I path/to/library/src` as shown in the following example.
|
||||
|
||||
The library has no external dependencies other than the C standard library.
|
||||
|
||||
## Minimal Example
|
||||
|
||||
Lets create a list of type `int`, add some elements to it and get the sum of all the elements printed on the screen.
|
||||
|
||||
### Include headers
|
||||
|
||||
```c
|
||||
#include <cmc/list.h>
|
||||
```
|
||||
|
||||
First you need to include only the necessary headers. It is not recommended that you include everything with `macro_collections.h`, only in cases where you might be using almost all features from the library.
|
||||
|
||||
### Generating a list
|
||||
|
||||
```c
|
||||
// CMC_GENERATE_LIST(PFX, SNAME, V)
|
||||
CMC_GENERATE_LIST(il, int_list, int)
|
||||
```
|
||||
|
||||
The List is also known as `vector`, `ArrayList` and dynamic array. In fact the latter is the best description for this collection. It is a dynamic array that can grow and shrink as needed. To generate it we can simply call the macro `CMC_GENERATE_LIST` after our includes. It has three parameters:
|
||||
|
||||
| Parameter name | Purpose |
|
||||
| :------------: | ------- |
|
||||
| __PFX__ | Functions prefix (namespace) |
|
||||
| __SNAME__ | Struct name |
|
||||
| __V__ | The list value type (can be any valid data type) |
|
||||
|
||||
<aside class="notice">
|
||||
All collections can be generated by using the macro CMC_GENERATE_ and then the name of the include (in this case list.h) but in uppercase without the `.h`.
|
||||
</aside>
|
||||
|
||||
### Initializing and adding elements
|
||||
|
||||
```c
|
||||
// Allocate an int_list with an initial capacity of 100
|
||||
int_list *list = il_new(100);
|
||||
|
||||
// Adding elements to the back of the list
|
||||
for (int i = 0; i <= 1000; i++)
|
||||
{
|
||||
if (!il_push_back(list, i))
|
||||
{
|
||||
// Something went wrong (check out documentation)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
To initialize any collection we call a function `_new`. Since the `PFX` parameter is set to `il` all functions related to our list of integer will be prefixed by `il`, so `_new` becomes `il_new`.
|
||||
|
||||
The list name is `int_list` as defined by `SNAME`. This type is a typedef like:
|
||||
|
||||
`typedef struct SNAME##_s SNAME;`
|
||||
|
||||
Which expands to:
|
||||
|
||||
`typedef struct int_list_s int_list`
|
||||
|
||||
To add elements to the back of a list we can use `il_push_back`. This function will first check if the list has enough space for a new element. If not, reallocate the internal buffer with doubled size and then add the new element.
|
||||
|
||||
### Iteration
|
||||
|
||||
> It is recommended that the iterator is allocated on the stack
|
||||
|
||||
```c
|
||||
// Resulting sum will be stored here
|
||||
int sum = 0;
|
||||
|
||||
// Declare our iterator (allocating it on the stack)
|
||||
int_list_iter it;
|
||||
|
||||
// Loop for each element
|
||||
for (il_iter_init(&it, list); !il_iter_end(&it); il_iter_next(&it))
|
||||
{
|
||||
// Accessing the value
|
||||
int elem = il_iter_value(&it);
|
||||
|
||||
sum += elem;
|
||||
}
|
||||
|
||||
printf("%d\n", sum);
|
||||
```
|
||||
|
||||
There are 6 (2 optional) steps to iterate over the elements of any collection:
|
||||
|
||||
| Step | Description | Function |
|
||||
| :--: | ----------- | -------- |
|
||||
| 1 | Allocate the iterator (optional) | `il_iter_new` |
|
||||
| 2 | Initialize the iterator given a target list | `il_iter_init` |
|
||||
| 3 | Access the iterator's elements (value, index, etc) | `il_iter_value` |
|
||||
| 4 | Go to the next element | `il_iter_next` |
|
||||
| 5 | Check if we haven't reached the end of the list | `il_iter_end` |
|
||||
| 6 | Free the iterator if it was allocated on the heap | `il_iter_free` |
|
||||
|
||||
Note that all iterator functions are prefixed by the user defined prefix and then by `iter` (`il + iter = il_iter`). Also, the steps __1__ and __6__ are optional so `il_iter_new` and `il_iter_free` are not used in the example.
|
||||
|
||||
By the end of the example we have the variable `sum` with the sum of all integers inside our list. We can then print it and this example is almost done.
|
||||
|
||||
### Freeing resources
|
||||
|
||||
```c
|
||||
// void il_free(int_list *_list_, void(*deallocator)(int))
|
||||
il_free(list, NULL);
|
||||
```
|
||||
|
||||
The list is currently allocated in memory. To deallocate it we call `il_free`. This function takes a pointer to the allocated list and a pointer to a `deallocator` function that will be responsible for deallocating each element in the list. This last parameter is optional and we won't be using it since our data type `int` is not allocated in the heap. This optional parameter can be set to `NULL` to be ignored by the `il_free` function.
|
||||
|
||||
### Compile and run
|
||||
|
||||
> source.c
|
||||
|
||||
```c
|
||||
#include <cmc/list.h>
|
||||
|
||||
CMC_GENERATE_LIST(il, int_list, int)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int_list *list = il_new(100);
|
||||
|
||||
for (int i = 0; i <= 1000; i++)
|
||||
{
|
||||
if (!il_push_back(list, i))
|
||||
{
|
||||
// Something went wrong (check out documentation)
|
||||
}
|
||||
}
|
||||
|
||||
int sum = 0;
|
||||
|
||||
int_list_iter it;
|
||||
|
||||
for (il_iter_init(&it, list); !il_iter_end(&it); il_iter_next(&it))
|
||||
{
|
||||
int elem = il_iter_value(&it);
|
||||
sum += elem;
|
||||
}
|
||||
|
||||
printf("%d\n", sum);
|
||||
|
||||
il_free(list, NULL);
|
||||
}
|
||||
```
|
||||
|
||||
If the source file is `source.c`, all you have to do is to include the `src` folder to the include path and compile. The following example uses `gcc`:
|
||||
|
||||
`gcc source.c -I path/to/library/src`
|
||||
|
||||
### Conclusion
|
||||
|
||||
The C Macro Collections Library is prepared to accept any data type and is highly customizable. It is also very easy to integrate and no previous compilation is required. The library also comes with many utilities in the `utl` folder that are designed to work well with all collections.
|
||||
|
||||
# Upcoming
|
||||
|
||||
This documentation is still being written. Here is some of the things you will be able to find here:
|
||||
|
||||
* Full explanation about data structures
|
||||
* Full documentation about all current collections and others to come
|
||||
* Fully explained examples
|
||||
* Benchmark examples
|
||||
@@ -0,0 +1,16 @@
|
||||
# Examples
|
||||
|
||||
* deque - DEQUE collection examples
|
||||
* hashmap - HASHMAP collection examples
|
||||
* hashset - HASHSET collection examples
|
||||
* heap - HEAP collection examples
|
||||
* iterators - Examples of using the collection's iterators
|
||||
* linkedlist - LINKEDLIST collection examples
|
||||
* list - LIST collection examples
|
||||
* multimap - MULTIMAP collection examples
|
||||
* multiset - MULTISET collection examples
|
||||
* others - Other examples, generally more robust and using multiple collections
|
||||
* queue - QUEUE collection examples
|
||||
* stack - STACK collection examples
|
||||
* treemap - TREEMAP collection examples
|
||||
* treeset - TREESET collection examples
|
||||
@@ -7,10 +7,10 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "cmc/deque.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include "deque.h"
|
||||
|
||||
DEQUE_GENERATE(d, deque, /* static */, size_t)
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "deque.h"
|
||||
#include "cmc/deque.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# Hashmap examples
|
||||
|
||||
* countries - Uses a hashmap to map the country name to its population
|
||||
* foreach.c - ForEach macro example
|
||||
* iterator.c - A complete example of the hashmap iterator
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "hashmap.h"
|
||||
#include "cmc/hashmap.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* iterator.c
|
||||
*
|
||||
* Creation Date: 23/05/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
* Foreach macro example
|
||||
*/
|
||||
#include "cmc/hashmap.h"
|
||||
#include "utl/foreach.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// Int hash function
|
||||
size_t inthash(int t)
|
||||
{
|
||||
size_t a = t;
|
||||
a += ~(a << 15);
|
||||
a ^= (a >> 10);
|
||||
a += (a << 3);
|
||||
a ^= (a >> 6);
|
||||
a += ~(a << 11);
|
||||
a ^= (a >> 16);
|
||||
return a;
|
||||
}
|
||||
|
||||
// Int compare function
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
// Generate all the code for the hashmap
|
||||
HASHMAP_GENERATE(hm, hashmap, /* FMOD */, int, double)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Create a new hashmap with custom hash function
|
||||
// The comparison function is only required to know if the key looked for was found
|
||||
hashmap *map = hm_new(1000, 0.6, intcmp, inthash);
|
||||
|
||||
// Add keys mapped to a value
|
||||
for (int i = 0; i < 5000; i++)
|
||||
{
|
||||
hm_insert(map, i, (double)i / 1000.0);
|
||||
}
|
||||
|
||||
// Iterate over the HashMap without the FOR_EACH macro
|
||||
for (hashmap_iter iter = map->it_start(map); !hm_iter_end(&iter); hm_iter_next(&iter))
|
||||
{
|
||||
printf("MAP[%4d] = %.3lf\n", hm_iter_key(&iter), hm_iter_value(&iter));
|
||||
}
|
||||
|
||||
// Iterate over the HashMap using FOR_EACH macro
|
||||
FOR_EACH(hm, hashmap, map, {
|
||||
// Inside, the variable 'iter' will be available and from it you can access
|
||||
// the key, value or index.
|
||||
printf("MAP[%4d] = %.3lf\n", hm_iter_key(&iter), hm_iter_value(&iter));
|
||||
});
|
||||
|
||||
hm_free(map);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "hashmap.h"
|
||||
#include "cmc/hashmap.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "hashset.h"
|
||||
#include "cmc/hashset.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -14,9 +14,9 @@
|
||||
// - Difference
|
||||
// - Symmetric Difference
|
||||
|
||||
#include "cmc/hashset.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "hashset.h"
|
||||
|
||||
HASHSET_GENERATE(set, set, /* static */, int)
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "cmc/heap.h"
|
||||
#include <stdio.h>
|
||||
#include "heap.h"
|
||||
|
||||
static int intcmp(int a, int b)
|
||||
{
|
||||
@@ -19,7 +19,7 @@ HEAP_GENERATE(h, heap, static, int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
heap *h = h_new(32, MaxHeap, intcmp);
|
||||
heap *h = h_new(32, cmc_max_heap, intcmp);
|
||||
|
||||
for (int i = 1; i < 101; i++)
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "heap.h"
|
||||
#include "cmc/heap.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
@@ -24,7 +24,7 @@ HEAP_GENERATE(h, heap, /* FMOD */, int)
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
// Create a new max heap with an initial capacity of 100
|
||||
heap *my_heap = h_new(100, MaxHeap, intcmp);
|
||||
heap *my_heap = h_new(100, cmc_max_heap, intcmp);
|
||||
|
||||
// Add elements to the heap
|
||||
for (int i = 0; i < 20; i++)
|
||||
|
||||
@@ -7,9 +7,8 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "deque.h"
|
||||
#include "cmc/deque.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
DEQUE_GENERATE(d, deque, static, int)
|
||||
|
||||
|
||||
@@ -7,9 +7,8 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "hashmap.h"
|
||||
#include "cmc/hashmap.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
HASHMAP_GENERATE(hm, hmap, static, int, double)
|
||||
|
||||
@@ -27,7 +26,7 @@ size_t inthash(int t)
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
@@ -61,14 +60,14 @@ int main(int argc, char const *argv[])
|
||||
|
||||
for (hm_iter_to_start(&iter); !hm_iter_end(&iter); hm_iter_next(&iter))
|
||||
{
|
||||
printf("M[%2d] = { %2d : %2.2lf }\n", hm_iter_index(&iter), hm_iter_key(&iter), hm_iter_value(&iter));
|
||||
printf("HM[%2d] = { %2d : %2.2lf }\n", hm_iter_index(&iter), hm_iter_key(&iter), hm_iter_value(&iter));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for (hm_iter_to_end(&iter); !hm_iter_start(&iter); hm_iter_prev(&iter))
|
||||
{
|
||||
printf("M[%2d] = { %2d : %2.2lf }\n", hm_iter_index(&iter), hm_iter_key(&iter), hm_iter_value(&iter));
|
||||
printf("HM[%2d] = { %2d : %2.2lf }\n", hm_iter_index(&iter), hm_iter_key(&iter), hm_iter_value(&iter));
|
||||
}
|
||||
|
||||
hm_free(hm);
|
||||
|
||||
@@ -7,9 +7,8 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "hashset.h"
|
||||
#include "cmc/hashset.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
HASHSET_GENERATE(hs, hset, static, int)
|
||||
|
||||
@@ -27,12 +26,12 @@ size_t inthash(int t)
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
// Initialize hash set and add some elements
|
||||
// Initialize hashset and add some elements
|
||||
hset *hs = hs_new(10, 0.9, intcmp, inthash);
|
||||
for (int i = 0; i < 10; i++)
|
||||
hs_insert(hs, i);
|
||||
@@ -60,14 +59,14 @@ int main(int argc, char const *argv[])
|
||||
|
||||
for (hs_iter_to_start(&iter); !hs_iter_end(&iter); hs_iter_next(&iter))
|
||||
{
|
||||
printf("S[%2d] = %2d\n", hs_iter_index(&iter), hs_iter_value(&iter));
|
||||
printf("HS[%2d] = %2d\n", hs_iter_index(&iter), hs_iter_value(&iter));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for (hs_iter_to_end(&iter); !hs_iter_start(&iter); hs_iter_prev(&iter))
|
||||
{
|
||||
printf("S[%2d] = %2d\n", hs_iter_index(&iter), hs_iter_value(&iter));
|
||||
printf("HS[%2d] = %2d\n", hs_iter_index(&iter), hs_iter_value(&iter));
|
||||
}
|
||||
|
||||
hs_free(hs);
|
||||
|
||||
@@ -7,13 +7,12 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "heap.h"
|
||||
#include "cmc/heap.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
HEAP_GENERATE(h, heap, static, int)
|
||||
|
||||
@@ -7,22 +7,21 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "linkedlist.h"
|
||||
#include "cmc/linkedlist.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
LINKEDLIST_GENERATE(ll, linkedlist, static, int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
// Initialize list and add some elements
|
||||
linkedlist *l = ll_new();
|
||||
// Initialize linkedlist and add some elements
|
||||
linkedlist *ll = ll_new();
|
||||
for (int i = 0; i < 10; i++)
|
||||
ll_push_back(l, i);
|
||||
ll_push_back(ll, i);
|
||||
|
||||
// Initialize iterator
|
||||
linkedlist_iter iter;
|
||||
ll_iter_init(&iter, l);
|
||||
ll_iter_init(&iter, ll);
|
||||
|
||||
// Make the iterator go back and forward
|
||||
for (int j = 0; j < 4; j++)
|
||||
@@ -53,7 +52,7 @@ int main(int argc, char const *argv[])
|
||||
printf("LL[%2d] = %2d\n", ll_iter_index(&iter), ll_iter_value(&iter));
|
||||
}
|
||||
|
||||
ll_free(l);
|
||||
ll_free(ll);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -7,9 +7,8 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "list.h"
|
||||
#include "cmc/list.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
LIST_GENERATE(l, list, static, int)
|
||||
|
||||
|
||||
+15
-15
@@ -7,17 +7,13 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "cmc/queue.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "queue.h"
|
||||
|
||||
QUEUE_GENERATE(q, queue, static, int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
size_t i;
|
||||
int r;
|
||||
|
||||
// Initialize queue and add some elements
|
||||
queue *q = q_new(10);
|
||||
for (int i = 0; i < 10; i++)
|
||||
@@ -30,26 +26,30 @@ int main(int argc, char const *argv[])
|
||||
// Make the iterator go back and forward
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
while (q_iter_next(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
while (!q_iter_end(&iter))
|
||||
{
|
||||
printf("C[%2d] = %2d\n", q_iter_index(&iter), q_iter_value(&iter));
|
||||
q_iter_next(&iter);
|
||||
}
|
||||
printf("\n");
|
||||
while (q_iter_prev(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
while (!q_iter_start(&iter))
|
||||
{
|
||||
printf("C[%2d] = %2d\n", q_iter_index(&iter), q_iter_value(&iter));
|
||||
q_iter_prev(&iter);
|
||||
}
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
for (q_iter_tostart(&iter); !q_iter_end(&iter); /**/)
|
||||
for (q_iter_to_start(&iter); !q_iter_end(&iter); q_iter_next(&iter))
|
||||
{
|
||||
q_iter_next(&iter, &r, &i);
|
||||
printf("Q[%2d] = %2d\n", i, r);
|
||||
printf("Q[%2d] = %2d\n", q_iter_index(&iter), q_iter_value(&iter));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for (q_iter_toend(&iter); !q_iter_start(&iter); /**/)
|
||||
for (q_iter_to_end(&iter); !q_iter_start(&iter); q_iter_prev(&iter))
|
||||
{
|
||||
q_iter_prev(&iter, &r, &i);
|
||||
printf("Q[%2d] = %2d\n", i, r);
|
||||
printf("Q[%2d] = %2d\n", q_iter_index(&iter), q_iter_value(&iter));
|
||||
}
|
||||
|
||||
q_free(q);
|
||||
|
||||
+15
-15
@@ -7,17 +7,13 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "cmc/stack.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "stack.h"
|
||||
|
||||
STACK_GENERATE(s, stack, static, int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
size_t i;
|
||||
int r;
|
||||
|
||||
// Initialize stack and add some elements
|
||||
stack *s = s_new(10);
|
||||
for (int i = 0; i < 10; i++)
|
||||
@@ -30,26 +26,30 @@ int main(int argc, char const *argv[])
|
||||
// Make the iterator go back and forward
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
while (s_iter_next(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
while (!s_iter_end(&iter))
|
||||
{
|
||||
printf("C[%2d] = %2d\n", s_iter_index(&iter), s_iter_value(&iter));
|
||||
s_iter_next(&iter);
|
||||
}
|
||||
printf("\n");
|
||||
while (s_iter_prev(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
while (!s_iter_start(&iter))
|
||||
{
|
||||
printf("C[%2d] = %2d\n", s_iter_index(&iter), s_iter_value(&iter));
|
||||
s_iter_prev(&iter);
|
||||
}
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
for (s_iter_tostart(&iter); !s_iter_end(&iter); /**/)
|
||||
for (s_iter_to_start(&iter); !s_iter_end(&iter); s_iter_next(&iter))
|
||||
{
|
||||
s_iter_next(&iter, &r, &i);
|
||||
printf("S[%2d] = %2d\n", i, r);
|
||||
printf("S[%2d] = %2d\n", s_iter_index(&iter), s_iter_value(&iter));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for (s_iter_toend(&iter); !s_iter_start(&iter); /**/)
|
||||
for (s_iter_to_end(&iter); !s_iter_start(&iter); s_iter_prev(&iter))
|
||||
{
|
||||
s_iter_prev(&iter, &r, &i);
|
||||
printf("S[%2d] = %2d\n", i, r);
|
||||
printf("S[%2d] = %2d\n", s_iter_index(&iter), s_iter_value(&iter));
|
||||
}
|
||||
|
||||
s_free(s);
|
||||
|
||||
@@ -7,58 +7,58 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "cmc/treemap.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "treemap.h"
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
TREEMAP_GENERATE(tm, tmap, static, int, double)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
size_t i;
|
||||
int k;
|
||||
double v, varr[10] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9};
|
||||
double varr[10] = {0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9};
|
||||
|
||||
// Initialize treemap and add some element
|
||||
tmap *tm = tm_new(intcmp);
|
||||
tmap *hm = tm_new(intcmp);
|
||||
for (int i = 0; i < 10; i++)
|
||||
tm_insert(tm, i, varr[i]);
|
||||
tm_insert(hm, i, varr[i]);
|
||||
|
||||
// Initialize iterator
|
||||
tmap_iter iter;
|
||||
tm_iter_init(&iter, tm);
|
||||
tm_iter_init(&iter, hm);
|
||||
|
||||
// Make the iterator go back and forward
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
while (tm_iter_next(&iter, &k, &v, &i))
|
||||
printf("S[%2d] = { %2d : %2.2lf }\n", i, k, v);
|
||||
while (!tm_iter_end(&iter))
|
||||
{
|
||||
printf("C[%2d] = { %2d : %2.2lf }\n", tm_iter_index(&iter), tm_iter_key(&iter), tm_iter_value(&iter));
|
||||
tm_iter_next(&iter);
|
||||
}
|
||||
printf("\n");
|
||||
while (tm_iter_prev(&iter, &k, &v, &i))
|
||||
printf("S[%2d] = { %2d : %2.2lf }\n", i, k, v);
|
||||
while (!tm_iter_start(&iter))
|
||||
{
|
||||
printf("C[%2d] = { %2d : %2.2lf }\n", tm_iter_index(&iter), tm_iter_key(&iter), tm_iter_value(&iter));
|
||||
tm_iter_prev(&iter);
|
||||
}
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
for (tm_iter_tostart(&iter); !tm_iter_end(&iter); /**/)
|
||||
for (tm_iter_to_start(&iter); !tm_iter_end(&iter); tm_iter_next(&iter))
|
||||
{
|
||||
tm_iter_next(&iter, &k, &v, &i);
|
||||
printf("M[%2d] = { %2d : %2.2lf }\n", i, k, v);
|
||||
printf("TM[%2d] = { %2d : %2.2lf }\n", tm_iter_index(&iter), tm_iter_key(&iter), tm_iter_value(&iter));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for (tm_iter_toend(&iter); !tm_iter_start(&iter); /**/)
|
||||
for (tm_iter_to_end(&iter); !tm_iter_start(&iter); tm_iter_prev(&iter))
|
||||
{
|
||||
tm_iter_prev(&iter, &k, &v, &i);
|
||||
printf("M[%2d] = { %2d : %2.2lf }\n", i, k, v);
|
||||
printf("TM[%2d] = { %2d : %2.2lf }\n", tm_iter_index(&iter), tm_iter_key(&iter), tm_iter_value(&iter));
|
||||
}
|
||||
|
||||
tm_free(tm);
|
||||
tm_free(hm);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -7,22 +7,18 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "cmc/treeset.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "treeset.h"
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
TREESET_GENERATE(ts, tset, static, int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
size_t i;
|
||||
int r;
|
||||
|
||||
// Initialize treeset and add some elements
|
||||
tset *ts = ts_new(intcmp);
|
||||
for (int i = 0; i < 10; i++)
|
||||
@@ -35,26 +31,30 @@ int main(int argc, char const *argv[])
|
||||
// Make the iterator go back and forward
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
while (ts_iter_next(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
while (!ts_iter_end(&iter))
|
||||
{
|
||||
printf("C[%2d] = %2d\n", ts_iter_index(&iter), ts_iter_value(&iter));
|
||||
ts_iter_next(&iter);
|
||||
}
|
||||
printf("\n");
|
||||
while (ts_iter_prev(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
while (!ts_iter_start(&iter))
|
||||
{
|
||||
printf("C[%2d] = %2d\n", ts_iter_index(&iter), ts_iter_value(&iter));
|
||||
ts_iter_prev(&iter);
|
||||
}
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
for (ts_iter_tostart(&iter); !ts_iter_end(&iter); /**/)
|
||||
for (ts_iter_to_start(&iter); !ts_iter_end(&iter); ts_iter_next(&iter))
|
||||
{
|
||||
ts_iter_next(&iter, &r, &i);
|
||||
printf("S[%2d] = %2d\n", i, r);
|
||||
printf("TS[%2d] = %2d\n", ts_iter_index(&iter), ts_iter_value(&iter));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for (ts_iter_toend(&iter); !ts_iter_start(&iter); /**/)
|
||||
for (ts_iter_to_end(&iter); !ts_iter_start(&iter); ts_iter_prev(&iter))
|
||||
{
|
||||
ts_iter_prev(&iter, &r, &i);
|
||||
printf("S[%2d] = %2d\n", i, r);
|
||||
printf("TS[%2d] = %2d\n", ts_iter_index(&iter), ts_iter_value(&iter));
|
||||
}
|
||||
|
||||
ts_free(ts);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "linkedlist.h"
|
||||
#include "cmc/linkedlist.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "linkedlist.h"
|
||||
#include "cmc/linkedlist.h"
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "list.h"
|
||||
#include "cmc/list.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "list.h"
|
||||
#include "cmc/list.h"
|
||||
#include <stdio.h>
|
||||
|
||||
LIST_GENERATE(l, list, /* FMOD */, int)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "list.h"
|
||||
#include "cmc/list.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# Multimap examples
|
||||
|
||||
* multimap.c - Basic examples of a multimap.
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "multimap.h"
|
||||
#include "foreach.h"
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
size_t inthash(int t)
|
||||
{
|
||||
size_t a = t;
|
||||
a += ~(a << 15);
|
||||
a ^= (a >> 10);
|
||||
a += (a << 3);
|
||||
a ^= (a >> 6);
|
||||
a += ~(a << 11);
|
||||
a ^= (a >> 16);
|
||||
return a;
|
||||
}
|
||||
|
||||
MULTIMAP_GENERATE(mm, multi_map, , int, int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
const int min = 1, max = 201;
|
||||
multi_map *map = mm_new(50, 0.8, intcmp, inthash);
|
||||
|
||||
printf("Multimap actual capacity: %" PRIu64 "\n", map->capacity);
|
||||
printf("Adding items from %d to %d twice\n", min, max);
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
if (!mm_insert(map, i, i))
|
||||
printf("ERROR with %d:%d\n", i, i);
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
if (!mm_insert(map, i, i))
|
||||
printf("ERROR with %d:%d\n", i, i);
|
||||
|
||||
printf("Multimap count : %" PRIu64 "\n", map->count);
|
||||
printf("Multimap capacity : %" PRIu64 "\n", map->capacity);
|
||||
printf("Multimap load : %lf\n", (double)map->count / (double)map->capacity);
|
||||
|
||||
int64_t sum = 0;
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
sum += mm_get(map, i);
|
||||
|
||||
printf("Sum (unique): %" PRId64 "\n", sum);
|
||||
|
||||
FOR_EACH_MAP(mm, multi_map, int, int, map, {
|
||||
printf("[%3" PRIu64 "] = { %3d : %3d }\n", index, key, value);
|
||||
})
|
||||
|
||||
printf("Removing all items\n");
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
if (!mm_remove_all(map, i))
|
||||
printf("ERROR with %d:%d\n", i, i);
|
||||
|
||||
sum = 0;
|
||||
|
||||
for (int i = min; i < max; i++)
|
||||
sum += mm_get(map, i);
|
||||
|
||||
printf("Sum: %" PRId64 "\n", sum);
|
||||
|
||||
FOR_EACH_MAP(mm, multi_map, int, int, map, {
|
||||
printf("[%2" PRIu64 "] = { %2d : %5d }\n", index, key, value);
|
||||
})
|
||||
|
||||
mm_free(map);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# Multiset examples
|
||||
|
||||
* multiset_operations.c - Demonstrates common multiset operations.
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* multiset_operations.c
|
||||
*
|
||||
* Creation Date: 01/08/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include <ext/multiset.h>
|
||||
#include <utl/assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
|
||||
MULTISET_GENERATE(ms, multiset, , char)
|
||||
|
||||
struct pair
|
||||
{
|
||||
char value;
|
||||
size_t mul;
|
||||
};
|
||||
|
||||
int charcmp(char a, char b)
|
||||
{
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
size_t charhash(char a)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
void print_set(multiset *M)
|
||||
{
|
||||
cmc_assert_not_equals(ptr, NULL, M);
|
||||
|
||||
size_t S = ms_count(M);
|
||||
|
||||
printf("%s : ", ms_to_string(M).s);
|
||||
|
||||
if (S == 0)
|
||||
{
|
||||
printf("{ EMPTY SET }\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (multiset_iter it = M->it_start(M); !ms_iter_end(&it); ms_iter_next(&it))
|
||||
{
|
||||
size_t index = ms_iter_index(&it);
|
||||
char value = ms_iter_value(&it);
|
||||
size_t mul = ms_iter_multiplicity(&it);
|
||||
|
||||
if (index == 0)
|
||||
printf("{ %c:%" PRIuMAX ", ", value, mul);
|
||||
else if (index == S - 1)
|
||||
printf("%c:%" PRIuMAX " }\n", value, mul);
|
||||
else
|
||||
printf("%c:%" PRIuMAX ", ", value, mul);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
struct pair mset1[4] = {{'a', 2}, {'b', 1}, {'c', 3}, {'d', 1}};
|
||||
struct pair mset2[4] = {{'c', 1}, {'d', 3}, {'e', 1}, {'f', 2}};
|
||||
|
||||
multiset *A = ms_new(20, 0.6, charcmp, charhash);
|
||||
multiset *B = ms_new(20, 0.6, charcmp, charhash);
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
cmc_assert(ms_insert_many(A, mset1[i].value, mset1[i].mul));
|
||||
cmc_assert(ms_insert_many(B, mset2[i].value, mset2[i].mul));
|
||||
}
|
||||
|
||||
printf("Given two sets A and B:\n");
|
||||
print_set(A);
|
||||
print_set(B);
|
||||
|
||||
multiset *U = ms_union(A, B);
|
||||
printf("\nA UNION B:\n");
|
||||
print_set(U);
|
||||
|
||||
multiset *I = ms_intersection(A, B);
|
||||
printf("\nA INTERSECTION B:\n");
|
||||
print_set(I);
|
||||
|
||||
multiset *DA = ms_difference(A, B);
|
||||
multiset *DB = ms_difference(B, A);
|
||||
printf("\nA - B:\n");
|
||||
print_set(DA);
|
||||
printf("\nB - A:\n");
|
||||
print_set(DB);
|
||||
|
||||
multiset *AB = ms_summation(A, B);
|
||||
printf("\nA + B:\n");
|
||||
print_set(AB);
|
||||
|
||||
multiset *BA = ms_summation(B, A);
|
||||
printf("\nB + A:\n");
|
||||
print_set(BA);
|
||||
|
||||
multiset *SD = ms_symmetric_difference(A, B);
|
||||
printf("\nA SYM DIFF B:\n");
|
||||
print_set(SD);
|
||||
|
||||
ms_free(A);
|
||||
ms_free(B);
|
||||
ms_free(U);
|
||||
ms_free(I);
|
||||
ms_free(DA);
|
||||
ms_free(DB);
|
||||
ms_free(AB);
|
||||
ms_free(BA);
|
||||
ms_free(SD);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
# Other examples
|
||||
|
||||
* book_store - A book store where its books are loaded onto a list from a csv file. The newly added ones go to a queue of featured books where old ones are removed to give place to new ones. You can find a book by looking up its ISBN using a treemap or traversing the treemap and find all ISBNs of a corresponding title.
|
||||
* separate_header_source - Example of how to used `PUBLIC` and `PRIVATE` parameters.
|
||||
* for_each.c - Using the `FOR_EACH`.
|
||||
* separate_header_source - Example of how to compile the header and source separately.
|
||||
* for_each.c - Using the `FOR_EACH` macro.
|
||||
@@ -9,11 +9,11 @@
|
||||
#include "book.c"
|
||||
#include "random.c"
|
||||
|
||||
COLLECTION_GENERATE(LIST, PUBLIC, l, list, , , book *)
|
||||
COLLECTION_GENERATE(LIST, PUBLIC, il, intlist, , , int)
|
||||
COLLECTION_GENERATE(QUEUE, PUBLIC, q, queue, , , book *)
|
||||
COLLECTION_GENERATE(TREEMAP, PUBLIC, map, map, , int, char *)
|
||||
COLLECTION_GENERATE(TREESET, PUBLIC, set, set, , , char *)
|
||||
COLLECTION_GENERATE(LIST, l, list, , , book *)
|
||||
COLLECTION_GENERATE(LIST, il, intlist, , , int)
|
||||
COLLECTION_GENERATE(QUEUE, q, queue, , , book *)
|
||||
COLLECTION_GENERATE(TREEMAP, map, map, , int, char *)
|
||||
COLLECTION_GENERATE(TREESET, set, set, , , char *)
|
||||
|
||||
list *load(void);
|
||||
void shuffle(list *l);
|
||||
@@ -48,7 +48,9 @@ int main(int argc, char const *argv[])
|
||||
set *counter = set_new(chrcmp);
|
||||
|
||||
// Add everything to its place.
|
||||
FOR_EACH(l, list, book *, books, {
|
||||
FOR_EACH(l, list, books, {
|
||||
book *value = l_iter_value(&iter);
|
||||
|
||||
// Keep the last added elements
|
||||
if (q_count(shelf) > 7)
|
||||
{
|
||||
@@ -59,14 +61,15 @@ int main(int argc, char const *argv[])
|
||||
|
||||
map_insert(library, value->isbn, value->name);
|
||||
set_insert(counter, value->name);
|
||||
})
|
||||
});
|
||||
|
||||
printf("Library Size: %d\n", map_count(library));
|
||||
printf("Unique book titles: %d\n\n", set_count(counter));
|
||||
printf("Showcase Books:\n");
|
||||
FOR_EACH(q, queue, book *, shelf, {
|
||||
FOR_EACH(q, queue, shelf, {
|
||||
book *value = q_iter_value(&iter);
|
||||
printf("[ %d : %-45s ]\n", value->isbn, value->name);
|
||||
})
|
||||
});
|
||||
printf("\n");
|
||||
|
||||
set_free(counter);
|
||||
@@ -113,24 +116,31 @@ int main(int argc, char const *argv[])
|
||||
|
||||
intlist *search_list = il_new(32);
|
||||
|
||||
FOR_EACH_MAP(map, map, int, char *, library, {
|
||||
FOR_EACH(map, map, library, {
|
||||
int key = map_iter_key(&iter);
|
||||
char *value = map_iter_value(&iter);
|
||||
|
||||
if (chrcmp(buffer, value) == 0)
|
||||
il_push_back(search_list, key);
|
||||
})
|
||||
});
|
||||
|
||||
FOR_EACH(il, intlist, int, search_list, {
|
||||
printf("[ %d ]\n", value);
|
||||
})
|
||||
FOR_EACH(il, intlist, search_list, {
|
||||
printf("[ %d ]\n", il_iter_value(&iter));
|
||||
});
|
||||
|
||||
il_free(search_list);
|
||||
}
|
||||
}
|
||||
|
||||
// Free all memory used by struct book
|
||||
FOR_EACH(l, list, book *, books, {
|
||||
free(value->name);
|
||||
free(value);
|
||||
})
|
||||
FOR_EACH(l, list, books, {
|
||||
book *b = l_iter_value(&iter);
|
||||
|
||||
if (b)
|
||||
free(b->name);
|
||||
|
||||
free(b);
|
||||
});
|
||||
|
||||
l_free(books);
|
||||
q_free(shelf);
|
||||
|
||||
+32
-20
@@ -1,19 +1,19 @@
|
||||
#include "macro_collections.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "macro_collections.h"
|
||||
|
||||
// Create containers to use only in this file
|
||||
COLLECTION_GENERATE(LIST, PUBLIC, l, list, static, , int)
|
||||
COLLECTION_GENERATE(LINKEDLIST, PUBLIC, ll, llist, static, , int)
|
||||
COLLECTION_GENERATE(STACK, PUBLIC, s, stack, static, , int)
|
||||
COLLECTION_GENERATE(QUEUE, PUBLIC, q, queue, static, , int)
|
||||
COLLECTION_GENERATE(DEQUE, PUBLIC, d, deque, static, , int)
|
||||
COLLECTION_GENERATE(TREESET, PUBLIC, ts, tset, static, , int)
|
||||
COLLECTION_GENERATE(LIST, l, list, static, , int)
|
||||
COLLECTION_GENERATE(LINKEDLIST, ll, llist, static, , int)
|
||||
COLLECTION_GENERATE(STACK, s, stack, static, , int)
|
||||
COLLECTION_GENERATE(QUEUE, q, queue, static, , int)
|
||||
COLLECTION_GENERATE(DEQUE, d, deque, static, , int)
|
||||
COLLECTION_GENERATE(TREESET, ts, tset, static, , int)
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
@@ -58,59 +58,71 @@ int main(int argc, char const *argv[])
|
||||
// - value : the elements that changes with each iteration
|
||||
// - index : iteration index (first iteration is 0 and last is count - 1)
|
||||
// - iter : iterator struct
|
||||
FOR_EACH(l, list, int, l, {
|
||||
FOR_EACH(l, list, l, {
|
||||
int value = l_iter_value(&iter);
|
||||
size_t index = l_iter_index(&iter);
|
||||
if (index == 0)
|
||||
printf("[ %d, ", value);
|
||||
else if (index == iter.target->count - 1)
|
||||
printf("%d ]\n", value);
|
||||
else
|
||||
printf("%d, ", value);
|
||||
})
|
||||
});
|
||||
|
||||
FOR_EACH(s, stack, int, s, {
|
||||
FOR_EACH(s, stack, s, {
|
||||
int value = s_iter_value(&iter);
|
||||
size_t index = s_iter_index(&iter);
|
||||
if (index == 0)
|
||||
printf("[ %d, ", value);
|
||||
else if (index == iter.target->count - 1)
|
||||
printf("%d ]\n", value);
|
||||
else
|
||||
printf("%d, ", value);
|
||||
})
|
||||
});
|
||||
|
||||
FOR_EACH(q, queue, int, q, {
|
||||
FOR_EACH(q, queue, q, {
|
||||
int value = q_iter_value(&iter);
|
||||
size_t index = q_iter_index(&iter);
|
||||
if (index == 0)
|
||||
printf("[ %d, ", value);
|
||||
else if (index == iter.target->count - 1)
|
||||
printf("%d ]\n", value);
|
||||
else
|
||||
printf("%d, ", value);
|
||||
})
|
||||
});
|
||||
|
||||
FOR_EACH(d, deque, int, d, {
|
||||
FOR_EACH(d, deque, d, {
|
||||
int value = d_iter_value(&iter);
|
||||
size_t index = d_iter_index(&iter);
|
||||
if (index == 0)
|
||||
printf("[ %d, ", value);
|
||||
else if (index == iter.target->count - 1)
|
||||
printf("%d ]\n", value);
|
||||
else
|
||||
printf("%d, ", value);
|
||||
})
|
||||
});
|
||||
|
||||
FOR_EACH(ll, llist, int, ll, {
|
||||
FOR_EACH(ll, llist, ll, {
|
||||
int value = ll_iter_value(&iter);
|
||||
size_t index = ll_iter_index(&iter);
|
||||
if (index == 0)
|
||||
printf("[ %d, ", value);
|
||||
else if (index == iter.target->count - 1)
|
||||
printf("%d ]\n", value);
|
||||
else
|
||||
printf("%d, ", value);
|
||||
})
|
||||
});
|
||||
|
||||
FOR_EACH(ts, tset, int, ts, {
|
||||
FOR_EACH(ts, tset, ts, {
|
||||
int value = ts_iter_value(&iter);
|
||||
size_t index = ts_iter_index(&iter);
|
||||
if (index == 0)
|
||||
printf("[ %d, ", value);
|
||||
else if (index == iter.target->count - 1)
|
||||
printf("%d ]\n", value);
|
||||
else
|
||||
printf("%d, ", value);
|
||||
})
|
||||
});
|
||||
|
||||
l_free(l);
|
||||
ll_free(ll);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "containers.h"
|
||||
|
||||
COLLECTION_GENERATE_SOURCE(LIST, PUBLIC, il, int_list, , , int)
|
||||
COLLECTION_GENERATE_SOURCE(STACK, PRIVATE, cs, char_stack, , , char)
|
||||
COLLECTION_GENERATE_SOURCE(QUEUE, PUBLIC, queue, index_queue, , , size_t)
|
||||
COLLECTION_GENERATE_SOURCE(DEQUE, PUBLIC, dq, deque, , , int)
|
||||
COLLECTION_GENERATE_SOURCE(LIST, il, int_list, , , int)
|
||||
COLLECTION_GENERATE_SOURCE(STACK, cs, char_stack, , , char)
|
||||
COLLECTION_GENERATE_SOURCE(QUEUE, queue, index_queue, , , size_t)
|
||||
COLLECTION_GENERATE_SOURCE(DEQUE, dq, deque, , , int)
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <stdbool.h>
|
||||
#include "macro_collections.h"
|
||||
|
||||
COLLECTION_GENERATE_HEADER(LIST, PUBLIC, il, int_list, , , int)
|
||||
COLLECTION_GENERATE_HEADER(STACK, PRIVATE, cs, char_stack, , , char)
|
||||
COLLECTION_GENERATE_HEADER(QUEUE, PUBLIC, queue, index_queue, , , size_t)
|
||||
COLLECTION_GENERATE_HEADER(DEQUE, PUBLIC, dq, deque, , , int)
|
||||
COLLECTION_GENERATE_HEADER(LIST, il, int_list, , , int)
|
||||
COLLECTION_GENERATE_HEADER(STACK, cs, char_stack, , , char)
|
||||
COLLECTION_GENERATE_HEADER(QUEUE, queue, index_queue, , , size_t)
|
||||
COLLECTION_GENERATE_HEADER(DEQUE, dq, deque, , , int)
|
||||
@@ -184,10 +184,9 @@ int main(void)
|
||||
il_push_back(integers, i);
|
||||
|
||||
int sum = 0;
|
||||
FOR_EACH(il, int_list, int, integers, {
|
||||
printf("%d, ", value);
|
||||
sum += value;
|
||||
})
|
||||
FOR_EACH(il, int_list, integers, {
|
||||
sum += il_iter_value(&iter);
|
||||
});
|
||||
|
||||
printf("\n\nSUM: %d\n", sum);
|
||||
|
||||
@@ -255,9 +254,9 @@ int main(void)
|
||||
|
||||
int sum3 = 0;
|
||||
|
||||
FOR_EACH(dq, deque, int, d, {
|
||||
sum3 += value;
|
||||
})
|
||||
FOR_EACH(dq, deque, d, {
|
||||
sum3 += dq_iter_value(&iter);
|
||||
});
|
||||
|
||||
printf("Deque sum: %d\n", sum3);
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
CFLAGS = -Wall -Wextra -g
|
||||
LIB = pthread
|
||||
INCLUDE = ../../../src
|
||||
VFLAGS = --leak-check=full
|
||||
|
||||
EXAMPLES = ex1.txt ex2.txt ex3.txt ex4.txt ex5.txt ex6.txt
|
||||
|
||||
SOURCES = ../../../src/cmc/deque.h \
|
||||
../../../src/cmc/hashmap.h \
|
||||
../../../src/cmc/hashset.h \
|
||||
../../../src/cmc/heap.h \
|
||||
../../../src/cmc/linkedlist.h \
|
||||
../../../src/cmc/list.h \
|
||||
../../../src/cmc/queue.h \
|
||||
../../../src/cmc/stack.h \
|
||||
../../../src/cmc/treemap.h \
|
||||
../../../src/cmc/treeset.h \
|
||||
|
||||
main: counter.c
|
||||
gcc counter.c $(CFLAGS) -I $(INCLUDE) -l$(LIB)
|
||||
|
||||
example: main $(EXAMPLES)
|
||||
./a.out $(EXAMPLES)
|
||||
|
||||
valgrind: main $(EXAMPLES)
|
||||
valgrind $(VFLAGS) ./a.out $(EXAMPLES)
|
||||
|
||||
source: main
|
||||
./a.out $(SOURCES)
|
||||
@@ -0,0 +1,269 @@
|
||||
/**
|
||||
* counter.c
|
||||
*
|
||||
* Creation Date: 05/07/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* A multi-threading word counter in C using the C Macro Collections library
|
||||
* and the pthread library.
|
||||
*/
|
||||
|
||||
#include <cmc/hashmap.h>
|
||||
#include <cmc/heap.h>
|
||||
#include <cmc/list.h>
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <pthread.h>
|
||||
#include <sac/stack.h>
|
||||
#include <stdio.h>
|
||||
#define CMC_LOG_COLOR
|
||||
#include <utl/log.h>
|
||||
|
||||
// Maximum number of threads
|
||||
#define n_threads 100
|
||||
|
||||
/* HashMap mutex */
|
||||
pthread_mutex_t table_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
/* printf, fprintf mutex */
|
||||
pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
/* A pair of word and number of how many times that word has appeared */
|
||||
/* This will be used by Heap */
|
||||
typedef struct wc_pair_s
|
||||
{
|
||||
char *word;
|
||||
size_t count;
|
||||
} wc_pair;
|
||||
|
||||
/* A stack allocated stack to keep track of each thread id */
|
||||
SAC_STACK_GENERATE(tl, TList, , pthread_t, n_threads)
|
||||
|
||||
/* A list used as a dynamic string */
|
||||
LIST_GENERATE(str, String, , char)
|
||||
|
||||
/* A mapping between word and word count */
|
||||
HASHMAP_GENERATE(hm, HashMap, , char *, size_t)
|
||||
|
||||
/* Used by HashMap to hash its keys (char *) */
|
||||
size_t djb2(char *str)
|
||||
{
|
||||
size_t hash = 5381;
|
||||
int c;
|
||||
|
||||
while ((c = *str++))
|
||||
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
/* Used by HashMap to compare its keys (char *) */
|
||||
int chrcmp(char *a, char *b)
|
||||
{
|
||||
return strcmp(a, b);
|
||||
}
|
||||
|
||||
/* Heap used to sort the { word : word_count } pairs */
|
||||
HEAP_GENERATE(hp, Heap, , wc_pair)
|
||||
|
||||
/* Used by Heap to sort wc_pair */
|
||||
int cmp_wcpair(wc_pair p1, wc_pair p2)
|
||||
{
|
||||
/* Sort by count, then by word */
|
||||
int cmp = (p1.count > p2.count) - (p1.count < p2.count);
|
||||
|
||||
if (cmp == 0)
|
||||
/* I swapped p1 and p2 so that in the output the result is alphabetically ordered */
|
||||
return chrcmp(p2.word, p1.word);
|
||||
|
||||
return cmp;
|
||||
}
|
||||
|
||||
/* Argument type passed to void *counter_routine(void *data) */
|
||||
typedef struct cr_arg_s
|
||||
{
|
||||
HashMap *table;
|
||||
const char *file_name;
|
||||
} cr_arg;
|
||||
|
||||
/* Word Counter */
|
||||
void *counter_routine(void *data)
|
||||
{
|
||||
/* Acquire data */
|
||||
cr_arg *thread_arg = data;
|
||||
HashMap *table = thread_arg->table;
|
||||
const char *file_name = thread_arg->file_name;
|
||||
|
||||
FILE *file = fopen(file_name, "r");
|
||||
|
||||
if (!file)
|
||||
{
|
||||
pthread_mutex_lock(&print_mutex);
|
||||
|
||||
fprintf(stderr, "Failed to open file %s\n", file_name);
|
||||
|
||||
pthread_mutex_unlock(&print_mutex);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ch = 0;
|
||||
String *word = str_new(100);
|
||||
|
||||
/* Begin reading file */
|
||||
/* Whenever a word is found, add it to the HashMap */
|
||||
while ((ch = fgetc(file)) != EOF)
|
||||
{
|
||||
/* Normalize */
|
||||
ch = tolower(ch);
|
||||
|
||||
/* Stop when is a space character or punctuation */
|
||||
/* Only count alphabetical letter */
|
||||
if ( ( isspace(ch) || ispunct(ch) ) && !str_empty(word) )
|
||||
{
|
||||
str_push_back(word, 0);
|
||||
|
||||
char *key = strdup(word->buffer);
|
||||
|
||||
pthread_mutex_lock(&table_mutex);
|
||||
|
||||
size_t *num = hm_get_ref(table, key);
|
||||
|
||||
if (num == NULL)
|
||||
hm_insert(table, key, 1);
|
||||
else
|
||||
{
|
||||
*num += 1;
|
||||
free(key);
|
||||
key = NULL;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&table_mutex);
|
||||
|
||||
str_clear(word);
|
||||
}
|
||||
else if (isalpha(ch))
|
||||
{
|
||||
str_push_back(word, ch);
|
||||
}
|
||||
}
|
||||
|
||||
str_free(word);
|
||||
fclose(file);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
/* Check arguments*/
|
||||
if (argc > n_threads)
|
||||
{
|
||||
fprintf(stderr, "Sorry but no more than %d arguments\n", n_threads);
|
||||
return 2;
|
||||
}
|
||||
else if (argc == 1)
|
||||
{
|
||||
fprintf(stderr, "Usage:\n %s path/to/file.txt path/to/another/file.txt\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
cr_arg *thread_args = calloc(n_threads, sizeof(cr_arg));
|
||||
|
||||
/* Threads id storage */
|
||||
TList ts = tl_new();
|
||||
|
||||
/* Resulting word - word_count mapping */
|
||||
HashMap *result = hm_new(100000, 0.6, chrcmp, djb2);
|
||||
|
||||
/* Create one thread for each file */
|
||||
for (int i = 1; i < argc; i++)
|
||||
{
|
||||
thread_args[i - 1].table = result;
|
||||
thread_args[i - 1].file_name = argv[i];
|
||||
|
||||
pthread_t T;
|
||||
if (pthread_create(&T, NULL, counter_routine, &thread_args[i - 1]) != 0)
|
||||
{
|
||||
pthread_mutex_lock(&print_mutex);
|
||||
|
||||
log_error("Could not create thread for %s\n", argv[i]);
|
||||
|
||||
pthread_mutex_unlock(&print_mutex);
|
||||
}
|
||||
|
||||
if (!tl_push(&ts, T))
|
||||
{
|
||||
pthread_mutex_lock(&print_mutex);
|
||||
|
||||
log_error("Could not push to stack %lu", T);
|
||||
|
||||
pthread_mutex_unlock(&print_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait for all threads to finish */
|
||||
for (TList_iter iter = ts.it_start(&ts); !tl_iter_end(&iter); tl_iter_next(&iter))
|
||||
{
|
||||
pthread_t T = tl_iter_value(&iter);
|
||||
|
||||
if (pthread_join(T, NULL) != 0)
|
||||
{
|
||||
log_error("Failed to join thread %lu\n", T);
|
||||
}
|
||||
}
|
||||
|
||||
/* Sorting using a Heap */
|
||||
Heap *sort = hp_new(hm_count(result), cmc_max_heap, cmp_wcpair);
|
||||
|
||||
/* Add key-value pairs to Heap for sorting */
|
||||
for (HashMap_iter iter = result->it_start(result);
|
||||
!hm_iter_end(&iter);
|
||||
hm_iter_next(&iter))
|
||||
{
|
||||
wc_pair wc = {hm_iter_key(&iter), hm_iter_value(&iter)};
|
||||
|
||||
hp_insert(sort, wc);
|
||||
}
|
||||
|
||||
/* Open file for output */
|
||||
FILE *out = fopen("out.txt", "w");
|
||||
|
||||
if (!out)
|
||||
{
|
||||
fprintf(stderr, "Failed to create file %s\n", "out.txt");
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(out, "A total of %" PRIuMAX " unique words were found\n\n", hp_count(sort));
|
||||
|
||||
/* Until heap is exausted, print the pair into the file */
|
||||
while (!hp_empty(sort))
|
||||
{
|
||||
wc_pair wc;
|
||||
|
||||
if (!hp_remove(sort, &wc))
|
||||
{
|
||||
fprintf(stderr, "Failed to remove element from heap");
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(out, "%s, %" PRIuMAX "\n", wc.word, wc.count);
|
||||
free(wc.word);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(out);
|
||||
}
|
||||
|
||||
hp_free(sort);
|
||||
hm_free(result);
|
||||
free(thread_args);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
Man, I'm sore all over. I feel like I just went ten rounds with mighty Thor.
|
||||
I'm a thing. But I've never been to the moon! Bender, this is Fry's decision… and he made it wrong. So it's time for us to interfere in his life. Five hours? Aw, man! Couldn't you just get me the death penalty?
|
||||
Shut up and get to the point! But I know you in the future. I cleaned your poop. Good man. Nixon's pro-war and pro-family. I usually try to keep my sadness pent up inside where it can fester quietly as a mental illness.
|
||||
Robot 1-X, save my friends! And Zoidberg!
|
||||
Bender, this is Fry's decision… and he made it wrong. So it's time for us to interfere in his life. Why would a robot need to drink? I can explain. It's very valuable. Ow, my spirit!
|
||||
Calculon is gonna kill us and it's all everybody else's fault!
|
||||
Of all the friends I've had… you're the first.
|
||||
I'm Santa Claus!
|
||||
Yeah, lots of people did.
|
||||
My fellow Earthicans, as I have explained in my book 'Earth in the Balance'', and the much more popular ''Harry Potter and the Balance of Earth', we need to defend our planet against pollution. Also dark wizards. I videotape every customer that comes in here, so that I may blackmail them later.
|
||||
Ask her how her day was.
|
||||
If rubbin' frozen dirt in your crotch is wrong, hey I don't wanna be right.
|
||||
And so we say goodbye to our beloved pet, Nibbler, who's gone to a place where I, too, hope one day to go. The toilet.
|
||||
Guards! Bring me the forms I need to fill out to have her taken away! Now what? Fry! Quit doing the right thing, you jerk! It doesn't look so shiny to me. Who said that? SURE you can die! You want to die?!
|
||||
And until then, I can never die? Shinier than yours, meatbag. Oh no! The professor will hit me! But if Zoidberg 'fixes' it… then perhaps gifts! Look, last night was a mistake.
|
||||
Tell them I hate them. Have you ever tried just turning off the TV, sitting down with your children, and hitting them? You'll have all the Slurm you can drink when you're partying with Slurms McKenzie!
|
||||
Fetal stemcells, aren't those controversial? You, minion. Lift my arm. AFTER HIM! OK, if everyone's finished being stupid. Kif might! Stop! Don't shoot fire stick in space canoe! Cause explosive decompression!
|
||||
Alright, let's mafia things up a bit. Joey, burn down the ship. Clamps, burn down the crew. You don't know how to do any of those. Uh, is the puppy mechanical in any way? I've been there. My folks were always on me to groom myself and wear underpants. What am I, the pope?
|
||||
Why not indeed! I'll tell them you went down prying the wedding ring off his cold, dead finger. Too much work. Let's burn it and say we dumped it in the sewer. Hey! I'm a porno-dealing monster, what do I care what you think?
|
||||
WINDMILLS DO NOT WORK THAT WAY! GOOD NIGHT! Ah, the 'Breakfast Club' soundtrack! I can't wait til I'm old enough to feel ways about stuff! No! Don't jump! Michelle, I don't regret this, but I both rue and lament it.
|
||||
Why would I want to know that? Well I'da done better, but it's plum hard pleading a case while awaiting trial for that there incompetence. Take me to your leader! Humans dating robots is sick. You people wonder why I'm still single? It's 'cause all the fine robot sisters are dating humans!
|
||||
Isn't it true that you have been paid for your testimony? Why am I sticky and naked? Did I miss something fun? Whoa a real live robot; or is that some kind of cheesy New Year's costume? Yep, I remember. They came in last at the Olympics, then retired to promote alcoholic beverages!
|
||||
Fry, we have a crate to deliver. These old Doomsday Devices are dangerously unstable. I'll rest easier not knowing where they are. No! The cat shelter's on to me. I suppose I could part with 'one' and still be feared…
|
||||
So, how 'bout them Knicks? It's just like the story of the grasshopper and the octopus. All year long, the grasshopper kept burying acorns for winter, while the octopus mooched off his girlfriend and watched TV. But then the winter came, and the grasshopper died, and the octopus ate all his acorns. Also he got a race car. Is any of this getting through to you?
|
||||
@@ -0,0 +1,10 @@
|
||||
If you get asteroids about a kilometer in size, those are large enough and carry enough energy into our system to disrupt transportation, communication, the food chains, and that can be a really bad day on Earth.
|
||||
In the animal kingdom, one of the keys to survival is to outwit your enemies. And when you're surrounded by carnivores, one of the best strategies is to fade into the background and disappear.
|
||||
Most religious people in America fully embrace science. So the argument that religion has some issue with science applies to a small fraction of those who declare that they are religious. They just happen to be a very vocal fraction, so you got the impression that there are more of them than there actually is.
|
||||
Do you realize that if you fall into a black hole, you will see the entire future of the Universe unfold in front of you in a matter of moments and you will emerge into another space-time created by the singularity of the black hole you just fell into?
|
||||
When you put money directly to a problem, it makes a good headline. It makes a good campaign slogan. You get to claim that you've engaged in these activities within an election cycle. But certain investments take longer than an election cycle.
|
||||
We have bred multiple generations of people who have not experienced knowing where you are the moment a news story broke, with that news story being great and grand and something that elevates society instead of diminishes it.
|
||||
We think scientific literacy flows out of how many science facts can you recite rather than how was your brain wired for thinking. And it's the brain wiring that I'm more interested in rather than the facts that come out of the curriculum or the lesson plan that's been proposed.
|
||||
What you need, above all else, is a love for your subject, whatever it is. You've got to be so deeply in love with your subject that when curve balls are thrown, when hurdles are put in place, you've got the energy to overcome them.
|
||||
We didn't build the interstate system to connect New York to Los Angeles because the West Coast was a priority. No, we webbed the highways so people can go to multiple places and invent ways of doing things not thought of by the persons building the roads.
|
||||
Stephen Hawking's been watching too many Hollywood movies. I think the only kind aliens in Hollywood are the ones created by Steven Spielberg - 'Close Encounters of the Third Kind' and 'E.T.,' for example. All other aliens are trying to suck our brains out.
|
||||
@@ -0,0 +1,6 @@
|
||||
Enlightenment snare transvaluation love right insofar madness zarathustra. God sexuality endless free disgust hatred hope. Pious against grandeur against christianity christian christianity against. Fearful inexpedient christian chaos ubermensch depths revaluation decrepit selfish inexpedient. Of society good oneself reason oneself. Eternal-return ocean spirit spirit evil society mountains. Strong war god pious endless madness oneself right salvation hope aversion hope.
|
||||
Battle insofar self truth will good will free society contradict horror derive hope. Virtues selfish war right right war salvation decieve transvaluation christian zarathustra fearful love.
|
||||
Superiority fearful law love sea burying decieve. Will against abstract snare reason truth noble of inexpedient. Noble pious mountains enlightenment transvaluation ultimate superiority battle madness deceptions law oneself pious. Revaluation morality society intentions decrepit inexpedient.
|
||||
Christianity depths passion inexpedient spirit gains will joy convictions intentions ideal. Sea against ultimate burying inexpedient evil. Overcome fearful insofar right contradict depths love hope love. Noble intentions war derive snare dead insofar.
|
||||
Faith philosophy abstract pinnacle war ocean philosophy free. Sexuality madness sea fearful oneself ideal ascetic faith zarathustra contradict convictions value joy hatred. Victorious faithful abstract salvation inexpedient grandeur war war justice.
|
||||
Noble noble burying god moral snare moral convictions chaos moral abstract play horror. Pious will mountains decieve.
|
||||
@@ -0,0 +1,10 @@
|
||||
My capacity is full no scraps hit the floor agile and we don't need to boil the ocean here get all your ducks in a row, nor run it up the flag pole. Time vampire. Bottleneck mice can we take this offline, and scope creep. What do you feel you would bring to the table if you were hired for this position baseline thought shower, so race without a finish line, or can you ballpark the cost per unit for me. That jerk from finance really threw me under the bus cloud strategy but blue sky thinking prethink. Meeting assassin waste of resources. Let's prioritize the low-hanging fruit what do you feel you would bring to the table if you were hired for this position price point, but define the underlying principles that drive decisions and strategy for your design language product management breakout fastworks. Level the playing field. Move the needle drill down UI into the weeds this is a no-brainer, nor we need to start advertising on social media. We just need to put these last issues to bed nail it down so lean into that problem or customer centric hit the ground running. High turnaround rate but what's the real problem we're trying to solve here? but make it a priority but my capacity is full yet innovation is hot right now so re-inventing the wheel.
|
||||
Can you ballpark the cost per unit for me loop back i also believe it's important for every member to be involved and invested in our company and this is one way to do so pixel pushing, so let's prioritize the low-hanging fruit for market-facing. Move the needle what about scaling components to a global audience? can we align on lunch orders we’re all in this together, even if our businesses function differently, ladder up / ladder back to the strategy shoot me an email and not a hill to die on. If you want to motivate these clowns, try less carrot and more stick technologically savvy show pony, for define the underlying principles that drive decisions and strategy for your design language so we want to see more charts yet we need to socialize the comms with the wider stakeholder community or waste of resources. Teams were able to drive adoption and awareness. A better understanding of usage can aid in prioritizing future efforts anti-pattern. Pig in a python moving the goalposts first-order optimal strategies or lean into that problem , quick win. We need to leverage our synergies. Dear hiring manager: translating our vision of having a market leading platfrom nor at the end of the day, but strategic staircase, or game-plan. Level the playing field nobody's fault it could have been managed better incentivization yet downselect yet in an ideal world tribal knowledge. Paddle on both sides helicopter view, and c-suite. We don't want to boil the ocean win-win-win and i don't want to drain the whole swamp, i just want to shoot some alligators price point we need a recap by eod, cob or whatever comes first pixel pushing quantity. Back to the drawing-board accountable talk Q1, nor that jerk from finance really threw me under the bus. Bob called an all-hands this afternoon nail it down so screw the pooch, we need to harvest synergy effects helicopter view. Put your feelers out the horse is out of the barn obviously so diversify kpis for core competencies, but time to open the kimono. Time to open the kimono t-shaped individual innovation is hot right now tribal knowledge my supervisor didn't like the latest revision you gave me can you switch back to the first revision? thinking outside the box, but high performance keywords. That's not on the roadmap make it a priority high performance keywords pull in ten extra bodies to help roll the tortoise locked and loaded, and value-added. The horse is out of the barn minimize backwards overflow. What's our go to market strategy? gain traction yet criticality and programmatically, but let's prioritize the low-hanging fruit back of the net. Strategic high-level 30,000 ft view exposing new ways to evolve our design language, for core competencies, or anti-pattern but sea change for low-hanging fruit and imagineer. Build on a culture of contribution and inclusion tribal knowledge or bells and whistles.
|
||||
Can you send me an invite? cloud strategy. First-order optimal strategies. Cannibalize back to the drawing-board strategic fit low engagement. Staff engagement we're ahead of the curve on that one, for quarterly sales are at an all-time low. Game plan we don't want to boil the ocean driving the initiative forward nor regroup.
|
||||
Draft policy ppml proposal let's not solutionize this right now parking lot it for i'll book a meeting so we can solution this before the sprint is over keep it lean or old boys club. Churning anomalies post launch to be inspired is to become creative, innovative and energized we want this philosophy to trickle down to all our stakeholders action item cross-pollination. But what's the real problem we're trying to solve here?. Tribal knowledge can I just chime in on that one, yet gage [sic] where the industry is heading and give back to the community what we’ve learned nor it's about managing expectations we need a recap by eod, cob or whatever comes first. Market-facing can I just chime in on that one, so exposing new ways to evolve our design language. Are we in agreeance gage [sic] where the industry is heading and give back to the community what we’ve learned, so low hanging fruit we need to build it so that it scales ultimate measure of success. Downselect into the weeds, but back of the net. Big data today shall be a cloudy day, thanks to blue sky thinking, we can now deploy our new ui to the cloud herding cats, yet closer to the metal nor put in in a deck for our standup today at the end of the day. Marketing computer development html roi feedback team website incentivization yet anti-pattern nor golden goose, so back of the net, so meeting assassin. Strategic staircase drink the Kool-aid design thinking, but blue money. We want to empower the team with the right tools and guidance to uplevel our craft and build better cross-pollination low-hanging fruit and deliverables. Imagineer roll back strategy can you ballpark the cost per unit for me. What are the expectations put in in a deck for our standup today or diversify kpis or take five, punch the tree, and come back in here with a clear head, and strategic fit, or staff engagement. Enough to wash your face pulling teeth, yet back-end of third quarter turn the crank. Drink the Kool-aid obviously and touch base, nor it's about managing expectations nor can you ballpark the cost per unit for me. High-level driving the initiative forward. Guerrilla marketing. Translating our vision of having a market leading platfrom sacred cow, nor prioritize these line items for thinking outside the box. Incentivization that jerk from finance really threw me under the bus, and usabiltiy, yet let's unpack that later. Poop personal development. PowerPointless push back for sea change nor pushback.
|
||||
Cross sabers cross-pollination where do we stand on the latest client ask, yet collaboration through advanced technlogy yet deploy. Single wringable neck hit the ground running can you send me an invite?. Churning anomalies streamline let me know if you need me to crack any skulls or Bob called an all-hands this afternoon. Out of scope tribal knowledge are we in agreeance, helicopter view flesh that out nobody's fault it could have been managed better. Gain traction hard stop. Customer centric please use "solutionise" instead of solution ideas! :) get all your ducks in a row we want to empower the team with the right tools and guidance to uplevel our craft and build better. Hit the ground running new economy. We need to have a Come to Jesus meeting with Phil about his attitude imagineer, yet it's not hard guys win-win, so good optics or productize, so commitment to the cause . Going forward. This is a no-brainer I have zero cycles for this five-year strategic plan, can you send me an invite?. Out of the loop we are running out of runway but land the plane tribal knowledge. Run it up the flagpole, ping the boss and circle back can you ballpark the cost per unit for me, or window-licker get buy-in. Root-and-branch review when does this sunset? but draft policy ppml proposal can I just chime in on that one. Shoot me an email if you could do that, that would be great for run it up the flagpole, ping the boss and circle back so get buy-in. Window of opportunity technologically savvy. Bottleneck mice strategic staircase, programmatically. Upsell we need to button up our approach. Back of the net going forward cannibalize regroup and value-added.
|
||||
Race without a finish line blue sky. We just need to put these last issues to bed innovation is hot right now. Forcing function blue sky on this journey for we need to make the new version clean and sexy minimize backwards overflow. Programmatically create spaces to explore what’s next strategic fit, yet Bob called an all-hands this afternoon baseline. Bottleneck mice. Deliverables. Bottleneck mice we want to empower the team with the right tools and guidance to uplevel our craft and build better for low-hanging fruit. Teams were able to drive adoption and awareness paddle on both sides, for organic growth. Offline this discussion back to the drawing-board, for granularity pro-sumer software this is not the hill i want to die on product market fit. Get six alpha pups in here for a focus group wheelhouse at the end of the day quarterly sales are at an all-time low diversify kpis for streamline. Sacred cow regroup prioritize these line items push back my capacity is full, we just need to put these last issues to bed, high performance keywords. Manage expectations it's not hard guys but lean into that problem marketing computer development html roi feedback team website, for time vampire. Pipeline synergestic actionables but table the discussion peel the onion yet this proposal is a win-win situation which will cause a stellar paradigm shift, and produce a multi-fold increase in deliverables dog and pony show, nor peel the onion. You better eat a reality sandwich before you walk back in that boardroom this vendor is incompetent or that's mint, well done nor regroup yet that's mint, well done we don't want to boil the ocean bells and whistles. That ipo will be a game-changer deep dive goalposts t-shaped individual. Touch base proceduralize. Overcome key issues to meet key milestones not a hill to die on bake it in. Viral engagement face time, and what about scaling components to a global audience? win-win, nor staff engagement. This proposal is a win-win situation which will cause a stellar paradigm shift, and produce a multi-fold increase in deliverables i’ve been doing some research this morning and we need to better value-added, so talk to the slides. Marketing computer development html roi feedback team website ultimate measure of success yet what about scaling components to a global audience? but show pony. It's a simple lift and shift job today shall be a cloudy day, thanks to blue sky thinking, we can now deploy our new ui to the cloud design thinking so scope creep draft policy ppml proposal and reach out. Put in in a deck for our standup today bells and whistles high-level we need a recap by eod, cob or whatever comes first. What's our go to market strategy?. Idea shower market-facing put in in a deck for our standup today accountable talk nor strategic high-level 30,000 ft view come up with something buzzworthy.
|
||||
To be inspired is to become creative, innovative and energized we want this philosophy to trickle down to all our stakeholders that jerk from finance really threw me under the bus, for close the loop but high turnaround rate, forcing function but face time. Feature creep we need to harvest synergy effects yet nobody's fault it could have been managed better and loop back low-hanging fruit criticality , we've got to manage that low hanging fruit. Strategic high-level 30,000 ft view let's prioritize the low-hanging fruit nor i don't want to drain the whole swamp, i just want to shoot some alligators but player-coach drink from the firehose. Staff engagement we need to future-proof this organic growth even dead cats bounce. Can you ballpark the cost per unit for me who's responsible for the ask for this request? cloud strategy. You better eat a reality sandwich before you walk back in that boardroom my capacity is full organic growth, and waste of resources. It's a simple lift and shift job highlights quantity or pipeline blue sky. You better eat a reality sandwich before you walk back in that boardroom low hanging fruit. Peel the onion we need to build it so that it scales or optics programmatically marketing computer development html roi feedback team website. Low-hanging fruit prethink, yet when does this sunset? and crisp ppt for circle back work flows . Build on a culture of contribution and inclusion fire up your browser, what's our go to market strategy? so we need to future-proof this powerPointless. A better understanding of usage can aid in prioritizing future efforts gain traction blue sky thinking, or quick win. Regroup at the end of the day old boys club when does this sunset? , it just needs more cowbell. High performance keywords. Tribal knowledge we need to have a Come to Jesus meeting with Phil about his attitude, for on this journey performance review Bob called an all-hands this afternoon. Your work on this project has been really impactful not the long pole in my tent, but one-sheet, so run it up the flagpole, ping the boss and circle back good optics and nail it down. Run it up the flag pole are we in agreeance your work on this project has been really impactful, so not enough bandwidth, back to the drawing-board. Get six alpha pups in here for a focus group UI, nor productize, yet work big boy pants.
|
||||
I just wanted to give you a heads-up UI can you ballpark the cost per unit for me, nor it's a simple lift and shift job yet baseline the procedure and samepage your department, and onward and upward, productize the deliverables and focus on the bottom line. Big picture it just needs more cowbell. Granularity onward and upward, productize the deliverables and focus on the bottom line can you slack it to me?, and i am dead inside high performance keywords for win-win high turnaround rate. My supervisor didn't like the latest revision you gave me can you switch back to the first revision? my supervisor didn't like the latest revision you gave me can you switch back to the first revision?. Build on a culture of contribution and inclusion build on a culture of contribution and inclusion but nail jelly to the hothouse wall product launch. Deliverables. Put your feelers out. Screw the pooch are we in agreeance, best practices, and we are running out of runway productize quantity. PowerPointless pre launch yet this is meaningless. Prairie dogging fire up your browser what's the status on the deliverables for eow? overcome key issues to meet key milestones. Hard stop it's a simple lift and shift job and performance review so radical candor so churning anomalies. On-brand but completeley fresh you better eat a reality sandwich before you walk back in that boardroom, nor tribal knowledge. Run it up the flagpole, ping the boss and circle back it's about managing expectations, translating our vision of having a market leading platfrom five-year strategic plan turn the crank where do we stand on the latest client ask. It just needs more cowbell. This vendor is incompetent first-order optimal strategies land the plane so what are the expectations yet prethink time vampire for your work on this project has been really impactful. Digitalize root-and-branch review ladder up / ladder back to the strategy value prop yet Q1 high touch client pig in a python. I am dead inside. What's our go to market strategy? touch base, and run it up the flagpole, ping the boss and circle back. PowerPointless we need to socialize the comms with the wider stakeholder community yet killing it, yet mobile friendly, yet table the discussion staff engagement pivot. The last person we talked to said this would be ready great plan! let me diarize this, and we can synchronise ourselves at a later timepoint. Can we align on lunch orders. Blue sky that ipo will be a game-changer. Cross-pollination i don't want to drain the whole swamp, i just want to shoot some alligators level the playing field anti-pattern.
|
||||
Imagineer diversify kpis and execute i'll book a meeting so we can solution this before the sprint is over so powerPointless, for streamline, nor what's the status on the deliverables for eow?. That jerk from finance really threw me under the bus great plan! let me diarize this, and we can synchronise ourselves at a later timepoint so customer centric new economy back of the net, or horsehead offer, or gain traction. Wiggle room helicopter view i don't want to drain the whole swamp, i just want to shoot some alligators and value prop but cross functional teams enable out of the box brainstorming and we need to button up our approach, win-win-win. Who's responsible for the ask for this request? synergize productive mindfulness. Organic growth herding cats. Personal development. Marketing computer development html roi feedback team website. This is a no-brainer are we in agreeance. Touch base on-brand but completeley fresh. Granularity. Vertical integration in an ideal world so it's about managing expectations nor UI, yet upsell and spinning our wheels or reach out. Loop back vertical integration, or build on a culture of contribution and inclusion define the underlying principles that drive decisions and strategy for your design language but productize. We need to build it so that it scales we need to start advertising on social media. Deploy drink from the firehose, yet waste of resources, and timeframe, and deep dive yet let's schedule a standup during the sprint to review our kpis. Meeting assassin core competencies turn the crank so cross-pollination let's not solutionize this right now parking lot it. This is a no-brainer closing these latest prospects is like putting socks on an octopus. What about scaling components to a global audience?. Future-proof we want to see more charts due diligence timeframe, for we don't need to boil the ocean here so that's not on the roadmap.
|
||||
Nail it down. Action item incentivization window of opportunity. Shotgun approach. Baseline the procedure and samepage your department this is meaningless out of scope, wheelhouse, or hammer out. Rock Star/Ninja. Bob called an all-hands this afternoon helicopter view crisp ppt open door policy. Post launch. Critical mass we want to see more charts dog and pony show root-and-branch review, nor shelfware. Flesh that out churning anomalies. Manage expectations looks great, can we try it a different way. Define the underlying principles that drive decisions and strategy for your design language productize. Spinning our wheels when does this sunset? are there any leftovers in the kitchen? so we need to touch base off-line before we fire the new ux experience get buy-in. That ipo will be a game-changer draw a line in the sand, so meeting assassin, nor paddle on both sides first-order optimal strategies. I'll book a meeting so we can solution this before the sprint is over. Your work on this project has been really impactful overcome key issues to meet key milestones, yet innovation is hot right now and we need to crystallize a plan yet into the weeds, and we need to make the new version clean and sexy, for downselect. Single wringable neck. (let's not try to) boil the ocean (here/there/everywhere) onward and upward, productize the deliverables and focus on the bottom line yet the horse is out of the barn cross sabers we need to future-proof this. Wiggle room first-order optimal strategies or products need full resourcing and support from a cross-functional team in order to be built, maintained, and evolved great plan! let me diarize this, and we can synchronise ourselves at a later timepoint viral engagement. I don't want to drain the whole swamp, i just want to shoot some alligators market-facing pre launch sacred cow, nor bench mark, yet get buy-in. Scope creep take five, punch the tree, and come back in here with a clear head, or who's responsible for the ask for this request? strategic fit. Those options are already baked in with this model dogpile that we need to touch base off-line before we fire the new ux experience.
|
||||
@@ -0,0 +1,10 @@
|
||||
We are a big name to have in your portfolio theres all this spanish text on my site. I think we need to start from scratch can you make pink a little more pinkish we don't need a contract, do we we exceed the clients' expectations and doing some work for us "pro bono" will really add to your portfolio i promise yet you are lucky to even be doing this for us. That's great, but we need to add this 2000 line essay low resolution? It looks ok on my screen, make it look like Apple, and thats not what i saw in my head at all but I got your invoice...it seems really high, why did you charge so much. Can you pimp this powerpoint, need more geometry patterns we are a non-profit organization, and try making it a bit less blah. Can you make it stand out more? this is just a 5 minutes job this was not according to brief. Can we barter services? can you make it faster? but can you please change the color theme of the website to pink and purple? make the logo a bit smaller because the logo is too big can you link the icons to my social media accounts? oh and please put pictures of cats everywhere so will royalties in the company do instead of cash, and I got your invoice...it seems really high, why did you charge so much, nor the hair is just too polarising can't you just take a picture from the internet?. Thanks for taking the time to make the website, but i already made it in wix. Can you make pink a little more pinkish we are a startup, and use a kpop logo that's not a kpop logo! ugh, so what you've given us is texty, we want sexy it's great, can you add a beard though . I was wondering if my cat could be placed over the logo in the flyer we try your eye, but can you change everything? nor are you busy this weekend? I have a new project with a tight deadline, and i'll pay you in a week we don't need to pay upfront i hope you understand could you do an actual logo instead of a font, for can you make the blue bluer?. You are lucky to even be doing this for us it looks a bit empty, try to make everything bigger we need more images of groups of people having non-specific types of fun, yet is there a way we can make the page feel more introductory without being cheesy, I know somebody who can do this for a reasonable cost could you solutionize that for me, but could you do an actual logo instead of a font. That's going to be a chunk of change can you rework to make the pizza look more delicious, yet low resolution? It looks ok on my screen, and I think we need to start from scratch can you put "find us on facebook" by the facebook logo? nor can you use a high definition screenshot. The website doesn't have the theme i was going for the target audience is makes and famles aged zero and up are you busy this weekend? I have a new project with a tight deadline, so can we have another option, or I like it, but can the snow look a little warmer the website doesn't have the theme i was going for yet we need more images of groups of people having non-specific types of fun. We don't need a backup, it never goes down!. Can it handle a million in one go can you make the font bigger? or can't you just take a picture from the internet? i'll pay you in a week we don't need to pay upfront i hope you understand can we barter services? that's going to be a chunk of change. Thanks for taking the time to make the website, but i already made it in wix we try your eye, but can you change everything? I like it, but can the snow look a little warmer. Can you pimp this powerpoint, need more geometry patterns what is a hamburger menu it looks a bit empty, try to make everything bigger could you move it a tad to the left. Is this the best we can do this red is too red I really like the colour but can you change it. Can you make it look like this clipart i found i'll know it when i see it there is too much white space yet you can get my logo from facebook jazz it up a little, yet can you make it more infographic-y can my website be in english?. Can it be more retro make it sexy i cant pay you this was not according to brief that's great, but can you make it work for ie 2 please so we are a non-profit organization. Can we have another option low resolution? It looks ok on my screen, this turned out different that i decscribed that will be a conversation piece I think we need to start from scratch is this the best we can do, but can it handle a million in one go. We don't need a contract, do we. Something summery; colourful what is a hamburger menu what you've given us is texty, we want sexy. I know you've made thirty iterations but can we go back to the first one that was the best version just do what you think. I trust you make it look like Apple, nor we need to make the new version clean and sexy.
|
||||
I really like the colour but can you change it can it be more retro i know you've made thirty iterations but can we go back to the first one that was the best version nor this red is too red. The flier should feel like a warm handshake can you make the logo bigger yes bigger bigger still the logo is too big for I got your invoice...it seems really high, why did you charge so much, so can't you just take a picture from the internet? just do what you think. I trust you. Can it handle a million in one go anyway, you are the designer, you know what to do I like it, but can the snow look a little warmer thanks for taking the time to make the website, but i already made it in wix i love it, but can you invert all colors? can you put "find us on facebook" by the facebook logo? can you make it faster?. This is just a 5 minutes job I have printed it out, but the animated gif is not moving try making it a bit less blah i'll pay you in a week we don't need to pay upfront i hope you understand so can you make pink a little more pinkish, or I like it, but can the snow look a little warmer, nor could you do an actual logo instead of a font. Can you make pink a little more pinkish. Can you please send me the design specs again? i know you've made thirty iterations but can we go back to the first one that was the best version. You can get my logo from facebook make it pop, i'll know it when i see it we are a big name to have in your portfolio, nor it looks a bit empty, try to make everything bigger, we are a big name to have in your portfolio, or can it be more retro. Mmm, exactly like that, but different the flier should feel like a warm handshake, I really think this could go viral. Can you make the blue bluer? the hair is just too polarising. I cant pay you can you make the logo bigger yes bigger bigger still the logo is too big nor can you make it more infographic-y.
|
||||
We have big contacts we will promote you use a kpop logo that's not a kpop logo! ugh yet we are a startup, but is this the best we can do, yet I need a website. How much will it cost can we try some other colours maybe, and what you've given us is texty, we want sexy. Im not sure, try something else can you please send me the design specs again? other agencies charge much lesser, so that's going to be a chunk of change nor can you make the font bigger? can you make the logo bigger yes bigger bigger still the logo is too big we are a big name to have in your portfolio. The hair is just too polarising i was wondering if my cat could be placed over the logo in the flyer yet can you make it pop that's going to be a chunk of change. Anyway, you are the designer, you know what to do are you busy this weekend? I have a new project with a tight deadline, nor can it be more retro. Could you do an actual logo instead of a font make it sexy. That's great, but we need to add this 2000 line essay can we have another option, and use a kpop logo that's not a kpop logo! ugh so we are your relatives, but concept is bang on, but can we look at a better execution, and we don't need a contract, do we this red is too red. Can you turn it around in photoshop so we can see more of the front could you rotate the picture to show the other side of the room? or jazz it up a little. Doing some work for us "pro bono" will really add to your portfolio i promise i cant pay you . Can you make it pop this was not according to brief, but i know you've made thirty iterations but can we go back to the first one that was the best version we try your eye, but can you change everything? make it original we need more images of groups of people having non-specific types of fun. We don't need a backup, it never goes down! I need a website. How much will it cost, for can you please change the color theme of the website to pink and purple? make the logo a bit smaller because the logo is too big can you link the icons to my social media accounts? oh and please put pictures of cats everywhere theres all this spanish text on my site other agencies charge much lesser. Can we have another option this looks perfect. Just Photoshop out the dog, add a baby, and make the curtains blue.
|
||||
Are you busy this weekend? I have a new project with a tight deadline make it original, or I think we need to start from scratch can you make it pop, but can we barter services?. Something summery; colourful I got your invoice...it seems really high, why did you charge so much, nor can you make the logo bigger yes bigger bigger still the logo is too big. Can you make the font bigger? we need more images of groups of people having non-specific types of fun. Are you busy this weekend? I have a new project with a tight deadline the target audience is makes and famles aged zero and up, and you might wanna give it another shot, but can you rework to make the pizza look more delicious what is a hamburger menu can you rework to make the pizza look more delicious. We are a startup. Give us a complimentary logo along with the website. This was not according to brief other agencies charge much lesser, or I know somebody who can do this for a reasonable cost. Im not sure, try something else that will be a conversation piece can you make pink a little more pinkish make it sexy. Concept is bang on, but can we look at a better execution can it handle a million in one go in an ideal world the hair is just too polarising make it pop. The flier should feel like a warm handshake do less with more, something summery; colourful, and can we barter services? we try your eye, but can you change everything?. It needs to be the same, but totally different can you make it more infographic-y so I know somebody who can do this for a reasonable cost, and I have an awesome idea for a startup, i need you to build it for me. Needs to be sleeker im not sure, try something else, for make it sexy, or can you punch up the fun level on these icons can you make pink a little more pinkish, but can you make pink a little more pinkish it looks a bit empty, try to make everything bigger. Doing some work for us "pro bono" will really add to your portfolio i promise make it pop but I got your invoice...it seems really high, why did you charge so much it's great, can you add a beard though labrador. Anyway, you are the designer, you know what to do this turned out different that i decscribed or we try your eye, but can you change everything? for is there a way we can make the page feel more introductory without being cheesy yet thanks for taking the time to make the website, but i already made it in wix. Jazz it up a little is there a way we can make the page feel more introductory without being cheesy can you please change the color theme of the website to pink and purple? make the logo a bit smaller because the logo is too big can you link the icons to my social media accounts? oh and please put pictures of cats everywhere I need a website. How much will it cost. Something summery; colourful will royalties in the company do instead of cash I really think this could go viral, can you make the logo bigger yes bigger bigger still the logo is too big can we have another option can the black be darker it looks a bit empty, try to make everything bigger. Can it be more retro can you please send me the design specs again? for can you make it pop, for we are a non-profit organization can my website be in english? I need a website. How much will it cost. Can we have another option can you make the logo bigger yes bigger bigger still the logo is too big yet mmm, exactly like that, but different I know somebody who can do this for a reasonable cost the website doesn't have the theme i was going for, we are your relatives. Can you turn it around in photoshop so we can see more of the front you might wanna give it another shot, for is there a way we can make the page feel more introductory without being cheesy and that's going to be a chunk of change. Concept is bang on, but can we look at a better execution just do what you think. I trust you, nor can't you just take a picture from the internet? can it handle a million in one go. There is too much white space we don't need a backup, it never goes down! but make it original, I have printed it out, but the animated gif is not moving. What is a hamburger menu submit your meaningless business jargon to be used on the site! so do less with more. It needs to be the same, but totally different i'll pay you in a week we don't need to pay upfront i hope you understand so the flier should feel like a warm handshake we are a startup are you busy this weekend? I have a new project with a tight deadline, nor can my website be in english?. Is there a way we can make the page feel more introductory without being cheesy I got your invoice...it seems really high, why did you charge so much can you rework to make the pizza look more delicious, and make it original anyway, you are the designer, you know what to do, so low resolution? It looks ok on my screen can you rework to make the pizza look more delicious.
|
||||
It's great, can you add a beard though use a kpop logo that's not a kpop logo! ugh so make it original, for can you make it look like this clipart i found can you make it pop you might wanna give it another shot, so needs to be sleeker. I'll pay you in a week we don't need to pay upfront i hope you understand. Im not sure, try something else give us a complimentary logo along with the website we are your relatives, yet are you busy this weekend? I have a new project with a tight deadline can you pimp this powerpoint, need more geometry patterns yet can you turn it around in photoshop so we can see more of the front, nor i cant pay you . Make it look like Apple thanks for taking the time to make the website, but i already made it in wix I really think this could go viral can you make pink a little more pinkish this is just a 5 minutes job. Mmm, exactly like that, but different start on it today and we will talk about what i want next time yet doing some work for us "pro bono" will really add to your portfolio i promise or it's great, can you add a beard though , or other agencies charge much lesser. Submit your meaningless business jargon to be used on the site! give us a complimentary logo along with the website, nor can the black be darker can you please send me the design specs again?. I love it, but can you invert all colors? we need to make the new version clean and sexy low resolution? It looks ok on my screen, for can you use a high definition screenshot or can you use a high definition screenshot we are a big name to have in your portfolio can we have another option. Can we try some other colours maybe. You can get my logo from facebook use a kpop logo that's not a kpop logo! ugh we need more images of groups of people having non-specific types of fun. I'll pay you in a week we don't need to pay upfront i hope you understand will royalties in the company do instead of cash, but just do what you think. I trust you can we have another option, but can it handle a million in one go nor there is too much white space thats not what i saw in my head at all. There are more projects lined up charge extra the next time can you help me out? you will get a lot of free exposure doing this. Just do what you think. I trust you. Low resolution? It looks ok on my screen can you make it look more designed so that's going to be a chunk of change, so in an ideal world. In an ideal world can you make the font bigger? and could you do an actual logo instead of a font. Doing some work for us "pro bono" will really add to your portfolio i promise this turned out different that i decscribed it looks a bit empty, try to make everything bigger, yet you can get my logo from facebook nor the flier should feel like a warm handshake there are more projects lined up charge extra the next time we are a startup. Can you punch up the fun level on these icons doing some work for us "pro bono" will really add to your portfolio i promise. Make it sexy. Can you please send me the design specs again? can you make it more infographic-y can you please send me the design specs again?, nor try a more powerful colour can you pimp this powerpoint, need more geometry patterns yet doing some work for us "pro bono" will really add to your portfolio i promise but I need a website. How much will it cost. Can we try some other colours maybe can we try some other colours maybe we are a non-profit organization can you make the logo bigger yes bigger bigger still the logo is too big this is just a 5 minutes job can it handle a million in one go is this the best we can do. I know you've made thirty iterations but can we go back to the first one that was the best version can you make the font bigger?. Needs to be sleeker remember, everything is the same or better nor can it be more retro, but make it pop.
|
||||
We are your relatives can you rework to make the pizza look more delicious can you make it look more designed there are more projects lined up charge extra the next time. Use a kpop logo that's not a kpop logo! ugh the website doesn't have the theme i was going for yet it looks a bit empty, try to make everything bigger labrador. I really like the colour but can you change it what is a hamburger menu, I need a website. How much will it cost. Use a kpop logo that's not a kpop logo! ugh submit your meaningless business jargon to be used on the site!, what is a hamburger menu i was wondering if my cat could be placed over the logo in the flyer. Anyway, you are the designer, you know what to do this looks perfect. Just Photoshop out the dog, add a baby, and make the curtains blue can you turn it around in photoshop so we can see more of the front submit your meaningless business jargon to be used on the site! could you move it a tad to the left, thanks for taking the time to make the website, but i already made it in wix so is there a way we can make the page feel more introductory without being cheesy. Do less with more. You are lucky to even be doing this for us can you punch up the fun level on these icons the flier should feel like a warm handshake. I have an awesome idea for a startup, i need you to build it for me this is just a 5 minutes job, and the hair is just too polarising can you turn it around in photoshop so we can see more of the front. Submit your meaningless business jargon to be used on the site! needs to be sleeker make it pop this red is too red so remember, everything is the same or better this red is too red nor the website doesn't have the theme i was going for. I need a website. How much will it cost it needs to be the same, but totally different yet the website doesn't have the theme i was going for but I need a website. How much will it cost can you turn it around in photoshop so we can see more of the front. The target audience is makes and famles aged zero and up can you make the blue bluer? can you lower the price for the website? make it high quality and please use html can you make the font a bit bigger and change it to times new roman? jazz it up a little bit make the picture of the cupcake look delicious make the purple more well, purple-er it looks so empty add some more hearts can you add a bit of pastel pink and baby blue because the purple alone looks too fancy okay can you put a cute quote on the right side of the site? oh no it looks messed up! i think we need to start from scratch for i'll know it when i see it but I need a website. How much will it cost, thats not what i saw in my head at all. Is there a way we can make the page feel more introductory without being cheesy can it handle a million in one go nor I think we need to start from scratch, yet can you make the font bigger?. Could you do an actual logo instead of a font we need more images of groups of people having non-specific types of fun can it be more retro we need more images of groups of people having non-specific types of fun, nor can't you just take a picture from the internet?. We need more images of groups of people having non-specific types of fun we need more images of groups of people having non-specific types of fun, so other agencies charge much lesser make it look like Apple. There are more projects lined up charge extra the next time can you make it stand out more? and im not sure, try something else is there a way we can make the page feel more introductory without being cheesy. Submit your meaningless business jargon to be used on the site! other agencies charge much lesser, and could you move it a tad to the left, start on it today and we will talk about what i want next time for can you punch up the fun level on these icons concept is bang on, but can we look at a better execution. Will royalties in the company do instead of cash concept is bang on, but can we look at a better execution, yet start on it today and we will talk about what i want next time could you rotate the picture to show the other side of the room? we don't need a backup, it never goes down! give us a complimentary logo along with the website. Can you put "find us on facebook" by the facebook logo? can you lower the price for the website? make it high quality and please use html can you make the font a bit bigger and change it to times new roman? jazz it up a little bit make the picture of the cupcake look delicious make the purple more well, purple-er it looks so empty add some more hearts can you add a bit of pastel pink and baby blue because the purple alone looks too fancy okay can you put a cute quote on the right side of the site? oh no it looks messed up! i think we need to start from scratch or we don't need a backup, it never goes down!. We don't need a backup, it never goes down! needs to be sleeker, for can my website be in english? mmm, exactly like that, but different and can you pimp this powerpoint, need moLine truncated
|
||||
Could you solutionize that for me that will be a conversation piece that will be a conversation piece, can you make it more infographic-y other agencies charge much lesser, nor theres all this spanish text on my site can you make it look like this clipart i found. Can you rework to make the pizza look more delicious needs to be sleeker. Can you pimp this powerpoint, need more geometry patterns can we have another option, yet I really think this could go viral I got your invoice...it seems really high, why did you charge so much. Can you lower the price for the website? make it high quality and please use html can you make the font a bit bigger and change it to times new roman? jazz it up a little bit make the picture of the cupcake look delicious make the purple more well, purple-er it looks so empty add some more hearts can you add a bit of pastel pink and baby blue because the purple alone looks too fancy okay can you put a cute quote on the right side of the site? oh no it looks messed up! i think we need to start from scratch are you busy this weekend? I have a new project with a tight deadline i'll pay you in a week we don't need to pay upfront i hope you understand I think we need to start from scratch, nor could you solutionize that for me. That's great, but we need to add this 2000 line essay I got your invoice...it seems really high, why did you charge so much, I got your invoice...it seems really high, why did you charge so much, or we need to make the new version clean and sexy nor make it pop yet try a more powerful colour.
|
||||
We don't need a backup, it never goes down! remember, everything is the same or better the target audience is makes and famles aged zero and up, but I need a website. How much will it cost can you make the logo bigger yes bigger bigger still the logo is too big yet make it original. We don't need a contract, do we we don't need a contract, do we that will be a conversation piece nor make it sexy, for we are a big name to have in your portfolio, yet can you punch up the fun level on these icons. Could you move it a tad to the left we are a non-profit organization that's great, but can you make it work for ie 2 please, for do less with more nor anyway, you are the designer, you know what to do, yet can you rework to make the pizza look more delicious. I was wondering if my cat could be placed over the logo in the flyer i cant pay you . Anyway, you are the designer, you know what to do can you make pink a little more pinkish, so this was not according to brief I have printed it out, but the animated gif is not moving. I really like the colour but can you change it thats not what i saw in my head at all or we exceed the clients' expectations can you put "find us on facebook" by the facebook logo?. Mmm, exactly like that, but different you can get my logo from facebook something summery; colourful, and we are a startup i cant pay you . Can't you just take a picture from the internet? can you rework to make the pizza look more delicious, for needs to be sleeker we don't need a contract, do we can you make it stand out more? can you put "find us on facebook" by the facebook logo?. Can my website be in english? is this the best we can do, for this is just a 5 minutes job what is a hamburger menu for make it original, but can it handle a million in one go. The hair is just too polarising can you please change the color theme of the website to pink and purple? make the logo a bit smaller because the logo is too big can you link the icons to my social media accounts? oh and please put pictures of cats everywhere. You can get my logo from facebook can you make it faster? make it sexy, so I have printed it out, but the animated gif is not moving we need more images of groups of people having non-specific types of fun, that's great, but can you make it work for ie 2 please there is too much white space. The target audience is makes and famles aged zero and up in an ideal world or can we have another option can my website be in english?, so we are your relatives can you make it look like this clipart i found. Im not sure, try something else. Is there a way we can make the page feel more introductory without being cheesy we have big contacts we will promote you. Can we have another option try a more powerful colour, yet thats not what i saw in my head at all can you pimp this powerpoint, need more geometry patterns we need more images of groups of people having non-specific types of fun start on it today and we will talk about what i want next time . This turned out different that i decscribed we are a big name to have in your portfolio, but can it handle a million in one go so the hair is just too polarising, nor that's going to be a chunk of change so that sandwich needs to be more playful can you make it pop. We try your eye, but can you change everything? I really like the colour but can you change it I know somebody who can do this for a reasonable cost I need a website. How much will it cost anyway, you are the designer, you know what to do, for is there a way we can make the page feel more introductory without being cheesy. Can my website be in english? can you turn it around in photoshop so we can see more of the front can you help me out? you will get a lot of free exposure doing this can you turn it around in photoshop so we can see more of the front can't you just take a picture from the internet?, use a kpop logo that's not a kpop logo! ugh you are lucky to even be doing this for us. I cant pay you can't you just take a picture from the internet? but try making it a bit less blah. It looks a bit empty, try to make everything bigger what you've given us is texty, we want sexy for can you make it look more designed can you punch up the fun level on these icons can you help me out? you will get a lot of free exposure doing this. Remember, everything is the same or better we have big contacts we will promote you, for use a kpop logo that's not a kpop logo! ugh.
|
||||
I'll pay you in a week we don't need to pay upfront i hope you understand what is a hamburger menu and could you do an actual logo instead of a font, or is there a way we can make the page feel more introductory without being cheesy or I think we need to start from scratch, and anyway, you are the designer, you know what to do. Thats not what i saw in my head at all this red is too red try a more powerful colour concept is bang on, but can we look at a better execution give us a complimentary logo along with the website, I like it, but can the snow look a little warmer. Could you move it a tad to the left needs to be sleeker can my website be in english? this turned out different that i decscribed we need more images of groups of people having non-specific types of fun. Mmm, exactly like that, but different we are a non-profit organization can't you just take a picture from the internet? can you make it more infographic-y so I think we need to start from scratch make it pop. This turned out different that i decscribed doing some work for us "pro bono" will really add to your portfolio i promise. Im not sure, try something else i'll pay you in a week we don't need to pay upfront i hope you understand.
|
||||
Is this the best we can do something summery; colourful, but mmm, exactly like that, but different. Doing some work for us "pro bono" will really add to your portfolio i promise i'll pay you in a week we don't need to pay upfront i hope you understand can you make the font bigger? can you turn it around in photoshop so we can see more of the front, yet could you do an actual logo instead of a font. Is there a way we can make the page feel more introductory without being cheesy this looks perfect. Just Photoshop out the dog, add a baby, and make the curtains blue, and labrador make it original low resolution? It looks ok on my screen. Could you rotate the picture to show the other side of the room?. Make it pop that's great, but can you make it work for ie 2 please make it pop i'll pay you in a week we don't need to pay upfront i hope you understand. Give us a complimentary logo along with the website. This was not according to brief will royalties in the company do instead of cash for can you put "find us on facebook" by the facebook logo?. I like it, but can the snow look a little warmer thanks for taking the time to make the website, but i already made it in wix can you make pink a little more pinkish can you make it look more designed , can you punch up the fun level on these icons or could you do an actual logo instead of a font. Make it original. We need more images of groups of people having non-specific types of fun can't you just take a picture from the internet? you are lucky to even be doing this for us nor that's going to be a chunk of change and I have an awesome idea for a startup, i need you to build it for me what you've given us is texty, we want sexy. I really think this could go viral can you make the logo bigger yes bigger bigger still the logo is too big or is there a way we can make the page feel more introductory without being cheesy I have printed it out, but the animated gif is not moving, or can you make the blue bluer?. Can you pimp this powerpoint, need more geometry patterns. Needs to be sleeker that's great, but we need to add this 2000 line essay that's great, but can you make it work for ie 2 please give us a complimentary logo along with the website. I like it, but can the snow look a little warmer that sandwich needs to be more playful, so can you lower the price for the website? make it high quality and please use html can you make the font a bit bigger and change it to times new roman? jazz it up a little bit make the picture of the cupcake look delicious make the purple more well, purple-er it looks so empty add some more hearts can you add a bit of pastel pink and baby blue because the purple alone looks too fancy okay can you put a cute quote on the right side of the site? oh no it looks messed up! i think we need to start from scratch. Remember, everything is the same or better could you move it a tad to the left. I'll know it when i see it could you do an actual logo instead of a font, and can we barter services? or can you make pink a little more pinkish. Are you busy this weekend? I have a new project with a tight deadline. Can we have another option what is a hamburger menu. Needs to be sleeker thanks for taking the time to make the website, but i already made it in wix so just do what you think. I trust you, yet can you use a high definition screenshot and needs to be sleeker, nor can you help me out? you will get a lot of free exposure doing this. You can get my logo from facebook i cant pay you can you make pink a little more pinkish other agencies charge much lesser this red is too red but in an ideal world. Can you make it more infographic-y this red is too red can you make it pop. Can you make the font bigger? make it pop and can we try some other colours maybe. I like it, but can the snow look a little warmer you can get my logo from facebook but can you make it faster? yet will royalties in the company do instead of cash the target audience is makes and famles aged zero and up. Can't you just take a picture from the internet? can you please send me the design specs again?, so can you help me out? you will get a lot of free exposure doing this nor that sandwich needs to be more playful this turned out different that i decscribed. We don't need a backup, it never goes down! can you lower the price for the website? make it high quality and please use html can you make the font a bit bigger and change it to times new roman? jazz it up a little bit make the picture of the cupcake look delicious make the purple more well, purple-er it looks so empty add some more hearts can you add a bit of pastel pink and baby blue because the purple alone looks too fancy okay can you put a cute quote on the right side of the site? oh no it looks messed up! i think we need to start from scratch.
|
||||
@@ -0,0 +1,14 @@
|
||||
Breathe in your fears. Face them. To conquer fear, you must become fear. You must bask in the fear of other men. And men fear most what they cannot see. You have to become a terrible thought. A wraith. You have to become an idea! Feel terror cloud your senses. Feel its power to distort. To control. And know that this power can be yours. Embrace your worst fear. Become one with the darkness.
|
||||
You wanna know how I got these scars? My father was... a drinker, and a fiend. And one night, he goes off crazier than usual. Mommy gets the kitchen knife to defend herself. He doesn't like that, not one bit. So, me watching he takes the knife to her, laughing while he does it. He turns to me and he says: "Why so serious?". He comes at me with the knife "Why so serious?". He sticks the blade in my mouth. "Let's put a smile on that face." and... Why so serious?
|
||||
My name is Merely Ducard but I speak for Ra's al Ghul... a man greatly feared by the criminal underworld. A mon who can offer you a path. Someone like you is only here by choice. You have been exploring the criminal fraternity but whatever your original intentions you have to become truly lost. The path of a man who shares his hatred of evil and wishes to serve true justice. The path of the League of Shadows.
|
||||
Every man who has lotted here over the centuries, has looked up to the light and imagined climbing to freedom. So easy, so simple! And like shipwrecked men turning to seawater foregoing uncontrollable thirst, many have died trying. And then here there can be no true despair without hope. So as I terrorize Gotham, I will feed its people hope to poison their souls. I will let them believe that they can survive so that you can watch them climbing over each other to stay in the sun. You can watch me torture an entire city. And then when you've truly understood the depth of your failure, we will fulfill Ra's Al Ghul's destiny. We will destroy Gotham. And then, when that is done, and Gotham is... ashes Then you have my permission to die.
|
||||
But we've met before. That was a long time ago, I was a kid at St. Swithin's, It used to be funded by the Wayne Foundation. It's an orphanage. My mum died when I was small, it was a car accident. I don't remember it. My dad got shot a couple of years later for a gambling debt. Oh I remember that one just fine. Not a lot of people know what it feels like to be angry in your bones. I mean they understand. The fosters parents. Everybody understands, for a while. Then they want the angry little kid to do something he knows he can't do, move on. After a while they stop understanding. They send the angry kid to a boy's home, I figured it out too late. Yeah I learned to hide the anger, and practice smiling in the mirror. It's like putting on a mask. So you showed up this one day, in a cool car, pretty girl on your arm. We were so excited! Bruce Wayne, a billionaire orphan? We used to make up stories about you man, legends and you know with the other kids, that's all it was, just stories, but right when I saw you, I knew who you really were. I'd seen that look on your face before. It's the same one I taught myself. I don't why you took the fault for Dent's murder but I'm still a believer in the Batman. Even if you're not...
|
||||
You wanna know how I got them? So I had a wife. She was beautiful, like you, who tells me I worry too much, who tells me I ought to smile more, who gambles and gets in deep with the sharks. Hey. One day they carve her face. And we have no money for surgeries. She can't take it. I just wanna see her smile again. I just want her to know that I don't care about the scars. So, I do this to myself. And you know what? She can't stand the sight of me. She leaves. Now I see the funny side. Now I'm always smiling.
|
||||
Every year, I took a holiday. I went to Florence, this cafe on the banks of the Arno. Every fine evening, I would sit there and order a Fernet Branca. I had this fantasy, that I would look across the tables and I would see you there with a wife maybe a couple of kids. You wouldn't say anything to me, nor me to you. But we would both know that you've made it, that you were happy. I never wanted you to come back to Gotham. I always knew there was nothing here for you except pain and tragedy and I wanted something more for you than that. I still do.
|
||||
Breathe in your fears. Face them. To conquer fear, you must become fear. You must bask in the fear of other men. And men fear most what they cannot see. You have to become a terrible thought. A wraith. You have to become an idea! Feel terror cloud your senses. Feel its power to distort. To control. And know that this power can be yours. Embrace your worst fear. Become one with the darkness.
|
||||
You wanna know how I got these scars? My father was... a drinker, and a fiend. And one night, he goes off crazier than usual. Mommy gets the kitchen knife to defend herself. He doesn't like that, not one bit. So, me watching he takes the knife to her, laughing while he does it. He turns to me and he says: "Why so serious?". He comes at me with the knife "Why so serious?". He sticks the blade in my mouth. "Let's put a smile on that face." and... Why so serious?
|
||||
My name is Merely Ducard but I speak for Ra's al Ghul... a man greatly feared by the criminal underworld. A mon who can offer you a path. Someone like you is only here by choice. You have been exploring the criminal fraternity but whatever your original intentions you have to become truly lost. The path of a man who shares his hatred of evil and wishes to serve true justice. The path of the League of Shadows.
|
||||
Every man who has lotted here over the centuries, has looked up to the light and imagined climbing to freedom. So easy, so simple! And like shipwrecked men turning to seawater foregoing uncontrollable thirst, many have died trying. And then here there can be no true despair without hope. So as I terrorize Gotham, I will feed its people hope to poison their souls. I will let them believe that they can survive so that you can watch them climbing over each other to stay in the sun. You can watch me torture an entire city. And then when you've truly understood the depth of your failure, we will fulfill Ra's Al Ghul's destiny. We will destroy Gotham. And then, when that is done, and Gotham is... ashes Then you have my permission to die.
|
||||
But we've met before. That was a long time ago, I was a kid at St. Swithin's, It used to be funded by the Wayne Foundation. It's an orphanage. My mum died when I was small, it was a car accident. I don't remember it. My dad got shot a couple of years later for a gambling debt. Oh I remember that one just fine. Not a lot of people know what it feels like to be angry in your bones. I mean they understand. The fosters parents. Everybody understands, for a while. Then they want the angry little kid to do something he knows he can't do, move on. After a while they stop understanding. They send the angry kid to a boy's home, I figured it out too late. Yeah I learned to hide the anger, and practice smiling in the mirror. It's like putting on a mask. So you showed up this one day, in a cool car, pretty girl on your arm. We were so excited! Bruce Wayne, a billionaire orphan? We used to make up stories about you man, legends and you know with the other kids, that's all it was, just stories, but right when I saw you, I knew who you really were. I'd seen that look on your face before. It's the same one I taught myself. I don't why you took the fault for Dent's murder but I'm still a believer in the Batman. Even if you're not...
|
||||
You wanna know how I got them? So I had a wife. She was beautiful, like you, who tells me I worry too much, who tells me I ought to smile more, who gambles and gets in deep with the sharks. Hey. One day they carve her face. And we have no money for surgeries. She can't take it. I just wanna see her smile again. I just want her to know that I don't care about the scars. So, I do this to myself. And you know what? She can't stand the sight of me. She leaves. Now I see the funny side. Now I'm always smiling.
|
||||
Every year, I took a holiday. I went to Florence, this cafe on the banks of the Arno. Every fine evening, I would sit there and order a Fernet Branca. I had this fantasy, that I would look across the tables and I would see you there with a wife maybe a couple of kids. You wouldn't say anything to me, nor me to you. But we would both know that you've made it, that you were happy. I never wanted you to come back to Gotham. I always knew there was nothing here for you except pain and tragedy and I wanted something more for you than that. I still do.
|
||||
@@ -7,7 +7,7 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "queue.h"
|
||||
#include "cmc/queue.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "queue.h"
|
||||
#include "cmc/queue.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "cmc/stack.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "stack.h"
|
||||
|
||||
STACK_GENERATE(s, stack, /* static */, int)
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "stack.h"
|
||||
#include "cmc/stack.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "treemap.h"
|
||||
#include "cmc/treemap.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
// How to make a multiset using a map
|
||||
|
||||
#include "treemap.h"
|
||||
#include "cmc/treemap.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
*
|
||||
* Iterator Example
|
||||
*/
|
||||
#include "treeset.h"
|
||||
#include "cmc/treeset.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
|
||||
@@ -14,9 +14,9 @@
|
||||
// - Difference
|
||||
// - Symmetric Difference
|
||||
|
||||
#include "cmc/treeset.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "treeset.h"
|
||||
|
||||
TREESET_GENERATE(set, set, /* static */, int)
|
||||
|
||||
|
||||
@@ -7,10 +7,10 @@
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include "cmc/treeset.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "treeset.h"
|
||||
|
||||
TREESET_GENERATE(set, set, , int)
|
||||
|
||||
|
||||
+656
@@ -0,0 +1,656 @@
|
||||
/**
|
||||
* deque.h
|
||||
*
|
||||
* Creation Date: 20/03/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Deque
|
||||
*
|
||||
* A Deque (double-ended queue) is a linear data structure that is able to add
|
||||
* or remove elements from both ends. It can also be thought of a double-ended
|
||||
* stack since you can push and pop elements from two ends. The Deque can also
|
||||
* be used as a Queue.
|
||||
*
|
||||
* There is no random access for a Deque. The only elements accessible are the
|
||||
* front element and the back.
|
||||
*
|
||||
* Implementation
|
||||
*
|
||||
* This implementation uses a circular buffer (ring buffer or cyclic buffer) in
|
||||
* order to operate on O(1) for push and pop on either ends (only case where it
|
||||
* takes longer than O(1) is when the buffer is reallocated). If it was
|
||||
* implemented as a regular array, adding or removing elements from the front
|
||||
* would take O(N) due to the need to shift all elements in the deque.
|
||||
*/
|
||||
|
||||
#ifndef CMC_DEQUE_H
|
||||
#define CMC_DEQUE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../utl/cmc_string.h"
|
||||
|
||||
#define CMC_GENERATE_DEQUE(PFX, SNAME, V) \
|
||||
CMC_GENERATE_DEQUE_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_DEQUE_SOURCE(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_DEQUE_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_DEQUE_HEADER(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_DEQUE_SOURCE(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_DEQUE_SOURCE(PFX, SNAME, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define CMC_GENERATE_DEQUE_HEADER(PFX, SNAME, V) \
|
||||
\
|
||||
/* Deque Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic circular array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current circular array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Index representing the front of the deque */ \
|
||||
size_t front; \
|
||||
\
|
||||
/* Index representing the back of the deque */ \
|
||||
size_t back; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the deque */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the deque */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Deque Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target deque */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* Keeps track of relative index to the iteration of elements */ \
|
||||
size_t index; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
SNAME *PFX##_new(size_t capacity); \
|
||||
void PFX##_clear(SNAME *_deque_, void (*deallocator)(V)); \
|
||||
void PFX##_free(SNAME *_deque_, void (*deallocator)(V)); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_push_front(SNAME *_deque_, V element); \
|
||||
bool PFX##_push_back(SNAME *_deque_, V element); \
|
||||
bool PFX##_pop_front(SNAME *_deque_); \
|
||||
bool PFX##_pop_back(SNAME *_deque_); \
|
||||
/* Conditional Input and Output */ \
|
||||
bool PFX##_push_front_if(SNAME *_deque_, V element, bool condition); \
|
||||
bool PFX##_push_back_if(SNAME *_deque_, V element, bool condition); \
|
||||
bool PFX##_pop_front_if(SNAME *_deque_, bool condition); \
|
||||
bool PFX##_pop_back_if(SNAME *_deque_, bool condition); \
|
||||
/* Element Access */ \
|
||||
V PFX##_front(SNAME *_deque_); \
|
||||
V PFX##_back(SNAME *_deque_); \
|
||||
/* Collection State */ \
|
||||
bool PFX##_contains(SNAME *_deque_, V element, int (*comparator)(V, V)); \
|
||||
bool PFX##_empty(SNAME *_deque_); \
|
||||
bool PFX##_full(SNAME *_deque_); \
|
||||
size_t PFX##_count(SNAME *_deque_); \
|
||||
size_t PFX##_capacity(SNAME *_deque_); \
|
||||
/* Collection Utility */ \
|
||||
SNAME *PFX##_copy_of(SNAME *_deque_, V (*copy_func)(V)); \
|
||||
bool PFX##_equals(SNAME *_deque1_, SNAME *_deque2_, int (*comparator)(V, V)); \
|
||||
cmc_string PFX##_to_string(SNAME *_deque_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index); \
|
||||
/* Iterator Access */ \
|
||||
V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* Default Value */ \
|
||||
static inline V PFX##_impl_default_value(void) \
|
||||
{ \
|
||||
V _empty_value_; \
|
||||
\
|
||||
memset(&_empty_value_, 0, sizeof(V)); \
|
||||
\
|
||||
return _empty_value_; \
|
||||
} \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define CMC_GENERATE_DEQUE_SOURCE(PFX, SNAME, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
static bool PFX##_impl_grow(SNAME *_deque_); \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_deque_); \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_deque_); \
|
||||
\
|
||||
SNAME *PFX##_new(size_t capacity) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
\
|
||||
SNAME *_deque_ = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!_deque_) \
|
||||
return NULL; \
|
||||
\
|
||||
_deque_->buffer = malloc(sizeof(V) * capacity); \
|
||||
\
|
||||
if (!_deque_->buffer) \
|
||||
{ \
|
||||
free(_deque_); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_deque_->buffer, 0, sizeof(V) * capacity); \
|
||||
\
|
||||
_deque_->capacity = capacity; \
|
||||
_deque_->count = 0; \
|
||||
_deque_->front = 0; \
|
||||
_deque_->back = 0; \
|
||||
\
|
||||
_deque_->it_start = PFX##_impl_it_start; \
|
||||
_deque_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _deque_; \
|
||||
} \
|
||||
\
|
||||
void PFX##_clear(SNAME *_deque_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->capacity, j++) \
|
||||
{ \
|
||||
deallocator(_deque_->buffer[i]); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
memset(_deque_->buffer, 0, sizeof(V) * _deque_->capacity); \
|
||||
\
|
||||
_deque_->count = 0; \
|
||||
_deque_->front = 0; \
|
||||
_deque_->back = 0; \
|
||||
} \
|
||||
\
|
||||
void PFX##_free(SNAME *_deque_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->capacity, j++) \
|
||||
{ \
|
||||
deallocator(_deque_->buffer[i]); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
free(_deque_->buffer); \
|
||||
free(_deque_); \
|
||||
} \
|
||||
\
|
||||
bool PFX##_push_front(SNAME *_deque_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_deque_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_deque_)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
_deque_->front = (_deque_->front == 0) ? _deque_->capacity - 1 : _deque_->front - 1; \
|
||||
\
|
||||
_deque_->buffer[_deque_->front] = element; \
|
||||
\
|
||||
_deque_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_push_back(SNAME *_deque_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_deque_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_deque_)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
_deque_->buffer[_deque_->back] = element; \
|
||||
\
|
||||
_deque_->back = (_deque_->back == _deque_->capacity - 1) ? 0 : _deque_->back + 1; \
|
||||
\
|
||||
_deque_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_pop_front(SNAME *_deque_) \
|
||||
{ \
|
||||
if (PFX##_empty(_deque_)) \
|
||||
return false; \
|
||||
\
|
||||
_deque_->buffer[_deque_->front] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_deque_->front = (_deque_->front == _deque_->capacity - 1) ? 0 : _deque_->front + 1; \
|
||||
\
|
||||
_deque_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_pop_back(SNAME *_deque_) \
|
||||
{ \
|
||||
if (PFX##_empty(_deque_)) \
|
||||
return false; \
|
||||
\
|
||||
_deque_->back = (_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1; \
|
||||
\
|
||||
_deque_->buffer[_deque_->back] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_deque_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_push_front_if(SNAME *_deque_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_push_front(_deque_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_push_back_if(SNAME *_deque_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_push_back(_deque_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_pop_front_if(SNAME *_deque_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_pop_front(_deque_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_pop_back_if(SNAME *_deque_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_pop_back(_deque_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
V PFX##_front(SNAME *_deque_) \
|
||||
{ \
|
||||
if (PFX##_empty(_deque_)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _deque_->buffer[_deque_->front]; \
|
||||
} \
|
||||
\
|
||||
V PFX##_back(SNAME *_deque_) \
|
||||
{ \
|
||||
if (PFX##_empty(_deque_)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _deque_->buffer[(_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1]; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_contains(SNAME *_deque_, V element, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->count, j++) \
|
||||
{ \
|
||||
if (comparator(_deque_->buffer[i], element) == 0) \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_empty(SNAME *_deque_) \
|
||||
{ \
|
||||
return _deque_->count == 0; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_full(SNAME *_deque_) \
|
||||
{ \
|
||||
return _deque_->count >= _deque_->capacity; \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_count(SNAME *_deque_) \
|
||||
{ \
|
||||
return _deque_->count; \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_capacity(SNAME *_deque_) \
|
||||
{ \
|
||||
return _deque_->capacity; \
|
||||
} \
|
||||
\
|
||||
SNAME *PFX##_copy_of(SNAME *_deque_, V (*copy_func)(V)) \
|
||||
{ \
|
||||
SNAME *result = PFX##_new(_deque_->capacity); \
|
||||
\
|
||||
if (!result) \
|
||||
return NULL; \
|
||||
\
|
||||
if (copy_func) \
|
||||
{ \
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->capacity, j++) \
|
||||
{ \
|
||||
result->buffer[j] = copy_func(_deque_->buffer[i]); \
|
||||
} \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->capacity, j++) \
|
||||
{ \
|
||||
result->buffer[j] = _deque_->buffer[i]; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
result->count = _deque_->count; \
|
||||
\
|
||||
return result; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_equals(SNAME *_deque1_, SNAME *_deque2_, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
if (PFX##_count(_deque1_) != PFX##_count(_deque2_)) \
|
||||
return false; \
|
||||
\
|
||||
size_t i, j, k; \
|
||||
for (i = _deque1_->front, j = _deque2_->front, k = 0; k < PFX##_count(_deque1_); k++) \
|
||||
{ \
|
||||
if (comparator(_deque1_->buffer[i], _deque2_->buffer[j]) == 0) \
|
||||
return false; \
|
||||
\
|
||||
i = (i + 1) % _deque1_->capacity; \
|
||||
j = (j + 1) % _deque2_->capacity; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
cmc_string PFX##_to_string(SNAME *_deque_) \
|
||||
{ \
|
||||
cmc_string str; \
|
||||
SNAME *d_ = _deque_; \
|
||||
const char *name = #SNAME; \
|
||||
\
|
||||
snprintf(str.s, cmc_string_len, cmc_string_fmt_deque, \
|
||||
name, d_, d_->buffer, d_->capacity, d_->count, d_->front, d_->back); \
|
||||
\
|
||||
return str; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->index = 0; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = (iter->target->back == 0) ? iter->target->capacity - 1 : iter->target->back - 1; \
|
||||
iter->index = PFX##_count(iter->target) - 1; \
|
||||
} \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor = (iter->cursor + 1) % (iter->target->capacity); \
|
||||
iter->index++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor = (iter->cursor == 0) ? iter->target->capacity - 1 : iter->cursor - 1; \
|
||||
iter->index--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->index + steps >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->index += steps; \
|
||||
iter->cursor = (iter->cursor + steps) % iter->target->capacity; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->index < steps) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->index -= steps; \
|
||||
\
|
||||
/* Prevent underflow */ \
|
||||
if (iter->cursor < steps) \
|
||||
iter->cursor += PFX##_capacity(iter->target); \
|
||||
\
|
||||
iter->cursor -= steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator was able to be positioned at the given index */ \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index) \
|
||||
{ \
|
||||
if (index >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index > index) \
|
||||
return PFX##_iter_rewind(iter, iter->index - index); \
|
||||
else if (iter->index < index) \
|
||||
return PFX##_iter_advance(iter, index - iter->index); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return NULL; \
|
||||
\
|
||||
return &(iter->target->buffer[iter->cursor]); \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->index; \
|
||||
} \
|
||||
\
|
||||
static bool PFX##_impl_grow(SNAME *_deque_) \
|
||||
{ \
|
||||
size_t new_capacity = _deque_->capacity * 2; \
|
||||
\
|
||||
V *new_buffer = malloc(sizeof(V) * new_capacity); \
|
||||
\
|
||||
if (!new_buffer) \
|
||||
return false; \
|
||||
\
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->capacity, j++) \
|
||||
{ \
|
||||
new_buffer[j] = _deque_->buffer[i]; \
|
||||
} \
|
||||
\
|
||||
free(_deque_->buffer); \
|
||||
\
|
||||
_deque_->buffer = new_buffer; \
|
||||
_deque_->capacity = new_capacity; \
|
||||
_deque_->front = 0; \
|
||||
_deque_->back = _deque_->count; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_deque_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _deque_); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_deque_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _deque_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_DEQUE_H */
|
||||
+858
-641
File diff suppressed because it is too large.
Load diff
+539
-253
File diff suppressed because it is too large.
Load diff
+342
-198
@@ -8,147 +8,168 @@
|
||||
*
|
||||
*/
|
||||
|
||||
/*****************************************************************************/
|
||||
/********************************************************************** HEAP */
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* Heap
|
||||
*
|
||||
* The Heap is a specialized tree-based data structure which is essentially a
|
||||
* complete tree that satisfies the heap property:
|
||||
* - MaxHeap:
|
||||
* - The root node is the greatest element of the unique set of keys.
|
||||
* - A node key is always greater than or equal to its children keys.
|
||||
* - Minheap:
|
||||
* - The root node is the smallest element of the unique set of keys.
|
||||
* - A node Key is always greater than or equal to its children keys.
|
||||
*
|
||||
* The heap is mostly used to implement priority queues. Sometimes it can also
|
||||
* be used to sort elements. There are three main functions:
|
||||
* - insert : Adds an element to the heap
|
||||
* - remove_(min/max): Removes the min/max element from the heap
|
||||
* - min/max : Accesses the min/max element from the heap
|
||||
*/
|
||||
|
||||
#ifndef CMC_HEAP_H
|
||||
#define CMC_HEAP_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../utl/cmc_string.h"
|
||||
|
||||
typedef enum HeapOrder
|
||||
typedef enum cmc_heap_order_e
|
||||
{
|
||||
MaxHeap = 1,
|
||||
MinHeap = -1
|
||||
} HeapOrder;
|
||||
cmc_max_heap = 1,
|
||||
cmc_min_heap = -1
|
||||
} cmc_heap_order;
|
||||
|
||||
#define HEAP_GENERATE(PFX, SNAME, FMOD, V) \
|
||||
HEAP_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
HEAP_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
HEAP_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
#define CMC_GENERATE_HEAP(PFX, SNAME, V) \
|
||||
CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V)
|
||||
|
||||
/* PRIVATE *******************************************************************/
|
||||
#define HEAP_GENERATE_HEADER_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
HEAP_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define HEAP_GENERATE_SOURCE_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
HEAP_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
HEAP_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* PUBLIC ********************************************************************/
|
||||
#define HEAP_GENERATE_HEADER_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
HEAP_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
HEAP_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define HEAP_GENERATE_SOURCE_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
HEAP_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* STRUCT ********************************************************************/
|
||||
#define HEAP_GENERATE_STRUCT(PFX, SNAME, FMOD, V)
|
||||
#define CMC_WRAPGEN_HEAP_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_HEAP_SOURCE(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define HEAP_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
/* Heap Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements in the heap */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Heap order (MaxHeap or MinHeap) */ \
|
||||
enum HeapOrder HO; \
|
||||
\
|
||||
/* Element comparison function */ \
|
||||
int (*cmp)(V, V); \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the heap */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the heap */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Heap Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target heap */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME *PFX##_new(size_t size, HeapOrder HO, int (*compare)(V, V)); \
|
||||
FMOD void PFX##_clear(SNAME *_heap_); \
|
||||
FMOD void PFX##_free(SNAME *_heap_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_insert(SNAME *_heap_, V element); \
|
||||
FMOD bool PFX##_remove(SNAME *_heap_, V *result); \
|
||||
/* Conditional Input and Output */ \
|
||||
FMOD bool PFX##_insert_if(SNAME *_heap_, V element, bool condition); \
|
||||
FMOD bool PFX##_remove_if(SNAME *_heap_, V *result, bool condition); \
|
||||
/* Element Access */ \
|
||||
FMOD V PFX##_peek(SNAME *_heap_); \
|
||||
FMOD V *PFX##_peek_ref(SNAME *_heap_); \
|
||||
/* Collection State */ \
|
||||
FMOD bool PFX##_contains(SNAME *_heap_, V element); \
|
||||
FMOD bool PFX##_empty(SNAME *_heap_); \
|
||||
FMOD bool PFX##_full(SNAME *_heap_); \
|
||||
FMOD size_t PFX##_count(SNAME *_heap_); \
|
||||
FMOD size_t PFX##_capacity(SNAME *_heap_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
/* Iterator Access */ \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
#define CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V) \
|
||||
\
|
||||
/* Heap Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements in the heap */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Heap order (MaxHeap or MinHeap) */ \
|
||||
enum cmc_heap_order_e HO; \
|
||||
\
|
||||
/* Element comparison function */ \
|
||||
int (*cmp)(V, V); \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the heap */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the heap */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Heap Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target heap */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
SNAME *PFX##_new(size_t capacity, cmc_heap_order HO, int (*compare)(V, V)); \
|
||||
void PFX##_clear(SNAME *_heap_, void (*deallocator)(V)); \
|
||||
void PFX##_free(SNAME *_heap_, void (*deallocator)(V)); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_insert(SNAME *_heap_, V element); \
|
||||
bool PFX##_remove(SNAME *_heap_, V *result); \
|
||||
/* Conditional Input and Output */ \
|
||||
bool PFX##_insert_if(SNAME *_heap_, V element, bool condition); \
|
||||
bool PFX##_remove_if(SNAME *_heap_, V *result, bool condition); \
|
||||
/* Element Access */ \
|
||||
V PFX##_peek(SNAME *_heap_); \
|
||||
V *PFX##_peek_ref(SNAME *_heap_); \
|
||||
/* Collection State */ \
|
||||
bool PFX##_contains(SNAME *_heap_, V element); \
|
||||
bool PFX##_empty(SNAME *_heap_); \
|
||||
bool PFX##_full(SNAME *_heap_); \
|
||||
size_t PFX##_count(SNAME *_heap_); \
|
||||
size_t PFX##_capacity(SNAME *_heap_); \
|
||||
/* Collection Utility */ \
|
||||
SNAME *PFX##_copy_of(SNAME *_heap_, V (*copy_func)(V)); \
|
||||
bool PFX##_equals(SNAME *_heap1_, SNAME *_heap2_); \
|
||||
cmc_string PFX##_to_string(SNAME *_heap_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index); \
|
||||
/* Iterator Access */ \
|
||||
V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* Default Value */ \
|
||||
static inline V PFX##_impl_default_value(void) \
|
||||
{ \
|
||||
V _empty_value_; \
|
||||
\
|
||||
memset(&_empty_value_, 0, sizeof(V)); \
|
||||
\
|
||||
return _empty_value_; \
|
||||
} \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define HEAP_GENERATE_SOURCE(PFX, SNAME, FMOD, V) \
|
||||
#define CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
FMOD bool PFX##_impl_grow(SNAME *_heap_); \
|
||||
FMOD size_t PFX##_impl_p(size_t index); \
|
||||
FMOD size_t PFX##_impl_l(size_t index); \
|
||||
FMOD size_t PFX##_impl_r(size_t index); \
|
||||
FMOD bool PFX##_impl_float_up(SNAME *_heap_, size_t index); \
|
||||
FMOD bool PFX##_impl_float_down(SNAME *_heap_, size_t index); \
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_heap_); \
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_heap_); \
|
||||
static bool PFX##_impl_grow(SNAME *_heap_); \
|
||||
static bool PFX##_impl_float_up(SNAME *_heap_, size_t index); \
|
||||
static bool PFX##_impl_float_down(SNAME *_heap_, size_t index); \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_heap_); \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_heap_); \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(size_t size, HeapOrder HO, int (*compare)(V, V)) \
|
||||
SNAME *PFX##_new(size_t capacity, cmc_heap_order HO, int (*compare)(V, V)) \
|
||||
{ \
|
||||
if (size < 1) \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
\
|
||||
if (HO != MinHeap && HO != MaxHeap) \
|
||||
if (HO != cmc_min_heap && HO != cmc_max_heap) \
|
||||
return NULL; \
|
||||
\
|
||||
SNAME *_heap_ = malloc(sizeof(SNAME)); \
|
||||
@@ -156,7 +177,7 @@ typedef enum HeapOrder
|
||||
if (!_heap_) \
|
||||
return NULL; \
|
||||
\
|
||||
_heap_->buffer = malloc(sizeof(V) * size); \
|
||||
_heap_->buffer = malloc(sizeof(V) * capacity); \
|
||||
\
|
||||
if (!_heap_->buffer) \
|
||||
{ \
|
||||
@@ -164,9 +185,9 @@ typedef enum HeapOrder
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_heap_->buffer, 0, sizeof(V) * size); \
|
||||
memset(_heap_->buffer, 0, sizeof(V) * capacity); \
|
||||
\
|
||||
_heap_->capacity = size; \
|
||||
_heap_->capacity = capacity; \
|
||||
_heap_->count = 0; \
|
||||
_heap_->HO = HO; \
|
||||
_heap_->cmp = compare; \
|
||||
@@ -177,20 +198,36 @@ typedef enum HeapOrder
|
||||
return _heap_; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_clear(SNAME *_heap_) \
|
||||
void PFX##_clear(SNAME *_heap_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _heap_->count; i++) \
|
||||
{ \
|
||||
deallocator(_heap_->buffer[i]); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
memset(_heap_->buffer, 0, sizeof(V) * _heap_->capacity); \
|
||||
\
|
||||
_heap_->count = 0; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_free(SNAME *_heap_) \
|
||||
void PFX##_free(SNAME *_heap_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _heap_->count; i++) \
|
||||
{ \
|
||||
deallocator(_heap_->buffer[i]); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
free(_heap_->buffer); \
|
||||
free(_heap_); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_insert(SNAME *_heap_, V element) \
|
||||
bool PFX##_insert(SNAME *_heap_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_heap_)) \
|
||||
{ \
|
||||
@@ -198,7 +235,7 @@ typedef enum HeapOrder
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (_heap_->count == 0) \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
{ \
|
||||
_heap_->buffer[_heap_->count++] = element; \
|
||||
return true; \
|
||||
@@ -212,14 +249,14 @@ typedef enum HeapOrder
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_remove(SNAME *_heap_, V *result) \
|
||||
bool PFX##_remove(SNAME *_heap_, V *result) \
|
||||
{ \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
return false; \
|
||||
\
|
||||
*result = _heap_->buffer[0]; \
|
||||
_heap_->buffer[0] = _heap_->buffer[_heap_->count - 1]; \
|
||||
_heap_->buffer[_heap_->count - 1] = 0; \
|
||||
_heap_->buffer[_heap_->count - 1] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_heap_->count--; \
|
||||
\
|
||||
@@ -229,7 +266,7 @@ typedef enum HeapOrder
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_insert_if(SNAME *_heap_, V element, bool condition) \
|
||||
bool PFX##_insert_if(SNAME *_heap_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_insert(_heap_, element); \
|
||||
@@ -237,7 +274,7 @@ typedef enum HeapOrder
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_remove_if(SNAME *_heap_, V *result, bool condition) \
|
||||
bool PFX##_remove_if(SNAME *_heap_, V *result, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_remove(_heap_, result); \
|
||||
@@ -245,15 +282,15 @@ typedef enum HeapOrder
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_peek(SNAME *_heap_) \
|
||||
V PFX##_peek(SNAME *_heap_) \
|
||||
{ \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
return 0; \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _heap_->buffer[0]; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_peek_ref(SNAME *_heap_) \
|
||||
V *PFX##_peek_ref(SNAME *_heap_) \
|
||||
{ \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
return NULL; \
|
||||
@@ -261,7 +298,7 @@ typedef enum HeapOrder
|
||||
return &(_heap_->buffer[0]); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_contains(SNAME *_heap_, V element) \
|
||||
bool PFX##_contains(SNAME *_heap_, V element) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _heap_->count; i++) \
|
||||
{ \
|
||||
@@ -272,27 +309,74 @@ typedef enum HeapOrder
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_empty(SNAME *_heap_) \
|
||||
bool PFX##_empty(SNAME *_heap_) \
|
||||
{ \
|
||||
return _heap_->count == 0; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_full(SNAME *_heap_) \
|
||||
bool PFX##_full(SNAME *_heap_) \
|
||||
{ \
|
||||
return _heap_->count >= _heap_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_count(SNAME *_heap_) \
|
||||
size_t PFX##_count(SNAME *_heap_) \
|
||||
{ \
|
||||
return _heap_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_capacity(SNAME *_heap_) \
|
||||
size_t PFX##_capacity(SNAME *_heap_) \
|
||||
{ \
|
||||
return _heap_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
SNAME *PFX##_copy_of(SNAME *_heap_, V (*copy_func)(V)) \
|
||||
{ \
|
||||
SNAME *result = PFX##_new(_heap_->capacity, _heap_->HO, _heap_->cmp); \
|
||||
\
|
||||
if (!result) \
|
||||
return NULL; \
|
||||
\
|
||||
if (copy_func) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _heap_->count; i++) \
|
||||
result->buffer[i] = copy_func(_heap_->buffer[i]); \
|
||||
} \
|
||||
else \
|
||||
memcpy(result->buffer, _heap_->buffer, sizeof(V) * _heap_->count); \
|
||||
\
|
||||
result->count = _heap_->count; \
|
||||
\
|
||||
return result; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_equals(SNAME *_heap1_, SNAME *_heap2_) \
|
||||
{ \
|
||||
if (PFX##_count(_heap1_) != PFX##_count(_heap2_)) \
|
||||
return false; \
|
||||
\
|
||||
for (size_t i = 0; i < PFX##_count(_heap1_); i++) \
|
||||
{ \
|
||||
if (_heap1_->cmp(_heap1_->buffer[i], _heap2_->buffer[i]) != 0) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
cmc_string PFX##_to_string(SNAME *_heap_) \
|
||||
{ \
|
||||
cmc_string str; \
|
||||
SNAME *h_ = _heap_; \
|
||||
const char *name = #SNAME; \
|
||||
const char *t = h_->HO == 1 ? "MaxHeap" : "MinHeap"; \
|
||||
\
|
||||
snprintf(str.s, cmc_string_len, cmc_string_fmt_heap, \
|
||||
name, h_, h_->buffer, h_->capacity, h_->count, t, h_->cmp); \
|
||||
\
|
||||
return str; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
@@ -304,12 +388,12 @@ typedef enum HeapOrder
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = 0; \
|
||||
@@ -317,74 +401,147 @@ typedef enum HeapOrder
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = PFX##_count(iter->target) - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = false; \
|
||||
\
|
||||
if (iter->cursor == iter->target->count - 1) \
|
||||
if (iter->cursor + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
else \
|
||||
iter->cursor++; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
else \
|
||||
iter->cursor--; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->cursor + steps >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
iter->cursor += steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->cursor < steps) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor -= steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator was able to be positioned at the given index */ \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index) \
|
||||
{ \
|
||||
if (index >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor > index) \
|
||||
return PFX##_iter_rewind(iter, iter->cursor - index); \
|
||||
else if (iter->cursor < index) \
|
||||
return PFX##_iter_advance(iter, index - iter->cursor); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return 0; \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->cursor; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_impl_grow(SNAME *_heap_) \
|
||||
static bool PFX##_impl_grow(SNAME *_heap_) \
|
||||
{ \
|
||||
size_t new_capacity = _heap_->capacity * 2; \
|
||||
\
|
||||
@@ -399,57 +556,43 @@ typedef enum HeapOrder
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_impl_p(size_t index) \
|
||||
{ \
|
||||
if (index == 0) \
|
||||
return 0; \
|
||||
\
|
||||
return (index - 1) / 2; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_impl_l(size_t index) \
|
||||
{ \
|
||||
return 2 * index + 1; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_impl_r(size_t index) \
|
||||
{ \
|
||||
return 2 * index + 2; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_impl_float_up(SNAME *_heap_, size_t index) \
|
||||
static bool PFX##_impl_float_up(SNAME *_heap_, size_t index) \
|
||||
{ \
|
||||
/* Current index */ \
|
||||
size_t C = index; \
|
||||
V child = _heap_->buffer[C]; \
|
||||
V parent = _heap_->buffer[PFX##_impl_p(C)]; \
|
||||
V parent = _heap_->buffer[(index - 1) / 2]; \
|
||||
\
|
||||
int mod = _heap_->HO; \
|
||||
\
|
||||
while (C > 0 && _heap_->cmp(child, parent) * mod > 0) \
|
||||
{ \
|
||||
/* Swap between C (current element) and its parent */ \
|
||||
V tmp = _heap_->buffer[C]; \
|
||||
_heap_->buffer[C] = _heap_->buffer[PFX##_impl_p(C)]; \
|
||||
_heap_->buffer[PFX##_impl_p(C)] = tmp; \
|
||||
_heap_->buffer[C] = _heap_->buffer[(C - 1) / 2]; \
|
||||
_heap_->buffer[(C - 1) / 2] = tmp; \
|
||||
\
|
||||
C = PFX##_impl_p(C); \
|
||||
/* Go to parent */ \
|
||||
C = (C - 1) / 2; \
|
||||
\
|
||||
child = _heap_->buffer[C]; \
|
||||
parent = _heap_->buffer[PFX##_impl_p(C)]; \
|
||||
parent = _heap_->buffer[(C - 1) / 2]; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_impl_float_down(SNAME *_heap_, size_t index) \
|
||||
static bool PFX##_impl_float_down(SNAME *_heap_, size_t index) \
|
||||
{ \
|
||||
int mod = _heap_->HO; \
|
||||
\
|
||||
while (index < _heap_->count) \
|
||||
{ \
|
||||
size_t L = PFX##_impl_l(index); \
|
||||
size_t R = PFX##_impl_r(index); \
|
||||
size_t L = 2 * index + 1; \
|
||||
size_t R = 2 * index + 2; \
|
||||
size_t C = index; \
|
||||
\
|
||||
/* Determine if we swap with the left or right element */ \
|
||||
if (L < _heap_->count && _heap_->cmp(_heap_->buffer[L], _heap_->buffer[C]) * mod > 0) \
|
||||
{ \
|
||||
C = L; \
|
||||
@@ -460,6 +603,7 @@ typedef enum HeapOrder
|
||||
C = R; \
|
||||
} \
|
||||
\
|
||||
/* Swap only if either left or right was picked, otherwise done */ \
|
||||
if (C != index) \
|
||||
{ \
|
||||
V tmp = _heap_->buffer[index]; \
|
||||
@@ -475,7 +619,7 @@ typedef enum HeapOrder
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_heap_) \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_heap_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
@@ -485,7 +629,7 @@ typedef enum HeapOrder
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_heap_) \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_heap_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
File diff suppressed because it is too large.
Load diff
+350
-188
@@ -8,141 +8,178 @@
|
||||
*
|
||||
*/
|
||||
|
||||
/*****************************************************************************/
|
||||
/********************************************************************** LIST */
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* List
|
||||
*
|
||||
* 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).
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef CMC_LIST_H
|
||||
#define CMC_LIST_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../utl/cmc_string.h"
|
||||
|
||||
#define LIST_GENERATE(PFX, SNAME, FMOD, V) \
|
||||
LIST_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
LIST_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
LIST_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
#define CMC_GENERATE_LIST(PFX, SNAME, V) \
|
||||
CMC_GENERATE_LIST_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_LIST_SOURCE(PFX, SNAME, V)
|
||||
|
||||
/* PRIVATE *******************************************************************/
|
||||
#define LIST_GENERATE_HEADER_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
LIST_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define LIST_GENERATE_SOURCE_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
LIST_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
LIST_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* PUBLIC ********************************************************************/
|
||||
#define LIST_GENERATE_HEADER_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
LIST_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
LIST_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define LIST_GENERATE_SOURCE_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
LIST_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* STRUCT ********************************************************************/
|
||||
#define LIST_GENERATE_STRUCT(PFX, SNAME, FMOD, V)
|
||||
#define CMC_WRAPGEN_LIST_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_LIST_HEADER(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_LIST_SOURCE(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_LIST_SOURCE(PFX, SNAME, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define LIST_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
/* List Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the list */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the list */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* List Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target list */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME *PFX##_new(size_t capacity); \
|
||||
FMOD SNAME *PFX##_new_from(V *elements, size_t size); \
|
||||
FMOD void PFX##_clear(SNAME *_list_); \
|
||||
FMOD void PFX##_free(SNAME *_list_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_push_front(SNAME *_list_, V element); \
|
||||
FMOD bool PFX##_push(SNAME *_list_, V element, size_t index); \
|
||||
FMOD bool PFX##_push_back(SNAME *_list_, V element); \
|
||||
FMOD bool PFX##_pop_front(SNAME *_list_); \
|
||||
FMOD bool PFX##_pop(SNAME *_list_, size_t index); \
|
||||
FMOD bool PFX##_pop_back(SNAME *_list_); \
|
||||
/* Conditional Input and Output */ \
|
||||
FMOD bool PFX##_push_if(SNAME *_list_, V element, size_t index, bool condition); \
|
||||
FMOD bool PFX##_pop_if(SNAME *_list_, size_t index, bool condition); \
|
||||
/* Collection Range Input and Output */ \
|
||||
FMOD bool PFX##_prepend(SNAME *_list_, V *elements, size_t size); \
|
||||
FMOD bool PFX##_insert(SNAME *_list_, V *elements, size_t size, size_t index); \
|
||||
FMOD bool PFX##_append(SNAME *_list_, V *elements, size_t size); \
|
||||
FMOD bool PFX##_remove(SNAME *_list_, size_t from, size_t to); \
|
||||
FMOD SNAME *PFX##_extract(SNAME *_list_, size_t from, size_t to); \
|
||||
/* Element Access */ \
|
||||
FMOD V PFX##_front(SNAME *_list_); \
|
||||
FMOD V PFX##_get(SNAME *_list_, size_t index); \
|
||||
FMOD V *PFX##_get_ref(SNAME *_list_, size_t index); \
|
||||
FMOD V PFX##_back(SNAME *_list_); \
|
||||
FMOD size_t PFX##_indexof(SNAME *_list_, V element, int (*comparator)(V, V), bool from_start); \
|
||||
/* Collection State */ \
|
||||
FMOD bool PFX##_contains(SNAME *_list_, V element, int (*comparator)(V, V)); \
|
||||
FMOD bool PFX##_empty(SNAME *_list_); \
|
||||
FMOD bool PFX##_full(SNAME *_list_); \
|
||||
FMOD size_t PFX##_count(SNAME *_list_); \
|
||||
FMOD bool PFX##_fits(SNAME *_list_, size_t size); \
|
||||
FMOD size_t PFX##_capacity(SNAME *_list_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
/* Iterator Access */ \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
#define CMC_GENERATE_LIST_HEADER(PFX, SNAME, V) \
|
||||
\
|
||||
/* List Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the list */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the list */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* List Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target list */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
SNAME *PFX##_new(size_t capacity); \
|
||||
SNAME *PFX##_new_from(V *elements, size_t size); \
|
||||
void PFX##_clear(SNAME *_list_, void (*deallocator)(V)); \
|
||||
void PFX##_free(SNAME *_list_, void (*deallocator)(V)); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_push_front(SNAME *_list_, V element); \
|
||||
bool PFX##_push(SNAME *_list_, V element, size_t index); \
|
||||
bool PFX##_push_back(SNAME *_list_, V element); \
|
||||
bool PFX##_pop_front(SNAME *_list_); \
|
||||
bool PFX##_pop(SNAME *_list_, size_t index); \
|
||||
bool PFX##_pop_back(SNAME *_list_); \
|
||||
/* Conditional Input and Output */ \
|
||||
bool PFX##_push_if(SNAME *_list_, V element, size_t index, bool condition); \
|
||||
bool PFX##_pop_if(SNAME *_list_, size_t index, bool condition); \
|
||||
/* Collection Range Input and Output */ \
|
||||
bool PFX##_prepend(SNAME *_list_, V *elements, size_t size); \
|
||||
bool PFX##_insert(SNAME *_list_, V *elements, size_t size, size_t index); \
|
||||
bool PFX##_append(SNAME *_list_, V *elements, size_t size); \
|
||||
bool PFX##_remove(SNAME *_list_, size_t from, size_t to); \
|
||||
SNAME *PFX##_extract(SNAME *_list_, size_t from, size_t to); \
|
||||
/* Element Access */ \
|
||||
V PFX##_front(SNAME *_list_); \
|
||||
V PFX##_get(SNAME *_list_, size_t index); \
|
||||
V *PFX##_get_ref(SNAME *_list_, size_t index); \
|
||||
V PFX##_back(SNAME *_list_); \
|
||||
size_t PFX##_indexof(SNAME *_list_, V element, int (*comparator)(V, V), bool from_start); \
|
||||
/* Collection State */ \
|
||||
bool PFX##_contains(SNAME *_list_, V element, int (*comparator)(V, V)); \
|
||||
bool PFX##_empty(SNAME *_list_); \
|
||||
bool PFX##_full(SNAME *_list_); \
|
||||
size_t PFX##_count(SNAME *_list_); \
|
||||
bool PFX##_fits(SNAME *_list_, size_t size); \
|
||||
size_t PFX##_capacity(SNAME *_list_); \
|
||||
/* Collection Utility */ \
|
||||
SNAME *PFX##_copy_of(SNAME *_list_, V (*copy_func)(V)); \
|
||||
bool PFX##_equals(SNAME *_list1_, SNAME *_list2_, int (*comparator)(V, V)); \
|
||||
cmc_string PFX##_to_string(SNAME *_list_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index); \
|
||||
/* Iterator Access */ \
|
||||
V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* Default Value */ \
|
||||
static inline V PFX##_impl_default_value(void) \
|
||||
{ \
|
||||
V _empty_value_; \
|
||||
\
|
||||
memset(&_empty_value_, 0, sizeof(V)); \
|
||||
\
|
||||
return _empty_value_; \
|
||||
} \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define LIST_GENERATE_SOURCE(PFX, SNAME, FMOD, V) \
|
||||
#define CMC_GENERATE_LIST_SOURCE(PFX, SNAME, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
FMOD bool PFX##_impl_grow(SNAME *_list_, size_t required); \
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_list_); \
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_list_); \
|
||||
static bool PFX##_impl_grow(SNAME *_list_, size_t required); \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_list_); \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_list_); \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(size_t capacity) \
|
||||
SNAME *PFX##_new(size_t capacity) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
@@ -171,7 +208,7 @@
|
||||
return _list_; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME *PFX##_new_from(V *elements, size_t size) \
|
||||
SNAME *PFX##_new_from(V *elements, size_t size) \
|
||||
{ \
|
||||
if (size == 0) \
|
||||
return NULL; \
|
||||
@@ -188,20 +225,32 @@
|
||||
return _list_; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_clear(SNAME *_list_) \
|
||||
void PFX##_clear(SNAME *_list_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _list_->count; i++) \
|
||||
deallocator(_list_->buffer[i]); \
|
||||
} \
|
||||
\
|
||||
memset(_list_->buffer, 0, sizeof(V) * _list_->capacity); \
|
||||
\
|
||||
_list_->count = 0; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_free(SNAME *_list_) \
|
||||
void PFX##_free(SNAME *_list_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _list_->count; i++) \
|
||||
deallocator(_list_->buffer[i]); \
|
||||
} \
|
||||
\
|
||||
free(_list_->buffer); \
|
||||
free(_list_); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_front(SNAME *_list_, V element) \
|
||||
bool PFX##_push_front(SNAME *_list_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_list_)) \
|
||||
{ \
|
||||
@@ -221,7 +270,7 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push(SNAME *_list_, V element, size_t index) \
|
||||
bool PFX##_push(SNAME *_list_, V element, size_t index) \
|
||||
{ \
|
||||
if (index > _list_->count) \
|
||||
return false; \
|
||||
@@ -250,7 +299,7 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_back(SNAME *_list_, V element) \
|
||||
bool PFX##_push_back(SNAME *_list_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_list_)) \
|
||||
{ \
|
||||
@@ -263,19 +312,19 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_front(SNAME *_list_) \
|
||||
bool PFX##_pop_front(SNAME *_list_) \
|
||||
{ \
|
||||
if (PFX##_empty(_list_)) \
|
||||
return false; \
|
||||
\
|
||||
memmove(_list_->buffer, _list_->buffer + 1, _list_->count * sizeof(V)); \
|
||||
\
|
||||
_list_->buffer[--_list_->count] = 0; \
|
||||
_list_->buffer[--_list_->count] = PFX##_impl_default_value(); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop(SNAME *_list_, size_t index) \
|
||||
bool PFX##_pop(SNAME *_list_, size_t index) \
|
||||
{ \
|
||||
if (PFX##_empty(_list_)) \
|
||||
return false; \
|
||||
@@ -294,22 +343,22 @@
|
||||
\
|
||||
memmove(_list_->buffer + index, _list_->buffer + index + 1, (_list_->count - index) * sizeof(V)); \
|
||||
\
|
||||
_list_->buffer[--_list_->count] = 0; \
|
||||
_list_->buffer[--_list_->count] = PFX##_impl_default_value(); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_back(SNAME *_list_) \
|
||||
bool PFX##_pop_back(SNAME *_list_) \
|
||||
{ \
|
||||
if (PFX##_empty(_list_)) \
|
||||
return false; \
|
||||
\
|
||||
_list_->buffer[--_list_->count] = 0; \
|
||||
_list_->buffer[--_list_->count] = PFX##_impl_default_value(); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_if(SNAME *_list_, V element, size_t index, bool condition) \
|
||||
bool PFX##_push_if(SNAME *_list_, V element, size_t index, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_push(_list_, element, index); \
|
||||
@@ -317,7 +366,7 @@
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_if(SNAME *_list_, size_t index, bool condition) \
|
||||
bool PFX##_pop_if(SNAME *_list_, size_t index, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_pop(_list_, index); \
|
||||
@@ -325,7 +374,7 @@
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_prepend(SNAME *_list_, V *elements, size_t size) \
|
||||
bool PFX##_prepend(SNAME *_list_, V *elements, size_t size) \
|
||||
{ \
|
||||
if (size == 0) \
|
||||
return false; \
|
||||
@@ -345,7 +394,7 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_insert(SNAME *_list_, V *elements, size_t size, size_t index) \
|
||||
bool PFX##_insert(SNAME *_list_, V *elements, size_t size, size_t index) \
|
||||
{ \
|
||||
if (size == 0 || index > _list_->count) \
|
||||
return false; \
|
||||
@@ -372,7 +421,7 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_append(SNAME *_list_, V *elements, size_t size) \
|
||||
bool PFX##_append(SNAME *_list_, V *elements, size_t size) \
|
||||
{ \
|
||||
if (size == 0) \
|
||||
return false; \
|
||||
@@ -390,7 +439,7 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_remove(SNAME *_list_, size_t from, size_t to) \
|
||||
bool PFX##_remove(SNAME *_list_, size_t from, size_t to) \
|
||||
{ \
|
||||
if (from > to || to >= _list_->count) \
|
||||
return false; \
|
||||
@@ -406,7 +455,7 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME *PFX##_extract(SNAME *_list_, size_t from, size_t to) \
|
||||
SNAME *PFX##_extract(SNAME *_list_, size_t from, size_t to) \
|
||||
{ \
|
||||
if (from > to || to >= _list_->count) \
|
||||
return false; \
|
||||
@@ -427,26 +476,23 @@
|
||||
return result; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_front(SNAME *_list_) \
|
||||
V PFX##_front(SNAME *_list_) \
|
||||
{ \
|
||||
if (PFX##_empty(_list_)) \
|
||||
return 0; \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _list_->buffer[0]; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_get(SNAME *_list_, size_t index) \
|
||||
V PFX##_get(SNAME *_list_, size_t index) \
|
||||
{ \
|
||||
if (index >= _list_->count) \
|
||||
return 0; \
|
||||
\
|
||||
if (PFX##_empty(_list_)) \
|
||||
return 0; \
|
||||
if (index >= _list_->count || PFX##_empty(_list_)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _list_->buffer[index]; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_get_ref(SNAME *_list_, size_t index) \
|
||||
V *PFX##_get_ref(SNAME *_list_, size_t index) \
|
||||
{ \
|
||||
if (index >= _list_->count) \
|
||||
return NULL; \
|
||||
@@ -457,15 +503,15 @@
|
||||
return &(_list_->buffer[index]); \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_back(SNAME *_list_) \
|
||||
V PFX##_back(SNAME *_list_) \
|
||||
{ \
|
||||
if (PFX##_empty(_list_)) \
|
||||
return 0; \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _list_->buffer[_list_->count - 1]; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_indexof(SNAME *_list_, V element, int (*comparator)(V, V), bool from_start) \
|
||||
size_t PFX##_indexof(SNAME *_list_, V element, int (*comparator)(V, V), bool from_start) \
|
||||
{ \
|
||||
if (from_start) \
|
||||
{ \
|
||||
@@ -487,7 +533,7 @@
|
||||
return _list_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_contains(SNAME *_list_, V element, int (*comparator)(V, V)) \
|
||||
bool PFX##_contains(SNAME *_list_, V element, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _list_->count; i++) \
|
||||
{ \
|
||||
@@ -498,32 +544,78 @@
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_empty(SNAME *_list_) \
|
||||
bool PFX##_empty(SNAME *_list_) \
|
||||
{ \
|
||||
return _list_->count == 0; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_full(SNAME *_list_) \
|
||||
bool PFX##_full(SNAME *_list_) \
|
||||
{ \
|
||||
return _list_->count >= _list_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_count(SNAME *_list_) \
|
||||
size_t PFX##_count(SNAME *_list_) \
|
||||
{ \
|
||||
return _list_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_fits(SNAME *_list_, size_t size) \
|
||||
bool PFX##_fits(SNAME *_list_, size_t size) \
|
||||
{ \
|
||||
return _list_->count + size <= _list_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_capacity(SNAME *_list_) \
|
||||
size_t PFX##_capacity(SNAME *_list_) \
|
||||
{ \
|
||||
return _list_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
SNAME *PFX##_copy_of(SNAME *_list_, V (*copy_func)(V)) \
|
||||
{ \
|
||||
SNAME *result = PFX##_new(_list_->capacity); \
|
||||
\
|
||||
if (!result) \
|
||||
return NULL; \
|
||||
\
|
||||
if (copy_func) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _list_->count; i++) \
|
||||
result->buffer[i] = copy_func(_list_->buffer[i]); \
|
||||
} \
|
||||
else \
|
||||
memcpy(result->buffer, _list_->buffer, sizeof(V) * _list_->count); \
|
||||
\
|
||||
result->count = _list_->count; \
|
||||
\
|
||||
return result; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_equals(SNAME *_list1_, SNAME *_list2_, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
if (PFX##_count(_list1_) != PFX##_count(_list2_)) \
|
||||
return false; \
|
||||
\
|
||||
for (size_t i = 0; i < PFX##_count(_list1_); i++) \
|
||||
{ \
|
||||
if (comparator(_list1_->buffer[i], _list2_->buffer[i]) != 0) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
cmc_string PFX##_to_string(SNAME *_list_) \
|
||||
{ \
|
||||
cmc_string str; \
|
||||
SNAME *l_ = _list_; \
|
||||
const char *name = #SNAME; \
|
||||
\
|
||||
snprintf(str.s, cmc_string_len, cmc_string_fmt_list, \
|
||||
name, l_, l_->buffer, l_->capacity, l_->count); \
|
||||
\
|
||||
return str; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
@@ -535,12 +627,12 @@
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = 0; \
|
||||
@@ -548,69 +640,139 @@
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->cursor = PFX##_empty(iter->target) ? 0 : iter->target->count - 1; \
|
||||
iter->end = true; \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->cursor = PFX##_empty(iter->target) ? 0 : iter->target->count - 1; \
|
||||
iter->end = true; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->cursor == iter->target->count - 1) \
|
||||
iter->end = true; \
|
||||
else \
|
||||
iter->cursor++; \
|
||||
iter->cursor++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
iter->start = true; \
|
||||
else \
|
||||
iter->cursor--; \
|
||||
iter->cursor--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor + 1 == iter->target->count) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->cursor + steps >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor += steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->cursor < steps) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor -= steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator was able to be positioned at the given index */ \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index) \
|
||||
{ \
|
||||
if (index >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor > index) \
|
||||
return PFX##_iter_rewind(iter, iter->cursor - index); \
|
||||
else if (iter->cursor < index) \
|
||||
return PFX##_iter_advance(iter, index - iter->cursor); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return 0; \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return NULL; \
|
||||
@@ -618,12 +780,12 @@
|
||||
return &(iter->target->buffer[iter->cursor]); \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->cursor; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_impl_grow(SNAME *_list_, size_t required) \
|
||||
static bool PFX##_impl_grow(SNAME *_list_, size_t required) \
|
||||
{ \
|
||||
size_t new_capacity = _list_->capacity * 2; \
|
||||
\
|
||||
@@ -641,7 +803,7 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_list_) \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_list_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
@@ -651,7 +813,7 @@
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_list_) \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_list_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
+603
@@ -0,0 +1,603 @@
|
||||
/**
|
||||
* queue.h
|
||||
*
|
||||
* Creation Date: 15/02/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Queue
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef CMC_QUEUE_H
|
||||
#define CMC_QUEUE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../utl/cmc_string.h"
|
||||
|
||||
#define CMC_GENERATE_QUEUE(PFX, SNAME, V) \
|
||||
CMC_GENERATE_QUEUE_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_QUEUE_SOURCE(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_QUEUE_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_QUEUE_HEADER(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_QUEUE_SOURCE(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_QUEUE_SOURCE(PFX, SNAME, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define CMC_GENERATE_QUEUE_HEADER(PFX, SNAME, V) \
|
||||
\
|
||||
/* Queue Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic circular array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current circular array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Index representing the front of the queue */ \
|
||||
size_t front; \
|
||||
\
|
||||
/* Index representing the back of the queue */ \
|
||||
size_t back; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the queue */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the queue */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Queue Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target queue */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* Keeps track of relative index to the iteration of elements */ \
|
||||
size_t index; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
SNAME *PFX##_new(size_t capacity); \
|
||||
void PFX##_clear(SNAME *_queue_, void (*deallocator)(V)); \
|
||||
void PFX##_free(SNAME *_queue_, void (*deallocator)(V)); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_enqueue(SNAME *_queue_, V element); \
|
||||
bool PFX##_dequeue(SNAME *_queue_); \
|
||||
/* Conditional Input and Output */ \
|
||||
bool PFX##_enqueue_if(SNAME *_queue_, V element, bool condition); \
|
||||
bool PFX##_dequeue_if(SNAME *_queue_, bool condition); \
|
||||
/* Element Access */ \
|
||||
V PFX##_peek(SNAME *_queue_); \
|
||||
/* Collection State */ \
|
||||
bool PFX##_contains(SNAME *_queue_, V element, int (*comparator)(V, V)); \
|
||||
bool PFX##_empty(SNAME *_queue_); \
|
||||
bool PFX##_full(SNAME *_queue_); \
|
||||
size_t PFX##_count(SNAME *_queue_); \
|
||||
size_t PFX##_capacity(SNAME *_queue_); \
|
||||
/* Collection Utility */ \
|
||||
SNAME *PFX##_copy_of(SNAME *_queue_, V (*copy_func)(V)); \
|
||||
bool PFX##_equals(SNAME *_queue1_, SNAME *_queue2_, int (*comparator)(V, V)); \
|
||||
cmc_string PFX##_to_string(SNAME *_queue_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index); \
|
||||
/* Iterator Access */ \
|
||||
V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* Default Value */ \
|
||||
static inline V PFX##_impl_default_value(void) \
|
||||
{ \
|
||||
V _empty_value_; \
|
||||
\
|
||||
memset(&_empty_value_, 0, sizeof(V)); \
|
||||
\
|
||||
return _empty_value_; \
|
||||
} \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define CMC_GENERATE_QUEUE_SOURCE(PFX, SNAME, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
static bool PFX##_impl_grow(SNAME *_queue_); \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_queue_); \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_queue_); \
|
||||
\
|
||||
SNAME *PFX##_new(size_t capacity) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
\
|
||||
SNAME *_queue_ = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!_queue_) \
|
||||
return NULL; \
|
||||
\
|
||||
_queue_->buffer = malloc(sizeof(V) * capacity); \
|
||||
\
|
||||
if (!_queue_->buffer) \
|
||||
{ \
|
||||
free(_queue_); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_queue_->buffer, 0, sizeof(V) * capacity); \
|
||||
\
|
||||
_queue_->capacity = capacity; \
|
||||
_queue_->count = 0; \
|
||||
_queue_->front = 0; \
|
||||
_queue_->back = 0; \
|
||||
\
|
||||
_queue_->it_start = PFX##_impl_it_start; \
|
||||
_queue_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _queue_; \
|
||||
} \
|
||||
\
|
||||
void PFX##_clear(SNAME *_queue_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->capacity, j++) \
|
||||
{ \
|
||||
deallocator(_queue_->buffer[i]); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
memset(_queue_->buffer, 0, sizeof(V) * _queue_->capacity); \
|
||||
\
|
||||
_queue_->count = 0; \
|
||||
_queue_->front = 0; \
|
||||
_queue_->back = 0; \
|
||||
} \
|
||||
\
|
||||
void PFX##_free(SNAME *_queue_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->capacity, j++) \
|
||||
{ \
|
||||
deallocator(_queue_->buffer[i]); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
free(_queue_->buffer); \
|
||||
free(_queue_); \
|
||||
} \
|
||||
\
|
||||
bool PFX##_enqueue(SNAME *_queue_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_queue_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_queue_)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
_queue_->buffer[_queue_->back] = element; \
|
||||
\
|
||||
_queue_->back = (_queue_->back == _queue_->capacity - 1) ? 0 : _queue_->back + 1; \
|
||||
_queue_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_dequeue(SNAME *_queue_) \
|
||||
{ \
|
||||
if (PFX##_empty(_queue_)) \
|
||||
return false; \
|
||||
\
|
||||
_queue_->buffer[_queue_->front] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_queue_->front = (_queue_->front == _queue_->capacity - 1) ? 0 : _queue_->front + 1; \
|
||||
_queue_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_enqueue_if(SNAME *_queue_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_enqueue(_queue_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_dequeue_if(SNAME *_queue_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_dequeue(_queue_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
V PFX##_peek(SNAME *_queue_) \
|
||||
{ \
|
||||
if (PFX##_empty(_queue_)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _queue_->buffer[_queue_->front]; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_contains(SNAME *_queue_, V element, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->count, j++) \
|
||||
{ \
|
||||
if (comparator(_queue_->buffer[i], element) == 0) \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_empty(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->count == 0; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_full(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->count >= _queue_->capacity; \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_count(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->count; \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_capacity(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->capacity; \
|
||||
} \
|
||||
\
|
||||
SNAME *PFX##_copy_of(SNAME *_queue_, V (*copy_func)(V)) \
|
||||
{ \
|
||||
SNAME *result = PFX##_new(_queue_->capacity); \
|
||||
\
|
||||
if (!result) \
|
||||
return NULL; \
|
||||
\
|
||||
if (copy_func) \
|
||||
{ \
|
||||
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->capacity, j++) \
|
||||
{ \
|
||||
result->buffer[j] = copy_func(_queue_->buffer[i]); \
|
||||
} \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->capacity, j++) \
|
||||
{ \
|
||||
result->buffer[j] = _queue_->buffer[i]; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
result->count = _queue_->count; \
|
||||
\
|
||||
return result; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_equals(SNAME *_queue1_, SNAME *_queue2_, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
if (PFX##_count(_queue1_) != PFX##_count(_queue2_)) \
|
||||
return false; \
|
||||
\
|
||||
size_t i, j, k; \
|
||||
for (i = _queue1_->front, j = _queue2_->front, k = 0; k < PFX##_count(_queue1_); k++) \
|
||||
{ \
|
||||
if (comparator(_queue1_->buffer[i], _queue2_->buffer[j]) == 0) \
|
||||
return false; \
|
||||
\
|
||||
i = (i + 1) % _queue1_->capacity; \
|
||||
j = (j + 1) % _queue2_->capacity; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
cmc_string PFX##_to_string(SNAME *_queue_) \
|
||||
{ \
|
||||
cmc_string str; \
|
||||
SNAME *q_ = _queue_; \
|
||||
const char *name = #SNAME; \
|
||||
\
|
||||
snprintf(str.s, cmc_string_len, cmc_string_fmt_queue, \
|
||||
name, q_, q_->buffer, q_->capacity, q_->count, q_->front, q_->back); \
|
||||
\
|
||||
return str; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
iter->cursor = 0; \
|
||||
else \
|
||||
iter->cursor = (iter->target->back == 0) ? iter->target->capacity - 1 : iter->target->back - 1; \
|
||||
\
|
||||
iter->index = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor = (iter->cursor + 1) % (iter->target->capacity); \
|
||||
iter->index++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor = (iter->cursor == 0) ? iter->target->capacity - 1 : iter->cursor - 1; \
|
||||
iter->index--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->index + steps >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->index += steps; \
|
||||
iter->cursor = (iter->cursor + steps) % iter->target->capacity; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->index < steps) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->index -= steps; \
|
||||
\
|
||||
/* Prevent underflow */ \
|
||||
if (iter->cursor < steps) \
|
||||
iter->cursor += PFX##_capacity(iter->target); \
|
||||
\
|
||||
iter->cursor -= steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator was able to be positioned at the given index */ \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index) \
|
||||
{ \
|
||||
if (index >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->index > index) \
|
||||
return PFX##_iter_rewind(iter, iter->index - index); \
|
||||
else if (iter->index < index) \
|
||||
return PFX##_iter_advance(iter, index - iter->index); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return NULL; \
|
||||
\
|
||||
return &(iter->target->buffer[iter->cursor]); \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->index; \
|
||||
} \
|
||||
\
|
||||
static bool PFX##_impl_grow(SNAME *_queue_) \
|
||||
{ \
|
||||
\
|
||||
size_t new_capacity = _queue_->capacity * 2; \
|
||||
\
|
||||
V *new_buffer = malloc(sizeof(V) * new_capacity); \
|
||||
\
|
||||
if (!new_buffer) \
|
||||
return false; \
|
||||
\
|
||||
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->capacity, j++) \
|
||||
{ \
|
||||
new_buffer[j] = _queue_->buffer[i]; \
|
||||
} \
|
||||
\
|
||||
free(_queue_->buffer); \
|
||||
\
|
||||
_queue_->buffer = new_buffer; \
|
||||
_queue_->capacity = new_capacity; \
|
||||
_queue_->front = 0; \
|
||||
_queue_->back = _queue_->count; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_queue_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _queue_); \
|
||||
PFX##_iter_to_start(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_queue_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _queue_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_QUEUE_H */
|
||||
+538
@@ -0,0 +1,538 @@
|
||||
/**
|
||||
* stack.h
|
||||
*
|
||||
* Creation Date: 14/02/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Stack
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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`).
|
||||
*
|
||||
* A Stack is used in algorithms like backtracking, depth-first search,
|
||||
* expression evaluation, syntax parsing and many more.
|
||||
*/
|
||||
|
||||
#ifndef CMC_STACK_H
|
||||
#define CMC_STACK_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../utl/cmc_string.h"
|
||||
|
||||
#define CMC_GENERATE_STACK(PFX, SNAME, V) \
|
||||
CMC_GENERATE_STACK_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_STACK_SOURCE(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_STACK_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_STACK_HEADER(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_STACK_SOURCE(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_STACK_SOURCE(PFX, SNAME, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define CMC_GENERATE_STACK_HEADER(PFX, SNAME, V) \
|
||||
\
|
||||
/* Stack Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the stack */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the stack */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Stack Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target stack */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
SNAME *PFX##_new(size_t capacity); \
|
||||
void PFX##_clear(SNAME *_stack_, void (*deallocator)(V)); \
|
||||
void PFX##_free(SNAME *_stack_, void (*deallocator)(V)); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_push(SNAME *_stack_, V element); \
|
||||
bool PFX##_pop(SNAME *_stack_); \
|
||||
/* Conditional Input and Output */ \
|
||||
bool PFX##_push_if(SNAME *_stack_, V element, bool condition); \
|
||||
bool PFX##_pop_if(SNAME *_stack_, bool condition); \
|
||||
/* Element Access */ \
|
||||
V PFX##_top(SNAME *_stack_); \
|
||||
/* Collection State */ \
|
||||
bool PFX##_contains(SNAME *_stack_, V element, int (*comparator)(V, V)); \
|
||||
bool PFX##_empty(SNAME *_stack_); \
|
||||
bool PFX##_full(SNAME *_stack_); \
|
||||
size_t PFX##_count(SNAME *_stack_); \
|
||||
size_t PFX##_capacity(SNAME *_stack_); \
|
||||
/* Collection Utility */ \
|
||||
SNAME *PFX##_copy_of(SNAME *_stack_, V (*copy_func)(V)); \
|
||||
bool PFX##_equals(SNAME *_stack1_, SNAME *_stack2_, int (*comparator)(V, V)); \
|
||||
cmc_string PFX##_to_string(SNAME *_stack_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index); \
|
||||
/* Iterator Access */ \
|
||||
V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* Default Value */ \
|
||||
static inline V PFX##_impl_default_value(void) \
|
||||
{ \
|
||||
V _empty_value_; \
|
||||
\
|
||||
memset(&_empty_value_, 0, sizeof(V)); \
|
||||
\
|
||||
return _empty_value_; \
|
||||
} \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define CMC_GENERATE_STACK_SOURCE(PFX, SNAME, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
static bool PFX##_impl_grow(SNAME *_stack_); \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_stack_); \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_stack_); \
|
||||
\
|
||||
SNAME *PFX##_new(size_t capacity) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
\
|
||||
SNAME *_stack_ = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!_stack_) \
|
||||
return NULL; \
|
||||
\
|
||||
_stack_->buffer = malloc(sizeof(V) * capacity); \
|
||||
\
|
||||
if (!_stack_->buffer) \
|
||||
{ \
|
||||
free(_stack_); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_stack_->buffer, 0, sizeof(V) * capacity); \
|
||||
\
|
||||
_stack_->capacity = capacity; \
|
||||
_stack_->count = 0; \
|
||||
\
|
||||
_stack_->it_start = PFX##_impl_it_start; \
|
||||
_stack_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _stack_; \
|
||||
} \
|
||||
\
|
||||
void PFX##_clear(SNAME *_stack_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _stack_->count; i++) \
|
||||
deallocator(_stack_->buffer[i]); \
|
||||
} \
|
||||
\
|
||||
memset(_stack_->buffer, 0, sizeof(V) * _stack_->capacity); \
|
||||
\
|
||||
_stack_->count = 0; \
|
||||
} \
|
||||
\
|
||||
void PFX##_free(SNAME *_stack_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
free(_stack_->buffer); \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _stack_->count; i++) \
|
||||
deallocator(_stack_->buffer[i]); \
|
||||
} \
|
||||
\
|
||||
free(_stack_); \
|
||||
} \
|
||||
\
|
||||
bool PFX##_push(SNAME *_stack_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_stack_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_stack_)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
_stack_->buffer[_stack_->count++] = element; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_pop(SNAME *_stack_) \
|
||||
{ \
|
||||
if (PFX##_empty(_stack_)) \
|
||||
return false; \
|
||||
\
|
||||
_stack_->buffer[--_stack_->count] = PFX##_impl_default_value(); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_push_if(SNAME *_stack_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_push(_stack_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_pop_if(SNAME *_stack_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_pop(_stack_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
V PFX##_top(SNAME *_stack_) \
|
||||
{ \
|
||||
if (PFX##_empty(_stack_)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _stack_->buffer[_stack_->count - 1]; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_contains(SNAME *_stack_, V element, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _stack_->count; i++) \
|
||||
{ \
|
||||
if (comparator(_stack_->buffer[i], element) == 0) \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_empty(SNAME *_stack_) \
|
||||
{ \
|
||||
return _stack_->count == 0; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_full(SNAME *_stack_) \
|
||||
{ \
|
||||
return _stack_->count >= _stack_->capacity; \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_count(SNAME *_stack_) \
|
||||
{ \
|
||||
return _stack_->count; \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_capacity(SNAME *_stack_) \
|
||||
{ \
|
||||
return _stack_->capacity; \
|
||||
} \
|
||||
\
|
||||
SNAME *PFX##_copy_of(SNAME *_stack_, V (*copy_func)(V)) \
|
||||
{ \
|
||||
SNAME *result = PFX##_new(_stack_->capacity); \
|
||||
\
|
||||
if (!result) \
|
||||
return NULL; \
|
||||
\
|
||||
if (copy_func) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _stack_->count; i++) \
|
||||
result->buffer[i] = copy_func(_stack_->buffer[i]); \
|
||||
} \
|
||||
else \
|
||||
memcpy(result->buffer, _stack_->buffer, sizeof(V) * _stack_->count); \
|
||||
\
|
||||
result->count = _stack_->count; \
|
||||
\
|
||||
return result; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_equals(SNAME *_stack1_, SNAME *_stack2_, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
if (PFX##_count(_stack1_) != PFX##_count(_stack2_)) \
|
||||
return false; \
|
||||
\
|
||||
for (size_t i = 0; i < PFX##_count(_stack1_); i++) \
|
||||
{ \
|
||||
if (comparator(_stack1_->buffer[i], _stack2_->buffer[i]) != 0) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
cmc_string PFX##_to_string(SNAME *_stack_) \
|
||||
{ \
|
||||
cmc_string str; \
|
||||
SNAME *s_ = _stack_; \
|
||||
const char *name = #SNAME; \
|
||||
\
|
||||
snprintf(str.s, cmc_string_len, cmc_string_fmt_stack, \
|
||||
name, s_, s_->buffer, s_->capacity, s_->count); \
|
||||
\
|
||||
return str; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = PFX##_empty(target) ? 0 : iter->target->count - 1; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = iter->target->count - 1; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->cursor < steps) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor -= steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->cursor + steps >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor += steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator was able to be positioned at the given */ \
|
||||
/* index */ \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index) \
|
||||
{ \
|
||||
if (index >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor > index) \
|
||||
return PFX##_iter_rewind(iter, iter->cursor - index); \
|
||||
else if (iter->cursor < index) \
|
||||
return PFX##_iter_advance(iter, index - iter->cursor); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return NULL; \
|
||||
\
|
||||
return &(iter->target->buffer[iter->cursor]); \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return 0; \
|
||||
\
|
||||
return iter->target->count - 1 - iter->cursor; \
|
||||
} \
|
||||
\
|
||||
static bool PFX##_impl_grow(SNAME *_stack_) \
|
||||
{ \
|
||||
size_t new_capacity = _stack_->capacity * 2; \
|
||||
\
|
||||
V *new_buffer = realloc(_stack_->buffer, sizeof(V) * new_capacity); \
|
||||
\
|
||||
if (!new_buffer) \
|
||||
return false; \
|
||||
\
|
||||
_stack_->buffer = new_buffer; \
|
||||
_stack_->capacity = new_capacity; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_stack_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _stack_); \
|
||||
PFX##_iter_to_start(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_stack_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _stack_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_STACK_H */
|
||||
+1055
File diff suppressed because it is too large.
Load diff
+1181
File diff suppressed because it is too large.
Load diff
-487
@@ -1,487 +0,0 @@
|
||||
/**
|
||||
* deque.h
|
||||
*
|
||||
* Creation Date: 20/03/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/*****************************************************************************/
|
||||
/********************************************************************* DEQUE */
|
||||
/*****************************************************************************/
|
||||
|
||||
#ifndef CMC_DEQUE_H
|
||||
#define CMC_DEQUE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DEQUE_GENERATE(PFX, SNAME, FMOD, V) \
|
||||
DEQUE_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
DEQUE_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
DEQUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
|
||||
/* PRIVATE *******************************************************************/
|
||||
#define DEQUE_GENERATE_HEADER_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
DEQUE_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define DEQUE_GENERATE_SOURCE_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
DEQUE_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
DEQUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* PUBLIC ********************************************************************/
|
||||
#define DEQUE_GENERATE_HEADER_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
DEQUE_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
DEQUE_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define DEQUE_GENERATE_SOURCE_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
DEQUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* STRUCT ********************************************************************/
|
||||
#define DEQUE_GENERATE_STRUCT(PFX, SNAME, FMOD, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define DEQUE_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
/* Deque Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic circular array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current circular array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Index representing the front of the deque */ \
|
||||
size_t front; \
|
||||
\
|
||||
/* Index representing the back of the deque */ \
|
||||
size_t back; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the deque */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the deque */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Deque Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target deque */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* Keeps track of relative index to the iteration of elements */ \
|
||||
size_t index; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME *PFX##_new(size_t size); \
|
||||
FMOD void PFX##_clear(SNAME *_deque_); \
|
||||
FMOD void PFX##_free(SNAME *_deque_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_push_front(SNAME *_deque_, V element); \
|
||||
FMOD bool PFX##_push_back(SNAME *_deque_, V element); \
|
||||
FMOD bool PFX##_pop_front(SNAME *_deque_); \
|
||||
FMOD bool PFX##_pop_back(SNAME *_deque_); \
|
||||
/* Conditional Input and Output */ \
|
||||
FMOD bool PFX##_push_front_if(SNAME *_deque_, V element, bool condition); \
|
||||
FMOD bool PFX##_push_back_if(SNAME *_deque_, V element, bool condition); \
|
||||
FMOD bool PFX##_pop_front_if(SNAME *_deque_, bool condition); \
|
||||
FMOD bool PFX##_pop_back_if(SNAME *_deque_, bool condition); \
|
||||
/* Element Access */ \
|
||||
FMOD V PFX##_front(SNAME *_deque_); \
|
||||
FMOD V PFX##_back(SNAME *_deque_); \
|
||||
/* Collection State */ \
|
||||
FMOD bool PFX##_contains(SNAME *_deque_, V element, int (*comparator)(V, V)); \
|
||||
FMOD bool PFX##_empty(SNAME *_deque_); \
|
||||
FMOD bool PFX##_full(SNAME *_deque_); \
|
||||
FMOD size_t PFX##_count(SNAME *_deque_); \
|
||||
FMOD size_t PFX##_capacity(SNAME *_deque_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
/* Iterator Access */ \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define DEQUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
FMOD bool PFX##_impl_grow(SNAME *_deque_); \
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_deque_); \
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_deque_); \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(size_t size) \
|
||||
{ \
|
||||
if (size < 1) \
|
||||
return NULL; \
|
||||
\
|
||||
SNAME *_deque_ = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!_deque_) \
|
||||
return NULL; \
|
||||
\
|
||||
_deque_->buffer = malloc(sizeof(V) * size); \
|
||||
\
|
||||
if (!_deque_->buffer) \
|
||||
{ \
|
||||
free(_deque_); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_deque_->buffer, 0, sizeof(V) * size); \
|
||||
\
|
||||
_deque_->capacity = size; \
|
||||
_deque_->count = 0; \
|
||||
_deque_->front = 0; \
|
||||
_deque_->back = 0; \
|
||||
\
|
||||
_deque_->it_start = PFX##_impl_it_start; \
|
||||
_deque_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _deque_; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_clear(SNAME *_deque_) \
|
||||
{ \
|
||||
memset(_deque_->buffer, 0, sizeof(V) * _deque_->capacity); \
|
||||
\
|
||||
_deque_->count = 0; \
|
||||
_deque_->front = 0; \
|
||||
_deque_->back = 0; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_free(SNAME *_deque_) \
|
||||
{ \
|
||||
free(_deque_->buffer); \
|
||||
free(_deque_); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_front(SNAME *_deque_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_deque_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_deque_)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
_deque_->front = (_deque_->front == 0) ? _deque_->capacity - 1 : _deque_->front - 1; \
|
||||
\
|
||||
_deque_->buffer[_deque_->front] = element; \
|
||||
\
|
||||
_deque_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_back(SNAME *_deque_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_deque_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_deque_)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
_deque_->buffer[_deque_->back] = element; \
|
||||
\
|
||||
_deque_->back = (_deque_->back == _deque_->capacity - 1) ? 0 : _deque_->back + 1; \
|
||||
\
|
||||
_deque_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_front(SNAME *_deque_) \
|
||||
{ \
|
||||
if (PFX##_empty(_deque_)) \
|
||||
return false; \
|
||||
\
|
||||
_deque_->buffer[_deque_->front] = 0; \
|
||||
\
|
||||
_deque_->front = (_deque_->front == _deque_->capacity - 1) ? 0 : _deque_->front + 1; \
|
||||
\
|
||||
_deque_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_back(SNAME *_deque_) \
|
||||
{ \
|
||||
if (PFX##_empty(_deque_)) \
|
||||
return false; \
|
||||
\
|
||||
_deque_->back = (_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1; \
|
||||
\
|
||||
_deque_->buffer[_deque_->back] = 0; \
|
||||
\
|
||||
_deque_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_front_if(SNAME *_deque_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_push_front(_deque_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_back_if(SNAME *_deque_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_push_back(_deque_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_front_if(SNAME *_deque_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_pop_front(_deque_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_back_if(SNAME *_deque_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_pop_back(_deque_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_front(SNAME *_deque_) \
|
||||
{ \
|
||||
if (PFX##_empty(_deque_)) \
|
||||
return 0; \
|
||||
\
|
||||
return _deque_->buffer[_deque_->front]; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_back(SNAME *_deque_) \
|
||||
{ \
|
||||
if (PFX##_empty(_deque_)) \
|
||||
return 0; \
|
||||
\
|
||||
return _deque_->buffer[(_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1]; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_contains(SNAME *_deque_, V element, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->count, j++) \
|
||||
{ \
|
||||
if (comparator(_deque_->buffer[i], element) == 0) \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_empty(SNAME *_deque_) \
|
||||
{ \
|
||||
return _deque_->count == 0; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_full(SNAME *_deque_) \
|
||||
{ \
|
||||
return _deque_->count >= _deque_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_count(SNAME *_deque_) \
|
||||
{ \
|
||||
return _deque_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_capacity(SNAME *_deque_) \
|
||||
{ \
|
||||
return _deque_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
iter->cursor = 0; \
|
||||
else \
|
||||
iter->cursor = (iter->target->back == 0) ? iter->target->capacity - 1 : iter->target->back - 1; \
|
||||
\
|
||||
iter->index = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->index == iter->target->count - 1) \
|
||||
iter->end = true; \
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = (iter->cursor + 1) % (iter->target->capacity); \
|
||||
iter->index++; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->index == 0) \
|
||||
iter->start = true; \
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = (iter->cursor == 0) ? iter->target->capacity - 1 : iter->cursor - 1; \
|
||||
iter->index--; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return 0; \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return NULL; \
|
||||
\
|
||||
return &(iter->target->buffer[iter->cursor]); \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->index; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_impl_grow(SNAME *_deque_) \
|
||||
{ \
|
||||
\
|
||||
size_t new_capacity = _deque_->capacity * 2; \
|
||||
\
|
||||
V *new_buffer = malloc(sizeof(V) * new_capacity); \
|
||||
\
|
||||
if (!new_buffer) \
|
||||
return false; \
|
||||
\
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->capacity, j++) \
|
||||
{ \
|
||||
new_buffer[j] = _deque_->buffer[i]; \
|
||||
} \
|
||||
\
|
||||
free(_deque_->buffer); \
|
||||
\
|
||||
_deque_->buffer = new_buffer; \
|
||||
_deque_->capacity = new_capacity; \
|
||||
_deque_->front = 0; \
|
||||
_deque_->back = _deque_->count; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_deque_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _deque_); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_deque_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _deque_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_DEQUE_H */
|
||||
+654
@@ -0,0 +1,654 @@
|
||||
/**
|
||||
* deque.h
|
||||
*
|
||||
* Creation Date: 21/06/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/*****************************************************************************/
|
||||
/********************************************************************* DEQUE */
|
||||
/*****************************************************************************/
|
||||
|
||||
#ifndef CMC_DEQUE_H
|
||||
#define CMC_DEQUE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
#include "utl/log.h"
|
||||
|
||||
#define DEQUE_GENERATE(PFX, SNAME, FMOD, V) \
|
||||
DEQUE_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
DEQUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
|
||||
#define DEQUE_WRAPGEN_HEADER(PFX, SNAME, FMOD, K, V) \
|
||||
DEQUE_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
|
||||
#define DEQUE_WRAPGEN_SOURCE(PFX, SNAME, FMOD, K, V) \
|
||||
DEQUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define DEQUE_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
/* Deque Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic circular array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current circular array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Index representing the front of the deque */ \
|
||||
size_t front; \
|
||||
\
|
||||
/* Index representing the back of the deque */ \
|
||||
size_t back; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the deque */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the deque */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Deque Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target deque */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* Keeps track of relative index to the iteration of elements */ \
|
||||
size_t index; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME *PFX##_new(size_t capacity); \
|
||||
FMOD void PFX##_clear(SNAME *_deque_); \
|
||||
FMOD void PFX##_free(SNAME *_deque_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_push_front(SNAME *_deque_, V element); \
|
||||
FMOD bool PFX##_push_back(SNAME *_deque_, V element); \
|
||||
FMOD bool PFX##_pop_front(SNAME *_deque_); \
|
||||
FMOD bool PFX##_pop_back(SNAME *_deque_); \
|
||||
/* Conditional Input and Output */ \
|
||||
FMOD bool PFX##_push_front_if(SNAME *_deque_, V element, bool condition); \
|
||||
FMOD bool PFX##_push_back_if(SNAME *_deque_, V element, bool condition); \
|
||||
FMOD bool PFX##_pop_front_if(SNAME *_deque_, bool condition); \
|
||||
FMOD bool PFX##_pop_back_if(SNAME *_deque_, bool condition); \
|
||||
/* Element Access */ \
|
||||
FMOD V PFX##_front(SNAME *_deque_); \
|
||||
FMOD V PFX##_back(SNAME *_deque_); \
|
||||
/* Collection State */ \
|
||||
FMOD bool PFX##_contains(SNAME *_deque_, V element, int (*comparator)(V, V)); \
|
||||
FMOD bool PFX##_empty(SNAME *_deque_); \
|
||||
FMOD bool PFX##_full(SNAME *_deque_); \
|
||||
FMOD size_t PFX##_count(SNAME *_deque_); \
|
||||
FMOD size_t PFX##_capacity(SNAME *_deque_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
/* Iterator Access */ \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* Default Value */ \
|
||||
static inline V PFX##_impl_default_value(void) \
|
||||
{ \
|
||||
V _empty_value_; \
|
||||
\
|
||||
memset(&_empty_value_, 0, sizeof(V)); \
|
||||
\
|
||||
return _empty_value_; \
|
||||
} \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define DEQUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
static bool PFX##_impl_grow(SNAME *_deque_); \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_deque_); \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_deque_); \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(size_t capacity) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
{ \
|
||||
log_warn("Invalid capacity for %s given: %s", "Heap", capacity); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
SNAME *_deque_ = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!_deque_) \
|
||||
{ \
|
||||
log_info("%s allocation failed", "Deque"); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
_deque_->buffer = malloc(sizeof(V) * capacity); \
|
||||
\
|
||||
if (!_deque_->buffer) \
|
||||
{ \
|
||||
log_info("%s allocation failed", "Deque buffer"); \
|
||||
free(_deque_); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_deque_->buffer, 0, sizeof(V) * capacity); \
|
||||
\
|
||||
_deque_->capacity = capacity; \
|
||||
_deque_->count = 0; \
|
||||
_deque_->front = 0; \
|
||||
_deque_->back = 0; \
|
||||
\
|
||||
_deque_->it_start = PFX##_impl_it_start; \
|
||||
_deque_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
log_info("Deque at %p successfully allocated", _deque_); \
|
||||
\
|
||||
return _deque_; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_clear(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
memset(_deque_->buffer, 0, sizeof(V) * _deque_->capacity); \
|
||||
\
|
||||
_deque_->count = 0; \
|
||||
_deque_->front = 0; \
|
||||
_deque_->back = 0; \
|
||||
\
|
||||
log_trace("Deque at %p cleared", _deque_); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_free(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
free(_deque_->buffer); \
|
||||
\
|
||||
uintptr_t address = (uintptr_t)(void *)_deque_; \
|
||||
\
|
||||
free(_deque_); \
|
||||
\
|
||||
log_info("Heap at %p successfully deallocated", address); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_front(SNAME *_deque_, V element) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
if (PFX##_full(_deque_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_deque_)) \
|
||||
{ \
|
||||
log_info("Deque at %p failed to be reallocated", _deque_); \
|
||||
return false; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
_deque_->front = (_deque_->front == 0) ? _deque_->capacity - 1 : _deque_->front - 1; \
|
||||
\
|
||||
_deque_->buffer[_deque_->front] = element; \
|
||||
\
|
||||
log_trace("Element added at %" PRIuMAX " to Deque at %p", _deque_->front, _deque_); \
|
||||
\
|
||||
_deque_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_back(SNAME *_deque_, V element) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
if (PFX##_full(_deque_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_deque_)) \
|
||||
{ \
|
||||
log_info("Deque at %p failed to be reallocated", _deque_); \
|
||||
return false; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
_deque_->buffer[_deque_->back] = element; \
|
||||
\
|
||||
log_trace("Element added at %" PRIuMAX " to Deque at %p", _deque_->back, _deque_); \
|
||||
\
|
||||
_deque_->back = (_deque_->back == _deque_->capacity - 1) ? 0 : _deque_->back + 1; \
|
||||
\
|
||||
_deque_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_front(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
if (PFX##_empty(_deque_)) \
|
||||
{ \
|
||||
log_debug("Attempt of removing an element from empty Deque at %p", _deque_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
_deque_->buffer[_deque_->front] = PFX##_impl_default_value(); \
|
||||
\
|
||||
log_trace("Element removed from %" PRIuMAX " to Deque at %p", _deque_->front, _deque_); \
|
||||
\
|
||||
_deque_->front = (_deque_->front == _deque_->capacity - 1) ? 0 : _deque_->front + 1; \
|
||||
\
|
||||
_deque_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_back(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
{ \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
} \
|
||||
\
|
||||
if (PFX##_empty(_deque_)) \
|
||||
{ \
|
||||
log_debug("Attempt of removing an element from empty Deque at %p", _deque_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
_deque_->back = (_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1; \
|
||||
\
|
||||
_deque_->buffer[_deque_->back] = PFX##_impl_default_value(); \
|
||||
\
|
||||
log_trace("Element removed from %" PRIuMAX " to Deque at %p", _deque_->back, _deque_); \
|
||||
\
|
||||
_deque_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_front_if(SNAME *_deque_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_push_front(_deque_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push_back_if(SNAME *_deque_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_push_back(_deque_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_front_if(SNAME *_deque_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_pop_front(_deque_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_pop_back_if(SNAME *_deque_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_pop_back(_deque_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_front(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
if (PFX##_empty(_deque_)) \
|
||||
{ \
|
||||
log_debug("Attempt to access element from empty Deque at %p", _deque_); \
|
||||
\
|
||||
PFX##_impl_default_value(); \
|
||||
} \
|
||||
\
|
||||
log_trace("Element access at %" PRIuMAX "", _deque_->front); \
|
||||
\
|
||||
return _deque_->buffer[_deque_->front]; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_back(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
if (PFX##_empty(_deque_)) \
|
||||
{ \
|
||||
log_debug("Attempt to access element from empty Deque at %p", _deque_); \
|
||||
\
|
||||
PFX##_impl_default_value(); \
|
||||
} \
|
||||
\
|
||||
size_t index = (_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1; \
|
||||
\
|
||||
log_trace("Element access at %" PRIuMAX "", index); \
|
||||
\
|
||||
return _deque_->buffer[index]; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_contains(SNAME *_deque_, V element, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
if (comparator == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "comparator"); \
|
||||
\
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->count, j++) \
|
||||
{ \
|
||||
if (comparator(_deque_->buffer[i], element) == 0) \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_empty(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
return _deque_->count == 0; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_full(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
return _deque_->count >= _deque_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_count(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
return _deque_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_capacity(SNAME *_deque_) \
|
||||
{ \
|
||||
if (_deque_ == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "_deque_"); \
|
||||
\
|
||||
return _deque_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
{ \
|
||||
log_info("%s allocation failed", "Deque Iterator"); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
log_info("Deque Iterator allocated at %p referencing Heap at %p", iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
uintptr_t address = (uintptr_t)(void *)iter; \
|
||||
\
|
||||
free(iter); \
|
||||
\
|
||||
log_info("Deque Iterator deallocated at %" PRIxPTR "", address); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
if (target == NULL) \
|
||||
log_fatal("Heap Iterator at %p initialized with null pointer target", iter); \
|
||||
\
|
||||
if (PFX##_empty(target)) \
|
||||
log_warn("Deque Iterator at %p initialized with empty target", iter); \
|
||||
\
|
||||
iter->target = target; \
|
||||
iter->cursor = target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "iter"); \
|
||||
\
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "iter"); \
|
||||
\
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "iter"); \
|
||||
\
|
||||
iter->cursor = iter->target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
log_trace("Deque Iterator at %p with cursor at %" PRIuMAX "", iter, iter->cursor); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "iter"); \
|
||||
\
|
||||
if (PFX##_empty(iter->target)) \
|
||||
iter->cursor = 0; \
|
||||
else \
|
||||
iter->cursor = (iter->target->back == 0) ? iter->target->capacity - 1 : iter->target->back - 1; \
|
||||
\
|
||||
iter->index = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
\
|
||||
log_trace("Deque Iterator at %p with cursor at %" PRIuMAX "", iter, iter->cursor); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "iter"); \
|
||||
\
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->index == iter->target->count - 1) \
|
||||
{ \
|
||||
log_trace("Deque Iterator at %p reached the end of Deque %p", iter, iter->target); \
|
||||
iter->end = true; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = (iter->cursor + 1) % (iter->target->capacity); \
|
||||
iter->index++; \
|
||||
\
|
||||
log_trace("Deque Iterator at %p moved to %" PRIuMAX "", iter, iter->cursor); \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "iter"); \
|
||||
\
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->index == 0) \
|
||||
{ \
|
||||
log_trace("Deque Iterator at %p reached the start of Deque %p", iter, iter->target); \
|
||||
iter->start = true; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = (iter->cursor == 0) ? iter->target->capacity - 1 : iter->cursor - 1; \
|
||||
iter->index--; \
|
||||
\
|
||||
log_trace("Deque Iterator at %p moved to %" PRIuMAX "", iter, iter->cursor); \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "iter"); \
|
||||
\
|
||||
if (PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
log_debug("Attempt to access element from iterator %p with empty target %p", iter, iter->target); \
|
||||
PFX##_impl_default_value(); \
|
||||
} \
|
||||
\
|
||||
log_trace("Element access at %" PRIuMAX "", iter->cursor); \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "iter"); \
|
||||
\
|
||||
if (PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
log_debug("Attempt to access element from iterator %p with empty target %p", iter, iter->target); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
log_trace("Element access at %" PRIuMAX "", iter->cursor); \
|
||||
\
|
||||
return &(iter->target->buffer[iter->cursor]); \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter == NULL) \
|
||||
log_fatal("Invalid null pointer parameter: %s", "iter"); \
|
||||
\
|
||||
return iter->index; \
|
||||
} \
|
||||
\
|
||||
static bool PFX##_impl_grow(SNAME *_deque_) \
|
||||
{ \
|
||||
size_t new_capacity = _deque_->capacity * 2; \
|
||||
\
|
||||
V *new_buffer = malloc(sizeof(V) * new_capacity); \
|
||||
\
|
||||
if (!new_buffer) \
|
||||
{ \
|
||||
log_info("%s reallocation failed", "Deque buffer"); \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->capacity, j++) \
|
||||
{ \
|
||||
new_buffer[j] = _deque_->buffer[i]; \
|
||||
} \
|
||||
\
|
||||
free(_deque_->buffer); \
|
||||
\
|
||||
_deque_->buffer = new_buffer; \
|
||||
_deque_->capacity = new_capacity; \
|
||||
_deque_->front = 0; \
|
||||
_deque_->back = _deque_->count; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_deque_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _deque_); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_deque_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _deque_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_DEQUE_H */
|
||||
@@ -0,0 +1,977 @@
|
||||
/**
|
||||
* intervalheap.h
|
||||
*
|
||||
* Creation Date: 06/07/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* An interval heap is a specialized heap with:
|
||||
*
|
||||
* - O(1) - Find Min
|
||||
* - O(1) - Find Max
|
||||
* - O(log n) - Insert
|
||||
* - O(log n) - Remove Min
|
||||
* - O(log n) - Remove Max
|
||||
*/
|
||||
|
||||
#ifndef CMC_INTERVALHEAP_H
|
||||
#define CMC_INTERVALHEAP_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../utl/cmc_string.h"
|
||||
|
||||
#define CMC_GENERATE_INTERVALHEAP(PFX, SNAME, V) \
|
||||
CMC_GENERATE_INTERVALHEAP_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_INTERVALHEAP_SOURCE(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_INTERVALHEAP_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_INTERVALHEAP_HEADER(PFX, SNAME, V)
|
||||
|
||||
#define CMC_WRAPGEN_INTERVALHEAP_SOURCE(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_INTERVALHEAP_SOURCE(PFX, SNAME, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define CMC_GENERATE_INTERVALHEAP_HEADER(PFX, SNAME, V) \
|
||||
\
|
||||
/* Heap Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic array of nodes */ \
|
||||
struct SNAME##_node_s *buffer; \
|
||||
\
|
||||
/* Current array capacity (how many nodes can be stored) */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of nodes in the dynamic array */ \
|
||||
size_t size; \
|
||||
\
|
||||
/* Current amount of elements in the heap */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Element comparison function */ \
|
||||
int (*cmp)(V, V); \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the heap */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the heap */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Heap Node */ \
|
||||
typedef struct SNAME##_node_s \
|
||||
{ \
|
||||
/* 0 - Value belonging to the MinHeap */ \
|
||||
/* 1 - Value belonging to the MaxHeap */ \
|
||||
V data[2]; \
|
||||
\
|
||||
} SNAME##_node, *SNAME##_node_ptr; \
|
||||
\
|
||||
/* Heap Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target heap */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
SNAME *PFX##_new(size_t capacity, int (*compare)(V, V)); \
|
||||
void PFX##_clear(SNAME *_heap_, void (*deallocator)(V)); \
|
||||
void PFX##_free(SNAME *_heap_, void (*deallocator)(V)); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_insert(SNAME *_heap_, V element); \
|
||||
bool PFX##_remove_max(SNAME *_heap_, V *result); \
|
||||
bool PFX##_remove_min(SNAME *_heap_, V *result); \
|
||||
/* Conditional Input and Output */ \
|
||||
bool PFX##_insert_if(SNAME *_heap_, V element, bool condition); \
|
||||
bool PFX##_remove_max_if(SNAME *_heap_, V *result, bool condition); \
|
||||
bool PFX##_remove_min_if(SNAME *_heap_, V *result, bool condition); \
|
||||
/* Collection Update */ \
|
||||
bool PFX##_update_max(SNAME *_heap_, V element); \
|
||||
bool PFX##_update_min(SNAME *_heap_, V element); \
|
||||
/* Element Access */ \
|
||||
bool PFX##_max(SNAME *_heap_, V *value); \
|
||||
bool PFX##_min(SNAME *_heap_, V *value); \
|
||||
/* Collection State */ \
|
||||
bool PFX##_contains(SNAME *_heap_, V element); \
|
||||
bool PFX##_empty(SNAME *_heap_); \
|
||||
bool PFX##_full(SNAME *_heap_); \
|
||||
size_t PFX##_count(SNAME *_heap_); \
|
||||
size_t PFX##_capacity(SNAME *_heap_); \
|
||||
/* Collection Utility */ \
|
||||
SNAME *PFX##_copy_of(SNAME *_set_, V (*copy_func)(V)); \
|
||||
bool PFX##_equals(SNAME *_heap1_, SNAME *_heap2_); \
|
||||
cmc_string PFX##_to_string(SNAME *_heap_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps); \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index); \
|
||||
/* Iterator Access */ \
|
||||
V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* Default Value */ \
|
||||
static inline V PFX##_impl_default_value(void) \
|
||||
{ \
|
||||
V _empty_value_; \
|
||||
\
|
||||
memset(&_empty_value_, 0, sizeof(V)); \
|
||||
\
|
||||
return _empty_value_; \
|
||||
} \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define CMC_GENERATE_INTERVALHEAP_SOURCE(PFX, SNAME, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
static bool PFX##_impl_grow(SNAME *_heap_); \
|
||||
static void PFX##_impl_float_up_max(SNAME *_heap_); \
|
||||
static void PFX##_impl_float_up_min(SNAME *_heap_); \
|
||||
static void PFX##_impl_float_down_max(SNAME *_heap_); \
|
||||
static void PFX##_impl_float_down_min(SNAME *_heap_); \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_heap_); \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_heap_); \
|
||||
\
|
||||
SNAME *PFX##_new(size_t capacity, int (*compare)(V, V)) \
|
||||
{ \
|
||||
SNAME *_heap_ = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!_heap_) \
|
||||
return NULL; \
|
||||
\
|
||||
/* Since each node can store two elements, divide the actual capacity by 2 */ \
|
||||
/* Round the capacity of nodes up */ \
|
||||
capacity = capacity % 2 == 0 ? capacity / 2 : (capacity + 1) / 2; \
|
||||
\
|
||||
_heap_->buffer = malloc(sizeof(SNAME##_node) * capacity); \
|
||||
\
|
||||
if (!_heap_->buffer) \
|
||||
{ \
|
||||
free(_heap_); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_heap_->buffer, 0, sizeof(SNAME##_node) * capacity); \
|
||||
\
|
||||
_heap_->capacity = capacity; \
|
||||
_heap_->size = 0; \
|
||||
_heap_->count = 0; \
|
||||
_heap_->cmp = compare; \
|
||||
\
|
||||
_heap_->it_start = PFX##_impl_it_start; \
|
||||
_heap_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _heap_; \
|
||||
} \
|
||||
\
|
||||
void PFX##_clear(SNAME *_heap_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _heap_->count; i++) \
|
||||
{ \
|
||||
deallocator(_heap_->buffer[i / 2].data[i % 2]); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
memset(_heap_->buffer, 0, sizeof(V) * _heap_->capacity); \
|
||||
\
|
||||
_heap_->size = 0; \
|
||||
_heap_->count = 0; \
|
||||
} \
|
||||
\
|
||||
void PFX##_free(SNAME *_heap_, void (*deallocator)(V)) \
|
||||
{ \
|
||||
if (deallocator) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _heap_->count; i++) \
|
||||
{ \
|
||||
deallocator(_heap_->buffer[i / 2].data[i % 2]); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
free(_heap_->buffer); \
|
||||
\
|
||||
free(_heap_); \
|
||||
} \
|
||||
\
|
||||
bool PFX##_insert(SNAME *_heap_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_heap_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_heap_)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (PFX##_count(_heap_) % 2 == 0) \
|
||||
{ \
|
||||
/* Occupying a new node */ \
|
||||
_heap_->buffer[_heap_->size].data[0] = element; \
|
||||
_heap_->buffer[_heap_->size].data[1] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_heap_->size++; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
SNAME##_node *curr_node = &(_heap_->buffer[_heap_->size - 1]); \
|
||||
\
|
||||
/* Decide if the new element goes into the MinHeap or MaxHeap */ \
|
||||
if (_heap_->cmp(curr_node->data[0], element) > 0) \
|
||||
{ \
|
||||
/* Swap current value and add new element to the MinHeap */ \
|
||||
curr_node->data[1] = curr_node->data[0]; \
|
||||
curr_node->data[0] = element; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
/* No need to swap and add the new element to the MaxHeap */ \
|
||||
curr_node->data[1] = element; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
_heap_->count++; \
|
||||
\
|
||||
if (PFX##_count(_heap_) <= 2) \
|
||||
return true; \
|
||||
\
|
||||
/* Determine wheather to do a MaxHeap insert or a MinHeap insert */ \
|
||||
SNAME##_node *parent = &(_heap_->buffer[(_heap_->size - 1) / 2]); \
|
||||
\
|
||||
if (_heap_->cmp(parent->data[0], element) > 0) \
|
||||
PFX##_impl_float_up_min(_heap_); \
|
||||
else if (_heap_->cmp(parent->data[1], element) < 0) \
|
||||
PFX##_impl_float_up_max(_heap_); \
|
||||
/* else no float up required */ \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_remove_max(SNAME *_heap_, V *result) \
|
||||
{ \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
return false; \
|
||||
\
|
||||
if (PFX##_count(_heap_) == 1) \
|
||||
{ \
|
||||
*result = _heap_->buffer[0].data[0]; \
|
||||
\
|
||||
_heap_->buffer[0].data[0] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_heap_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
else \
|
||||
*result = _heap_->buffer[0].data[1]; \
|
||||
\
|
||||
/* Swap the result with the last element in the MaxHeap and float it down */ \
|
||||
SNAME##_node *last_node = &(_heap_->buffer[_heap_->size - 1]); \
|
||||
\
|
||||
if (PFX##_count(_heap_) % 2 == 1) \
|
||||
{ \
|
||||
/* Grab from MinHeap and discard last node */ \
|
||||
_heap_->buffer[0].data[1] = last_node->data[0]; \
|
||||
\
|
||||
last_node->data[0] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_heap_->size--; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
_heap_->buffer[0].data[1] = last_node->data[1]; \
|
||||
\
|
||||
last_node->data[1] = PFX##_impl_default_value(); \
|
||||
} \
|
||||
\
|
||||
_heap_->count--; \
|
||||
\
|
||||
/* FLoat Down on the MaxHeap */ \
|
||||
PFX##_impl_float_down_max(_heap_); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_remove_min(SNAME *_heap_, V *result) \
|
||||
{ \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
return false; \
|
||||
\
|
||||
*result = _heap_->buffer[0].data[0]; \
|
||||
\
|
||||
if (PFX##_count(_heap_) == 1) \
|
||||
{ \
|
||||
_heap_->buffer[0].data[0] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_heap_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Swap the result with the last element in the MinHeap and float it down */ \
|
||||
SNAME##_node *last_node = &(_heap_->buffer[_heap_->size - 1]); \
|
||||
\
|
||||
_heap_->buffer[0].data[0] = last_node->data[0]; \
|
||||
\
|
||||
if (PFX##_count(_heap_) % 2 == 1) \
|
||||
{ \
|
||||
/* Discard last node */ \
|
||||
last_node->data[0] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_heap_->size--; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
/* Place the MaxHeap value in the MinHeap value spot */ \
|
||||
last_node->data[0] = last_node->data[1]; \
|
||||
last_node->data[1] = PFX##_impl_default_value(); \
|
||||
} \
|
||||
\
|
||||
_heap_->count--; \
|
||||
\
|
||||
/* FLoat Down on the MinHeap */ \
|
||||
PFX##_impl_float_down_min(_heap_); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_insert_if(SNAME *_heap_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_insert(_heap_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_remove_max_if(SNAME *_heap_, V *result, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_remove_max(_heap_, result); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_remove_min_if(SNAME *_heap_, V *result, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_remove_min(_heap_, result); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_update_max(SNAME *_heap_, V element) \
|
||||
{ \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
return false; \
|
||||
\
|
||||
if (PFX##_count(_heap_) == 1) \
|
||||
{ \
|
||||
_heap_->buffer[0].data[0] = element; \
|
||||
} \
|
||||
else if (_heap_->cmp(element, _heap_->buffer[0].data[0]) < 0) \
|
||||
{ \
|
||||
/* Corner case: we are updating the Max value but it is less than */ \
|
||||
/* the Min value */ \
|
||||
_heap_->buffer[0].data[1] = _heap_->buffer[0].data[0]; \
|
||||
_heap_->buffer[0].data[0] = element; \
|
||||
\
|
||||
PFX##_impl_float_down_max(_heap_); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
/* Update Max element and float it down */ \
|
||||
_heap_->buffer[0].data[1] = element; \
|
||||
\
|
||||
PFX##_impl_float_down_max(_heap_); \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_update_min(SNAME *_heap_, V element) \
|
||||
{ \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
return false; \
|
||||
\
|
||||
if (PFX##_count(_heap_) == 1) \
|
||||
{ \
|
||||
_heap_->buffer[0].data[0] = element; \
|
||||
} \
|
||||
else if (_heap_->cmp(element, _heap_->buffer[0].data[1]) > 0) \
|
||||
{ \
|
||||
/* Corner case: we are updating the Min value but it is greater */ \
|
||||
/* than the Max value. */ \
|
||||
_heap_->buffer[0].data[0] = _heap_->buffer[0].data[1]; \
|
||||
_heap_->buffer[0].data[1] = element; \
|
||||
\
|
||||
PFX##_impl_float_down_min(_heap_); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
/* Update Min element and float it down */ \
|
||||
_heap_->buffer[0].data[0] = element; \
|
||||
\
|
||||
PFX##_impl_float_down_min(_heap_); \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_max(SNAME *_heap_, V *value) \
|
||||
{ \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
return false; \
|
||||
\
|
||||
/* If there is only one element, then the maximum element is the same */ \
|
||||
/* as the one in the MinHeap */ \
|
||||
if (PFX##_count(_heap_) == 1) \
|
||||
*value = _heap_->buffer[0].data[0]; \
|
||||
else \
|
||||
*value = _heap_->buffer[0].data[1]; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_min(SNAME *_heap_, V *value) \
|
||||
{ \
|
||||
if (PFX##_empty(_heap_)) \
|
||||
return false; \
|
||||
\
|
||||
*value = _heap_->buffer[0].data[0]; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_contains(SNAME *_heap_, V element) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _heap_->count; i++) \
|
||||
{ \
|
||||
if (_heap_->cmp(_heap_->buffer[i / 2].data[i % 2], element) == 0) \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_empty(SNAME *_heap_) \
|
||||
{ \
|
||||
return _heap_->count == 0; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_full(SNAME *_heap_) \
|
||||
{ \
|
||||
/* The heap is full if all nodes are completely filled */ \
|
||||
return _heap_->size >= _heap_->capacity && _heap_->count % 2 == 0; \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_count(SNAME *_heap_) \
|
||||
{ \
|
||||
return _heap_->count; \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_capacity(SNAME *_heap_) \
|
||||
{ \
|
||||
return _heap_->capacity; \
|
||||
} \
|
||||
\
|
||||
SNAME *PFX##_copy_of(SNAME *_heap_, V (*copy_func)(V)) \
|
||||
{ \
|
||||
SNAME *result = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!result) \
|
||||
return NULL; \
|
||||
\
|
||||
memcpy(result, _heap_, sizeof(SNAME)); \
|
||||
\
|
||||
result->buffer = malloc(sizeof(SNAME##_node) * _heap_->capacity); \
|
||||
\
|
||||
if (!result->buffer) \
|
||||
{ \
|
||||
free(result); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
if (copy_func) \
|
||||
{ \
|
||||
for (size_t i = 0; i < _heap_->count; i++) \
|
||||
result->buffer[i / 2].data[i % 2] = copy_func(_heap_->buffer[i / 2].data[i % 2]); \
|
||||
} \
|
||||
else \
|
||||
memcpy(result->buffer, _heap_->buffer, sizeof(SNAME##_node) * _heap_->capacity); \
|
||||
\
|
||||
return result; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_equals(SNAME *_heap1_, SNAME *_heap2_) \
|
||||
{ \
|
||||
if (PFX##_count(_heap1_) != PFX##_count(_heap2_)) \
|
||||
return false; \
|
||||
\
|
||||
for (size_t i = 0; i < _heap1_->count; i++) \
|
||||
{ \
|
||||
V element1 = _heap1_->buffer[i / 2].data[i % 2]; \
|
||||
V element2 = _heap2_->buffer[i / 2].data[i % 2]; \
|
||||
\
|
||||
if (_heap1_->cmp(element1, element2) == 0) \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
cmc_string PFX##_to_string(SNAME *_heap_) \
|
||||
{ \
|
||||
cmc_string str; \
|
||||
SNAME *h_ = _heap_; \
|
||||
const char *name = #SNAME; \
|
||||
\
|
||||
snprintf(str.s, cmc_string_len, cmc_string_fmt_intervalheap, \
|
||||
name, h_, h_->buffer, h_->capacity, h_->size, h_->count, h_->cmp); \
|
||||
\
|
||||
return str; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor + 1 == PFX##_count(iter->target)) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_advance(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->cursor + steps >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor += steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_rewind(SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
if (steps == 0 || iter->cursor < steps) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor -= steps; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
/* Returns true only if the iterator was able to be positioned at the given index */ \
|
||||
bool PFX##_iter_go_to(SNAME##_iter *iter, size_t index) \
|
||||
{ \
|
||||
if (index >= PFX##_count(iter->target)) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor > index) \
|
||||
return PFX##_iter_rewind(iter, iter->cursor - index); \
|
||||
else if (iter->cursor < index) \
|
||||
return PFX##_iter_advance(iter, index - iter->cursor); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor / 2].data[iter->cursor % 2]; \
|
||||
} \
|
||||
\
|
||||
size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->cursor; \
|
||||
} \
|
||||
\
|
||||
static bool PFX##_impl_grow(SNAME *_heap_) \
|
||||
{ \
|
||||
size_t new_cap = _heap_->capacity * 2; \
|
||||
\
|
||||
SNAME##_node *new_buffer = realloc(_heap_->buffer, sizeof(SNAME##_node) * new_cap); \
|
||||
\
|
||||
if (!new_buffer) \
|
||||
return false; \
|
||||
\
|
||||
memset(new_buffer + _heap_->capacity, 0, sizeof(SNAME##_node) * _heap_->capacity); \
|
||||
\
|
||||
_heap_->buffer = new_buffer; \
|
||||
_heap_->capacity = new_cap; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
static void PFX##_impl_float_up_max(SNAME *_heap_) \
|
||||
{ \
|
||||
size_t index = _heap_->size - 1; \
|
||||
\
|
||||
SNAME##_node *curr_node = &(_heap_->buffer[index]); \
|
||||
\
|
||||
/* FLoat Up on the MaxHeap */ \
|
||||
while (index > 0) \
|
||||
{ \
|
||||
/* Parent index */ \
|
||||
size_t P_index = (index - 1) / 2; \
|
||||
\
|
||||
SNAME##_node *parent = &(_heap_->buffer[P_index]); \
|
||||
\
|
||||
if (index == _heap_->size - 1 && PFX##_count(_heap_) % 2 != 0) \
|
||||
{ \
|
||||
/* In this case, the current node has no MaxHeap value so we */ \
|
||||
/* instead compare with the MinHeap value */ \
|
||||
if (_heap_->cmp(curr_node->data[0], parent->data[1]) < 0) \
|
||||
break; \
|
||||
\
|
||||
/* Since the comparison above passed now we need to swap the */ \
|
||||
/* current node with the parent but it will have to be done */ \
|
||||
/* with the node's MinHeap value since the MaxHeap value */ \
|
||||
/* doesn't exist */ \
|
||||
V tmp = curr_node->data[0]; \
|
||||
curr_node->data[0] = parent->data[1]; \
|
||||
parent->data[1] = tmp; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
/* Usual case, just compare both MaxHeap values */ \
|
||||
if (_heap_->cmp(curr_node->data[1], parent->data[1]) < 0) \
|
||||
break; \
|
||||
\
|
||||
/* Swap with parent and repeat */ \
|
||||
V tmp = curr_node->data[1]; \
|
||||
curr_node->data[1] = parent->data[1]; \
|
||||
parent->data[1] = tmp; \
|
||||
} \
|
||||
\
|
||||
/* Update indexe and node */ \
|
||||
index = P_index; \
|
||||
curr_node = parent; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
static void PFX##_impl_float_up_min(SNAME *_heap_) \
|
||||
{ \
|
||||
size_t index = _heap_->size - 1; \
|
||||
\
|
||||
SNAME##_node *curr_node = &(_heap_->buffer[index]); \
|
||||
\
|
||||
/* FLoat Up on the MinHeap */ \
|
||||
while (index > 0) \
|
||||
{ \
|
||||
/* Parent index */ \
|
||||
size_t P_index = (index - 1) / 2; \
|
||||
\
|
||||
SNAME##_node *parent = &(_heap_->buffer[P_index]); \
|
||||
\
|
||||
if (_heap_->cmp(curr_node->data[0], parent->data[0]) >= 0) \
|
||||
break; \
|
||||
\
|
||||
/* Swap with parent and repeat */ \
|
||||
V tmp = curr_node->data[0]; \
|
||||
curr_node->data[0] = parent->data[0]; \
|
||||
parent->data[0] = tmp; \
|
||||
\
|
||||
/* Update indexe and node */ \
|
||||
index = P_index; \
|
||||
curr_node = parent; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
static void PFX##_impl_float_down_max(SNAME *_heap_) \
|
||||
{ \
|
||||
/* Floats Down from the MinHeap */ \
|
||||
size_t index = 0; \
|
||||
\
|
||||
SNAME##_node *curr_node = &(_heap_->buffer[index]); \
|
||||
\
|
||||
while (true) \
|
||||
{ \
|
||||
/* If there are no children it is done */ \
|
||||
if (2 * index + 1 >= _heap_->size) \
|
||||
break; \
|
||||
\
|
||||
/* Child index to be compared with current value */ \
|
||||
size_t child; \
|
||||
size_t L_index = index * 2 + 1; \
|
||||
size_t R_index = index * 2 + 2; \
|
||||
\
|
||||
/* If there are two children, pick the smallest one */ \
|
||||
if (R_index < _heap_->size) \
|
||||
{ \
|
||||
SNAME##_node *L = &(_heap_->buffer[L_index]); \
|
||||
SNAME##_node *R = &(_heap_->buffer[R_index]); \
|
||||
\
|
||||
/* If the right child is the last node and there is no MaxHeap value */ \
|
||||
/* then do the comparison with the MinHeap value */ \
|
||||
if (R_index == _heap_->size - 1 && PFX##_count(_heap_) % 2 != 0) \
|
||||
child = _heap_->cmp(L->data[1], R->data[0]) > 0 ? L_index : R_index; \
|
||||
else \
|
||||
child = _heap_->cmp(L->data[1], R->data[1]) > 0 ? L_index : R_index; \
|
||||
} \
|
||||
/* Pick the only one available */ \
|
||||
else \
|
||||
child = L_index; \
|
||||
\
|
||||
SNAME##_node *child_node = &(_heap_->buffer[child]); \
|
||||
\
|
||||
/* Again, check if the child node is the last one and has no MaxHeap value */ \
|
||||
if (child == _heap_->size - 1 && PFX##_count(_heap_) % 2 != 0) \
|
||||
{ \
|
||||
/* Odd case, compare with MinHeap value */ \
|
||||
/* If current value is not less than the child node's value, it is done */ \
|
||||
if (_heap_->cmp(curr_node->data[1], child_node->data[0]) >= 0) \
|
||||
break; \
|
||||
\
|
||||
/* Otherwise swap and continue */ \
|
||||
V tmp = child_node->data[0]; \
|
||||
child_node->data[0] = curr_node->data[1]; \
|
||||
curr_node->data[1] = tmp; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
/* If current value is not less than the child node's value, it is done */ \
|
||||
if (_heap_->cmp(curr_node->data[1], child_node->data[1]) >= 0) \
|
||||
break; \
|
||||
\
|
||||
/* Otherwise swap and continue */ \
|
||||
V tmp = child_node->data[1]; \
|
||||
child_node->data[1] = curr_node->data[1]; \
|
||||
curr_node->data[1] = tmp; \
|
||||
\
|
||||
/* Check if the MinHeap and MaxHeap values need to be swapped */ \
|
||||
if (_heap_->cmp(child_node->data[0], child_node->data[1]) > 0) \
|
||||
{ \
|
||||
/* Swap because the MinHeap value is greater than the MaxHeap value */ \
|
||||
tmp = child_node->data[0]; \
|
||||
child_node->data[0] = child_node->data[1]; \
|
||||
child_node->data[1] = tmp; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
/* Update index and node */ \
|
||||
index = child; \
|
||||
curr_node = child_node; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
static void PFX##_impl_float_down_min(SNAME *_heap_) \
|
||||
{ \
|
||||
/* Floats Down from the MinHeap */ \
|
||||
size_t index = 0; \
|
||||
\
|
||||
SNAME##_node *curr_node = &(_heap_->buffer[index]); \
|
||||
\
|
||||
while (true) \
|
||||
{ \
|
||||
/* If there are no children it is done */ \
|
||||
if (2 * index + 1 >= _heap_->size) \
|
||||
break; \
|
||||
\
|
||||
/* Child index to be compared with current value */ \
|
||||
size_t child; \
|
||||
size_t L_index = index * 2 + 1; \
|
||||
size_t R_index = index * 2 + 2; \
|
||||
\
|
||||
/* If there are two children, pick the smallest one */ \
|
||||
if (R_index < _heap_->size) \
|
||||
{ \
|
||||
SNAME##_node *L = &(_heap_->buffer[L_index]); \
|
||||
SNAME##_node *R = &(_heap_->buffer[R_index]); \
|
||||
\
|
||||
child = _heap_->cmp(L->data[0], R->data[0]) < 0 ? L_index : R_index; \
|
||||
} \
|
||||
/* Pick the only one available */ \
|
||||
else \
|
||||
child = L_index; \
|
||||
\
|
||||
SNAME##_node *child_node = &(_heap_->buffer[child]); \
|
||||
\
|
||||
/* If current value is smaller than the child node's value, it is done */ \
|
||||
if (_heap_->cmp(curr_node->data[0], child_node->data[0]) < 0) \
|
||||
break; \
|
||||
\
|
||||
/* Otherwise swap and continue */ \
|
||||
V tmp = child_node->data[0]; \
|
||||
child_node->data[0] = curr_node->data[0]; \
|
||||
curr_node->data[0] = tmp; \
|
||||
\
|
||||
/* If the child node is the last node check if the MinHeap and */ \
|
||||
/* MaxHeap values need to be swapped */ \
|
||||
if (child != _heap_->size - 1 || PFX##_count(_heap_) % 2 == 0) \
|
||||
{ \
|
||||
if (_heap_->cmp(child_node->data[0], child_node->data[1]) > 0) \
|
||||
{ \
|
||||
/* Swap because the MinHeap value is greater than the MaxHeap value */ \
|
||||
tmp = child_node->data[0]; \
|
||||
child_node->data[0] = child_node->data[1]; \
|
||||
child_node->data[1] = tmp; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
/* Update index and node */ \
|
||||
index = child; \
|
||||
curr_node = child_node; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_heap_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _heap_); \
|
||||
PFX##_iter_to_start(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_heap_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _heap_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_INTERVALHEAP_H */
|
||||
+1140
File diff suppressed because it is too large.
Load diff
+1221
File diff suppressed because it is too large.
Load diff
@@ -1,32 +0,0 @@
|
||||
/**
|
||||
* @file for_each.h
|
||||
*
|
||||
* Creation Date: 25/02/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CMC_FOR_EACH
|
||||
#define CMC_FOR_EACH
|
||||
|
||||
#define FOR_EACH(PFX, SNAME, TARGET, BODY) \
|
||||
do \
|
||||
{ \
|
||||
for (SNAME##_iter iter = TARGET->it_start(TARGET); !PFX##_iter_end(&iter); PFX##_iter_next(&iter)) \
|
||||
{ \
|
||||
BODY; \
|
||||
} \
|
||||
} while (0);
|
||||
|
||||
#define FOR_EACH_REV(PFX, SNAME, TARGET, BODY) \
|
||||
do \
|
||||
{ \
|
||||
for (SNAME##_iter iter = TARGET->it_end(TARGET); !PFX##_iter_start(&iter); PFX##_iter_prev(&iter)) \
|
||||
{ \
|
||||
BODY; \
|
||||
} \
|
||||
} while (0);
|
||||
|
||||
#endif /* CMC_FOR_EACH */
|
||||
+35
-29
@@ -8,41 +8,47 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CMC_MACRO_COLLECTIONS
|
||||
#define CMC_MACRO_COLLECTIONS
|
||||
#ifndef CMC_MACRO_COLLECTIONS_H
|
||||
#define CMC_MACRO_COLLECTIONS_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#define CMC_CONCATH_(C) CMC_WRAPGEN_##C##_HEADER
|
||||
#define CMC_CONCATC_(C) CMC_WRAPGEN_##C##_SOURCE
|
||||
|
||||
#define CONCATH_(C, P) C##_GENERATE_HEADER##_##P
|
||||
#define CONCATC_(C, P) C##_GENERATE_SOURCE##_##P
|
||||
#define CMC_CONCATH(C) CMC_CONCATH_(C)
|
||||
#define CMC_CONCATC(C) CMC_CONCATC_(C)
|
||||
|
||||
#define CONCATH(C, P) CONCATH_(C, P)
|
||||
#define CONCATC(C, P) CONCATC_(C, P)
|
||||
#define CMC_COLLECTION_GENERATE(C, PFX, SNAME, K, V) \
|
||||
CMC_COLLECTION_GENERATE_HEADER(C, PFX, SNAME, K, V) \
|
||||
CMC_COLLECTION_GENERATE_SOURCE(C, PFX, SNAME, K, V)
|
||||
|
||||
#define COLLECTION_GENERATE(C, P, PFX, SNAME, FMOD, K, V) \
|
||||
COLLECTION_GENERATE_HEADER(C, P, PFX, SNAME, FMOD, K, V) \
|
||||
COLLECTION_GENERATE_SOURCE(C, P, PFX, SNAME, FMOD, K, V)
|
||||
#define CMC_COLLECTION_GENERATE_HEADER(C, PFX, SNAME, K, V) \
|
||||
CMC_CONCATH(C)(PFX, SNAME, K, V)
|
||||
|
||||
#define COLLECTION_GENERATE_HEADER(C, P, PFX, SNAME, FMOD, K, V) \
|
||||
CONCATH(C, P) \
|
||||
(PFX, SNAME, FMOD, K, V)
|
||||
#define CMC_COLLECTION_GENERATE_SOURCE(C, PFX, SNAME, K, V) \
|
||||
CMC_CONCATC(C)(PFX, SNAME, K, V)
|
||||
|
||||
#define COLLECTION_GENERATE_SOURCE(C, P, PFX, SNAME, FMOD, K, V) \
|
||||
CONCATC(C, P) \
|
||||
(PFX, SNAME, FMOD, K, V)
|
||||
#include "cmc/deque.h"
|
||||
#include "cmc/hashmap.h"
|
||||
#include "cmc/hashset.h"
|
||||
#include "cmc/heap.h"
|
||||
#include "cmc/linkedlist.h"
|
||||
#include "cmc/list.h"
|
||||
#include "cmc/queue.h"
|
||||
#include "cmc/stack.h"
|
||||
#include "cmc/treemap.h"
|
||||
#include "cmc/treeset.h"
|
||||
|
||||
#include "list.h"
|
||||
#include "linkedlist.h"
|
||||
#include "stack.h"
|
||||
#include "queue.h"
|
||||
#include "deque.h"
|
||||
#include "heap.h"
|
||||
#include "treeset.h"
|
||||
#include "treemap.h"
|
||||
#include "hashset.h"
|
||||
#include "hashmap.h"
|
||||
#include "ext/intervalheap.h"
|
||||
#include "ext/multimap.h"
|
||||
#include "ext/multiset.h"
|
||||
|
||||
#include "foreach.h"
|
||||
#include "sac/queue.h"
|
||||
#include "sac/stack.h"
|
||||
|
||||
#endif /* CMC_MACRO_COLLECTIONS */
|
||||
#include "utl/assert.h"
|
||||
#include "utl/foreach.h"
|
||||
#include "utl/log.h"
|
||||
#include "utl/test.h"
|
||||
#include "utl/timer.h"
|
||||
|
||||
#endif /* CMC_MACRO_COLLECTIONS_H */
|
||||
-426
@@ -1,426 +0,0 @@
|
||||
/**
|
||||
* queue.h
|
||||
*
|
||||
* Creation Date: 15/02/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/*****************************************************************************/
|
||||
/********************************************************************* QUEUE */
|
||||
/*****************************************************************************/
|
||||
|
||||
#ifndef CMC_QUEUE_H
|
||||
#define CMC_QUEUE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define QUEUE_GENERATE(PFX, SNAME, FMOD, V) \
|
||||
QUEUE_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
QUEUE_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
QUEUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
|
||||
/* PRIVATE *******************************************************************/
|
||||
#define QUEUE_GENERATE_HEADER_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
QUEUE_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define QUEUE_GENERATE_SOURCE_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
QUEUE_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
QUEUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* PUBLIC ********************************************************************/
|
||||
#define QUEUE_GENERATE_HEADER_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
QUEUE_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
QUEUE_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define QUEUE_GENERATE_SOURCE_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
QUEUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* STRUCT ********************************************************************/
|
||||
#define QUEUE_GENERATE_STRUCT(PFX, SNAME, FMOD, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define QUEUE_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
/* Queue Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic circular array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current circular array capacity */ \
|
||||
size_t capacity; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Index representing the front of the queue */ \
|
||||
size_t front; \
|
||||
\
|
||||
/* Index representing the back of the queue */ \
|
||||
size_t rear; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the queue */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the queue */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Queue Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target queue */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* Keeps track of relative index to the iteration of elements */ \
|
||||
size_t index; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME *PFX##_new(size_t size); \
|
||||
FMOD void PFX##_clear(SNAME *_queue_); \
|
||||
FMOD void PFX##_free(SNAME *_queue_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_enqueue(SNAME *_queue_, V element); \
|
||||
FMOD bool PFX##_dequeue(SNAME *_queue_); \
|
||||
/* Conditional Input and Output */ \
|
||||
FMOD bool PFX##_enqueue_if(SNAME *_queue_, V element, bool condition); \
|
||||
FMOD bool PFX##_dequeue_if(SNAME *_queue_, bool condition); \
|
||||
/* Element Access */ \
|
||||
FMOD V PFX##_peek(SNAME *_queue_); \
|
||||
/* Collection State */ \
|
||||
FMOD bool PFX##_contains(SNAME *_queue_, V element, int (*comparator)(V, V)); \
|
||||
FMOD bool PFX##_empty(SNAME *_queue_); \
|
||||
FMOD bool PFX##_full(SNAME *_queue_); \
|
||||
FMOD size_t PFX##_count(SNAME *_queue_); \
|
||||
FMOD size_t PFX##_capacity(SNAME *_queue_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
/* Iterator Access */ \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define QUEUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
FMOD bool PFX##_grow(SNAME *_queue_); \
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_queue_); \
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_queue_); \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(size_t size) \
|
||||
{ \
|
||||
if (size < 1) \
|
||||
return NULL; \
|
||||
\
|
||||
SNAME *_queue_ = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!_queue_) \
|
||||
return NULL; \
|
||||
\
|
||||
_queue_->buffer = malloc(sizeof(V) * size); \
|
||||
\
|
||||
if (!_queue_->buffer) \
|
||||
{ \
|
||||
free(_queue_); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_queue_->buffer, 0, sizeof(V) * size); \
|
||||
\
|
||||
_queue_->capacity = size; \
|
||||
_queue_->count = 0; \
|
||||
_queue_->front = 0; \
|
||||
_queue_->rear = 0; \
|
||||
\
|
||||
_queue_->it_start = PFX##_impl_it_start; \
|
||||
_queue_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _queue_; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_clear(SNAME *_queue_) \
|
||||
{ \
|
||||
memset(_queue_->buffer, 0, sizeof(V) * _queue_->capacity); \
|
||||
\
|
||||
_queue_->count = 0; \
|
||||
_queue_->front = 0; \
|
||||
_queue_->rear = 0; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_free(SNAME *_queue_) \
|
||||
{ \
|
||||
free(_queue_->buffer); \
|
||||
free(_queue_); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_enqueue(SNAME *_queue_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_queue_)) \
|
||||
{ \
|
||||
if (!PFX##_grow(_queue_)) \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
_queue_->buffer[_queue_->rear] = element; \
|
||||
\
|
||||
_queue_->rear = (_queue_->rear == _queue_->capacity - 1) ? 0 : _queue_->rear + 1; \
|
||||
_queue_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_dequeue(SNAME *_queue_) \
|
||||
{ \
|
||||
if (PFX##_empty(_queue_)) \
|
||||
return false; \
|
||||
\
|
||||
_queue_->buffer[_queue_->front] = 0; \
|
||||
\
|
||||
_queue_->front = (_queue_->front == _queue_->capacity - 1) ? 0 : _queue_->front + 1; \
|
||||
_queue_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_enqueue_if(SNAME *_queue_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_enqueue(_queue_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_dequeue_if(SNAME *_queue_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_dequeue(_queue_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_peek(SNAME *_queue_) \
|
||||
{ \
|
||||
if (PFX##_empty(_queue_)) \
|
||||
return 0; \
|
||||
\
|
||||
return _queue_->buffer[_queue_->front]; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_contains(SNAME *_queue_, V element, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->count, j++) \
|
||||
{ \
|
||||
if (comparator(_queue_->buffer[i], element) == 0) \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_empty(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->count == 0; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_full(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->count >= _queue_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_count(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_capacity(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
iter->cursor = 0; \
|
||||
else \
|
||||
iter->cursor = (iter->target->rear == 0) ? iter->target->capacity - 1 : iter->target->rear - 1; \
|
||||
\
|
||||
iter->index = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->index == iter->target->count - 1) \
|
||||
iter->end = true; \
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = (iter->cursor + 1) % (iter->target->capacity); \
|
||||
iter->index++; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->index == 0) \
|
||||
iter->start = true; \
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = (iter->cursor == 0) ? iter->target->capacity - 1 : iter->cursor - 1; \
|
||||
iter->index--; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return 0; \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return NULL; \
|
||||
\
|
||||
return &(iter->target->buffer[iter->cursor]); \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->index; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_grow(SNAME *_queue_) \
|
||||
{ \
|
||||
\
|
||||
size_t new_capacity = _queue_->capacity * 2; \
|
||||
\
|
||||
V *new_buffer = malloc(sizeof(V) * new_capacity); \
|
||||
\
|
||||
if (!new_buffer) \
|
||||
return false; \
|
||||
\
|
||||
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->capacity, j++) \
|
||||
{ \
|
||||
new_buffer[j] = _queue_->buffer[i]; \
|
||||
} \
|
||||
\
|
||||
free(_queue_->buffer); \
|
||||
\
|
||||
_queue_->buffer = new_buffer; \
|
||||
_queue_->capacity = new_capacity; \
|
||||
_queue_->front = 0; \
|
||||
_queue_->rear = _queue_->count; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_queue_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _queue_); \
|
||||
PFX##_iter_to_start(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_queue_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _queue_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_QUEUE_H */
|
||||
+357
@@ -0,0 +1,357 @@
|
||||
/**
|
||||
* queue.h
|
||||
*
|
||||
* Creation Date: 24/06/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* A Purely Stack Allocated Queue
|
||||
*
|
||||
* This means that the Queue has a fixed size, but it also has the advantage of
|
||||
* not requiring for manually allocating and deallocating the struct.
|
||||
*
|
||||
* Generating a collection with too big of a storage can be dangerous and might
|
||||
* quickly cause a stack overflow.
|
||||
*
|
||||
* It is recommended to not generate any Stack Allocated Collection with less
|
||||
* than 1000 internal storage.
|
||||
*/
|
||||
|
||||
#ifndef CMC_SAC_QUEUE_H
|
||||
#define CMC_SAC_QUEUE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SAC_QUEUE_GENERATE(PFX, SNAME, FMOD, V, SIZE) \
|
||||
SAC_QUEUE_GENERATE_HEADER(PFX, SNAME, FMOD, V, SIZE) \
|
||||
SAC_QUEUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V, SIZE)
|
||||
|
||||
#define SAC_QUEUE_WRAPGEN_HEADER(PFX, SNAME, FMOD, K, V, SIZE) \
|
||||
SAC_QUEUE_GENERATE_HEADER(PFX, SNAME, FMOD, V, SIZE)
|
||||
|
||||
#define SAC_QUEUE_WRAPGEN_SOURCE(PFX, SNAME, FMOD, K, V, SIZE) \
|
||||
SAC_QUEUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V, SIZE)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define SAC_QUEUE_GENERATE_HEADER(PFX, SNAME, FMOD, V, SIZE) \
|
||||
\
|
||||
/* Queue Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Internal Storage */ \
|
||||
V buffer[SIZE]; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Index representing the front of the queue */ \
|
||||
size_t front; \
|
||||
\
|
||||
/* Index representing the back of the queue */ \
|
||||
size_t rear; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the queue */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the queue */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Queue Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target queue */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's position (index) */ \
|
||||
size_t cursor; \
|
||||
\
|
||||
/* Keeps track of relative index to the iteration of elements */ \
|
||||
size_t index; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME PFX##_new(void); \
|
||||
FMOD void PFX##_clear(SNAME *_queue_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_enqueue(SNAME *_queue_, V element); \
|
||||
FMOD bool PFX##_dequeue(SNAME *_queue_); \
|
||||
/* Conditional Input and Output */ \
|
||||
FMOD bool PFX##_enqueue_if(SNAME *_queue_, V element, bool condition); \
|
||||
FMOD bool PFX##_dequeue_if(SNAME *_queue_, bool condition); \
|
||||
/* Element Access */ \
|
||||
FMOD V PFX##_peek(SNAME *_queue_); \
|
||||
/* Collection State */ \
|
||||
FMOD bool PFX##_contains(SNAME *_queue_, V element, int (*comparator)(V, V)); \
|
||||
FMOD bool PFX##_empty(SNAME *_queue_); \
|
||||
FMOD bool PFX##_full(SNAME *_queue_); \
|
||||
FMOD size_t PFX##_count(SNAME *_queue_); \
|
||||
FMOD size_t PFX##_capacity(void); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
/* Iterator Access */ \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* Default Value */ \
|
||||
static inline V PFX##_impl_default_value(void) \
|
||||
{ \
|
||||
V _empty_value_; \
|
||||
\
|
||||
memset(&_empty_value_, 0, sizeof(V)); \
|
||||
\
|
||||
return _empty_value_; \
|
||||
} \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define SAC_QUEUE_GENERATE_SOURCE(PFX, SNAME, FMOD, V, SIZE) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_queue_); \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_queue_); \
|
||||
\
|
||||
FMOD SNAME PFX##_new(void) \
|
||||
{ \
|
||||
SNAME result; \
|
||||
\
|
||||
memset(&result, 0, sizeof(SNAME)); \
|
||||
\
|
||||
result.it_start = PFX##_impl_it_start; \
|
||||
result.it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return result; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_clear(SNAME *_queue_) \
|
||||
{ \
|
||||
memset(_queue_->buffer, 0, sizeof(_queue_->buffer)); \
|
||||
\
|
||||
_queue_->count = 0; \
|
||||
_queue_->front = 0; \
|
||||
_queue_->rear = 0; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_enqueue(SNAME *_queue_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_queue_)) \
|
||||
return false; \
|
||||
\
|
||||
_queue_->buffer[_queue_->rear] = element; \
|
||||
\
|
||||
_queue_->rear = (_queue_->rear == SIZE - 1) ? 0 : _queue_->rear + 1; \
|
||||
_queue_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_dequeue(SNAME *_queue_) \
|
||||
{ \
|
||||
if (PFX##_empty(_queue_)) \
|
||||
return false; \
|
||||
\
|
||||
_queue_->buffer[_queue_->front] = PFX##_impl_default_value(); \
|
||||
\
|
||||
_queue_->front = (_queue_->front == SIZE - 1) ? 0 : _queue_->front + 1; \
|
||||
_queue_->count--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_enqueue_if(SNAME *_queue_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_enqueue(_queue_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_dequeue_if(SNAME *_queue_, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_dequeue(_queue_); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_peek(SNAME *_queue_) \
|
||||
{ \
|
||||
if (PFX##_empty(_queue_)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _queue_->buffer[_queue_->front]; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_contains(SNAME *_queue_, V element, int (*comparator)(V, V)) \
|
||||
{ \
|
||||
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->count, j++) \
|
||||
{ \
|
||||
if (comparator(_queue_->buffer[i], element) == 0) \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_empty(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->count == 0; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_full(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->count >= SIZE; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_count(SNAME *_queue_) \
|
||||
{ \
|
||||
return _queue_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_capacity(void) \
|
||||
{ \
|
||||
return SIZE; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->front; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
iter->cursor = 0; \
|
||||
else \
|
||||
iter->cursor = (iter->target->rear == 0) ? SIZE - 1 : iter->target->rear - 1; \
|
||||
\
|
||||
iter->index = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->index == iter->target->count - 1) \
|
||||
iter->end = true; \
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = (iter->cursor + 1) % (SIZE); \
|
||||
iter->index++; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->index == 0) \
|
||||
iter->start = true; \
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = (iter->cursor == 0) ? SIZE - 1 : iter->cursor - 1; \
|
||||
iter->index--; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return NULL; \
|
||||
\
|
||||
return &(iter->target->buffer[iter->cursor]); \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->index; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_queue_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _queue_); \
|
||||
PFX##_iter_to_start(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_queue_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _queue_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_SAC_QUEUE_H */
|
||||
+58
-115
@@ -1,55 +1,51 @@
|
||||
/**
|
||||
* stack.h
|
||||
*
|
||||
* Creation Date: 14/02/2019
|
||||
* Creation Date: 21/06/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/*****************************************************************************/
|
||||
/********************************************************************* STACK */
|
||||
/*****************************************************************************/
|
||||
/**
|
||||
* A Purely Stack Allocated Stack
|
||||
*
|
||||
* This means that the Stack has a fixed size, but it also has the advantage of
|
||||
* not requiring for manually allocating and deallocating the struct.
|
||||
*
|
||||
* Generating a collection with too big of a storage can be dangerous and might
|
||||
* quickly cause a stack overflow.
|
||||
*
|
||||
* It is recommended to not generate any Stack Allocated Collection with less
|
||||
* than 1000 internal storage.
|
||||
*/
|
||||
|
||||
#ifndef CMC_STACK_H
|
||||
#define CMC_STACK_H
|
||||
#ifndef CMC_SAC_STACK_H
|
||||
#define CMC_SAC_STACK_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define STACK_GENERATE(PFX, SNAME, FMOD, V) \
|
||||
STACK_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
STACK_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
STACK_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
#define SAC_STACK_GENERATE(PFX, SNAME, FMOD, V, SIZE) \
|
||||
SAC_STACK_GENERATE_HEADER(PFX, SNAME, FMOD, V, SIZE) \
|
||||
SAC_STACK_GENERATE_SOURCE(PFX, SNAME, FMOD, V, SIZE)
|
||||
|
||||
/* PRIVATE *******************************************************************/
|
||||
#define STACK_GENERATE_HEADER_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
STACK_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define STACK_GENERATE_SOURCE_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
STACK_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
STACK_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* PUBLIC ********************************************************************/
|
||||
#define STACK_GENERATE_HEADER_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
STACK_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
STACK_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define STACK_GENERATE_SOURCE_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
STACK_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* STRUCT ********************************************************************/
|
||||
#define STACK_GENERATE_STRUCT(PFX, SNAME, FMOD, V)
|
||||
#define SAC_STACK_WRAPGEN_HEADER(PFX, SNAME, FMOD, K, V, SIZE) \
|
||||
SAC_STACK_GENERATE_HEADER(PFX, SNAME, FMOD, V, SIZE)
|
||||
|
||||
#define SAC_STACK_WRAPGEN_SOURCE(PFX, SNAME, FMOD, K, V, SIZE) \
|
||||
SAC_STACK_GENERATE_SOURCE(PFX, SNAME, FMOD, V, SIZE)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define STACK_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
#define SAC_STACK_GENERATE_HEADER(PFX, SNAME, FMOD, V, SIZE) \
|
||||
\
|
||||
/* Stack Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Dynamic array of elements */ \
|
||||
V *buffer; \
|
||||
\
|
||||
/* Current array capacity */ \
|
||||
size_t capacity; \
|
||||
/* Internal Storage */ \
|
||||
V buffer[SIZE]; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
@@ -81,9 +77,8 @@
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME *PFX##_new(size_t size); \
|
||||
FMOD SNAME PFX##_new(void); \
|
||||
FMOD void PFX##_clear(SNAME *_stack_); \
|
||||
FMOD void PFX##_free(SNAME *_stack_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_push(SNAME *_stack_, V element); \
|
||||
FMOD bool PFX##_pop(SNAME *_stack_); \
|
||||
@@ -97,12 +92,9 @@
|
||||
FMOD bool PFX##_empty(SNAME *_stack_); \
|
||||
FMOD bool PFX##_full(SNAME *_stack_); \
|
||||
FMOD size_t PFX##_count(SNAME *_stack_); \
|
||||
FMOD size_t PFX##_capacity(SNAME *_stack_); \
|
||||
FMOD size_t PFX##_capacity(void); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
@@ -118,63 +110,46 @@
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* Default Value */ \
|
||||
static inline V PFX##_impl_default_value(void) \
|
||||
{ \
|
||||
V _empty_value_; \
|
||||
\
|
||||
memset(&_empty_value_, 0, sizeof(V)); \
|
||||
\
|
||||
return _empty_value_; \
|
||||
} \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define STACK_GENERATE_SOURCE(PFX, SNAME, FMOD, V) \
|
||||
#define SAC_STACK_GENERATE_SOURCE(PFX, SNAME, FMOD, V, SIZE) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
FMOD bool PFX##_impl_grow(SNAME *_stack_); \
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_stack_); \
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_stack_); \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_stack_); \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_stack_); \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(size_t size) \
|
||||
FMOD SNAME PFX##_new(void) \
|
||||
{ \
|
||||
if (size < 1) \
|
||||
return NULL; \
|
||||
SNAME result; \
|
||||
\
|
||||
SNAME *_stack_ = malloc(sizeof(SNAME)); \
|
||||
memset(&result, 0, sizeof(SNAME)); \
|
||||
\
|
||||
if (!_stack_) \
|
||||
return NULL; \
|
||||
result.it_start = PFX##_impl_it_start; \
|
||||
result.it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
_stack_->buffer = malloc(sizeof(V) * size); \
|
||||
\
|
||||
if (!_stack_->buffer) \
|
||||
{ \
|
||||
free(_stack_); \
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
memset(_stack_->buffer, 0, sizeof(V) * size); \
|
||||
\
|
||||
_stack_->capacity = size; \
|
||||
_stack_->count = 0; \
|
||||
\
|
||||
_stack_->it_start = PFX##_impl_it_start; \
|
||||
_stack_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _stack_; \
|
||||
return result; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_clear(SNAME *_stack_) \
|
||||
{ \
|
||||
memset(_stack_->buffer, 0, sizeof(V) * _stack_->capacity); \
|
||||
memset(_stack_->buffer, 0, sizeof(_stack_->buffer)); \
|
||||
\
|
||||
_stack_->count = 0; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_free(SNAME *_stack_) \
|
||||
{ \
|
||||
free(_stack_->buffer); \
|
||||
free(_stack_); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_push(SNAME *_stack_, V element) \
|
||||
{ \
|
||||
if (PFX##_full(_stack_)) \
|
||||
{ \
|
||||
if (!PFX##_impl_grow(_stack_)) \
|
||||
return false; \
|
||||
} \
|
||||
return false; \
|
||||
\
|
||||
_stack_->buffer[_stack_->count++] = element; \
|
||||
\
|
||||
@@ -186,7 +161,7 @@
|
||||
if (PFX##_empty(_stack_)) \
|
||||
return false; \
|
||||
\
|
||||
_stack_->buffer[--_stack_->count] = 0; \
|
||||
_stack_->buffer[--_stack_->count] = PFX##_impl_default_value(); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
@@ -210,7 +185,7 @@
|
||||
FMOD V PFX##_top(SNAME *_stack_) \
|
||||
{ \
|
||||
if (PFX##_empty(_stack_)) \
|
||||
return 0; \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return _stack_->buffer[_stack_->count - 1]; \
|
||||
} \
|
||||
@@ -233,7 +208,7 @@
|
||||
\
|
||||
FMOD bool PFX##_full(SNAME *_stack_) \
|
||||
{ \
|
||||
return _stack_->count >= _stack_->capacity; \
|
||||
return _stack_->count >= SIZE; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_count(SNAME *_stack_) \
|
||||
@@ -241,26 +216,9 @@
|
||||
return _stack_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_capacity(SNAME *_stack_) \
|
||||
FMOD size_t PFX##_capacity(void) \
|
||||
{ \
|
||||
return _stack_->capacity; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
return SIZE; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
@@ -328,7 +286,7 @@
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (PFX##_empty(iter->target)) \
|
||||
return 0; \
|
||||
return PFX##_impl_default_value(); \
|
||||
\
|
||||
return iter->target->buffer[iter->cursor]; \
|
||||
} \
|
||||
@@ -346,22 +304,7 @@
|
||||
return iter->target->count - 1 - iter->cursor; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_impl_grow(SNAME *_stack_) \
|
||||
{ \
|
||||
size_t new_capacity = _stack_->capacity * 2; \
|
||||
\
|
||||
V *new_buffer = realloc(_stack_->buffer, sizeof(V) * new_capacity); \
|
||||
\
|
||||
if (!new_buffer) \
|
||||
return false; \
|
||||
\
|
||||
_stack_->buffer = new_buffer; \
|
||||
_stack_->capacity = new_capacity; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_stack_) \
|
||||
static SNAME##_iter PFX##_impl_it_start(SNAME *_stack_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
@@ -371,7 +314,7 @@
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_stack_) \
|
||||
static SNAME##_iter PFX##_impl_it_end(SNAME *_stack_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
@@ -381,4 +324,4 @@
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_STACK_H */
|
||||
#endif /* CMC_SAC_STACK_H */
|
||||
-863
@@ -1,863 +0,0 @@
|
||||
/**
|
||||
* treemap.h
|
||||
*
|
||||
* Creation Date: 28/03/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/*****************************************************************************/
|
||||
/******************************************************************* TREEMAP */
|
||||
/*****************************************************************************/
|
||||
|
||||
#ifndef CMC_TREEMAP_H
|
||||
#define CMC_TREEMAP_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define TREEMAP_GENERATE(PFX, SNAME, FMOD, K, V) \
|
||||
TREEMAP_GENERATE_STRUCT(PFX, SNAME, FMOD, K, V) \
|
||||
TREEMAP_GENERATE_HEADER(PFX, SNAME, FMOD, K, V) \
|
||||
TREEMAP_GENERATE_SOURCE(PFX, SNAME, FMOD, K, V)
|
||||
|
||||
/* PRIVATE *******************************************************************/
|
||||
#define TREEMAP_GENERATE_HEADER_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
TREEMAP_GENERATE_HEADER(PFX, SNAME, FMOD, K, V)
|
||||
#define TREEMAP_GENERATE_SOURCE_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
TREEMAP_GENERATE_STRUCT(PFX, SNAME, FMOD, K, V) \
|
||||
TREEMAP_GENERATE_SOURCE(PFX, SNAME, FMOD, K, V)
|
||||
/* PUBLIC ********************************************************************/
|
||||
#define TREEMAP_GENERATE_HEADER_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
TREEMAP_GENERATE_STRUCT(PFX, SNAME, FMOD, K, V) \
|
||||
TREEMAP_GENERATE_HEADER(PFX, SNAME, FMOD, K, V)
|
||||
#define TREEMAP_GENERATE_SOURCE_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
TREEMAP_GENERATE_SOURCE(PFX, SNAME, FMOD, K, V)
|
||||
/* STRUCT ********************************************************************/
|
||||
#define TREEMAP_GENERATE_STRUCT(PFX, SNAME, FMOD, K, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define TREEMAP_GENERATE_HEADER(PFX, SNAME, FMOD, K, V) \
|
||||
\
|
||||
/* Treemap Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Root node */ \
|
||||
struct SNAME##_node_s *root; \
|
||||
\
|
||||
/* Current amount of keys */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Key comparison function */ \
|
||||
int (*cmp)(K, K); \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the hashmap */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the hashmap */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Treemap Node */ \
|
||||
typedef struct SNAME##_node_s \
|
||||
{ \
|
||||
/* Node Key */ \
|
||||
K key; \
|
||||
\
|
||||
/* Node Value */ \
|
||||
V value; \
|
||||
\
|
||||
/* Node height used by the AVL tree to keep it strictly balanced */ \
|
||||
unsigned char height; \
|
||||
\
|
||||
/* Right child node or subtree */ \
|
||||
struct SNAME##_node_s *right; \
|
||||
\
|
||||
/* Left child node or subtree */ \
|
||||
struct SNAME##_node_s *left; \
|
||||
\
|
||||
/* Parent node */ \
|
||||
struct SNAME##_node_s *parent; \
|
||||
\
|
||||
} SNAME##_node, *SNAME##_node_ptr; \
|
||||
\
|
||||
/* Treemap Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target treemap */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's current node */ \
|
||||
struct SNAME##_node_s *cursor; \
|
||||
\
|
||||
/* The first node in the iteration */ \
|
||||
struct SNAME##_node_s *first; \
|
||||
\
|
||||
/* The last node in the iteration */ \
|
||||
struct SNAME##_node_s *last; \
|
||||
\
|
||||
/* Keeps track of relative index to the iteration of elements */ \
|
||||
size_t index; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
\
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME *PFX##_new(int (*compare)(K, K)); \
|
||||
FMOD void PFX##_clear(SNAME *_map_); \
|
||||
FMOD void PFX##_free(SNAME *_map_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_insert(SNAME *_map_, K key, V value); \
|
||||
FMOD bool PFX##_remove(SNAME *_map_, K key, V *value); \
|
||||
/* Conditional Input and Output */ \
|
||||
FMOD bool PFX##_insert_if(SNAME *_map_, K key, V value, bool condition); \
|
||||
FMOD bool PFX##_remove_if(SNAME *_map_, K key, V *value, bool condition); \
|
||||
/* Element Access */ \
|
||||
FMOD bool PFX##_max(SNAME *_map_, K *key, V *value); \
|
||||
FMOD bool PFX##_min(SNAME *_map_, K *key, V *value); \
|
||||
FMOD V PFX##_get(SNAME *_map_, K key); \
|
||||
FMOD V *PFX##_get_ref(SNAME *_map_, K key); \
|
||||
/* Collection State */ \
|
||||
FMOD bool PFX##_contains(SNAME *_map_, K key); \
|
||||
FMOD bool PFX##_empty(SNAME *_map_); \
|
||||
FMOD size_t PFX##_count(SNAME *_map_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
/* Iterator Access */ \
|
||||
FMOD K PFX##_iter_key(SNAME##_iter *iter); \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define TREEMAP_GENERATE_SOURCE(PFX, SNAME, FMOD, K, V) \
|
||||
\
|
||||
/* Implementation Detail Functions */ \
|
||||
FMOD SNAME##_node *PFX##_impl_new_node(K key, V value); \
|
||||
FMOD SNAME##_node *PFX##_impl_get_node(SNAME *_map_, K key); \
|
||||
FMOD unsigned char PFX##_impl_h(SNAME##_node *node); \
|
||||
FMOD unsigned char PFX##_impl_hupdate(SNAME##_node *node); \
|
||||
FMOD void PFX##_impl_rotate_right(SNAME##_node **Z); \
|
||||
FMOD void PFX##_impl_rotate_left(SNAME##_node **Z); \
|
||||
FMOD void PFX##_impl_rebalance(SNAME *_map_, SNAME##_node *node); \
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_map_); \
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_map_); \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(int (*compare)(K, K)) \
|
||||
{ \
|
||||
SNAME *_map_ = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!_map_) \
|
||||
return NULL; \
|
||||
\
|
||||
_map_->count = 0; \
|
||||
_map_->root = NULL; \
|
||||
_map_->cmp = compare; \
|
||||
\
|
||||
_map_->it_start = PFX##_impl_it_start; \
|
||||
_map_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _map_; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_clear(SNAME *_map_) \
|
||||
{ \
|
||||
SNAME##_node *scan = _map_->root; \
|
||||
SNAME##_node *up = NULL; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
if (scan->left != NULL) \
|
||||
{ \
|
||||
SNAME##_node *left = scan->left; \
|
||||
\
|
||||
scan->left = up; \
|
||||
up = scan; \
|
||||
scan = left; \
|
||||
} \
|
||||
else if (scan->right != NULL) \
|
||||
{ \
|
||||
SNAME##_node *right = scan->right; \
|
||||
\
|
||||
scan->left = up; \
|
||||
scan->right = NULL; \
|
||||
up = scan; \
|
||||
scan = right; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
if (up == NULL) \
|
||||
{ \
|
||||
free(scan); \
|
||||
scan = NULL; \
|
||||
} \
|
||||
\
|
||||
while (up != NULL) \
|
||||
{ \
|
||||
free(scan); \
|
||||
\
|
||||
if (up->right != NULL) \
|
||||
{ \
|
||||
scan = up->right; \
|
||||
up->right = NULL; \
|
||||
break; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
scan = up; \
|
||||
up = up->left; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
_map_->count = 0; \
|
||||
_map_->root = NULL; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_free(SNAME *_map_) \
|
||||
{ \
|
||||
PFX##_clear(_map_); \
|
||||
\
|
||||
free(_map_); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_insert(SNAME *_map_, K key, V value) \
|
||||
{ \
|
||||
if (PFX##_empty(_map_)) \
|
||||
{ \
|
||||
_map_->root = PFX##_impl_new_node(key, value); \
|
||||
\
|
||||
if (!_map_->root) \
|
||||
return false; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
SNAME##_node *scan = _map_->root; \
|
||||
SNAME##_node *parent = scan; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
parent = scan; \
|
||||
\
|
||||
if (_map_->cmp(scan->key, key) > 0) \
|
||||
scan = scan->left; \
|
||||
else if (_map_->cmp(scan->key, key) < 0) \
|
||||
scan = scan->right; \
|
||||
else \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
SNAME##_node *node; \
|
||||
\
|
||||
if (_map_->cmp(parent->key, key) > 0) \
|
||||
{ \
|
||||
parent->left = PFX##_impl_new_node(key, value); \
|
||||
\
|
||||
if (!parent->left) \
|
||||
return false; \
|
||||
\
|
||||
parent->left->parent = parent; \
|
||||
node = parent->left; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
parent->right = PFX##_impl_new_node(key, value); \
|
||||
\
|
||||
if (!parent->right) \
|
||||
return false; \
|
||||
\
|
||||
parent->right->parent = parent; \
|
||||
node = parent->right; \
|
||||
} \
|
||||
\
|
||||
PFX##_impl_rebalance(_map_, node); \
|
||||
} \
|
||||
\
|
||||
_map_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_remove(SNAME *_map_, K key, V *value) \
|
||||
{ \
|
||||
SNAME##_node *node = PFX##_impl_get_node(_map_, key); \
|
||||
\
|
||||
if (!node) \
|
||||
return false; \
|
||||
\
|
||||
*value = node->value; \
|
||||
\
|
||||
SNAME##_node *temp = NULL, *unbalanced = NULL; \
|
||||
\
|
||||
bool is_root = node->parent == NULL; \
|
||||
\
|
||||
if (node->left == NULL && node->right == NULL) \
|
||||
{ \
|
||||
if (is_root) \
|
||||
_map_->root = NULL; \
|
||||
else \
|
||||
{ \
|
||||
unbalanced = node->parent; \
|
||||
\
|
||||
if (node->parent->right == node) \
|
||||
node->parent->right = NULL; \
|
||||
else \
|
||||
node->parent->left = NULL; \
|
||||
} \
|
||||
\
|
||||
free(node); \
|
||||
} \
|
||||
else if (node->left == NULL) \
|
||||
{ \
|
||||
if (is_root) \
|
||||
{ \
|
||||
_map_->root = node->right; \
|
||||
_map_->root->parent = NULL; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
unbalanced = node->parent; \
|
||||
\
|
||||
node->right->parent = node->parent; \
|
||||
\
|
||||
if (node->parent->right == node) \
|
||||
node->parent->right = node->right; \
|
||||
else \
|
||||
node->parent->left = node->right; \
|
||||
} \
|
||||
\
|
||||
free(node); \
|
||||
} \
|
||||
else if (node->right == NULL) \
|
||||
{ \
|
||||
if (is_root) \
|
||||
{ \
|
||||
_map_->root = node->left; \
|
||||
_map_->root->parent = NULL; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
unbalanced = node->parent; \
|
||||
\
|
||||
node->left->parent = node->parent; \
|
||||
\
|
||||
if (node->parent->right == node) \
|
||||
node->parent->right = node->left; \
|
||||
else \
|
||||
node->parent->left = node->left; \
|
||||
} \
|
||||
\
|
||||
free(node); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
temp = node->right; \
|
||||
while (temp->left != NULL) \
|
||||
temp = temp->left; \
|
||||
\
|
||||
K temp_key = temp->key; \
|
||||
V temp_val = temp->value; \
|
||||
\
|
||||
unbalanced = temp->parent; \
|
||||
\
|
||||
if (temp->left == NULL && temp->right == NULL) \
|
||||
{ \
|
||||
if (temp->parent->right == temp) \
|
||||
temp->parent->right = NULL; \
|
||||
else \
|
||||
temp->parent->left = NULL; \
|
||||
} \
|
||||
else if (temp->left == NULL) \
|
||||
{ \
|
||||
temp->right->parent = temp->parent; \
|
||||
\
|
||||
if (temp->parent->right == temp) \
|
||||
temp->parent->right = temp->right; \
|
||||
else \
|
||||
temp->parent->left = temp->right; \
|
||||
} \
|
||||
else if (temp->right == NULL) \
|
||||
{ \
|
||||
temp->left->parent = temp->parent; \
|
||||
\
|
||||
if (temp->parent->right == temp) \
|
||||
temp->parent->right = temp->left; \
|
||||
else \
|
||||
temp->parent->left = temp->left; \
|
||||
} \
|
||||
\
|
||||
free(temp); \
|
||||
\
|
||||
node->key = temp_key; \
|
||||
node->value = temp_val; \
|
||||
} \
|
||||
\
|
||||
if (unbalanced != NULL) \
|
||||
PFX##_impl_rebalance(_map_, unbalanced); \
|
||||
\
|
||||
_map_->count--; \
|
||||
\
|
||||
if (_map_->count == 0) \
|
||||
_map_->root = NULL; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_insert_if(SNAME *_map_, K key, V value, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_insert(_map_, key, value); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_remove_if(SNAME *_map_, K key, V *value, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_remove(_map_, key, value); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_max(SNAME *_map_, K *key, V *value) \
|
||||
{ \
|
||||
if (PFX##_empty(_map_)) \
|
||||
return false; \
|
||||
\
|
||||
SNAME##_node *scan = _map_->root; \
|
||||
\
|
||||
while (scan->right != NULL) \
|
||||
scan = scan->right; \
|
||||
\
|
||||
*key = scan->key; \
|
||||
*value = scan->value; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_min(SNAME *_map_, K *key, V *value) \
|
||||
{ \
|
||||
if (PFX##_empty(_map_)) \
|
||||
return false; \
|
||||
\
|
||||
SNAME##_node *scan = _map_->root; \
|
||||
\
|
||||
while (scan->left != NULL) \
|
||||
scan = scan->left; \
|
||||
\
|
||||
*key = scan->key; \
|
||||
*value = scan->value; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_get(SNAME *_map_, K key) \
|
||||
{ \
|
||||
SNAME##_node *node = PFX##_impl_get_node(_map_, key); \
|
||||
\
|
||||
if (!node) \
|
||||
return 0; \
|
||||
\
|
||||
return node->value; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_get_ref(SNAME *_map_, K key) \
|
||||
{ \
|
||||
SNAME##_node *node = PFX##_impl_get_node(_map_, key); \
|
||||
\
|
||||
if (!node) \
|
||||
return NULL; \
|
||||
\
|
||||
return &(node->value); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_contains(SNAME *_map_, K key) \
|
||||
{ \
|
||||
SNAME##_node *scan = _map_->root; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
if (_map_->cmp(scan->key, key) > 0) \
|
||||
scan = scan->left; \
|
||||
else if (_map_->cmp(scan->key, key) < 0) \
|
||||
scan = scan->right; \
|
||||
else \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_empty(SNAME *_map_) \
|
||||
{ \
|
||||
return _map_->count == 0; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_count(SNAME *_map_) \
|
||||
{ \
|
||||
return _map_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
\
|
||||
iter->cursor = target->root; \
|
||||
while (iter->cursor->left != NULL) \
|
||||
iter->cursor = iter->cursor->left; \
|
||||
\
|
||||
iter->first = iter->cursor; \
|
||||
\
|
||||
iter->last = target->root; \
|
||||
while (iter->last->right != NULL) \
|
||||
iter->last = iter->last->right; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor = iter->first; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->index = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
\
|
||||
iter->cursor = iter->last; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = false; \
|
||||
\
|
||||
if (iter->cursor->right != NULL) \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->right; \
|
||||
while (iter->cursor->left != NULL) \
|
||||
iter->cursor = iter->cursor->left; \
|
||||
\
|
||||
iter->index++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
while (true) \
|
||||
{ \
|
||||
if (iter->cursor == iter->last) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
if (iter->cursor->parent->left == iter->cursor) \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->parent; \
|
||||
\
|
||||
iter->index++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
iter->cursor = iter->cursor->parent; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = false; \
|
||||
\
|
||||
if (iter->cursor->left != NULL) \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->left; \
|
||||
\
|
||||
while (iter->cursor->right != NULL) \
|
||||
iter->cursor = iter->cursor->right; \
|
||||
\
|
||||
iter->index--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
while (true) \
|
||||
{ \
|
||||
if (iter->cursor == iter->first) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
if (iter->cursor->parent->right == iter->cursor) \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->parent; \
|
||||
\
|
||||
iter->index--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
iter->cursor = iter->cursor->parent; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
FMOD K PFX##_iter_key(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->cursor->key; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->cursor->value; \
|
||||
} \
|
||||
\
|
||||
FMOD V *PFX##_iter_rvalue(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return &(iter->cursor->value); \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->index; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_node *PFX##_impl_new_node(K key, V value) \
|
||||
{ \
|
||||
SNAME##_node *node = malloc(sizeof(SNAME##_node)); \
|
||||
\
|
||||
if (!node) \
|
||||
return NULL; \
|
||||
\
|
||||
node->key = key; \
|
||||
node->value = value; \
|
||||
node->right = NULL; \
|
||||
node->left = NULL; \
|
||||
node->parent = NULL; \
|
||||
node->height = 0; \
|
||||
\
|
||||
return node; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_node *PFX##_impl_get_node(SNAME *_map_, K key) \
|
||||
{ \
|
||||
if (PFX##_empty(_map_)) \
|
||||
return NULL; \
|
||||
\
|
||||
SNAME##_node *scan = _map_->root; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
if (_map_->cmp(scan->key, key) > 0) \
|
||||
scan = scan->left; \
|
||||
else if (_map_->cmp(scan->key, key) < 0) \
|
||||
scan = scan->right; \
|
||||
else \
|
||||
return scan; \
|
||||
} \
|
||||
\
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
FMOD unsigned char PFX##_impl_h(SNAME##_node *node) \
|
||||
{ \
|
||||
if (node == NULL) \
|
||||
return 0; \
|
||||
\
|
||||
return node->height; \
|
||||
} \
|
||||
\
|
||||
FMOD unsigned char PFX##_impl_hupdate(SNAME##_node *node) \
|
||||
{ \
|
||||
if (node == NULL) \
|
||||
return 0; \
|
||||
\
|
||||
unsigned char h_l = PFX##_impl_h(node->left); \
|
||||
unsigned char h_r = PFX##_impl_h(node->right); \
|
||||
\
|
||||
return 1 + (h_l > h_r ? h_l : h_r); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_impl_rotate_right(SNAME##_node **Z) \
|
||||
{ \
|
||||
SNAME##_node *root = *Z; \
|
||||
SNAME##_node *new_root = root->left; \
|
||||
\
|
||||
if (root->parent != NULL) \
|
||||
{ \
|
||||
if (root->parent->left == root) \
|
||||
root->parent->left = new_root; \
|
||||
else \
|
||||
root->parent->right = new_root; \
|
||||
} \
|
||||
\
|
||||
new_root->parent = root->parent; \
|
||||
\
|
||||
root->parent = new_root; \
|
||||
root->left = new_root->right; \
|
||||
\
|
||||
if (root->left) \
|
||||
root->left->parent = root; \
|
||||
\
|
||||
new_root->right = root; \
|
||||
\
|
||||
root->height = PFX##_impl_hupdate(root); \
|
||||
new_root->height = PFX##_impl_hupdate(new_root); \
|
||||
\
|
||||
*Z = new_root; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_impl_rotate_left(SNAME##_node **Z) \
|
||||
{ \
|
||||
SNAME##_node *root = *Z; \
|
||||
SNAME##_node *new_root = root->right; \
|
||||
\
|
||||
if (root->parent != NULL) \
|
||||
{ \
|
||||
if (root->parent->right == root) \
|
||||
root->parent->right = new_root; \
|
||||
else \
|
||||
root->parent->left = new_root; \
|
||||
} \
|
||||
\
|
||||
new_root->parent = root->parent; \
|
||||
\
|
||||
root->parent = new_root; \
|
||||
root->right = new_root->left; \
|
||||
\
|
||||
if (root->right) \
|
||||
root->right->parent = root; \
|
||||
\
|
||||
new_root->left = root; \
|
||||
\
|
||||
root->height = PFX##_impl_hupdate(root); \
|
||||
new_root->height = PFX##_impl_hupdate(new_root); \
|
||||
\
|
||||
*Z = new_root; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_impl_rebalance(SNAME *_map_, SNAME##_node *node) \
|
||||
{ \
|
||||
SNAME##_node *scan = node, *child = NULL; \
|
||||
\
|
||||
int balance; \
|
||||
bool is_root = false; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
if (scan->parent == NULL) \
|
||||
is_root = true; \
|
||||
\
|
||||
scan->height = PFX##_impl_hupdate(scan); \
|
||||
balance = PFX##_impl_h(scan->right) - PFX##_impl_h(scan->left); \
|
||||
\
|
||||
if (balance >= 2) \
|
||||
{ \
|
||||
child = scan->right; \
|
||||
\
|
||||
if (PFX##_impl_h(child->right) < PFX##_impl_h(child->left)) \
|
||||
PFX##_impl_rotate_right(&(scan->right)); \
|
||||
\
|
||||
PFX##_impl_rotate_left(&scan); \
|
||||
} \
|
||||
else if (balance <= -2) \
|
||||
{ \
|
||||
child = scan->left; \
|
||||
\
|
||||
if (PFX##_impl_h(child->left) < PFX##_impl_h(child->right)) \
|
||||
PFX##_impl_rotate_left(&(scan->left)); \
|
||||
\
|
||||
PFX##_impl_rotate_right(&scan); \
|
||||
} \
|
||||
\
|
||||
if (is_root) \
|
||||
{ \
|
||||
_map_->root = scan; \
|
||||
is_root = false; \
|
||||
} \
|
||||
\
|
||||
scan = scan->parent; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_map_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _map_); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_map_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _map_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_TREEMAP_H */
|
||||
-917
@@ -1,917 +0,0 @@
|
||||
/**
|
||||
* treeset.h
|
||||
*
|
||||
* Creation Date: 27/03/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/*****************************************************************************/
|
||||
/******************************************************************* TREESET */
|
||||
/*****************************************************************************/
|
||||
|
||||
#ifndef CMC_TREESET_H
|
||||
#define CMC_TREESET_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define TREESET_GENERATE(PFX, SNAME, FMOD, V) \
|
||||
TREESET_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
TREESET_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
TREESET_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
|
||||
/* PRIVATE *******************************************************************/
|
||||
#define TREESET_GENERATE_HEADER_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
TREESET_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define TREESET_GENERATE_SOURCE_PRIVATE(PFX, SNAME, FMOD, K, V) \
|
||||
TREESET_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
TREESET_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* PUBLIC ********************************************************************/
|
||||
#define TREESET_GENERATE_HEADER_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
TREESET_GENERATE_STRUCT(PFX, SNAME, FMOD, V) \
|
||||
TREESET_GENERATE_HEADER(PFX, SNAME, FMOD, V)
|
||||
#define TREESET_GENERATE_SOURCE_PUBLIC(PFX, SNAME, FMOD, K, V) \
|
||||
TREESET_GENERATE_SOURCE(PFX, SNAME, FMOD, V)
|
||||
/* STRUCT ********************************************************************/
|
||||
#define TREESET_GENERATE_STRUCT(PFX, SNAME, FMOD, V)
|
||||
|
||||
/* HEADER ********************************************************************/
|
||||
#define TREESET_GENERATE_HEADER(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
/* Treeset Structure */ \
|
||||
typedef struct SNAME##_s \
|
||||
{ \
|
||||
/* Root node */ \
|
||||
struct SNAME##_node_s *root; \
|
||||
\
|
||||
/* Current amount of elements */ \
|
||||
size_t count; \
|
||||
\
|
||||
/* Element comparison function */ \
|
||||
int (*cmp)(V, V); \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the hashset */ \
|
||||
struct SNAME##_iter_s (*it_start)(struct SNAME##_s *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the hashset */ \
|
||||
struct SNAME##_iter_s (*it_end)(struct SNAME##_s *); \
|
||||
\
|
||||
} SNAME, *SNAME##_ptr; \
|
||||
\
|
||||
/* Treeset Node */ \
|
||||
typedef struct SNAME##_node_s \
|
||||
{ \
|
||||
/* Node element */ \
|
||||
V value; \
|
||||
\
|
||||
/* Node height used by the AVL tree to keep it strictly balanced */ \
|
||||
unsigned char height; \
|
||||
\
|
||||
/* Right child node or subtree */ \
|
||||
struct SNAME##_node_s *right; \
|
||||
\
|
||||
/* Left child node or subtree */ \
|
||||
struct SNAME##_node_s *left; \
|
||||
\
|
||||
/* Parent node */ \
|
||||
struct SNAME##_node_s *parent; \
|
||||
\
|
||||
} SNAME##_node, *SNAME##_node_ptr; \
|
||||
\
|
||||
/* Treeset Iterator */ \
|
||||
typedef struct SNAME##_iter_s \
|
||||
{ \
|
||||
/* Target treeset */ \
|
||||
struct SNAME##_s *target; \
|
||||
\
|
||||
/* Cursor's current node */ \
|
||||
struct SNAME##_node_s *cursor; \
|
||||
\
|
||||
/* The first node in the iteration */ \
|
||||
struct SNAME##_node_s *first; \
|
||||
\
|
||||
/* The last node in the iteration */ \
|
||||
struct SNAME##_node_s *last; \
|
||||
\
|
||||
/* Keeps track of relative index to the iteration of elements */ \
|
||||
size_t index; \
|
||||
\
|
||||
/* If the iterator has reached the start of the iteration */ \
|
||||
bool start; \
|
||||
\
|
||||
/* If the iterator has reached the end of the iteration */ \
|
||||
bool end; \
|
||||
} SNAME##_iter, *SNAME##_iter_ptr; \
|
||||
\
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
FMOD SNAME *PFX##_new(int (*compare)(V, V)); \
|
||||
FMOD void PFX##_clear(SNAME *_set_); \
|
||||
FMOD void PFX##_free(SNAME *_set_); \
|
||||
/* Collection Input and Output */ \
|
||||
FMOD bool PFX##_insert(SNAME *_set_, V element); \
|
||||
FMOD bool PFX##_remove(SNAME *_set_, V element); \
|
||||
/* Conditional Input and Output */ \
|
||||
FMOD bool PFX##_insert_if(SNAME *_set_, V element, bool condition); \
|
||||
FMOD bool PFX##_remove_if(SNAME *_set_, V element, bool condition); \
|
||||
/* Element Access */ \
|
||||
FMOD V PFX##_max(SNAME *_set_); \
|
||||
FMOD V PFX##_min(SNAME *_set_); \
|
||||
/* Collection State */ \
|
||||
FMOD bool PFX##_contains(SNAME *_set_, V element); \
|
||||
FMOD bool PFX##_empty(SNAME *_set_); \
|
||||
FMOD size_t PFX##_count(SNAME *_set_); \
|
||||
\
|
||||
/* Set Operations */ \
|
||||
FMOD SNAME *PFX##_union(SNAME *_set1_, SNAME *_set2_); \
|
||||
FMOD SNAME *PFX##_intersection(SNAME *_set1_, SNAME *_set2_); \
|
||||
FMOD SNAME *PFX##_difference(SNAME *_set1_, SNAME *_set2_); \
|
||||
FMOD SNAME *PFX##_symmetric_difference(SNAME *_set1_, SNAME *_set2_); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target); \
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter); \
|
||||
/* Iterator Access */ \
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter); \
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter); \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define TREESET_GENERATE_SOURCE(PFX, SNAME, FMOD, V) \
|
||||
\
|
||||
FMOD SNAME##_node *PFX##_new_node(V element); \
|
||||
FMOD SNAME##_node *PFX##_get_node(SNAME *_set_, V element); \
|
||||
FMOD unsigned char PFX##_h(SNAME##_node *node); \
|
||||
FMOD unsigned char PFX##_hupdate(SNAME##_node *node); \
|
||||
FMOD void PFX##_rotate_right(SNAME##_node **Z); \
|
||||
FMOD void PFX##_rotate_left(SNAME##_node **Z); \
|
||||
FMOD void PFX##_rebalance(SNAME *_set_, SNAME##_node *node); \
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_set_); \
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_set_); \
|
||||
\
|
||||
FMOD SNAME *PFX##_new(int (*compare)(V, V)) \
|
||||
{ \
|
||||
SNAME *_set_ = malloc(sizeof(SNAME)); \
|
||||
\
|
||||
if (!_set_) \
|
||||
return NULL; \
|
||||
\
|
||||
_set_->count = 0; \
|
||||
_set_->root = NULL; \
|
||||
_set_->cmp = compare; \
|
||||
\
|
||||
_set_->it_start = PFX##_impl_it_start; \
|
||||
_set_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _set_; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_clear(SNAME *_set_) \
|
||||
{ \
|
||||
SNAME##_node *scan = _set_->root; \
|
||||
SNAME##_node *up = NULL; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
if (scan->left != NULL) \
|
||||
{ \
|
||||
SNAME##_node *left = scan->left; \
|
||||
\
|
||||
scan->left = up; \
|
||||
up = scan; \
|
||||
scan = left; \
|
||||
} \
|
||||
else if (scan->right != NULL) \
|
||||
{ \
|
||||
SNAME##_node *right = scan->right; \
|
||||
\
|
||||
scan->left = up; \
|
||||
scan->right = NULL; \
|
||||
up = scan; \
|
||||
scan = right; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
if (up == NULL) \
|
||||
{ \
|
||||
free(scan); \
|
||||
scan = NULL; \
|
||||
} \
|
||||
\
|
||||
while (up != NULL) \
|
||||
{ \
|
||||
free(scan); \
|
||||
\
|
||||
if (up->right != NULL) \
|
||||
{ \
|
||||
scan = up->right; \
|
||||
up->right = NULL; \
|
||||
break; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
scan = up; \
|
||||
up = up->left; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
_set_->count = 0; \
|
||||
_set_->root = NULL; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_free(SNAME *_set_) \
|
||||
{ \
|
||||
PFX##_clear(_set_); \
|
||||
\
|
||||
free(_set_); \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_insert(SNAME *_set_, V element) \
|
||||
{ \
|
||||
if (PFX##_empty(_set_)) \
|
||||
{ \
|
||||
_set_->root = PFX##_new_node(element); \
|
||||
\
|
||||
if (!_set_->root) \
|
||||
return false; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
SNAME##_node *scan = _set_->root; \
|
||||
SNAME##_node *parent = scan; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
parent = scan; \
|
||||
\
|
||||
if (_set_->cmp(scan->value, element) > 0) \
|
||||
scan = scan->left; \
|
||||
else if (_set_->cmp(scan->value, element) < 0) \
|
||||
scan = scan->right; \
|
||||
else \
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
SNAME##_node *node; \
|
||||
\
|
||||
if (_set_->cmp(parent->value, element) > 0) \
|
||||
{ \
|
||||
parent->left = PFX##_new_node(element); \
|
||||
\
|
||||
if (!parent->left) \
|
||||
return false; \
|
||||
\
|
||||
parent->left->parent = parent; \
|
||||
node = parent->left; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
parent->right = PFX##_new_node(element); \
|
||||
\
|
||||
if (!parent->right) \
|
||||
return false; \
|
||||
\
|
||||
parent->right->parent = parent; \
|
||||
node = parent->right; \
|
||||
} \
|
||||
\
|
||||
PFX##_rebalance(_set_, node); \
|
||||
} \
|
||||
\
|
||||
_set_->count++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_remove(SNAME *_set_, V element) \
|
||||
{ \
|
||||
SNAME##_node *node = PFX##_get_node(_set_, element); \
|
||||
\
|
||||
if (!node) \
|
||||
return false; \
|
||||
\
|
||||
SNAME##_node *temp = NULL, *unbalanced = NULL; \
|
||||
\
|
||||
bool is_root = node->parent == NULL; \
|
||||
\
|
||||
if (node->left == NULL && node->right == NULL) \
|
||||
{ \
|
||||
if (is_root) \
|
||||
_set_->root = NULL; \
|
||||
else \
|
||||
{ \
|
||||
unbalanced = node->parent; \
|
||||
\
|
||||
if (node->parent->right == node) \
|
||||
node->parent->right = NULL; \
|
||||
else \
|
||||
node->parent->left = NULL; \
|
||||
} \
|
||||
\
|
||||
free(node); \
|
||||
} \
|
||||
else if (node->left == NULL) \
|
||||
{ \
|
||||
if (is_root) \
|
||||
{ \
|
||||
_set_->root = node->right; \
|
||||
_set_->root->parent = NULL; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
unbalanced = node->parent; \
|
||||
\
|
||||
node->right->parent = node->parent; \
|
||||
\
|
||||
if (node->parent->right == node) \
|
||||
node->parent->right = node->right; \
|
||||
else \
|
||||
node->parent->left = node->right; \
|
||||
} \
|
||||
\
|
||||
free(node); \
|
||||
} \
|
||||
else if (node->right == NULL) \
|
||||
{ \
|
||||
if (is_root) \
|
||||
{ \
|
||||
_set_->root = node->left; \
|
||||
_set_->root->parent = NULL; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
unbalanced = node->parent; \
|
||||
\
|
||||
node->left->parent = node->parent; \
|
||||
\
|
||||
if (node->parent->right == node) \
|
||||
node->parent->right = node->left; \
|
||||
else \
|
||||
node->parent->left = node->left; \
|
||||
} \
|
||||
\
|
||||
free(node); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
temp = node->right; \
|
||||
while (temp->left != NULL) \
|
||||
temp = temp->left; \
|
||||
\
|
||||
V temp_value = temp->value; \
|
||||
\
|
||||
unbalanced = temp->parent; \
|
||||
\
|
||||
if (temp->left == NULL && temp->right == NULL) \
|
||||
{ \
|
||||
if (temp->parent->right == temp) \
|
||||
temp->parent->right = NULL; \
|
||||
else \
|
||||
temp->parent->left = NULL; \
|
||||
} \
|
||||
else if (temp->left == NULL) \
|
||||
{ \
|
||||
temp->right->parent = temp->parent; \
|
||||
\
|
||||
if (temp->parent->right == temp) \
|
||||
temp->parent->right = temp->right; \
|
||||
else \
|
||||
temp->parent->left = temp->right; \
|
||||
} \
|
||||
else if (temp->right == NULL) \
|
||||
{ \
|
||||
temp->left->parent = temp->parent; \
|
||||
\
|
||||
if (temp->parent->right == temp) \
|
||||
temp->parent->right = temp->left; \
|
||||
else \
|
||||
temp->parent->left = temp->left; \
|
||||
} \
|
||||
\
|
||||
free(temp); \
|
||||
\
|
||||
node->value = temp_value; \
|
||||
} \
|
||||
\
|
||||
if (unbalanced != NULL) \
|
||||
PFX##_rebalance(_set_, unbalanced); \
|
||||
\
|
||||
_set_->count--; \
|
||||
\
|
||||
if (_set_->count == 0) \
|
||||
_set_->root = NULL; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_insert_if(SNAME *_set_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_insert(_set_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_remove_if(SNAME *_set_, V element, bool condition) \
|
||||
{ \
|
||||
if (condition) \
|
||||
return PFX##_remove(_set_, element); \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_max(SNAME *_set_) \
|
||||
{ \
|
||||
if (PFX##_empty(_set_)) \
|
||||
return 0; \
|
||||
\
|
||||
SNAME##_node *scan = _set_->root; \
|
||||
\
|
||||
while (scan->right != NULL) \
|
||||
scan = scan->right; \
|
||||
\
|
||||
return scan->value; \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_min(SNAME *_set_) \
|
||||
{ \
|
||||
if (PFX##_empty(_set_)) \
|
||||
return 0; \
|
||||
\
|
||||
SNAME##_node *scan = _set_->root; \
|
||||
\
|
||||
while (scan->left != NULL) \
|
||||
scan = scan->left; \
|
||||
\
|
||||
return scan->value; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_contains(SNAME *_set_, V element) \
|
||||
{ \
|
||||
SNAME##_node *scan = _set_->root; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
if (_set_->cmp(scan->value, element) > 0) \
|
||||
scan = scan->left; \
|
||||
else if (_set_->cmp(scan->value, element) < 0) \
|
||||
scan = scan->right; \
|
||||
else \
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_empty(SNAME *_set_) \
|
||||
{ \
|
||||
return _set_->count == 0; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_count(SNAME *_set_) \
|
||||
{ \
|
||||
return _set_->count; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME *PFX##_union(SNAME *_set1_, SNAME *_set2_) \
|
||||
{ \
|
||||
SNAME *_set_r_ = PFX##_new(_set1_->cmp); \
|
||||
\
|
||||
if (!_set_r_) \
|
||||
return false; \
|
||||
\
|
||||
SNAME##_iter iter1, iter2; \
|
||||
PFX##_iter_init(&iter1, _set1_); \
|
||||
PFX##_iter_init(&iter2, _set2_); \
|
||||
\
|
||||
for (PFX##_iter_to_start(&iter1); !PFX##_iter_end(&iter1); PFX##_iter_next(&iter1)) \
|
||||
{ \
|
||||
PFX##_insert(_set_r_, PFX##_iter_value(&iter1)); \
|
||||
} \
|
||||
\
|
||||
for (PFX##_iter_to_start(&iter2); !PFX##_iter_end(&iter2); PFX##_iter_next(&iter2)) \
|
||||
{ \
|
||||
PFX##_insert(_set_r_, PFX##_iter_value(&iter2)); \
|
||||
} \
|
||||
\
|
||||
return _set_r_; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME *PFX##_intersection(SNAME *_set1_, SNAME *_set2_) \
|
||||
{ \
|
||||
SNAME *_set_r_ = PFX##_new(_set1_->cmp); \
|
||||
\
|
||||
if (!_set_r_) \
|
||||
return false; \
|
||||
\
|
||||
SNAME *_set_A_ = _set1_->count < _set2_->count ? _set1_ : _set2_; \
|
||||
SNAME *_set_B_ = _set_A_ == _set1_ ? _set2_ : _set1_; \
|
||||
\
|
||||
SNAME##_iter iter; \
|
||||
PFX##_iter_init(&iter, _set_A_); \
|
||||
\
|
||||
for (PFX##_iter_to_start(&iter); !PFX##_iter_end(&iter); PFX##_iter_next(&iter)) \
|
||||
{ \
|
||||
V value = PFX##_iter_value(&iter); \
|
||||
\
|
||||
if (PFX##_get_node(_set_B_, value) != NULL) \
|
||||
PFX##_insert(_set_r_, value); \
|
||||
} \
|
||||
\
|
||||
return _set_r_; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME *PFX##_difference(SNAME *_set1_, SNAME *_set2_) \
|
||||
{ \
|
||||
SNAME *_set_r_ = PFX##_new(_set1_->cmp); \
|
||||
\
|
||||
if (!_set_r_) \
|
||||
return false; \
|
||||
\
|
||||
SNAME##_iter iter; \
|
||||
PFX##_iter_init(&iter, _set1_); \
|
||||
\
|
||||
for (PFX##_iter_to_start(&iter); !PFX##_iter_end(&iter); PFX##_iter_next(&iter)) \
|
||||
{ \
|
||||
V value = PFX##_iter_value(&iter); \
|
||||
\
|
||||
if (PFX##_get_node(_set2_, value) == NULL) \
|
||||
PFX##_insert(_set_r_, value); \
|
||||
} \
|
||||
\
|
||||
return _set_r_; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME *PFX##_symmetric_difference(SNAME *_set1_, SNAME *_set2_) \
|
||||
{ \
|
||||
SNAME *_set_r_ = PFX##_new(_set1_->cmp); \
|
||||
\
|
||||
if (!_set_r_) \
|
||||
return false; \
|
||||
\
|
||||
SNAME##_iter iter1, iter2; \
|
||||
PFX##_iter_init(&iter1, _set1_); \
|
||||
PFX##_iter_init(&iter2, _set2_); \
|
||||
\
|
||||
for (PFX##_iter_to_start(&iter1); !PFX##_iter_end(&iter1); PFX##_iter_next(&iter1)) \
|
||||
{ \
|
||||
V value = PFX##_iter_value(&iter1); \
|
||||
\
|
||||
if (PFX##_get_node(_set2_, value) == NULL) \
|
||||
PFX##_insert(_set_r_, value); \
|
||||
} \
|
||||
\
|
||||
for (PFX##_iter_to_start(&iter2); !PFX##_iter_end(&iter2); PFX##_iter_next(&iter2)) \
|
||||
{ \
|
||||
V value = PFX##_iter_value(&iter2); \
|
||||
\
|
||||
if (PFX##_get_node(_set1_, value) == NULL) \
|
||||
PFX##_insert(_set_r_, value); \
|
||||
} \
|
||||
\
|
||||
return _set_r_; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_iter *PFX##_iter_new(SNAME *target) \
|
||||
{ \
|
||||
SNAME##_iter *iter = malloc(sizeof(SNAME##_iter)); \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_free(SNAME##_iter *iter) \
|
||||
{ \
|
||||
free(iter); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
\
|
||||
iter->cursor = target->root; \
|
||||
while (iter->cursor->left != NULL) \
|
||||
iter->cursor = iter->cursor->left; \
|
||||
\
|
||||
iter->first = iter->cursor; \
|
||||
\
|
||||
iter->last = target->root; \
|
||||
while (iter->last->right != NULL) \
|
||||
iter->last = iter->last->right; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
iter->cursor = iter->first; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_to_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->index = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
\
|
||||
iter->cursor = iter->last; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->cursor->right != NULL) \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->right; \
|
||||
while (iter->cursor->left != NULL) \
|
||||
iter->cursor = iter->cursor->left; \
|
||||
\
|
||||
iter->index++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
while (true) \
|
||||
{ \
|
||||
if (iter->cursor == iter->last) \
|
||||
{ \
|
||||
iter->end = true; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
if (iter->cursor->parent->left == iter->cursor) \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->parent; \
|
||||
\
|
||||
iter->index++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
iter->cursor = iter->cursor->parent; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
if (iter->cursor->left != NULL) \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->left; \
|
||||
while (iter->cursor->right != NULL) \
|
||||
iter->cursor = iter->cursor->right; \
|
||||
\
|
||||
iter->index--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
while (true) \
|
||||
{ \
|
||||
if (iter->cursor == iter->first) \
|
||||
{ \
|
||||
iter->start = true; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
if (iter->cursor->parent->right == iter->cursor) \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->parent; \
|
||||
\
|
||||
iter->index--; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
iter->cursor = iter->cursor->parent; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
FMOD V PFX##_iter_value(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->cursor->value; \
|
||||
} \
|
||||
\
|
||||
FMOD size_t PFX##_iter_index(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->index; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_node *PFX##_new_node(V element) \
|
||||
{ \
|
||||
SNAME##_node *node = malloc(sizeof(SNAME##_node)); \
|
||||
\
|
||||
if (!node) \
|
||||
return NULL; \
|
||||
\
|
||||
node->value = element; \
|
||||
node->right = NULL; \
|
||||
node->left = NULL; \
|
||||
node->parent = NULL; \
|
||||
node->height = 0; \
|
||||
\
|
||||
return node; \
|
||||
} \
|
||||
\
|
||||
FMOD SNAME##_node *PFX##_get_node(SNAME *_set_, V element) \
|
||||
{ \
|
||||
if (PFX##_empty(_set_)) \
|
||||
return NULL; \
|
||||
\
|
||||
SNAME##_node *scan = _set_->root; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
if (_set_->cmp(scan->value, element) > 0) \
|
||||
scan = scan->left; \
|
||||
else if (_set_->cmp(scan->value, element) < 0) \
|
||||
scan = scan->right; \
|
||||
else \
|
||||
return scan; \
|
||||
} \
|
||||
\
|
||||
return NULL; \
|
||||
} \
|
||||
\
|
||||
FMOD unsigned char PFX##_h(SNAME##_node *node) \
|
||||
{ \
|
||||
if (node == NULL) \
|
||||
return 0; \
|
||||
\
|
||||
return node->height; \
|
||||
} \
|
||||
\
|
||||
FMOD unsigned char PFX##_hupdate(SNAME##_node *node) \
|
||||
{ \
|
||||
if (node == NULL) \
|
||||
return 0; \
|
||||
\
|
||||
unsigned char h_l = PFX##_h(node->left); \
|
||||
unsigned char h_r = PFX##_h(node->right); \
|
||||
\
|
||||
return 1 + (h_l > h_r ? h_l : h_r); \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_rotate_right(SNAME##_node **Z) \
|
||||
{ \
|
||||
SNAME##_node *root = *Z; \
|
||||
SNAME##_node *new_root = root->left; \
|
||||
\
|
||||
if (root->parent != NULL) \
|
||||
{ \
|
||||
if (root->parent->left == root) \
|
||||
root->parent->left = new_root; \
|
||||
else \
|
||||
root->parent->right = new_root; \
|
||||
} \
|
||||
\
|
||||
new_root->parent = root->parent; \
|
||||
\
|
||||
root->parent = new_root; \
|
||||
root->left = new_root->right; \
|
||||
\
|
||||
if (root->left) \
|
||||
root->left->parent = root; \
|
||||
\
|
||||
new_root->right = root; \
|
||||
\
|
||||
root->height = PFX##_hupdate(root); \
|
||||
new_root->height = PFX##_hupdate(new_root); \
|
||||
\
|
||||
*Z = new_root; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_rotate_left(SNAME##_node **Z) \
|
||||
{ \
|
||||
SNAME##_node *root = *Z; \
|
||||
SNAME##_node *new_root = root->right; \
|
||||
\
|
||||
if (root->parent != NULL) \
|
||||
{ \
|
||||
if (root->parent->right == root) \
|
||||
root->parent->right = new_root; \
|
||||
else \
|
||||
root->parent->left = new_root; \
|
||||
} \
|
||||
\
|
||||
new_root->parent = root->parent; \
|
||||
\
|
||||
root->parent = new_root; \
|
||||
root->right = new_root->left; \
|
||||
\
|
||||
if (root->right) \
|
||||
root->right->parent = root; \
|
||||
\
|
||||
new_root->left = root; \
|
||||
\
|
||||
root->height = PFX##_hupdate(root); \
|
||||
new_root->height = PFX##_hupdate(new_root); \
|
||||
\
|
||||
*Z = new_root; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_rebalance(SNAME *_set_, SNAME##_node *node) \
|
||||
{ \
|
||||
SNAME##_node *scan = node, *child = NULL; \
|
||||
\
|
||||
int balance; \
|
||||
bool is_root = false; \
|
||||
\
|
||||
while (scan != NULL) \
|
||||
{ \
|
||||
if (scan->parent == NULL) \
|
||||
is_root = true; \
|
||||
\
|
||||
scan->height = PFX##_hupdate(scan); \
|
||||
balance = PFX##_h(scan->right) - PFX##_h(scan->left); \
|
||||
\
|
||||
if (balance >= 2) \
|
||||
{ \
|
||||
child = scan->right; \
|
||||
\
|
||||
if (PFX##_h(child->right) < PFX##_h(child->left)) \
|
||||
PFX##_rotate_right(&(scan->right)); \
|
||||
\
|
||||
PFX##_rotate_left(&scan); \
|
||||
} \
|
||||
else if (balance <= -2) \
|
||||
{ \
|
||||
child = scan->left; \
|
||||
\
|
||||
if (PFX##_h(child->left) < PFX##_h(child->right)) \
|
||||
PFX##_rotate_left(&(scan->left)); \
|
||||
\
|
||||
PFX##_rotate_right(&scan); \
|
||||
} \
|
||||
\
|
||||
if (is_root) \
|
||||
{ \
|
||||
_set_->root = scan; \
|
||||
is_root = false; \
|
||||
} \
|
||||
\
|
||||
scan = scan->parent; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_start(SNAME *_set_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _set_); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
SNAME##_iter PFX##_impl_it_end(SNAME *_set_) \
|
||||
{ \
|
||||
SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _set_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_TREESET_H */
|
||||
+2131
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* cmc_string.h
|
||||
*
|
||||
* Creation Date: 17/07/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/* A very simple fixed size string used by all collections' method to_string */
|
||||
|
||||
#ifndef CMC_STRING_H
|
||||
#define CMC_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
static const size_t cmc_string_len = 200;
|
||||
|
||||
typedef struct cmc_string_s
|
||||
{
|
||||
char s[200];
|
||||
} cmc_string;
|
||||
|
||||
/* to_string formats */
|
||||
static const char *cmc_string_fmt_deque = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", front:%" PRIuMAX ", back:%" PRIuMAX " }";
|
||||
static const char *cmc_string_fmt_hashmap = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", load:%lf, cmp:%p, hash:%p }";
|
||||
static const char *cmc_string_fmt_hashset = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", load:%lf, cmp:%p, hash:%p }";
|
||||
static const char *cmc_string_fmt_heap = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", type:%s, cmp:%p }";
|
||||
static const char *cmc_string_fmt_linkedlist = "%s at %p { count:%" PRIuMAX ", head:%p, tail:%p }";
|
||||
static const char *cmc_string_fmt_list = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX " }";
|
||||
static const char *cmc_string_fmt_queue = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", front:%" PRIuMAX ", back:%" PRIuMAX " }";
|
||||
static const char *cmc_string_fmt_stack = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX " }";
|
||||
static const char *cmc_string_fmt_treemap = "%s at %p { root:%p, count:%" PRIuMAX ", cmp:%p }";
|
||||
static const char *cmc_string_fmt_treeset = "%s at %p { root:%p, count:%" PRIuMAX ", cmp:%p }";
|
||||
|
||||
static const char *cmc_string_fmt_intervalheap = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", size:%" PRIuMAX ", count:%" PRIuMAX ", cmp:%p }";
|
||||
static const char *cmc_string_fmt_multimap = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", load:%lf, cmp:%p, hash:%p }";
|
||||
static const char *cmc_string_fmt_multiset = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", cardinality:%" PRIuMAX ", load:%lf, cmp:%p, hash:%p }";;
|
||||
|
||||
#endif /* CMC_STRING_H */
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* for_each.h
|
||||
*
|
||||
* Creation Date: 25/02/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CMC_FOR_EACH_H
|
||||
#define CMC_FOR_EACH_H
|
||||
|
||||
#define CMC_FOR_EACH(PFX, SNAME, TARGET, BODY) \
|
||||
do \
|
||||
{ \
|
||||
for (SNAME##_iter iter = (TARGET)->it_start(TARGET); !PFX##_iter_end(&iter); PFX##_iter_next(&iter)) \
|
||||
{ \
|
||||
BODY; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define CMC_FOR_EACH_REV(PFX, SNAME, TARGET, BODY) \
|
||||
do \
|
||||
{ \
|
||||
for (SNAME##_iter iter = (TARGET)->it_end(TARGET); !PFX##_iter_start(&iter); PFX##_iter_prev(&iter)) \
|
||||
{ \
|
||||
BODY; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#endif /* CMC_FOR_EACH_H */
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* log.h
|
||||
*
|
||||
* Creation Date: 21/06/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple logging macros
|
||||
* You can toggle colors defining the macro CMC_LOG_COLOR but be sure that
|
||||
* your terminal supports ANSI color escape codes.
|
||||
*
|
||||
* You can customize the logging utility by accessing cmc_log_config and
|
||||
* directly modifying its values to better suit your needs.
|
||||
*/
|
||||
|
||||
/**
|
||||
* What each error code means
|
||||
*
|
||||
* TRACE - Trace is used to describe the inner workings of the function or an
|
||||
* algorithm. It can be useful when trying to find for an error in the
|
||||
* algorithm or an untreated case.
|
||||
* DEBUG - Mainly used for debugging, tracking certain variables or anything
|
||||
* useful to help visualizing what your program is doing.
|
||||
* INFO - Info can be used as a heads up. It is also used to track the main
|
||||
* collection's allocation and deallocation.
|
||||
* WARN - Warnings are attributed to all cases where the function can recover
|
||||
* from and they are usually treated by it. In most cases the user can
|
||||
* treat these warnings himself.
|
||||
* ERROR - Something really bad happened. Your program will not crash yet but
|
||||
* depending on what comes next, it might crash indeed.
|
||||
* 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.
|
||||
*
|
||||
* Log Level
|
||||
*
|
||||
* TRACE - 1
|
||||
* DEBUG - 2
|
||||
* INFO - 3
|
||||
* WARN - 4
|
||||
* ERROR - 5
|
||||
* FATAL - 6
|
||||
*
|
||||
* How the Log Level is calculated
|
||||
*
|
||||
* - If the log level is 0 all logs are enabled.
|
||||
* - If the log level (X) is positive:
|
||||
* - All levels less than X will be disabled
|
||||
* - If X is greater than the level of FATAL, all logs are disabled
|
||||
* - If the log level (X) is negative:
|
||||
* - ALl levels greater than X are enabled
|
||||
* - If X is less than the level of FATAL, all logs are enabled
|
||||
*/
|
||||
|
||||
#ifndef CMC_LOG_H
|
||||
#define CMC_LOG_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
static const char *cmc_log_names[] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"};
|
||||
|
||||
#ifdef CMC_LOG_COLOR
|
||||
|
||||
static const char *cmc_log_color[] = {"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"};
|
||||
|
||||
#endif
|
||||
|
||||
static struct
|
||||
{
|
||||
int tlevel; /* Terminal Level */
|
||||
int flevel; /* File Level */
|
||||
bool tenabled; /* Terminal Output Enabled */
|
||||
bool fenabled; /* File Output Enabled */
|
||||
FILE *file; /* File for Output */
|
||||
bool enabled; /* Logging enabled */
|
||||
|
||||
} cmc_log_config = {0, 0, true, true, NULL, true};
|
||||
|
||||
typedef enum cmc_log_type_e
|
||||
{
|
||||
CMC_LOG_TRACE = 1,
|
||||
CMC_LOG_DEBUG = 2,
|
||||
CMC_LOG_INFO = 3,
|
||||
CMC_LOG_WARN = 4,
|
||||
CMC_LOG_ERROR = 5,
|
||||
CMC_LOG_FATAL = 6
|
||||
|
||||
} cmc_log_type;
|
||||
|
||||
#define cmc_log_trace(fmt, ...) cmc_log(CMC_LOG_TRACE, __FILE__, __func__, __LINE__, fmt, __VA_ARGS__)
|
||||
#define cmc_log_debug(fmt, ...) cmc_log(CMC_LOG_DEBUG, __FILE__, __func__, __LINE__, fmt, __VA_ARGS__)
|
||||
#define cmc_log_info(fmt, ...) cmc_log(CMC_LOG_INFO, __FILE__, __func__, __LINE__, fmt, __VA_ARGS__)
|
||||
#define cmc_log_warn(fmt, ...) cmc_log(CMC_LOG_WARN, __FILE__, __func__, __LINE__, fmt, __VA_ARGS__)
|
||||
#define cmc_log_error(fmt, ...) cmc_log(CMC_LOG_ERROR, __FILE__, __func__, __LINE__, fmt, __VA_ARGS__)
|
||||
#define cmc_log_fatal(fmt, ...) cmc_log(CMC_LOG_FATAL, __FILE__, __func__, __LINE__, fmt, __VA_ARGS__)
|
||||
|
||||
static void cmc_log(cmc_log_type log, const char *filename, const char *funcname, int line, const char *fmt, ...)
|
||||
{
|
||||
if (!cmc_log_config.enabled)
|
||||
return;
|
||||
|
||||
if (cmc_log_config.tenabled)
|
||||
{
|
||||
if (cmc_log_config.tlevel < 0 && ((int)log * -1) < cmc_log_config.tlevel)
|
||||
return;
|
||||
else if (cmc_log_config.tlevel > 0 && (int)log < cmc_log_config.tlevel)
|
||||
return;
|
||||
|
||||
int i = (int)log - 1;
|
||||
|
||||
time_t t = time(NULL);
|
||||
struct tm *lt = localtime(&t);
|
||||
|
||||
char time[16];
|
||||
time[strftime(time, sizeof(time), "%H:%M:%S", lt)] = '\0';
|
||||
|
||||
#ifdef CMC_LOG_COLOR
|
||||
fprintf(stderr,
|
||||
"%s %s%5s\x1b[0m \x1b[90m%s at %s:%d:\x1b[0m ",
|
||||
time, cmc_log_color[i], cmc_log_names[i], filename, funcname, line);
|
||||
#else
|
||||
fprintf(stderr,
|
||||
"%s %5s %s at %s:%d: ",
|
||||
time, cmc_log_names[i], filename, funcname, line);
|
||||
#endif
|
||||
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
if (cmc_log_config.file && cmc_log_config.fenabled)
|
||||
{
|
||||
if (cmc_log_config.flevel < 0 && ((int)log * -1) < cmc_log_config.flevel)
|
||||
return;
|
||||
else if (cmc_log_config.flevel > 0 && (int)log < cmc_log_config.flevel)
|
||||
return;
|
||||
|
||||
int i = (int)log - 1;
|
||||
|
||||
time_t t = time(NULL);
|
||||
struct tm *lt = localtime(&t);
|
||||
|
||||
char time[32];
|
||||
time[strftime(time, sizeof(time), "%Y-%m-%d %H:%M:%S", lt)] = '\0';
|
||||
|
||||
fprintf(cmc_log_config.file, "%s %5s %s at %s:%d: ", time, cmc_log_names[i], filename, funcname, line);
|
||||
|
||||
va_list file_args;
|
||||
va_start(file_args, fmt);
|
||||
vfprintf(cmc_log_config.file, fmt, file_args);
|
||||
va_end(file_args);
|
||||
|
||||
fprintf(cmc_log_config.file, "\n");
|
||||
fflush(cmc_log_config.file);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CMC_LOG_H */
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* test.h
|
||||
*
|
||||
* Creation Date: 26/06/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple Unit Test Utility
|
||||
*
|
||||
* Required imports:
|
||||
* - assert.h (to integrate with the cmc_assert_state variable)
|
||||
* - timer.h (to calculate the execution time for the unit test)
|
||||
*
|
||||
* CMC_CREATE_UNIT
|
||||
* Create a UnitTest
|
||||
* Parameters:
|
||||
* - UNAME : UnitTest name (const char *)
|
||||
* - VERBOSE : Print PASSED / FAILED messages if true (bool)
|
||||
* - BODY : Body containing all tests related to this UnitTest
|
||||
*
|
||||
* CMC_CREATE_TEST
|
||||
* Create a Test inside a UnitTest.
|
||||
* Parameters:
|
||||
* - TNAME : Test name (const char *)
|
||||
* - BODY : Block of code containing a specific test
|
||||
* Inside the BODY use the following macros:
|
||||
* - CMC_TEST_PASS
|
||||
* Pass current test.
|
||||
* - CMC_TEST_FAIL
|
||||
* Fail current test.
|
||||
* - CMC_TEST_PASS_ELSE_FAIL
|
||||
* If given expression is true, pass current test, else fail.
|
||||
* - CMC_TEST_ABORT
|
||||
* Abort current unit test and displays error message. All tests
|
||||
* after the abort won't be called.
|
||||
* All of these macros can only be called once per test.
|
||||
*
|
||||
* CMC_TEST_COLOR
|
||||
* Define this macro to allow colored output.
|
||||
*/
|
||||
|
||||
#ifndef CMC_TEST_H
|
||||
#define CMC_TEST_H
|
||||
|
||||
#include "assert.h"
|
||||
#include "timer.h"
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef CMC_TEST_COLOR
|
||||
static const char *cmc_test_color[] = {"\x1b[32m", "\x1b[31m", "\x1b[35m"};
|
||||
#endif
|
||||
|
||||
typedef struct cmc_test_info_s
|
||||
{
|
||||
uintmax_t total;
|
||||
uintmax_t passed;
|
||||
uintmax_t failed;
|
||||
bool aborted;
|
||||
bool verbose;
|
||||
} cmc_test_info;
|
||||
|
||||
/* Logging function */
|
||||
static void cmc_test_log(const char *unit_name, const char *current_test, bool aborted, bool passed)
|
||||
{
|
||||
if (aborted)
|
||||
{
|
||||
#ifdef CMC_TEST_COLOR
|
||||
printf("UNIT_TEST %s %sABORTED\x1b[0m AT %s\n",
|
||||
unit_name,
|
||||
cmc_test_color[2],
|
||||
current_test);
|
||||
#else
|
||||
printf("UNIT_TEST %s ABORTED at %s\n", unit_name, current_test);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef CMC_TEST_COLOR
|
||||
printf("Test from %s %s%7s\x1b[0m %s\n",
|
||||
unit_name,
|
||||
passed ? cmc_test_color[0] : cmc_test_color[1],
|
||||
passed ? "PASSED" : "FAILED",
|
||||
current_test);
|
||||
#else
|
||||
printf("Test from %s %7s %s\n", unit_name, passed ? "PASSED" : "FAILED", current_test);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#define CMC_CREATE_UNIT(UNAME, VERBOSE, BODY) \
|
||||
uintmax_t UNAME(void) \
|
||||
{ \
|
||||
const char *unit_name = #UNAME; \
|
||||
const char *current_test = NULL; \
|
||||
\
|
||||
cmc_test_info tinfo = {0}; \
|
||||
cmc_timer timer = {0}; \
|
||||
\
|
||||
tinfo.verbose = VERBOSE; \
|
||||
\
|
||||
/* Tests */ \
|
||||
timer_start(timer); \
|
||||
\
|
||||
BODY; \
|
||||
\
|
||||
timer_stop(timer); \
|
||||
timer_calc(timer); \
|
||||
\
|
||||
unittest_abort: \
|
||||
if (tinfo.aborted) \
|
||||
{ \
|
||||
cmc_test_log(unit_name, current_test, true, false); \
|
||||
} \
|
||||
\
|
||||
printf("+--------------------------------------------------+\n"); \
|
||||
printf("| Unit Test Report : %-30s|\n", unit_name); \
|
||||
printf("| TOTAL : %10" PRIuMAX " |\n", tinfo.total); \
|
||||
printf("| PASSED : %10" PRIuMAX " |\n", tinfo.passed); \
|
||||
printf("| FAILED : %10" PRIuMAX " |\n", tinfo.failed); \
|
||||
printf("| |\n"); \
|
||||
printf("| Total Unit Test Runtime : %9.0f milliseconds |\n", timer.result); \
|
||||
printf("+--------------------------------------------------+\n"); \
|
||||
\
|
||||
return tinfo.failed; \
|
||||
}
|
||||
|
||||
#define CMC_CREATE_TEST(TNAME, BODY) \
|
||||
do \
|
||||
{ \
|
||||
current_test = #TNAME; \
|
||||
\
|
||||
tinfo.total += 1; \
|
||||
\
|
||||
/* Provided by assert.h */ \
|
||||
cmc_assert_state = true; \
|
||||
\
|
||||
BODY; \
|
||||
\
|
||||
if (!cmc_assert_state) \
|
||||
{ \
|
||||
tinfo.failed += 1; \
|
||||
cmc_test_log(unit_name, current_test, false, false); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
tinfo.passed += 1; \
|
||||
cmc_test_log(unit_name, current_test, false, true); \
|
||||
} \
|
||||
\
|
||||
} while (0)
|
||||
|
||||
#define CMC_TEST_ABORT() \
|
||||
do \
|
||||
{ \
|
||||
tinfo.aborted = true; \
|
||||
tinfo.total -= 1; \
|
||||
goto unittest_abort; \
|
||||
\
|
||||
} while (0)
|
||||
|
||||
#endif /* CMC_TEST_H */
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* timer.h
|
||||
*
|
||||
* Creation Date: 12/04/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/* Simple timer macros utility */
|
||||
|
||||
#ifndef CMC_TIMER_H
|
||||
#define CMC_TIMER_H
|
||||
|
||||
#include <time.h>
|
||||
|
||||
typedef struct cmc_timer_s
|
||||
{
|
||||
clock_t start;
|
||||
clock_t stop;
|
||||
double result;
|
||||
} cmc_timer;
|
||||
|
||||
#define timer_start(timer) \
|
||||
do \
|
||||
{ \
|
||||
cmc_timer *t = &(timer); \
|
||||
t->start = clock(); \
|
||||
\
|
||||
} while (0)
|
||||
|
||||
#define timer_stop(timer) \
|
||||
do \
|
||||
{ \
|
||||
cmc_timer *t = &(timer);\
|
||||
t->stop = clock(); \
|
||||
\
|
||||
} while (0)
|
||||
|
||||
#define timer_calc(timer) \
|
||||
do \
|
||||
{ \
|
||||
cmc_timer *t = &(timer); \
|
||||
t->result = (double)(t->stop - t->start) * 1000.0 / CLOCKS_PER_SEC; \
|
||||
\
|
||||
} while (0)
|
||||
|
||||
#endif /* CMC_TIMER_H */
|
||||
@@ -0,0 +1,25 @@
|
||||
CC = gcc
|
||||
CFLAGS = -std=c11 -Wall -Wextra
|
||||
CFLAGS += -Wno-unused-function -Wno-unused-parameter -Wno-unused-variable -Wno-unused-label
|
||||
|
||||
main: FORCE
|
||||
$(CC) foreach.c -o foreach.exe $(CFLAGS)
|
||||
./foreach.exe
|
||||
$(CC) iterators.c -o iterators.exe $(CFLAGS)
|
||||
./iterators.exe
|
||||
$(CC) contains.c -o contains.exe $(CFLAGS)
|
||||
./contains.exe
|
||||
$(CC) types.c -o types.exe $(CFLAGS)
|
||||
./types.exe
|
||||
$(CC) -c ./sep/sep.c -o ./sep/sep.o $(CFLAGS)
|
||||
$(CC) -c ./sep/main.c -o ./sep/main.o $(CFLAGS)
|
||||
$(CC) ./sep/sep.o ./sep/main.o -o ./sep/sep.exe
|
||||
./sep/sep.exe
|
||||
|
||||
dev: main
|
||||
rm *.exe ./sep/*.o ./sep/*.exe
|
||||
|
||||
windev: main
|
||||
del *.exe .\\sep\\*.o .\\sep\\*.exe
|
||||
|
||||
FORCE:
|
||||
@@ -0,0 +1,8 @@
|
||||
# Tests
|
||||
|
||||
* `main` - Unit tests for the main C Macro Collections library with code coverage;
|
||||
* `sep/main.c`, `sep/sep.c` and `sep/sep.h` - Tests separate header and source compilation;
|
||||
* `contains.c` - Tests the `contains` function for all collections;
|
||||
* `foreach.c` - Tests the ForEach macros for all collections;
|
||||
* `iterators.c` - Tests all collection's iterators;
|
||||
* `types.c` - Tests if all containers accept all possible C data types (struct, union, enum, pointers, function pointers)
|
||||
+110
-56
@@ -20,16 +20,20 @@ size_t inthash(int t)
|
||||
return a;
|
||||
}
|
||||
|
||||
COLLECTION_GENERATE(LIST, PUBLIC, l, list, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(STACK, PUBLIC, s, stack, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(QUEUE, PUBLIC, q, queue, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(DEQUE, PUBLIC, d, deque, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(LINKEDLIST, PUBLIC, ll, linked, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HEAP, PUBLIC, h, heap, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(TREESET, PUBLIC, ts, tset, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(TREEMAP, PUBLIC, tm, tmap, /* FMOD */, int, int)
|
||||
COLLECTION_GENERATE(HASHSET, PUBLIC, hs, hset, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HASHMAP, PUBLIC, hm, hmap, /* FMOD */, int, int)
|
||||
CMC_COLLECTION_GENERATE(LIST, l, list, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(STACK, s, stack, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(QUEUE, q, queue, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(DEQUE, d, deque, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(LINKEDLIST, ll, linked, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(HEAP, h, heap, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(TREESET, ts, tset, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(TREEMAP, tm, tmap, int, int)
|
||||
CMC_COLLECTION_GENERATE(HASHSET, hs, hset, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(HASHMAP, hm, hmap, int, int)
|
||||
|
||||
CMC_COLLECTION_GENERATE(INTERVALHEAP, ih, iheap, , int)
|
||||
CMC_COLLECTION_GENERATE(MULTIMAP, mm, mmap, int, int)
|
||||
CMC_COLLECTION_GENERATE(MULTISET, ms, mset, , int)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -38,15 +42,19 @@ int main(void)
|
||||
queue *q = q_new(1000);
|
||||
deque *d = d_new(1000);
|
||||
linked *ll = ll_new();
|
||||
heap *h = h_new(1000, MaxHeap, intcmp);
|
||||
heap *h = h_new(1000, cmc_max_heap, intcmp);
|
||||
tset *ts = ts_new(intcmp);
|
||||
tmap *tm = tm_new(intcmp);
|
||||
hset *hs = hs_new(1000, 0.9, intcmp, inthash);
|
||||
hmap *hm = hm_new(1000, 0.9, intcmp, inthash);
|
||||
hset *hs = hs_new(1000, 0.6, intcmp, inthash);
|
||||
hmap *hm = hm_new(1000, 0.6, intcmp, inthash);
|
||||
|
||||
bool a[10] = {false, false, false, false, false, false, false, false, false, false},
|
||||
b[10] = {false, false, false, false, false, false, false, false, false, false},
|
||||
c[10] = {true, true, true, true, true, true, true, true, true, true};
|
||||
iheap *ih = ih_new(1000, intcmp);
|
||||
mmap *mm = mm_new(1000, 0.8, intcmp, inthash);
|
||||
mset *ms = ms_new(1000, 0.6, intcmp, inthash);
|
||||
|
||||
bool a[13] = {false, false, false, false, false, false, false, false, false, false, false, false, false},
|
||||
b[13] = {false, false, false, false, false, false, false, false, false, false, false, false, false},
|
||||
c[13] = {true, true, true, true, true, true, true, true, true, true, true, true, true};
|
||||
|
||||
b[0] = d_contains(d, 1, intcmp);
|
||||
b[1] = hm_contains(hm, 1);
|
||||
@@ -59,6 +67,10 @@ int main(void)
|
||||
b[8] = tm_contains(tm, 1);
|
||||
b[9] = ts_contains(ts, 1);
|
||||
|
||||
b[10] = ih_contains(ih, 1);
|
||||
b[11] = mm_contains(mm, 1);
|
||||
b[12] = ms_contains(ms, 1);
|
||||
|
||||
for (int i = 1; i < 1001; i++)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
@@ -81,6 +93,10 @@ int main(void)
|
||||
tm_insert(tm, i, i);
|
||||
hs_insert(hs, i);
|
||||
hm_insert(hm, i, i);
|
||||
|
||||
ih_insert(ih, i);
|
||||
mm_insert(mm, i, i);
|
||||
ms_insert(ms, i);
|
||||
}
|
||||
|
||||
for (int i = 1; i < 1001; i++)
|
||||
@@ -95,6 +111,10 @@ int main(void)
|
||||
c[7] = c[7] && s_contains(s, i, intcmp);
|
||||
c[8] = c[8] && tm_contains(tm, i);
|
||||
c[9] = c[9] && ts_contains(ts, i);
|
||||
|
||||
c[10] = c[10] && ih_contains(ih, i);
|
||||
c[11] = c[11] && mm_contains(mm, i);
|
||||
c[12] = c[12] && ms_contains(ms, i);
|
||||
}
|
||||
|
||||
for (int i = -500; i < 1; i++)
|
||||
@@ -109,6 +129,10 @@ int main(void)
|
||||
a[7] = a[7] || s_contains(s, i, intcmp);
|
||||
a[8] = a[8] || tm_contains(tm, i);
|
||||
a[9] = a[9] || ts_contains(ts, i);
|
||||
|
||||
a[10] = a[10] || ih_contains(ih, i);
|
||||
a[11] = a[11] || mm_contains(mm, i);
|
||||
a[12] = a[12] || ms_contains(ms, i);
|
||||
}
|
||||
|
||||
for (int i = 1001; i < 1500; i++)
|
||||
@@ -123,18 +147,26 @@ int main(void)
|
||||
a[7] = a[7] || s_contains(s, i, intcmp);
|
||||
a[8] = a[8] || tm_contains(tm, i);
|
||||
a[9] = a[9] || ts_contains(ts, i);
|
||||
|
||||
a[10] = a[10] || ih_contains(ih, i);
|
||||
a[11] = a[11] || mm_contains(mm, i);
|
||||
a[12] = a[12] || ms_contains(ms, i);
|
||||
}
|
||||
|
||||
d_clear(d);
|
||||
hm_clear(hm);
|
||||
hs_clear(hs);
|
||||
h_clear(h);
|
||||
ll_clear(ll);
|
||||
l_clear(l);
|
||||
q_clear(q);
|
||||
s_clear(s);
|
||||
tm_clear(tm);
|
||||
ts_clear(ts);
|
||||
d_clear(d, NULL);
|
||||
hm_clear(hm, NULL);
|
||||
hs_clear(hs, NULL);
|
||||
h_clear(h, NULL);
|
||||
ll_clear(ll, NULL);
|
||||
l_clear(l, NULL);
|
||||
q_clear(q, NULL);
|
||||
s_clear(s, NULL);
|
||||
tm_clear(tm, NULL);
|
||||
ts_clear(ts, NULL);
|
||||
|
||||
ih_clear(ih, NULL);
|
||||
mm_clear(mm, NULL);
|
||||
ms_clear(ms, NULL);
|
||||
|
||||
b[0] = b[0] || d_contains(d, 1, intcmp);
|
||||
b[1] = b[1] || hm_contains(hm, 1);
|
||||
@@ -147,58 +179,80 @@ int main(void)
|
||||
b[8] = b[8] || tm_contains(tm, 1);
|
||||
b[9] = b[9] || ts_contains(ts, 1);
|
||||
|
||||
b[10] = b[10] || ih_contains(ih, 1);
|
||||
b[11] = b[11] || mm_contains(mm, 1);
|
||||
b[12] = b[12] || ms_contains(ms, 1);
|
||||
|
||||
printf("-------------------- CONTAINS --------------------\n");
|
||||
if (c[0] && !a[0] && !b[0])
|
||||
printf("%10s PASSED\n", "DEQUE");
|
||||
printf("%12s PASSED\n", "DEQUE");
|
||||
else
|
||||
printf("%10s FAILED\n", "DEQUE");
|
||||
printf("%12s FAILED\n", "DEQUE");
|
||||
if (c[1] && !a[1] && !b[1])
|
||||
printf("%10s PASSED\n", "HASHMAP");
|
||||
printf("%12s PASSED\n", "HASHMAP");
|
||||
else
|
||||
printf("%10s FAILED\n", "HASHMAP");
|
||||
printf("%12s FAILED\n", "HASHMAP");
|
||||
if (c[2] && !a[2] && !b[2])
|
||||
printf("%10s PASSED\n", "HASHSET");
|
||||
printf("%12s PASSED\n", "HASHSET");
|
||||
else
|
||||
printf("%10s FAILED\n", "HASHSET");
|
||||
printf("%12s FAILED\n", "HASHSET");
|
||||
if (c[3] && !a[3] && !b[3])
|
||||
printf("%10s PASSED\n", "HEAP");
|
||||
printf("%12s PASSED\n", "HEAP");
|
||||
else
|
||||
printf("%10s FAILED\n", "HEAP");
|
||||
printf("%12s FAILED\n", "HEAP");
|
||||
if (c[4] && !a[4] && !b[4])
|
||||
printf("%10s PASSED\n", "LINKEDLIST");
|
||||
printf("%12s PASSED\n", "LINKEDLIST");
|
||||
else
|
||||
printf("%10s FAILED\n", "LINKEDLIST");
|
||||
printf("%12s FAILED\n", "LINKEDLIST");
|
||||
if (c[5] && !a[5] && !b[5])
|
||||
printf("%10s PASSED\n", "LIST");
|
||||
printf("%12s PASSED\n", "LIST");
|
||||
else
|
||||
printf("%10s FAILED\n", "LIST");
|
||||
printf("%12s FAILED\n", "LIST");
|
||||
if (c[6] && !a[6] && !b[6])
|
||||
printf("%10s PASSED\n", "QUEUE");
|
||||
printf("%12s PASSED\n", "QUEUE");
|
||||
else
|
||||
printf("%10s FAILED\n", "QUEUE");
|
||||
printf("%12s FAILED\n", "QUEUE");
|
||||
if (c[7] && !a[7] && !b[7])
|
||||
printf("%10s PASSED\n", "STACK");
|
||||
printf("%12s PASSED\n", "STACK");
|
||||
else
|
||||
printf("%10s FAILED\n", "STACK");
|
||||
printf("%12s FAILED\n", "STACK");
|
||||
if (c[8] && !a[8] && !b[8])
|
||||
printf("%10s PASSED\n", "TREEMAP");
|
||||
printf("%12s PASSED\n", "TREEMAP");
|
||||
else
|
||||
printf("%10s FAILED\n", "TREEMAP");
|
||||
printf("%12s FAILED\n", "TREEMAP");
|
||||
if (c[9] && !a[9] && !b[9])
|
||||
printf("%10s PASSED\n", "TREESET");
|
||||
printf("%12s PASSED\n", "TREESET");
|
||||
else
|
||||
printf("%10s FAILED\n", "TREESET");
|
||||
printf("%12s FAILED\n", "TREESET");
|
||||
if (c[10] && !a[10] && !b[10])
|
||||
printf("%12s PASSED\n", "INTERVALHEAP");
|
||||
else
|
||||
printf("%12s FAILED\n", "INTERVALHEAP");
|
||||
if (c[11] && !a[11] && !b[11])
|
||||
printf("%12s PASSED\n", "MULTIMAP");
|
||||
else
|
||||
printf("%12s FAILED\n", "MULTIMAP");
|
||||
if (c[12] && !a[12] && !b[12])
|
||||
printf("%12s PASSED\n", "MULTISET");
|
||||
else
|
||||
printf("%12s FAILED\n", "MULTISET");
|
||||
|
||||
l_free(l);
|
||||
ll_free(ll);
|
||||
s_free(s);
|
||||
q_free(q);
|
||||
d_free(d);
|
||||
h_free(h);
|
||||
ts_free(ts);
|
||||
tm_free(tm);
|
||||
hs_free(hs);
|
||||
hm_free(hm);
|
||||
printf("\n\n");
|
||||
|
||||
l_free(l, NULL);
|
||||
ll_free(ll, NULL);
|
||||
s_free(s, NULL);
|
||||
q_free(q, NULL);
|
||||
d_free(d, NULL);
|
||||
h_free(h, NULL);
|
||||
ts_free(ts, NULL);
|
||||
tm_free(tm, NULL);
|
||||
hs_free(hs, NULL);
|
||||
hm_free(hm, NULL);
|
||||
|
||||
ih_free(ih, NULL);
|
||||
mm_free(mm, NULL);
|
||||
ms_free(ms, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+236
-167
@@ -1,10 +1,11 @@
|
||||
#include "../src/macro_collections.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
size_t inthash(int t)
|
||||
@@ -19,16 +20,20 @@ size_t inthash(int t)
|
||||
return a;
|
||||
}
|
||||
|
||||
COLLECTION_GENERATE(LIST, PUBLIC, l, list, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(STACK, PUBLIC, s, stack, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(QUEUE, PUBLIC, q, queue, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(DEQUE, PUBLIC, d, deque, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(LINKEDLIST, PUBLIC, ll, linked, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HEAP, PUBLIC, h, heap, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(TREESET, PUBLIC, ts, tset, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(TREEMAP, PUBLIC, tm, tmap, /* FMOD */, int, int)
|
||||
COLLECTION_GENERATE(HASHSET, PUBLIC, hs, hset, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HASHMAP, PUBLIC, hm, hmap, /* FMOD */, int, int)
|
||||
CMC_COLLECTION_GENERATE(LIST, l, list, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(STACK, s, stack, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(QUEUE, q, queue, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(DEQUE, d, deque, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(LINKEDLIST, ll, linked, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(HEAP, h, heap, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(TREESET, ts, tset, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(TREEMAP, tm, tmap, int, int)
|
||||
CMC_COLLECTION_GENERATE(HASHSET, hs, hset, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(HASHMAP, hm, hmap, int, int)
|
||||
|
||||
CMC_COLLECTION_GENERATE(INTERVALHEAP, ih, iheap, , int)
|
||||
CMC_COLLECTION_GENERATE(MULTIMAP, mm, mmap, int, int)
|
||||
CMC_COLLECTION_GENERATE(MULTISET, ms, mset, , int)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -37,222 +42,286 @@ int main(void)
|
||||
queue *q = q_new(1000);
|
||||
deque *d = d_new(1000);
|
||||
linked *ll = ll_new();
|
||||
heap *h = h_new(1000, MaxHeap, intcmp);
|
||||
heap *h = h_new(1000, cmc_max_heap, intcmp);
|
||||
tset *ts = ts_new(intcmp);
|
||||
tmap *tm = tm_new(intcmp);
|
||||
hset *hs = hs_new(1000, 0.9, intcmp, inthash);
|
||||
hmap *hm = hm_new(1000, 0.9, intcmp, inthash);
|
||||
hset *hs = hs_new(1000, 0.6, intcmp, inthash);
|
||||
hmap *hm = hm_new(1000, 0.6, intcmp, inthash);
|
||||
|
||||
iheap *ih = ih_new(1000, intcmp);
|
||||
mmap *mm = mm_new(1000, 0.8, intcmp, inthash);
|
||||
mset *ms = ms_new(1000, 0.6, intcmp, inthash);
|
||||
|
||||
bool p = true;
|
||||
|
||||
for (int i = 1; i < 10001; i++)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
{
|
||||
d_push_front(d, i);
|
||||
l_push_front(l, i);
|
||||
ll_push_front(ll, i);
|
||||
assert(d_push_front(d, i));
|
||||
assert(l_push_front(l, i));
|
||||
assert(ll_push_front(ll, i));
|
||||
}
|
||||
else
|
||||
{
|
||||
d_push_back(d, i);
|
||||
l_push_back(l, i);
|
||||
ll_push_back(ll, i);
|
||||
assert(d_push_back(d, i));
|
||||
assert(l_push_back(l, i));
|
||||
assert(ll_push_back(ll, i));
|
||||
}
|
||||
|
||||
s_push(s, i);
|
||||
q_enqueue(q, i);
|
||||
h_insert(h, i);
|
||||
ts_insert(ts, i);
|
||||
tm_insert(tm, i, i);
|
||||
hs_insert(hs, i);
|
||||
hm_insert(hm, i, i);
|
||||
assert(s_push(s, i));
|
||||
assert(q_enqueue(q, i));
|
||||
assert(h_insert(h, i));
|
||||
assert(ts_insert(ts, i));
|
||||
assert(tm_insert(tm, i, i));
|
||||
assert(hs_insert(hs, i));
|
||||
assert(hm_insert(hm, i, i));
|
||||
|
||||
assert(ih_insert(ih, i));
|
||||
assert(mm_insert(mm, i, i));
|
||||
assert(ms_insert(ms, i));
|
||||
}
|
||||
|
||||
int sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0,
|
||||
sum7 = 0, sum8 = 0, sum9 = 0, sum10 = 0, sum11 = 0;
|
||||
int sums[16] = {0};
|
||||
|
||||
FOR_EACH(d, deque, d, {
|
||||
sum3 += d_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(d, deque, d, {
|
||||
sums[0] += d_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH(hm, hmap, hm, {
|
||||
sum10 += hm_iter_key(&iter);
|
||||
sum11 += hm_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(hm, hmap, hm, {
|
||||
sums[1] += hm_iter_key(&iter);
|
||||
sums[2] += hm_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH(hs, hset, hs, {
|
||||
sum9 += hs_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(hs, hset, hs, {
|
||||
sums[3] += hs_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH(h, heap, h, {
|
||||
sum5 += h_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(h, heap, h, {
|
||||
sums[4] += h_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH(ll, linked, ll, {
|
||||
sum4 += ll_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(ll, linked, ll, {
|
||||
sums[5] += ll_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH(l, list, l, {
|
||||
sum0 += l_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(l, list, l, {
|
||||
sums[6] += l_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH(q, queue, q, {
|
||||
sum2 += q_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(q, queue, q, {
|
||||
sums[7] += q_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH(s, stack, s, {
|
||||
sum1 += s_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(s, stack, s, {
|
||||
sums[8] += s_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH(tm, tmap, tm, {
|
||||
sum7 += tm_iter_key(&iter);
|
||||
sum8 += tm_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(tm, tmap, tm, {
|
||||
sums[9] += tm_iter_key(&iter);
|
||||
sums[10] += tm_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH(ts, tset, ts, {
|
||||
sum6 += ts_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH(ts, tset, ts, {
|
||||
sums[11] += ts_iter_value(&iter);
|
||||
});
|
||||
|
||||
printf("\n-------------------- FOR_EACH --------------------\n");
|
||||
if (sum3 == 50005000)
|
||||
printf("%10s PASSED\n", "DEQUE");
|
||||
CMC_FOR_EACH(ih, iheap, ih, {
|
||||
sums[12] += ih_iter_value(&iter);
|
||||
});
|
||||
|
||||
CMC_FOR_EACH(mm, mmap, mm, {
|
||||
sums[13] += mm_iter_key(&iter);
|
||||
sums[14] += mm_iter_value(&iter);
|
||||
});
|
||||
|
||||
CMC_FOR_EACH(ms, mset, ms, {
|
||||
sums[15] += ms_iter_value(&iter);
|
||||
});
|
||||
|
||||
printf("\n-------------------- CMC_FOR_EACH --------------------\n");
|
||||
if (sums[0] == 50005000)
|
||||
printf("%12s PASSED\n", "DEQUE");
|
||||
else
|
||||
printf("%10s FAILED\n", "DEQUE");
|
||||
if (sum9 == 50005000)
|
||||
printf("%10s PASSED\n", "HASHSET");
|
||||
printf("%12s FAILED\n", "DEQUE");
|
||||
if (sums[1] == 50005000 && sums[2] == 50005000)
|
||||
printf("%12s PASSED\n", "HASHMAP");
|
||||
else
|
||||
printf("%10s FAILED\n", "HASHSET");
|
||||
if (sum10 == 50005000 && sum11 == 50005000)
|
||||
printf("%10s PASSED\n", "HASHMAP");
|
||||
printf("%12s FAILED\n", "HASHMAP");
|
||||
if (sums[3] == 50005000)
|
||||
printf("%12s PASSED\n", "HASHSET");
|
||||
else
|
||||
printf("%10s FAILED\n", "HASHMAP");
|
||||
if (sum5 == 50005000)
|
||||
printf("%10s PASSED\n", "HEAP");
|
||||
printf("%12s FAILED\n", "HASHSET");
|
||||
if (sums[4] == 50005000)
|
||||
printf("%12s PASSED\n", "HEAP");
|
||||
else
|
||||
printf("%10s FAILED\n", "HEAP");
|
||||
if (sum4 == 50005000)
|
||||
printf("%10s PASSED\n", "LINKEDLIST");
|
||||
printf("%12s FAILED\n", "HEAP");
|
||||
if (sums[5] == 50005000)
|
||||
printf("%12s PASSED\n", "LINKEDLIST");
|
||||
else
|
||||
printf("%10s FAILED\n", "LINKEDLIST");
|
||||
if (sum0 == 50005000)
|
||||
printf("%10s PASSED\n", "LIST");
|
||||
printf("%12s FAILED\n", "LINKEDLIST");
|
||||
if (sums[6] == 50005000)
|
||||
printf("%12s PASSED\n", "LIST");
|
||||
else
|
||||
printf("%10s FAILED\n", "LIST");
|
||||
if (sum2 == 50005000)
|
||||
printf("%10s PASSED\n", "QUEUE");
|
||||
printf("%12s FAILED\n", "LIST");
|
||||
if (sums[7] == 50005000)
|
||||
printf("%12s PASSED\n", "QUEUE");
|
||||
else
|
||||
printf("%10s FAILED\n", "QUEUE");
|
||||
if (sum1 == 50005000)
|
||||
printf("%10s PASSED\n", "STACK");
|
||||
printf("%12s FAILED\n", "QUEUE");
|
||||
if (sums[8] == 50005000)
|
||||
printf("%12s PASSED\n", "STACK");
|
||||
else
|
||||
printf("%10s FAILED\n", "STACK");
|
||||
if (sum7 == 50005000 && sum8 == 50005000)
|
||||
printf("%10s PASSED\n", "TREEMAP");
|
||||
printf("%12s FAILED\n", "STACK");
|
||||
if (sums[9] == 50005000 && sums[10] == 50005000)
|
||||
printf("%12s PASSED\n", "TREEMAP");
|
||||
else
|
||||
printf("%10s FAILED\n", "TREEMAP");
|
||||
if (sum6 == 50005000)
|
||||
printf("%10s PASSED\n", "TREESET");
|
||||
printf("%12s FAILED\n", "TREEMAP");
|
||||
if (sums[11] == 50005000)
|
||||
printf("%12s PASSED\n", "TREESET");
|
||||
else
|
||||
printf("%10s FAILED\n", "TREESET");
|
||||
printf("%12s FAILED\n", "TREESET");
|
||||
if (sums[12] == 50005000)
|
||||
printf("%12s PASSED\n", "INTERVALHEAP");
|
||||
else
|
||||
printf("%12s FAILED\n", "INTERVALHEAP");
|
||||
if (sums[13] == 50005000 && sums[14] == 50005000)
|
||||
printf("%12s PASSED\n", "MULTIMAP");
|
||||
else
|
||||
printf("%12s FAILED\n", "MULTIMAP");
|
||||
if (sums[15] == 50005000)
|
||||
printf("%12s PASSED\n", "MULTISET");
|
||||
else
|
||||
printf("%12s FAILED\n", "MULTISET");
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0,
|
||||
sum7 = 0, sum8 = 0, sum9 = 0, sum10 = 0, sum11 = 0;
|
||||
memset(sums, 0, sizeof sums);
|
||||
|
||||
FOR_EACH_REV(d, deque, d, {
|
||||
sum3 += d_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(d, deque, d, {
|
||||
sums[0] += d_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH_REV(hm, hmap, hm, {
|
||||
sum10 += hm_iter_key(&iter);
|
||||
sum11 += hm_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(hm, hmap, hm, {
|
||||
sums[1] += hm_iter_key(&iter);
|
||||
sums[2] += hm_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH_REV(hs, hset, hs, {
|
||||
sum9 += hs_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(hs, hset, hs, {
|
||||
sums[3] += hs_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH_REV(h, heap, h, {
|
||||
sum5 += h_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(h, heap, h, {
|
||||
sums[4] += h_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH_REV(ll, linked, ll, {
|
||||
sum4 += ll_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(ll, linked, ll, {
|
||||
sums[5] += ll_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH_REV(l, list, l, {
|
||||
sum0 += l_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(l, list, l, {
|
||||
sums[6] += l_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH_REV(q, queue, q, {
|
||||
sum2 += q_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(q, queue, q, {
|
||||
sums[7] += q_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH_REV(s, stack, s, {
|
||||
sum1 += s_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(s, stack, s, {
|
||||
sums[8] += s_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH_REV(tm, tmap, tm, {
|
||||
sum7 += tm_iter_key(&iter);
|
||||
sum8 += tm_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(tm, tmap, tm, {
|
||||
sums[9] += tm_iter_key(&iter);
|
||||
sums[10] += tm_iter_value(&iter);
|
||||
});
|
||||
|
||||
FOR_EACH_REV(ts, tset, ts, {
|
||||
sum6 += ts_iter_value(&iter);
|
||||
})
|
||||
CMC_FOR_EACH_REV(ts, tset, ts, {
|
||||
sums[11] += ts_iter_value(&iter);
|
||||
});
|
||||
|
||||
printf("-------------------- FOR_EACH_REV --------------------\n");
|
||||
if (sum3 == 50005000)
|
||||
printf("%10s PASSED\n", "DEQUE");
|
||||
CMC_FOR_EACH_REV(ih, iheap, ih, {
|
||||
sums[12] += ih_iter_value(&iter);
|
||||
});
|
||||
|
||||
CMC_FOR_EACH_REV(mm, mmap, mm, {
|
||||
sums[13] += mm_iter_key(&iter);
|
||||
sums[14] += mm_iter_value(&iter);
|
||||
});
|
||||
|
||||
CMC_FOR_EACH_REV(ms, mset, ms, {
|
||||
sums[15] += ms_iter_value(&iter);
|
||||
});
|
||||
|
||||
printf("-------------------- CMC_FOR_EACH_REV --------------------\n");
|
||||
if (sums[0] == 50005000)
|
||||
printf("%12s PASSED\n", "DEQUE");
|
||||
else
|
||||
printf("%10s FAILED\n", "DEQUE");
|
||||
if (sum10 == 50005000 && sum11 == 50005000)
|
||||
printf("%10s PASSED\n", "HASHMAP");
|
||||
printf("%12s FAILED\n", "DEQUE");
|
||||
if (sums[1] == 50005000 && sums[2] == 50005000)
|
||||
printf("%12s PASSED\n", "HASHMAP");
|
||||
else
|
||||
printf("%10s FAILED\n", "HASHMAP");
|
||||
if (sum9 == 50005000)
|
||||
printf("%10s PASSED\n", "HASHSET");
|
||||
printf("%12s FAILED\n", "HASHMAP");
|
||||
if (sums[3] == 50005000)
|
||||
printf("%12s PASSED\n", "HASHSET");
|
||||
else
|
||||
printf("%10s FAILED\n", "HASHSET");
|
||||
if (sum5 == 50005000)
|
||||
printf("%10s PASSED\n", "HEAP");
|
||||
printf("%12s FAILED\n", "HASHSET");
|
||||
if (sums[4] == 50005000)
|
||||
printf("%12s PASSED\n", "HEAP");
|
||||
else
|
||||
printf("%10s FAILED\n", "HEAP");
|
||||
if (sum4 == 50005000)
|
||||
printf("%10s PASSED\n", "LINKEDLIST");
|
||||
printf("%12s FAILED\n", "HEAP");
|
||||
if (sums[5] == 50005000)
|
||||
printf("%12s PASSED\n", "LINKEDLIST");
|
||||
else
|
||||
printf("%10s FAILED\n", "LINKEDLIST");
|
||||
if (sum0 == 50005000)
|
||||
printf("%10s PASSED\n", "LIST");
|
||||
printf("%12s FAILED\n", "LINKEDLIST");
|
||||
if (sums[6] == 50005000)
|
||||
printf("%12s PASSED\n", "LIST");
|
||||
else
|
||||
printf("%10s FAILED\n", "LIST");
|
||||
if (sum2 == 50005000)
|
||||
printf("%10s PASSED\n", "QUEUE");
|
||||
printf("%12s FAILED\n", "LIST");
|
||||
if (sums[7] == 50005000)
|
||||
printf("%12s PASSED\n", "QUEUE");
|
||||
else
|
||||
printf("%10s FAILED\n", "QUEUE");
|
||||
if (sum1 == 50005000)
|
||||
printf("%10s PASSED\n", "STACK");
|
||||
printf("%12s FAILED\n", "QUEUE");
|
||||
if (sums[8] == 50005000)
|
||||
printf("%12s PASSED\n", "STACK");
|
||||
else
|
||||
printf("%10s FAILED\n", "STACK");
|
||||
if (sum7 == 50005000 && sum8 == 50005000)
|
||||
printf("%10s PASSED\n", "TREEMAP");
|
||||
printf("%12s FAILED\n", "STACK");
|
||||
if (sums[9] == 50005000 && sums[10] == 50005000)
|
||||
printf("%12s PASSED\n", "TREEMAP");
|
||||
else
|
||||
printf("%10s FAILED\n", "TREEMAP");
|
||||
if (sum6 == 50005000)
|
||||
printf("%10s PASSED\n", "TREESET");
|
||||
printf("%12s FAILED\n", "TREEMAP");
|
||||
if (sums[11] == 50005000)
|
||||
printf("%12s PASSED\n", "TREESET");
|
||||
else
|
||||
printf("%10s FAILED\n", "TREESET");
|
||||
printf("%12s FAILED\n", "TREESET");
|
||||
if (sums[12] == 50005000)
|
||||
printf("%12s PASSED\n", "INTERVALHEAP");
|
||||
else
|
||||
printf("%12s FAILED\n", "INTERVALHEAP");
|
||||
if (sums[13] == 50005000 && sums[14] == 50005000)
|
||||
printf("%12s PASSED\n", "MULTIMAP");
|
||||
else
|
||||
printf("%12s FAILED\n", "MULTIMAP");
|
||||
if (sums[15] == 50005000)
|
||||
printf("%12s PASSED\n", "MULTISET");
|
||||
else
|
||||
printf("%12s FAILED\n", "MULTISET");
|
||||
|
||||
l_free(l);
|
||||
ll_free(ll);
|
||||
s_free(s);
|
||||
q_free(q);
|
||||
d_free(d);
|
||||
h_free(h);
|
||||
ts_free(ts);
|
||||
tm_free(tm);
|
||||
hs_free(hs);
|
||||
hm_free(hm);
|
||||
printf("\n\n");
|
||||
|
||||
l_free(l, NULL);
|
||||
ll_free(ll, NULL);
|
||||
s_free(s, NULL);
|
||||
q_free(q, NULL);
|
||||
d_free(d, NULL);
|
||||
h_free(h, NULL);
|
||||
ts_free(ts, NULL);
|
||||
tm_free(tm, NULL);
|
||||
hs_free(hs, NULL);
|
||||
hm_free(hm, NULL);
|
||||
|
||||
ih_free(ih, NULL);
|
||||
mm_free(mm, NULL);
|
||||
ms_free(ms, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+31
-19
@@ -1,10 +1,9 @@
|
||||
#include "../src/macro_collections.h"
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
int intcmp(int a, int b)
|
||||
{
|
||||
return a - b;
|
||||
return (a > b) - (a < b);
|
||||
}
|
||||
|
||||
size_t inthash(int t)
|
||||
@@ -19,16 +18,20 @@ size_t inthash(int t)
|
||||
return a;
|
||||
}
|
||||
|
||||
COLLECTION_GENERATE(LIST, PUBLIC, l, list, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(STACK, PUBLIC, s, stack, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(QUEUE, PUBLIC, q, queue, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(DEQUE, PUBLIC, d, deque, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(LINKEDLIST, PUBLIC, ll, linked, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HEAP, PUBLIC, h, heap, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(TREESET, PUBLIC, ts, tset, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(TREEMAP, PUBLIC, tm, tmap, /* FMOD */, int, double)
|
||||
COLLECTION_GENERATE(HASHSET, PUBLIC, hs, hset, /* FMOD */, /* K */, int)
|
||||
COLLECTION_GENERATE(HASHMAP, PUBLIC, hm, hmap, /* FMOD */, int, double)
|
||||
CMC_COLLECTION_GENERATE(LIST, l, list, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(STACK, s, stack, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(QUEUE, q, queue, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(DEQUE, d, deque, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(LINKEDLIST, ll, linked, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(HEAP, h, heap, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(TREESET, ts, tset, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(TREEMAP, tm, tmap, int, double)
|
||||
CMC_COLLECTION_GENERATE(HASHSET, hs, hset, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(HASHMAP, hm, hmap, int, double)
|
||||
|
||||
CMC_COLLECTION_GENERATE(INTERVALHEAP, ih, iheap, /* K */, int)
|
||||
CMC_COLLECTION_GENERATE(MULTIMAP, mm, mmap, int, double)
|
||||
CMC_COLLECTION_GENERATE(MULTISET, ms, mset, /* K */, int)
|
||||
|
||||
#define ITERATOR_TEST(NAME, PFX, sname, initfunc, insertbody, sumbody, validation1, validation2) \
|
||||
void NAME##_iterator_test(void) \
|
||||
@@ -69,24 +72,27 @@ COLLECTION_GENERATE(HASHMAP, PUBLIC, hm, hmap, /* FMOD */, int, double)
|
||||
sumbody; \
|
||||
} \
|
||||
\
|
||||
PFX##_free(coll); \
|
||||
PFX##_free(coll, NULL); \
|
||||
\
|
||||
if (validation1 && validation2) \
|
||||
printf("%10s PASSED\n", #NAME); \
|
||||
if ((validation1) && (validation2)) \
|
||||
printf("%12s PASSED\n", #NAME); \
|
||||
else \
|
||||
printf("%10s FAILED\n", #NAME); \
|
||||
printf("%12s FAILED\n", #NAME); \
|
||||
}
|
||||
|
||||
ITERATOR_TEST(DEQUE, d, deque, d_new(20), d_push_front(coll, i), sum1 += d_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(HASHMAP, hm, hmap, hm_new(50, 0.6, intcmp, inthash), hm_insert(coll, i, (double)i / 10.0), sum1 += hm_iter_key(&iter); sum2 += hm_iter_value(&iter), sum1 == 450, sum2 == 45.0)
|
||||
ITERATOR_TEST(HASHMAP, hm, hmap, hm_new(50, 0.6, intcmp, inthash), hm_insert(coll, i, (double)i / 10.0), sum1 += hm_iter_key(&iter); sum2 += hm_iter_value(&iter), sum1 == 450, (int)sum2 == (int)45)
|
||||
ITERATOR_TEST(HASHSET, hs, hset, hs_new(50, 0.6, intcmp, inthash), hs_insert(coll, i), sum1 += hs_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(HEAP, h, heap, h_new(20, MaxHeap, intcmp), h_insert(coll, i), sum1 += h_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(HEAP, h, heap, h_new(20, cmc_max_heap, intcmp), h_insert(coll, i), sum1 += h_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(LINKEDLIST, ll, linked, ll_new(), ll_push_back(coll, i), sum1 += ll_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(LIST, l, list, l_new(20), l_push_back(coll, i), sum1 += l_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(QUEUE, q, queue, q_new(20), q_enqueue(coll, i), sum1 += q_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(STACK, s, stack, s_new(20), s_push(coll, i), sum1 += s_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(TREEMAP, tm, tmap, tm_new(intcmp), tm_insert(coll, i, (double)i / 10.0), sum1 += tm_iter_key(&iter); sum2 += tm_iter_value(&iter), sum1 == 450, sum2 == 45.0)
|
||||
ITERATOR_TEST(TREEMAP, tm, tmap, tm_new(intcmp), tm_insert(coll, i, (double)i / 10.0), sum1 += tm_iter_key(&iter); sum2 += tm_iter_value(&iter), sum1 == 450, (int)sum2 == (int)45)
|
||||
ITERATOR_TEST(TREESET, ts, tset, ts_new(intcmp), ts_insert(coll, i), sum1 += ts_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(INTERVALHEAP, ih, iheap, ih_new(20, intcmp), ih_insert(coll, i), sum1 += ih_iter_value(&iter), sum1 == 450, 1)
|
||||
ITERATOR_TEST(MULTIMAP, mm, mmap, mm_new(50, 0.8, intcmp, inthash), mm_insert(coll, i, (double)i / 10.0), sum1 += mm_iter_key(&iter); sum2 += mm_iter_value(&iter), sum1 == 450, (int)sum2 == (int)45)
|
||||
ITERATOR_TEST(MULTISET, ms, mset, ms_new(50, 0.6, intcmp, inthash), ms_insert(coll, i), sum1 += ms_iter_value(&iter); sum2 += ms_iter_multiplicity(&iter), sum1 == 450, (size_t)sum2 == 100)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -102,5 +108,11 @@ int main(void)
|
||||
TREEMAP_iterator_test();
|
||||
TREESET_iterator_test();
|
||||
|
||||
INTERVALHEAP_iterator_test();
|
||||
MULTIMAP_iterator_test();
|
||||
MULTISET_iterator_test();
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loaded 100 of 133 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user