#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
+#include <assert.h>
#include "array.h"
static void extend(struct array* array)
if (array->count == array->max)
{
int newmax = (array->max == 0) ? 8 : (array->max * 2);
- void** newarray = realloc(array->item, newmax * sizeof(void*));
+ struct array* newarray = realloc(array->item, newmax * sizeof(*newarray));
array->max = newmax;
array->item = newarray;
array->count++;
}
-bool array_contains(void* arrayp, void* value)
+int array_indexof(void* arrayp, void* value)
{
struct array* array = arrayp;
int i;
for (i=0; i<array->count; i++)
if (array->item[i] == value)
- return true;
+ return i;
+
+ return -1;
+}
- return false;
+bool array_contains(void* arrayp, void* value)
+{
+ return (array_indexof(arrayp, value) != -1);
}
bool array_appendu(void* arrayp, void* value)
}
}
+void* array_pop(void* arrayp)
+{
+ struct array* array = arrayp;
+
+ assert(array->count > 0);
+ return array->item[array->count--];
+}
+
/* vim: set sw=4 ts=4 expandtab : */
extern void array_insert(void* array, void* value, int before);
extern void array_remove(void* array, void* value);
extern bool array_contains(void* array, void* value);
+extern int array_indexof(void* array, void* value);
+
+#define array_push(a, v) array_append(a, v)
+extern void* array_pop(void* array);
#endif