Skip to content

Commit d12828f

Browse files
committed
add array and vector c lib
1 parent 887c95e commit d12828f

11 files changed

+661
-20
lines changed

Source/Library/Array.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
4+
#include "Array.h"
5+
6+
struct array
7+
{
8+
size_t size;
9+
uint8_t sizeOfType;
10+
void *data;
11+
};
12+
13+
array *array_create(size_t size, size_t sizeOfType)
14+
{
15+
if (size == 0 || sizeOfType == 0)
16+
{
17+
return NULL;
18+
}
19+
array *arr = (array *)malloc(sizeof(array));
20+
if (arr == NULL)
21+
{
22+
return NULL;
23+
}
24+
arr->data = calloc(size, sizeOfType);
25+
if (arr->data == NULL)
26+
{
27+
free(arr);
28+
return NULL;
29+
}
30+
arr->size = size;
31+
arr->sizeOfType = sizeOfType;
32+
return arr;
33+
}
34+
35+
void *array_at(array *array, size_t index)
36+
{
37+
if (index >= array_size(array))
38+
{
39+
return NULL;
40+
}
41+
uint8_t *casted = (uint8_t *)array->data;
42+
uint8_t *ptr = casted + index * array->sizeOfType;
43+
return (void *)ptr;
44+
}
45+
46+
void *array_front(array *array) { return array_at(array, 0); }
47+
48+
void *array_back(array *array) { return array_at(array, array_size(array) - 1); }
49+
50+
size_t array_size(array *array) { return array->size; }
51+
52+
size_t array_size_of_type(array *array) { return array->sizeOfType; }
53+
54+
void *array_data(array *array) { return array->data; }
55+
56+
void array_copy(array *source, array *destination)
57+
{
58+
size_t bytes = array_size(source) * array_size_of_type(source);
59+
memcpy(array_data(destination), array_data(source), bytes);
60+
}
61+
62+
void array_destroy(array *array)
63+
{
64+
if (array == NULL)
65+
{
66+
return;
67+
}
68+
free(array->data);
69+
free(array);
70+
}

Source/Library/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ add_library(SandboxLib
55
Map.cpp
66
Cache.cpp
77
MemoryManager.cpp
8-
DependencyInjector.cpp
8+
Array.c
9+
Vector.c
910
)
1011

