From ab34cfecd9c7ce98b15c20edfa2d221f030f86b5 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 15 Nov 2015 09:55:36 -0800 Subject: [PATCH] Replace TreeVector with a more generic Vector struct --- spec/runtime/stack_spec.cc | 3 +- src/runtime/stack.c | 12 ++++--- src/runtime/tree_vector.h | 55 ------------------------------- src/runtime/vector.h | 67 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 61 deletions(-) delete mode 100644 src/runtime/tree_vector.h create mode 100644 src/runtime/vector.h diff --git a/spec/runtime/stack_spec.cc b/spec/runtime/stack_spec.cc index 05be07e8..b2d12208 100644 --- a/spec/runtime/stack_spec.cc +++ b/spec/runtime/stack_spec.cc @@ -1,4 +1,5 @@ #include "runtime/runtime_spec_helper.h" +#include "runtime/helpers/tree_helpers.h" #include "runtime/stack.h" #include "runtime/tree.h" #include "runtime/length.h" @@ -43,7 +44,7 @@ describe("Stack", [&]() { TSLength len = ts_length_make(2, 2); for (size_t i = 0; i < tree_count; i++) - trees[i] = ts_tree_make_leaf(ts_builtin_sym_start + i, len, len, TSNodeTypeNamed); + trees[i] = ts_tree_make_leaf(i, len, len, TSNodeTypeNamed); }); after_each([&]() { diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 3a4dd308..89efb4b8 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -1,6 +1,6 @@ #include "tree_sitter/parser.h" #include "runtime/tree.h" -#include "runtime/tree_vector.h" +#include "runtime/vector.h" #include "runtime/stack.h" #include "runtime/length.h" #include @@ -229,7 +229,8 @@ StackPopResultList ts_stack_pop(Stack *self, int head_index, int child_count, int capacity = (child_count == -1) ? STARTING_TREE_CAPACITY : child_count; size_t tree_counts_by_path[MAX_POP_PATH_COUNT] = { child_count }; StackNode *nodes_by_path[MAX_POP_PATH_COUNT] = { previous_head }; - TreeVector trees_by_path[MAX_POP_PATH_COUNT] = { tree_vector_new(capacity) }; + Vector trees_by_path[MAX_POP_PATH_COUNT] = { vector_new(sizeof(TSTree *), + capacity) }; bool is_shared_by_path[MAX_POP_PATH_COUNT] = { false }; /* @@ -257,10 +258,11 @@ StackPopResultList ts_stack_pop(Stack *self, int head_index, int child_count, * the additional successors. */ if (is_shared_by_path[path]) { - trees_by_path[path] = tree_vector_copy(&trees_by_path[path]); + trees_by_path[path] = vector_copy(&trees_by_path[path]); is_shared_by_path[path] = false; } - tree_vector_push(&trees_by_path[path], node->entry.tree); + ts_tree_retain(node->entry.tree); + vector_push(&trees_by_path[path], &node->entry.tree); for (int i = 0; i < node->successor_count; i++) { int next_path; @@ -283,7 +285,7 @@ StackPopResultList ts_stack_pop(Stack *self, int head_index, int child_count, for (int path = 0; path < path_count; path++) { if (!is_shared_by_path[path]) - tree_vector_reverse(&trees_by_path[path]); + vector_reverse(&trees_by_path[path]); int index = -1; if (path == 0) { stack_node_retain(nodes_by_path[path]); diff --git a/src/runtime/tree_vector.h b/src/runtime/tree_vector.h deleted file mode 100644 index 4464e52c..00000000 --- a/src/runtime/tree_vector.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef RUNTIME_TREE_VECTOR_H_ -#define RUNTIME_TREE_VECTOR_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include "./tree.h" - -typedef struct { - TSTree **contents; - size_t capacity; - size_t size; -} TreeVector; - -static inline TreeVector tree_vector_new(size_t size) { - return (TreeVector){ - .contents = malloc(size * sizeof(TSTree *)), .capacity = size, .size = 0, - }; -} - -static inline void tree_vector_push(TreeVector *self, TSTree *tree) { - if (self->size == self->capacity) { - self->capacity += 4; - self->contents = realloc(self->contents, self->capacity * sizeof(TSTree *)); - } - ts_tree_retain(tree); - self->contents[self->size++] = tree; -} - -static inline void tree_vector_reverse(TreeVector *self) { - TSTree *swap; - size_t limit = self->size / 2; - for (size_t i = 0; i < limit; i++) { - swap = self->contents[i]; - self->contents[i] = self->contents[self->size - 1 - i]; - self->contents[self->size - 1 - i] = swap; - } -} - -static inline TreeVector tree_vector_copy(TreeVector *self) { - return (TreeVector){ - .contents = memcpy(malloc(self->capacity * sizeof(TSTree *)), - self->contents, self->size * sizeof(TSTree *)), - .capacity = self->capacity, - .size = self->size, - }; -} - -#ifdef __cplusplus -} -#endif - -#endif // RUNTIME_TREE_VECTOR_H_ diff --git a/src/runtime/vector.h b/src/runtime/vector.h new file mode 100644 index 00000000..c62a43c9 --- /dev/null +++ b/src/runtime/vector.h @@ -0,0 +1,67 @@ +#ifndef RUNTIME_VECTOR_H_ +#define RUNTIME_VECTOR_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +typedef struct { + void *contents; + size_t size; + size_t capacity; + size_t element_size; +} Vector; + +static inline Vector vector_new(size_t element_size, size_t capacity) { + return (Vector){ + .contents = malloc(capacity * element_size), + .size = 0, + .capacity = capacity, + .element_size = element_size, + }; +} + +static inline void vector_push(Vector *self, void *entry) { + if (self->size == self->capacity) { + self->capacity += 4; + self->contents = + realloc(self->contents, self->capacity * self->element_size); + } + + char *contents = (char *)self->contents; + memcpy(contents + (self->size * self->element_size), (char *)entry, + self->element_size); + self->size++; +} + +static inline void vector_reverse(Vector *self) { + char swap[self->element_size]; + char *contents = (char *)self->contents; + size_t limit = self->size / 2; + for (size_t i = 0; i < limit; i++) { + size_t offset = i * self->element_size; + size_t reverse_offset = (self->size - 1 - i) * self->element_size; + memcpy(&swap, contents + offset, self->element_size); + memcpy(contents + offset, contents + reverse_offset, self->element_size); + memcpy(contents + reverse_offset, &swap, self->element_size); + } +} + +static inline Vector vector_copy(Vector *self) { + return (Vector){ + .contents = memcpy(malloc(self->capacity * self->element_size), + self->contents, self->size * self->element_size), + .size = self->size, + .capacity = self->capacity, + .element_size = self->element_size, + }; +} + +#ifdef __cplusplus +} +#endif + +#endif // RUNTIME_VECTOR_H_