Skip to content

Instantly share code, notes, and snippets.

@FurryHead
Created July 15, 2011 19:00
Show Gist options
  • Save FurryHead/1085293 to your computer and use it in GitHub Desktop.
Save FurryHead/1085293 to your computer and use it in GitHub Desktop.
Dynamic array and map in C (header).
#ifndef ARRAY_H
#define ARRAY_H
#ifdef __cplusplus
extern "C" {
#endif
#define ARRAY_EXPAND_SIZE 10
struct array {
void **items;
int size;
int _num_allocated;
int _num_elements;
};
struct array *array_new(void);
int array_set(struct array *arr, int index, void *item);
int array_append(struct array *arr, void *item);
void *array_pop(struct array *arr);
void *array_get(struct array *arr, int index);
void *array_remove(struct array *arr, int index);
void array_free(struct array *arr);
struct map {
struct array *keys;
struct array *values;
};
struct map *map_new(void);
void *map_get(struct map *m, void *key);
int map_set(struct map *m, void *key, void *value);
void *map_remove(struct map *m, void *key);
void map_free(struct map *m);
#ifdef __cplusplus
}
#endif
#endif /* ARRAY_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment