aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTucker Evans <tucker@tuckerevans.com>2020-07-02 20:21:28 -0400
committerTucker Evans <tucker@tuckerevans.com>2020-07-04 22:28:40 -0400
commit80d98a39a6bc4b71c74f240df617495e816b98b9 (patch)
tree56f914caab810f3ffd05d7098400f50b203f930f
parente4ea23a6c23acdec86e5b5fcd34c653118f6898e (diff)
Add printing functions for vectors
-rw-r--r--collections/vector/vector.adoc45
-rw-r--r--collections/vector/vector.c26
-rw-r--r--collections/vector/vector.h1
3 files changed, 71 insertions, 1 deletions
diff --git a/collections/vector/vector.adoc b/collections/vector/vector.adoc
index 3ffd10c..fbb0b32 100644
--- a/collections/vector/vector.adoc
+++ b/collections/vector/vector.adoc
@@ -1,7 +1,7 @@
Vector
======
Tucker Evans
-v0.5, 2020-07-02
+v0.6, 2020-07-02
A basic vector, that hold pointers to your data structures.
@@ -169,3 +169,46 @@ vec_clear(vector);
assert(vec_size(vector) == 0);
vec_free(vector);
----
+
+[[vec_print]]
++void vec_print(vec *self, (char* to_string(void*)))+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Prints out the contents of the vector +self+ to +stdout+ (surounded by square
+brackets and separated by commas ','). +to_string+ is a function that takes a
+pointer to the type of elements stored in +self+ and returns a string
+representation.
+
+Examples
+^^^^^^^^
+[source,c]
+----
+#include "vector.h"
+#include <string.h>
+
+char* to_string(str)
+void *str;
+{
+ return str;
+}
+
+int main()
+{
+char *str1 = "ONE";
+char *str2 = "TWO";
+char *str3 = "THREE";
+
+vec *vector = vec_new();
+vec_push(vector, str_dup(str1));
+vec_push(vector, str_dup(str2));
+vec_push(vector, str_dup(str3));
+
+printf("VEC CONTENTS:\n\t")
+vec_print(vector, to_string)
+}
+----
+
+Output:
+----
+VEC_CONTENTS:
+ [ONE,TWO,THREE]
+----
diff --git a/collections/vector/vector.c b/collections/vector/vector.c
index 9544917..b3751dd 100644
--- a/collections/vector/vector.c
+++ b/collections/vector/vector.c
@@ -118,3 +118,29 @@ 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");
+}
diff --git a/collections/vector/vector.h b/collections/vector/vector.h
index 3996030..54e40a7 100644
--- a/collections/vector/vector.h
+++ b/collections/vector/vector.h
@@ -11,4 +11,5 @@ void* vec_index(vec*, int);
void* vec_pop(vec*);
void vec_free(vec*);
void vec_clear(vec*);
+void vec_print(vec*, char* (void*));
#endif