Skip to content

Instantly share code, notes, and snippets.

@jonbro
Created April 23, 2015 23:38
Show Gist options
  • Select an option

  • Save jonbro/6e8dc7328b13e8782d03 to your computer and use it in GitHub Desktop.

Select an option

Save jonbro/6e8dc7328b13e8782d03 to your computer and use it in GitHub Desktop.
vector implementation
// http://www.happybearsoftware.com/implementing-a-dynamic-array.html, but with void pointers
#include "lipsynth_vector.h"
void lipsynth_vector_init(LipsynthVector *vector, size_t itemSize) {
// initialize size and capacity
vector->size = 0;
vector->itemSize = itemSize;
vector->capacity = VECTOR_INITIAL_CAPACITY;
// allocate memory for vector->data
vector->data = calloc(0, vector->itemSize * vector->capacity);
}
void lipsynth_vector_append(LipsynthVector *vector, const void* value) {
// make sure there's room to expand into
lipsynth_vector_double_capacity_if_full(vector);
// append the value and increment vector->size
memcpy(vector->data+vector->size*vector->itemSize, value, vector->itemSize);
vector->size++;
}
void lipsynth_vector_append_zero(LipsynthVector *vector){
lipsynth_vector_double_capacity_if_full(vector);
memset(vector->data+vector->size*vector->itemSize, 0, vector->itemSize);
vector->size++;
}
void* lipsynth_vector_get(LipsynthVector *vector, int index) {
if (index >= vector->size || index < 0) {
printf("Index %d out of bounds for vector of size %d\n", index, vector->size);
exit(1);
}
// return the pointer value in this index
return vector->data+vector->itemSize*index;
}
void lipsynth_vector_set(LipsynthVector *vector, int index, void *value) {
// zero fill the vector up to the desired index
while (index >= vector->size) {
lipsynth_vector_append_zero(vector);
}
// set the value at the desired index
memcpy(vector->data+index*vector->itemSize, value, vector->itemSize);
}
void lipsynth_vector_double_capacity_if_full(LipsynthVector *vector) {
if (vector->size >= vector->capacity) {
// double vector->capacity and resize the allocated memory accordingly
vector->capacity *= 2;
vector->data = realloc(vector->data, vector->itemSize * vector->capacity);
}
}
void lipsynth_vector_free(LipsynthVector *vector) {
free(vector->data);
}
#ifndef __LipSynth__lipsynth_vector__
#define __LipSynth__lipsynth_vector__
#define VECTOR_INITIAL_CAPACITY 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int size; // slots used so far
int capacity; // total available slots
void *data; // array of integers we're storing
size_t itemSize;
} LipsynthVector;
void lipsynth_vector_init(LipsynthVector *vector, size_t itemSize);
void lipsynth_vector_append(LipsynthVector *vector, const void* value);
void lipsynth_vector_append_zero(LipsynthVector *vector);
void *lipsynth_vector_get(LipsynthVector *vector, int index);
void lipsynth_vector_set(LipsynthVector *vector, int index, void* value);
void lipsynth_vector_double_capacity_if_full(LipsynthVector *vector);
void lipsynth_vector_free(LipsynthVector *vector);
#endif /* defined(__LipSynth__lipsynth_vector__) */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment