Initial commit

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
This commit is contained in:
Lucien Cartier-Tilet 2020-10-03 16:07:26 +02:00
commit 1e86a8736f
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
3 changed files with 190 additions and 0 deletions

68
.clang-format Normal file
View File

@ -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
...

84
vector.c Normal file
View File

@ -0,0 +1,84 @@
#include "vector.h"
#include <stdlib.h>
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);
}

38
vector.h Normal file
View File

@ -0,0 +1,38 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#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