Last active
November 18, 2015 13:09
-
-
Save 1995eaton/64ea5182c0e593b8379a to your computer and use it in GitHub Desktop.
Dynamic arrays in C
This file contains hidden or 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 <string.h> | |
#include <stdlib.h> | |
#define ARRAY_STRING | |
#if defined ARRAY_STRING | |
#define ARRAY_PRINT "s" | |
typedef char* array_t; | |
#else | |
#define ARRAY_PRINT "d" | |
typedef int array_t; | |
#endif | |
typedef struct Array { | |
array_t *data; | |
size_t capacity; | |
size_t length; | |
} Array; | |
Array create_array() { | |
Array arr; | |
arr.capacity = 8; | |
arr.length = 0; | |
arr.data = (array_t*) malloc(sizeof(array_t) * arr.capacity); | |
return arr; | |
} | |
void array_resize(Array *arr, size_t size) { | |
arr->data = realloc(arr->data, sizeof(array_t) * size); | |
arr->capacity = size; | |
} | |
void array_push(Array *arr, array_t item) { | |
arr->data[arr->length++] = item; | |
if (arr->length >= arr->capacity) { | |
array_resize(arr, arr->capacity * 2); | |
} | |
} | |
const array_t array_pop(Array *arr) { | |
if (arr->length > 0) { | |
return arr->data[--(arr->length)]; | |
} | |
#if defined ARRAY_STRING | |
return NULL; | |
#else | |
return -1; | |
#endif | |
} | |
const array_t array_popn(Array *arr, int index) { | |
if (index < 0 || index >= arr->length) { | |
#if defined ARRAY_STRING | |
return NULL; | |
#else | |
return -1; | |
#endif | |
} | |
array_t item = arr->data[index]; | |
arr->length--; | |
memmove(arr->data + index, arr->data + index + 1, | |
sizeof(array_t) * (arr->length - index)); | |
return item; | |
} | |
const array_t array_get(Array *arr, int index) { | |
return arr->data[index]; | |
} | |
void array_reverse(Array *arr) { | |
array_t *start = arr->data; | |
array_t *end = arr->data + arr->length - 1; | |
while (start < end) { | |
array_t temp = *start; | |
*start++ = *end; | |
*end-- = temp; | |
} | |
} | |
int array_indexof(Array *arr, array_t item) { | |
for (size_t i = 0; i < arr->length; i++) { | |
if (memcmp(&arr->data[i], &item, sizeof(array_t)) == 0) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
void print_array(Array *arr) { | |
if (arr->length == 0) { | |
printf("[]\n"); | |
return; | |
} | |
printf("[\n"); | |
for (size_t i = 0; i < arr->length - 1; i++) { | |
printf(" %" ARRAY_PRINT ",\n", arr->data[i]); | |
} | |
printf(" %" ARRAY_PRINT "\n]\n", arr->data[arr->length - 1]); | |
} | |
void clear_array(Array *arr) { | |
#ifdef ARRAY_STRING | |
for (size_t i = 0; i < arr->length; i++) { | |
free(arr->data[i]); | |
} | |
#endif | |
free(arr->data); | |
arr->data = calloc(8, sizeof(array_t)); | |
arr->capacity = 8; | |
arr->length = 0; | |
} | |
#include <sys/time.h> | |
char *random_string(int N) { | |
char *result = calloc(N, sizeof(char)); | |
for (int i = 0; i < N; i++) { | |
result[i] = (int) (rand() % 93) + 33; | |
} | |
return result; | |
} | |
int main(void) { | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
srand(tv.tv_usec * tv.tv_sec); | |
Array arr = create_array(); | |
for (int i = 0; i < 100; i++) { | |
array_push(&arr, random_string(80)); | |
} | |
print_array(&arr); | |
clear_array(&arr); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment