Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added const qualifier to vector functions #13

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ CMakeUserPresets.json
DartConfiguration.tcl

#Misc
string/
string/
local-test.bash
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ It uses CMake as the build system, Valgrind for checking memory safety and GitHu
- [x] Supports primitive data structures as well as pointer-to-structs.

### Currently Tested Platforms
- Ubuntu
- gcc
- clang
- Windows
- cl (MSVC)

### To Do
- [x] Look into GitHub actions based testing
- [x] Valgrind testing?
- [x] How can I integrate CMake, CTest, GitHub actions and Valgrind?
- [ ] AddressSanitizer too?
- [ ] AddressSanitizer too? (GitHub actions cannot fin ASan or LSan)
- [ ] Write primitive as well as pointer-to-structs examples
- [ ] [Technical Debt] Use constant pointers, pointers to constants and constant pointers to constant wherever applicable.

Expand Down
31 changes: 17 additions & 14 deletions include/data_structures/vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ NAME* new_##NAME(uint64_t size){ /*Size is set only after allocation of vector->
new_obj->size = size;\
return new_obj;\
}\
NAME* construct_from_arr_##NAME(TYPE* arr, uint64_t size){\
NAME* construct_from_arr_##NAME(TYPE* const arr, uint64_t size){\
if(!arr){\
return NULL;\
}\
Expand All @@ -46,7 +46,7 @@ NAME* construct_from_arr_##NAME(TYPE* arr, uint64_t size){\
}\
return new_obj;\
}\
bool is_empty_##NAME(NAME* obj){\
bool is_empty_##NAME(const NAME* const obj){\
if(!obj){\
return false;\
}\
Expand All @@ -55,7 +55,7 @@ bool is_empty_##NAME(NAME* obj){\
}\
return false;\
}\
bool at_##NAME(NAME* obj, uint64_t index, TYPE* val){\
bool at_##NAME(const NAME* const obj, uint64_t index, TYPE* const val){\
if(!obj || !val){\
return false;\
}\
Expand All @@ -65,17 +65,20 @@ bool at_##NAME(NAME* obj, uint64_t index, TYPE* val){\
*val = obj->data[index];\
return true;\
}\
bool set_at_##NAME(NAME* obj, uint64_t index, TYPE val){\
bool set_at_##NAME(const NAME* const obj, uint64_t index, TYPE val){\
if(!obj){\
return false;\
}\
if(index > obj->size -1){\
if(obj->size == 0){\
return false;\
}\
if(index > obj->size - 1){\
return false;\
}\
obj->data[index] = val;\
return true;\
}\
bool set_whole_##NAME(NAME* obj, TYPE val){\
bool set_whole_##NAME(const NAME* const obj, TYPE val){\
if(!obj || !(obj->data) || (obj->size == 0)){\
return false;\
}\
Expand All @@ -84,7 +87,7 @@ bool set_whole_##NAME(NAME* obj, TYPE val){\
}\
return true;\
}\
bool add_at_end_##NAME(NAME* obj, TYPE val){\
bool add_at_end_##NAME(NAME* const obj, TYPE val){\
if(!obj){\
return false;\
}\
Expand All @@ -97,7 +100,7 @@ bool add_at_end_##NAME(NAME* obj, TYPE val){\
obj->data[obj->size - 1] = val;\
return true;\
}\
bool add_at_beginning_##NAME(NAME* obj, TYPE val){\
bool add_at_beginning_##NAME(NAME* const obj, TYPE val){\
if(!obj){\
return false;\
}\
Expand All @@ -113,7 +116,7 @@ bool add_at_beginning_##NAME(NAME* obj, TYPE val){\
obj->data[0] = val;\
return true;\
}\
bool remove_from_beginning_##NAME(NAME* obj, TYPE* val, bool (*delete_element)(TYPE)){\
bool remove_from_beginning_##NAME(NAME* const obj, TYPE* const val, bool (*delete_element)(TYPE)){\
if(!obj || !obj->data || obj->size == 0){\
return false;\
}\
Expand All @@ -134,7 +137,7 @@ bool remove_from_beginning_##NAME(NAME* obj, TYPE* val, bool (*delete_element)(T
--(obj->size);\
return true;\
}\
bool remove_from_end_##NAME(NAME* obj, TYPE* val, bool (*delete_type)(TYPE)){\
bool remove_from_end_##NAME(NAME* const obj, TYPE* const val, bool (*delete_type)(TYPE)){\
if(!obj || !obj->data || obj->size == 0){\
return false;\
}\
Expand All @@ -152,15 +155,15 @@ bool remove_from_end_##NAME(NAME* obj, TYPE* val, bool (*delete_type)(TYPE)){\
--(obj->size);\
return true;\
}\
void print_##NAME(NAME* obj, void (*print_element)(TYPE)){\
void print_##NAME(const NAME* const obj, void (*print_element)(TYPE)){\
if(!obj || !print_element || !(obj->data)){\
return;\
}\
for(uint64_t i = 0; i < obj->size; ++i){\
print_element(obj->data[i]);\
}\
}\
bool linear_search_##NAME(NAME* obj, TYPE ref ,bool (*comparator)(TYPE, TYPE), uint64_t *index){\
bool linear_search_##NAME(const NAME* const obj, TYPE ref ,bool (*comparator)(TYPE, TYPE), uint64_t* const index){\
if(!obj || !obj->data || obj->size == 0 || !(comparator)){\
return false;\
}\
Expand All @@ -172,7 +175,7 @@ bool linear_search_##NAME(NAME* obj, TYPE ref ,bool (*comparator)(TYPE, TYPE), u
}\
return false;\
}\
bool delete_##NAME(NAME** obj, bool (*delete_type)(TYPE)){\
bool delete_##NAME(NAME** const obj, bool (*delete_type)(TYPE)){\
if(!obj || !*obj){\
return false;\
}\
Expand All @@ -188,7 +191,7 @@ bool delete_##NAME(NAME** obj, bool (*delete_type)(TYPE)){\
*obj = NULL;\
return true;\
}\
bool clear_##NAME(NAME* obj, bool (*delete_type)(TYPE), TYPE val){\
bool clear_##NAME(NAME* const obj, bool (*delete_type)(TYPE)){\
if(!obj){\
return false;\
}\
Expand Down