erase() function should be fairly good

This commit is contained in:
Andrew Haynes
2026-04-28 13:44:56 -04:00
parent 280e728520
commit 0a8819b798
3 changed files with 49 additions and 14 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ SpaceBeforeCpp11BracedList: true
SeparateDefinitionBlocks: Always
Cpp11BracedListStyle: false
BreakAfterReturnType: TopLevel
AlignTrailingComments:
Kind: Always
# AlignTrailingComments:
# Kind: Always
ReflowComments: Always
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
+12
View File
@@ -0,0 +1,12 @@
# AI Agent Rules
## AI Usage Policy (from src/main.c:46-55)
- Absolutely no AI is permitted when writing source code.
- AI is allowed to generate tests for code; however, test results and the AI cannot give code as to how to fix the source aside from pseudo code that has no clear relation to the source aside from a recreation of the problem in a different way entirely.
- Source code comments are for the author's short-term memory and do not affect AI behavior.
## Project Code Conventions
- **Formatting**: Follow `.clang-format` (Google-based style, tabs (width 8), Allman braces, short functions/if/loops can be single-line)
- **Linting**: Follow `.clang-tidy` (bugprone, concurrency, misc, modernize, performance, readability, portability checks; `lower_case` naming for variables and functions)
- **Build**: C23 standard, CMake 3.16+
- **Naming**: Variables and functions use `lower_case` (enforced by clang-tidy)
+35 -12
View File
@@ -12,16 +12,17 @@
* > at() Returns an indexed element from a vector
* > back() Returns the last element of a vector
* > begin() Returns an iterator pointing to the beginning of a vector
* > capacity() Returns the number of elements that a vector's reserved memory
* is able to store > front() Returns the first element of a vector > end()
* Returns an iterator pointing to the end of a vector > push_back() Adds an
* element to the end of a vector > size() Returns the number of elements
* in a vector
* > capacity() Returns the number of elements that a vector's reserved memory is able to store
* > front() Returns the first element of a vector
* > end() Returns an iterator pointing to the end of a vector
* > push_back() Adds an element to the end of a vector
* > size() Returns the number of elements in a vector
* > clear() Removes all of the contents of a vector
* > empty() Checks whether a vector is empty or not
*
* assign() Fills a vector with multiple values
* clear() Removes all of the contents of a vector
* data() Returns a pointer to the block of memory where a vector's elements are
* stored empty() Checks whether a vector is empty or not erase() Removes
* stored erase() Removes
* a number of elements from a vector insert() Inserts a number of elements
* into a vector max_size() Returns the maximum number of elements that a
* vector can have pop_back() Removes the last element of a vector rbegin()
@@ -124,6 +125,19 @@ at(const Vec8_t* vec, const int idx)
return -1;
}
Vec8_t*
erase(Vec8_t* vec, const int iter) {
/*
* Clear the value at iter to 0
* shift all the values in the vector over to the left one
* */
if(vec == nullptr) return nullptr;
if(vec->arr == nullptr) return nullptr;
vec->arr[iter] = 0;
memmove(&vec[iter], &vec[iter + 1], (vec->size * sizeof(char)) -1);
return vec;
}
void
print_vec(const Vec8_t* vec)
{
@@ -173,6 +187,15 @@ back(const Vec8_t* vec)
return -1;
}
int
empty(const Vec8_t* vec) {
if(vec->size > 0) {
return 1;
}
return 0;
}
Vec8_t
add_back(Vec8_t* vec, const char val)
{
@@ -201,11 +224,11 @@ main()
vec = add_back(&vec, '6');
vec = add_back(&vec, '8');
vec = add_back(&vec, '6');
clear(&vec);
for(int i = 0; i < vec.size; i++) {
printf("%i\n", at(&vec, i));
}
// print_vec(&vec);
vec = *erase(&vec, begin(&vec) + 2);
// for(int i = 0; i < vec.size; i++) {
// printf("%i\n", at(&vec, i));
// }
print_vec(&vec);
// printf("%c", at(nullptr, 0));
delete(&vec);