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