Last active
December 10, 2015 15:49
-
-
Save cleure/4457194 to your computer and use it in GitHub Desktop.
Vector Abstract Data Type in C89. Symbols exported via VECTOR_EXPORT() Macro, in case of symbol clashing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <memory.h> | |
#include <stdint.h> | |
#define VECTOR_EXPORT(SYM) SYM | |
struct VECTOR_EXPORT(vector) { | |
uint32_t elmsize; | |
uint32_t capacity; | |
uint32_t len; | |
uint8_t *v; | |
}; | |
struct | |
VECTOR_EXPORT(vector *) | |
VECTOR_EXPORT(vector_new) | |
(uint32_t capacity, size_t elmsize) | |
{ | |
struct VECTOR_EXPORT(vector *) v; | |
if (!(v = malloc(sizeof(*v)))) return NULL; | |
if (!(v->v = malloc(elmsize * capacity))) return NULL; | |
memset(v->v, 0, elmsize * capacity); | |
v->elmsize = elmsize; | |
v->capacity = capacity; | |
v->len = 0; | |
return v; | |
} | |
void | |
VECTOR_EXPORT(vector_delete) | |
(struct VECTOR_EXPORT(vector *) v) | |
{ | |
if (!v) return; | |
if (v->v) free(v->v); | |
free(v); | |
} | |
int VECTOR_EXPORT(vector_resize) | |
(struct VECTOR_EXPORT(vector *)v, size_t capacity) | |
{ | |
uint8_t *nv; | |
if (!v) return 0; | |
if (!(nv = realloc(v->v, v->elmsize * capacity))) return 0; | |
v->capacity = capacity; | |
v->v = nv; | |
return 1; | |
} | |
void VECTOR_EXPORT(vector_clear) | |
(struct VECTOR_EXPORT(vector *)v) | |
{ | |
if (!v || !v->v) return; | |
memset(v->v, 0, v->elmsize * v->capacity); | |
} | |
int | |
VECTOR_EXPORT(vector_set) | |
(struct VECTOR_EXPORT(vector *) v, uint32_t idx, void *d) | |
{ | |
if (!v || !(idx < v->capacity)) return 0; | |
memcpy(&(v->v[v->elmsize*idx]), d, v->elmsize); | |
v->len++; | |
return 1; | |
} | |
void * | |
VECTOR_EXPORT(vector_getptr) | |
(struct VECTOR_EXPORT(vector *) v, uint32_t idx) | |
{ | |
if (!v || !(idx < v->capacity)) return NULL; | |
return &(v->v[v->elmsize*idx]); | |
} | |
void | |
VECTOR_EXPORT(vector_getcpy) | |
(struct VECTOR_EXPORT(vector *) v, uint32_t idx, void *dst) | |
{ | |
if (!v || !(idx < v->capacity)) return; | |
memcpy(dst, &(v->v[v->elmsize*idx]), v->elmsize); | |
} | |
int | |
VECTOR_EXPORT(vector_del) | |
(struct VECTOR_EXPORT(vector *) v, uint32_t idx) | |
{ | |
if (!v || !(idx < v->capacity)) return 0; | |
memset(&(v->v[v->elmsize*idx]), 0, v->elmsize); | |
v->len--; | |
return 1; | |
} | |
int main(int argc, char **argv) | |
{ | |
int i, len; | |
int data[] = {100, 200, 400, 512, 1024, 4096}; | |
struct vector *v; | |
len = sizeof(data)/sizeof(data[0]); | |
v = vector_new(512, sizeof(int)); | |
for (i = 0; i < len; i++) { | |
vector_set(v, i, &data[i]); | |
} | |
for (i = 0; i < len; i++) { | |
printf("%d\n", *(int*)vector_getptr(v, i)); | |
} | |
vector_delete(v); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment