Skip to content

Instantly share code, notes, and snippets.

@leeonix
Created May 13, 2015 12:18
Show Gist options
  • Save leeonix/50d926e8433ac37f9ed6 to your computer and use it in GitHub Desktop.
Save leeonix/50d926e8433ac37f9ed6 to your computer and use it in GitHub Desktop.
dynamic array structure
/*
* =====================================================================================
*
* Filename: array.c
* Description: array structure
* Created: 2011-10-14 9:52:47
*
* =====================================================================================
*/
#include <malloc.h>
#include <string.h>
#include "array.h"
int array_setcapacity(struct array_t *ary, int val)
{
if (ary->capacity == val) {
return ARRAY_SUCCESS;
}
if (val == 0) {
free(ary->array);
ary->array = NULL;
} else {
void **newbuf = realloc(ary->array, val * sizeof(void *));
if (newbuf == NULL) {
free(ary->array);
ary->array = NULL;
ary->count = ary->capacity = 0;
return ARRAY_MALLOC_ERROR;
}
ary->array = newbuf;
}
ary->capacity = val;
return ARRAY_SUCCESS;
} /* ----- end of function array_setcapacity ----- */
static int array_grow(struct array_t *ary)
{
if (ary->count < ary->capacity) {
return ARRAY_SUCCESS;
} else {
int n = ary->capacity;
int delta = n > 64 ? n / 4 : 16;
return array_setcapacity(ary, n + delta);
}
} /* ----- end of function array_grow ----- */
int array_create(struct array_t **out)
{
struct array_t *ary = (struct array_t *) malloc(sizeof(struct array_t));
if (ary == NULL) {
return ARRAY_MALLOC_ERROR;
}
memset(ary, 0, sizeof(struct array_t));
*out = ary;
return ARRAY_SUCCESS;
} /* ----- end of function array_create ----- */
void array_destroy(struct array_t *ary)
{
array_clear(ary);
free(ary);
} /* ----- end of function array_release ----- */
void array_clear(struct array_t *ary)
{
if (ary->count == 0) {
return;
}
if (ary->capacity > 0) {
free(ary->array);
ary->array = NULL;
ary->capacity = 0;
}
ary->count = 0;
} /* ----- end of function array_clear ----- */
int array_push(struct array_t *ary, void *val)
{
int res = array_grow(ary);
if (res == ARRAY_SUCCESS) {
ary->array[ary->count++] = val;
}
return res;
} /* ----- end of function array_push ----- */
int array_insert(struct array_t *ary, int index, void *val)
{
int res;
if (index < 0 || index > ary->count) {
return ARRAY_INDEX_ERROR;
}
res = array_grow(ary);
if (res == ARRAY_SUCCESS) {
if (index < ary->count) {
void **end = ary->array + index;
void **p = ary->array + ary->count - 0;
for (; p >= end; --p) {
*(p + 1) = *p;
}
}
ary->array[index] = val;
ary->count++;
}
return res;
} /* ----- end of function array_insert ----- */
int array_indexof(struct array_t *ary, void *val)
{
int i = 0;
while (i < ary->count && ary->array[i] != val) {
++i;
}
return i == ary->count ? -1 : i;
} /* ----- end of function array_indexof ----- */
int array_delete(struct array_t *ary, int index)
{
if (index < 0 || index >= ary->count) {
return ARRAY_INDEX_ERROR;
}
if (index < --ary->count) {
void **p = ary->array + index;
void **end = ary->array + ary->count;
for (; p < end; ++p) {
*p = *(p + 1);
}
}
return ARRAY_SUCCESS;
} /* ----- end of function array_delete ----- */
int array_remove(struct array_t *ary, void *val)
{
int index = array_indexof(ary, val);
int res = index >= 0;
if (res) {
array_delete(ary, index);
}
return res;
} /* ----- end of function array_remove ----- */
int array_back(struct array_t *ary, void **val)
{
if (ary->count == 0) {
return ARRAY_INDEX_ERROR;
}
*val = ary->array[ary->count - 1];
return ARRAY_SUCCESS;
} /* ----- end of function array_back ----- */
int array_get(struct array_t *ary, int index, void **val)
{
if (index < 0 || index >= ary->count) {
return ARRAY_INDEX_ERROR;
}
*val = ary->array[index];
return ARRAY_SUCCESS;
}
int array_set(struct array_t *ary, int index, void *val)
{
if (index < 0 || index >= ary->count) {
return ARRAY_INDEX_ERROR;
}
ary->array[index] = val;
return ARRAY_SUCCESS;
}
/*
* =====================================================================================
*
* Filename: array.h
* Description: dynamic array
* Created: 2011-10-15 0:24:14
*
* =====================================================================================
*/
#ifndef ___array_INC___
#define ___array_INC___
enum ARRAY_ERROR_TYPE {
ARRAY_SUCCESS = 0,
ARRAY_MALLOC_ERROR,
ARRAY_INDEX_ERROR,
};
struct array_t {
void **array;
int capacity;
int count;
};
int array_setcapacity(struct array_t *ary, int val);
int array_create(struct array_t **out);
void array_destroy(struct array_t *ary);
void array_clear(struct array_t *ary);
int array_push(struct array_t *ary, void *val);
int array_insert(struct array_t *ary, int, void *val);
int array_indexof(struct array_t *ary, void *val);
int array_delete(struct array_t *ary, int index);
int array_remove(struct array_t *ary, void *val);
int array_back(struct array_t *ary, void **val);
int array_get(struct array_t *ary, int, void **val);
int array_set(struct array_t *ary, int, void *val);
#endif /* ----- #ifndef ___array_INC___ ----- */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment