Replace TreeVector with a more generic Vector struct
This commit is contained in:
parent
0824d3e1f3
commit
ab34cfecd9
4 changed files with 76 additions and 61 deletions
|
|
@ -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([&]() {
|
||||
|
|
|
|||
|
|
@ -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 <assert.h>
|
||||
|
|
@ -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]);
|
||||
|
|
|
|||
|
|
@ -1,55 +0,0 @@
|
|||
#ifndef RUNTIME_TREE_VECTOR_H_
|
||||
#define RUNTIME_TREE_VECTOR_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#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_
|
||||
67
src/runtime/vector.h
Normal file
67
src/runtime/vector.h
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#ifndef RUNTIME_VECTOR_H_
|
||||
#define RUNTIME_VECTOR_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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_
|
||||
Loading…
Add table
Add a link
Reference in a new issue