aboutsummaryrefslogtreecommitdiff
path: root/collections/vector/vector.c
diff options
context:
space:
mode:
Diffstat (limited to 'collections/vector/vector.c')
-rw-r--r--collections/vector/vector.c117
1 files changed, 58 insertions, 59 deletions
diff --git a/collections/vector/vector.c b/collections/vector/vector.c
index 8aaffb6..23e5c47 100644
--- a/collections/vector/vector.c
+++ b/collections/vector/vector.c
@@ -1,11 +1,10 @@
#include "vector.h"
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
-#include <stdio.h>
-
#define START_SIZE 64;
struct vector {
@@ -13,6 +12,19 @@ struct vector {
int end, limit;
};
+void vec_debug_print(root)
+vec *root;
+{
+ int i;
+
+ fprintf(stderr, "VEC[base: %p, end: %p, limit:%p]:\n\t ",
+ root->base, root->end, root->limit);
+ for (i=0; i < root->end; i++){
+ fprintf(stderr, "[%p]", vec_index(root,i));
+ }
+ fprintf(stderr, "\n");
+}
+
vec* vec_new()
{
vec *root;
@@ -61,6 +73,40 @@ vec *root;
assert(root->base);
}
+vec* vec_cp(root)
+vec *root;
+{
+ vec *copy;
+
+ if (!root)
+ return NULL;
+
+ copy = vec_with_capacity(root->limit);
+
+ copy->base = memcpy(copy->base, root->base,
+ vec_size(root) * sizeof(void*));
+ assert(copy->base);
+
+ copy->end = root->end;
+ copy->limit = root->limit;
+
+ return copy;
+}
+
+void vec_print(root, to_string)
+vec *root;
+char* to_string(void*);
+{
+ int i;
+
+ printf("[");
+ for(i = 0; i < root->end; i++) {
+ printf("%s", to_string(vec_index(root, i)));
+ }
+ printf("\b]\n");
+
+}
+
void vec_push(root, item)
vec *root;
void *item;
@@ -76,17 +122,6 @@ void *item;
root->base[root->end++] = item;
}
-void* vec_index(root, index)
-vec *root;
-int index;
-{
- if (!root || index >= root->end || index < 0) {
- return NULL;
- }
-
- return root->base[index];
-}
-
void* vec_pop(root)
vec *root;
{
@@ -97,13 +132,15 @@ vec *root;
return root->base[--root->end];
}
-void vec_free(root)
+void* vec_index(root, index)
vec *root;
+int index;
{
- free(root->base);
- root->base = NULL;
+ if (!root || index >= root->end || index < 0) {
+ return NULL;
+ }
- free(root);
+ return root->base[index];
}
void vec_clear(root)
@@ -118,49 +155,11 @@ vec *root;
root->end = 0;
}
-void vec_print(root, to_string)
-vec *root;
-char* to_string(void*);
-{
- int i;
-
- printf("[");
- for(i = 0; i < root->end; i++) {
- printf("%s", to_string(vec_index(root, i)));
- }
- printf("\b]\n");
-
-}
-
-void vec_debug_print(root)
-vec *root;
-{
- int i;
-
- fprintf(stderr, "VEC[base: %p, end: %p, limit:%p]:\n\t ",
- root->base, root->end, root->limit);
- for (i=0; i < root->end; i++){
- fprintf(stderr, "[%p]", vec_index(root,i));
- }
- fprintf(stderr, "\n");
-}
-
-vec* vec_cp(root)
+void vec_free(root)
vec *root;
{
- vec *copy;
-
- if (!root)
- return NULL;
-
- copy = vec_with_capacity(root->limit);
-
- copy->base = memcpy(copy->base, root->base,
- vec_size(root) * sizeof(void*));
- assert(copy->base);
-
- copy->end = root->end;
- copy->limit = root->limit;
+ free(root->base);
+ root->base = NULL;
- return copy;
+ free(root);
}