1112
target_include_directories(SandboxLib PUBLIC

Source/Library/Vector.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
4+
#include "Array.h"
5+
#include "Vector.h"
6+
7+
struct vector
8+
{
9+
size_t size;
10+
array *array;
11+
};
12+
13+
size_t get_array_size(size_t vectorSize)
14+
{
15+
if (vectorSize < 5)
16+
{
17+
return 5;
18+
}
19+
return vectorSize + 10;
20+
}
21+
22+
vector *vector_create(size_t size, size_t sizeOfType)
23+
{
24+
vector *vec = (vector *)malloc(sizeof(vector));
25+
if (vec == NULL)
26+
{
27+
return NULL;
28+
}
29+
30+
array *array = array_create(get_array_size(size), sizeOfType);
31+
if (array == NULL)
32+
{
33+
free(vec);
34+
return NULL;
35+
}
36+
vec->size = size;
37+
vec->array = array;
38+
return vec;
39+
}
40+
41+
void *vector_at(vector *vector, size_t index)
42+
{
43+
if (index >= vector_size(vector))
44+
{
45+
return NULL;
46+
}
47+
return array_at(vector->array, index);
48+
}
49+
50+
void *vector_front(vector *vector)
51+
{
52+
if (vector_empty(vector))
53+
{
54+
return NULL;
55+
}
56+
return array_front(vector->array);
57+
}
58+
59+
void *vector_back(vector *vector)
60+
{
61+
if (vector_empty(vector))
62+
{
63+
return NULL;
64+
}
65+
return array_at(vector->array, vector_size(vector) - 1);
66+
}
67+
68+
void *vector_data(vector *vector) { return array_data(vector->array); }
69+
70+
size_t vector_size(vector *vector) { return vector->size; }
71+
72+
bool vector_empty(vector *vector) { return vector_size(vector) == 0; }
73+
74+
void *vector_expand(vector *vector)
75+
{
76+
if (vector_size(vector) < array_size(vector->array))
77+
{
78+
vector->size++;
79+
return vector_back(vector);
80+
}
81+
size_t newArraySize = get_array_size(vector->size + 1);
82+
83+
array *newArray = array_create(newArraySize, array_size_of_type(vector->array));
84+
if (newArray == NULL)
85+
{
86+
return NULL;
87+
}
88+
89+
array_copy(vector->array, newArray);
90+
array_destroy(vector->array);
91+
vector->array = newArray;
92+
vector->size++;
93+
return vector_back(vector);
94+
}
95+
96+
void vector_shrink(vector *vector)
97+
{
98+
if (vector->size > 0)
99+
{
100+
vector->size--;
101+
}
102+
}
103+
104+
void vector_erase(vector *vector, size_t index) {}
105+
106+
void vector_clear(vector *vector) { vector->size = 0; }
107+
108+
void vector_destroy(vector *vector)
109+
{
110+
if (vector == NULL)
111+
{
112+
return;
113+
}
114+
array_destroy(vector->array);
115+
free(vector);
116+
}

Source/Library/h/Array.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
#include <stdint.h>
5+
#include <stdio.h>
6+
7+
#ifdef __cplusplus
8+
extern "C"
9+
{
10+
#endif
11+
typedef struct array array;
12+
13+
array *array_create(size_t size, size_t sizeOfType);
14+
15+
void *array_at(array *array, size_t index);
16+
17+
void *array_front(array *array);
18+
19+
void *array_back(array *array);
20+
21+
void *array_data(array *array);
22+
23+
void array_copy(array *source, array *destination);
24+
25+
size_t array_size(array *array);
26+
27+
size_t array_size_of_type(array *array);
28+
29+
void array_destroy(array *array);
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif

Source/Library/h/DependencyInjector.hpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ namespace sd
2121
template <class T> class Singeleton
2222
{
2323
private:
24-
T *_object = nullptr;
24+
T *_service = nullptr;
2525

2626
public:
2727
using Type = T;
2828

2929
Singeleton() = default;
30-
Singeleton(T *object) : _object(object) {}
30+
Singeleton(T *service) : _service(service) {}
3131

32-
T *get() const { return _object; }
32+
T *get() const { return _service; }
3333
};
3434

3535
template <int T, class O> class Shared
@@ -69,27 +69,23 @@ namespace sd
6969
enum Type
7070
{
7171
Singeleton = 1,
72-
Shared,
73-
Unique,
72+
Scoped,
73+
Transient,
7474
};
7575

7676
private:
7777
const Type type;
78-
int token = 0;
7978

8079
Scope(Type type) : type(type) {}
81-
Scope(int token) : type(Type::Shared), token(token) {}
8280

8381
public:
8482
static Scope singeleton() { return {Type::Singeleton}; }
85-
static Scope unique() { return {Type::Unique}; }
86-
static Scope shared(int token) { return {token}; }
83+
static Scope transient() { return {Type::Transient}; }
84+
static Scope scoped() { return {Type::Scoped}; }
8785

8886
bool isSingeleton() const { return type == Type::Singeleton; }
89-
bool isUnique() const { return type == Type::Unique; }
90-
bool isShared() const { return type == Type::Shared; }
91-
92-
int getToken() const { return token; }
87+
bool isTransient() const { return type == Type::Transient; }
88+
bool isScoped() const { return type == Type::Scoped; }
9389
};
9490

9591
class DependencyInjector
@@ -354,11 +350,10 @@ namespace sd
354350

355351
public:
356352
template <class I, class T> void addSingeleton() { add<I, T>(Scope::singeleton()); }
357-
template <class I, class T> void addUnique() { add<I, T>(Scope::unique()); }
358-
template <class I, class T> void addShared(int token) { add<I, T>(Scope::shared(token)); }
353+
template <class I, class T> void addTransient() { add<I, T>(Scope::transient()); }
354+
template <class I, class T> void addScoped() { add<I, T>(Scope::scoped()); }
359355

360-
template <class I> I *getPtr(int token = 0) { return (I *)getFromObjectsMap(typeid(I), token).get(); }
361-
template <class I> I &getRef(int token = 0) { return *((I *)getFromObjectsMapSafe(typeid(I), token).get()); }
356+
template <class I> std::shared_ptr<I> get() { return (I *)getFromObjectsMap(typeid(I)).get(); }
362357

363358
void build()
364359
{

Source/Library/h/Path.hpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
#pragma once
3+
4+
#include <string>
5+
#include <vector>
6+
7+
namespace sd::filesystem
8+
{
9+
class Path
10+
{
11+
public:
12+
enum Format
13+
{
14+
Native,
15+
Standard,
16+
Auto
17+
};
18+
19+
private:
20+
Format _format = Auto;
21+
std::vector<std::string> _segments;
22+
23+
public:
24+
static const char NativeSeparator;
25+
static const char StandardSeparator;
26+
27+
Path() = default;
28+
Path(const Path &path);
29+
Path(Path &&path);
30+
Path(const std::string &path, Format fmt = Auto);
31+
template <class InputIt> Path(InputIt first, InputIt last, Format fmt = Auto);
32+
33+
Path &operator=(const Path &path);
34+
Path &operator=(Path &&path);
35+
36+
Path &Assign(const Path &path);
37+
Path &Assign(Path &&path);
38+
39+
Path &operator/=(const Path &path);
40+
Path &operator/=(Path &&path);
41+
42+
Path &Append(const Path &path);
43+
Path &Append(Path &&path);
44+
45+
Path &operator+=(const Path &path);
46+
Path &operator+=(Path &&path);
47+
48+
Path &Concat(const Path &path);
49+
Path &Concat(Path &&path);
50+
51+
Path &RemoveFileName();
52+
Path &ReplaceFileName(const Path &path);
53+
Path &ReplaceExtension(const Path &path);
54+
55+
std::string ToString() const;
56+
std::string ToNativeString() const;
57+
std::string ToStandardString() const;
58+
59+
Path RootName() const;
60+
Path RootDirectory() const;
61+
Path RootPath() const;
62+
Path ParentPath() const;
63+
Path FileName() const;
64+
Path Extension() const;
65+
Path Stem() const;
66+
67+
bool HasRootName() const;
68+
bool HasRootDirectory() const;
69+
bool HasRootPath() const;
70+
bool HasParentPath() const;
71+
bool HasFileName() const;
72+
bool HasExtension() const;
73+
bool HasStem() const;
74+
75+
void Swap(Path &path);
76+
77+
void Clear();
78+
79+
bool Empty() const;
80+
81+
private:
82+
static Path FromString(std::string path, Path::Format fmt = Path::Format::Auto);
83+
static Path FromStringSegments(std::vector<std::string> segments, Path::Format fmt = Path::Format::Auto);
84+
};
85+
} // namespace sd::filesystem

0 commit comments

Comments
 (0)