You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.9 KiB
C

9 years ago
#include "vector.h"
#include <stdio.h>
inline int __vector_PushPtr(Vector *v, void *elem) {
if (v->top == v->cap) {
Vector_Resize(v, v->cap ? v->cap * 2 : 1);
}
8 years ago
__vector_PutPtr(v, v->top++, elem);
return v->top;
9 years ago
}
8 years ago
inline int Vector_Get(Vector *v, size_t pos, void *ptr) {
// return 0 if pos is out of bounds
if (pos >= v->top) {
return 0;
}
8 years ago
memcpy(ptr, v->data + (pos * v->elemSize), v->elemSize);
return 1;
9 years ago
}
8 years ago
/* Get the element at the end of the vector, decreasing the size by one */
inline int Vector_Pop(Vector *v, void *ptr) {
if (v->top > 0) {
if (ptr != NULL) {
Vector_Get(v, v->top - 1, ptr);
8 years ago
}
v->top--;
return 1;
}
return 0;
8 years ago
}
9 years ago
inline int __vector_PutPtr(Vector *v, size_t pos, void *elem) {
// resize if pos is out of bounds
if (pos >= v->cap) {
Vector_Resize(v, pos + 1);
}
8 years ago
if (elem) {
memcpy(v->data + pos * v->elemSize, elem, v->elemSize);
} else {
memset(v->data + pos * v->elemSize, 0, v->elemSize);
}
// move the end offset to pos if we grew
if (pos >= v->top) {
v->top = pos + 1;
}
return 1;
9 years ago
}
int Vector_Resize(Vector *v, size_t newcap) {
int oldcap = v->cap;
v->cap = newcap;
8 years ago
v->data = realloc(v->data, v->cap * v->elemSize);
8 years ago
// If we grew:
// put all zeros at the newly realloc'd part of the vector
if (newcap > oldcap) {
int offset = oldcap * v->elemSize;
memset(v->data + offset, 0, v->cap * v->elemSize - offset);
}
return v->cap;
9 years ago
}
Vector *__newVectorSize(size_t elemSize, size_t cap) {
Vector *vec = malloc(sizeof(Vector));
vec->data = calloc(cap, elemSize);
vec->top = 0;
vec->elemSize = elemSize;
vec->cap = cap;
8 years ago
return vec;
9 years ago
}
void Vector_Free(Vector *v) {
free(v->data);
free(v);
9 years ago
}
/* return the used size of the vector, regardless of capacity */
inline int Vector_Size(Vector *v) { return v->top; }
/* return the actual capacity */
inline int Vector_Cap(Vector *v) { return v->cap; }