From 1e86a8736f64a806c8b911f30e9ef621cfb53ddf Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Sat, 3 Oct 2020 16:07:26 +0200 Subject: [PATCH] Initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The code has not yet been tested, the only “test” is the static code analysis from within Emacs with LSP with clangd as its backend --- .clang-format | 68 +++++++++++++++++++++++++++++++++++++++++ vector.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ vector.h | 38 +++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 .clang-format create mode 100644 vector.c create mode 100644 vector.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..7e44223 --- /dev/null +++ b/.clang-format @@ -0,0 +1,68 @@ +--- +BasedOnStyle: WebKit +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: 'true' +AlignConsecutiveAssignments: 'true' +AlignConsecutiveDeclarations: 'true' +AlignEscapedNewlines: Left +AlignOperands: 'true' +AlignTrailingComments: 'true' +AllowAllArgumentsOnNextLine: 'true' +AllowAllConstructorInitializersOnNextLine: 'true' +AllowAllParametersOfDeclarationOnNextLine: 'true' +AllowShortBlocksOnASingleLine: 'true' +AllowShortCaseLabelsOnASingleLine: 'true' +AllowShortFunctionsOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: Empty +AllowShortLoopsOnASingleLine: 'false' +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: 'false' +AlwaysBreakTemplateDeclarations: 'Yes' +BinPackArguments: 'false' +BinPackParameters: 'false' +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: 'true' +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: 'true' +ColumnLimit: '80' +CompactNamespaces: 'false' +ConstructorInitializerAllOnOneLineOrOnePerLine: 'false' +ConstructorInitializerIndentWidth: '2' +ContinuationIndentWidth: '2' +Cpp11BracedListStyle: 'true' +FixNamespaceComments: 'true' +IncludeBlocks: Regroup +IndentCaseLabels: 'true' +IndentPPDirectives: AfterHash +IndentWidth: '2' +IndentWrappedFunctionNames: 'true' +KeepEmptyLinesAtTheStartOfBlocks: 'false' +MaxEmptyLinesToKeep: '2' +NamespaceIndentation: All +PointerAlignment: Right +ReflowComments: 'true' +SortIncludes: 'true' +SortUsingDeclarations: 'true' +SpaceAfterCStyleCast: 'false' +SpaceAfterLogicalNot: 'false' +SpaceAfterTemplateKeyword: 'false' +SpaceBeforeAssignmentOperators: 'true' +SpaceBeforeCpp11BracedList: 'false' +SpaceBeforeCtorInitializerColon: 'false' +SpaceBeforeInheritanceColon: 'false' +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: 'true' +SpaceInEmptyParentheses: 'false' +SpacesBeforeTrailingComments: '1' +SpacesInAngles: 'false' +SpacesInCStyleCastParentheses: 'false' +SpacesInContainerLiterals: 'false' +SpacesInParentheses: 'false' +SpacesInSquareBrackets: 'false' +Standard: Auto +TabWidth: '2' +UseTab: ForIndentation + +... diff --git a/vector.c b/vector.c new file mode 100644 index 0000000..c292fd3 --- /dev/null +++ b/vector.c @@ -0,0 +1,84 @@ +#include "vector.h" + +#include + +Vector *vec_new(uint32_t obj_size, Destructor destructor) +{ + Vector *self = (Vector *)malloc(sizeof(Vector)); + (*self).elements = NULL; + (*self).obj_size = obj_size; + (*self).len = 0; + (*self).destructor = destructor; + return self; +} + +Vector *vec_new_with_capacity(uint32_t obj_size, + uint32_t obj_number, + Destructor destructor) +{ + Vector *self = (Vector *)malloc(sizeof(Vector)); + (*self).elements = (void **)malloc(obj_number); + (*self).obj_size = obj_size; + (*self).len = 0; + (*self).destructor = destructor; + return self; +} + +uint32_t vec_len(Vector *self) +{ + return self->len; +} + +uint32_t vec_capacity(Vector *self) +{ + return sizeof(self->elements); +} + +uint8_t vec_realloc(Vector *self) +{ + void ** elements = self->elements; + uint32_t new_capacity = (vec_capacity(self) > 0) ? vec_capacity(self) * 2 : 1; + self->elements = realloc(self->elements, new_capacity); + if (!self->elements) { + self->elements = elements; + return 1; + } + return 0; +} + +uint8_t vec_push(Vector *self, void *element) +{ + UNINPLEMENTED; + return 1; + /* if (self->len + 1 > vec_capacity(self)) { */ + /* vec_realloc(self); */ + /* } */ + /* ++(*self).len; */ +} + +uint8_t vec_pop(Vector *self) { + UNINPLEMENTED; + return 1; +} // TODO + +void *vec_at(Vector *self, uint32_t index) +{ + return self->elements + (index * self->obj_size); +} + +void *vec_at_safe(Vector *self, uint32_t index) +{ + if (index < 0 || index >= self->len) { + return NULL; + } + return vec_at(self, index); +} + +void vec_delete(Vector *self) +{ + foreach (void *elem, self) { + self->destructor(elem); + } + free(self->elements); + free(self); +} diff --git a/vector.h b/vector.h new file mode 100644 index 0000000..66576be --- /dev/null +++ b/vector.h @@ -0,0 +1,38 @@ +#include +#include +#include + +#define UNINPLEMENTED printf("%s:%d: Not yet implemented", __FILE__, __LINE__) + +#define foreach(item, vector) \ + for (int keep = 1, \ + count = 0, \ + size = sizeof(vector->elements) / sizeof *(vector->elements); \ + keep && count != size; \ + keep = !keep, count++) \ + for (item = (vector->elements) + (count * vector->obj_size); keep; \ + keep = !keep) + +typedef void (*Destructor)(void *element); + +struct Vector_s { + void ** elements; + uint32_t obj_size; + uint32_t len; + Destructor destructor; +}; + +typedef struct Vector_s Vector; + +Vector * vec_new_with_capacity(uint32_t obj_size, + uint32_t obj_number, + Destructor destructor); // DONE +Vector * vec_new(uint32_t obj_size, + Destructor destructor); // DONE +uint32_t vec_len(Vector *self); // DONE +uint32_t vec_capacity(Vector *self); // DONE +uint8_t vec_push(Vector *self, void *element); // TODO +uint8_t vec_pop(Vector *self); // TODO +void * vec_at(Vector *self, uint32_t index); // DONE +void * vec_at_safe(Vector *self, uint32_t index); // DONE +void vec_delete(Vector *self); // DONE