-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
77 lines (60 loc) · 2.52 KB
/
vector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef VECTOR_H
#define VECTOR_H
#include <stdbool.h>
#include <stddef.h>
#define vecForeach(elem, vec) \
for(size_t __cont = 1, __i = 0; __cont && __i < (vec).size; __cont = !__cont, __i++) \
for(elem = vecIterator(&(vec), __i); __cont; __cont = !__cont)
typedef struct Vector {
size_t size, capacity;
size_t elemSize;
void* data;
} Vector;
// -----------------------------------------------------------------------------
// CONSTRUCTORS
// -----------------------------------------------------------------------------
void vecInit(Vector* vec, size_t elemSize);
Vector vecNew(size_t elemSize);
Vector vecCopy(const Vector* vec);
void vecCopyAssign(Vector* dest, const Vector* src);
Vector vecMove(Vector* vec);
void vecMoveAssign(Vector* dest, Vector* src);
void vecFree(Vector* vec);
// -----------------------------------------------------------------------------
// ELEMENT ACCESS
// -----------------------------------------------------------------------------
void* vecGet(Vector* vec, size_t i);
const void* vecConstGet(const Vector* vec, size_t i);
void* vecData(Vector* vec);
const void* vecConstData(const Vector* vec);
void* vecFront(Vector* vec);
const void* vecConstFront(const Vector* vec);
void* vecBack(Vector* vec);
const void* vecConstBack(const Vector* vec);
// -----------------------------------------------------------------------------
// MODIFIERS
// -----------------------------------------------------------------------------
void vecClear(Vector* vec);
size_t vecPush(Vector* vec, void* elem);
void vecSet(Vector* vec, size_t i, void* elem);
void* vecInsert(Vector* vec, size_t i, void* elem);
void* vecErase(Vector* vec, size_t i);
void vecPop(Vector* vec);
void vecSwap(Vector* v1, Vector* v2);
// -----------------------------------------------------------------------------
// CAPACITY
// -----------------------------------------------------------------------------
bool vecEmpty(const Vector* vec);
size_t vecSize(const Vector* vec);
size_t vecCapacity(const Vector* vec);
bool vecIsInitialized(const Vector* vec);
void vecReserve(Vector* vec, size_t required);
void vecShrinkToFit(Vector* vec);
// -----------------------------------------------------------------------------
// ITERATOR
// -----------------------------------------------------------------------------
void* vecBegin(Vector* vec);
void* vecEnd(Vector* vec);
void* vecIterator(Vector* vec, size_t i);
size_t vecIteratorIndex(const Vector* vec, void* it);
#endif // VECTOR_H