Move computation of tree size/offset into tree constructor
This commit is contained in:
parent
868a09b0b0
commit
baec9f2c9a
4 changed files with 70 additions and 32 deletions
|
|
@ -81,7 +81,6 @@ ts_tree * ts_stack_reduce(ts_stack *stack,
|
|||
// later collapse the stack again when the document is edited.
|
||||
// We store the children and immediate children in the same array,
|
||||
// to reduce allocations.
|
||||
size_t size = 0, offset = 0;
|
||||
size_t child_index = child_count;
|
||||
ts_tree **children = malloc((child_count + immediate_child_count) * sizeof(ts_tree *));
|
||||
ts_tree **immediate_children = children + child_count;
|
||||
|
|
@ -99,16 +98,9 @@ ts_tree * ts_stack_reduce(ts_stack *stack,
|
|||
child_index--;
|
||||
children[child_index] = child;
|
||||
}
|
||||
|
||||
if (child_index == 0) {
|
||||
offset += ts_tree_offset(child);
|
||||
size += ts_tree_size(child);
|
||||
} else {
|
||||
size += ts_tree_offset(child) + ts_tree_size(child);
|
||||
}
|
||||
}
|
||||
|
||||
ts_tree *lookahead = ts_tree_make_node(symbol, child_count, immediate_child_count, children, size, offset);
|
||||
ts_tree *lookahead = ts_tree_make_node(symbol, child_count, immediate_child_count, children);
|
||||
ts_stack_shrink(stack, stack->size - immediate_child_count);
|
||||
return lookahead;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,10 +40,19 @@ ts_tree * ts_tree_make_leaf(ts_symbol symbol, size_t size, size_t offset) {
|
|||
return result;
|
||||
}
|
||||
|
||||
ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, size_t immediate_child_count, ts_tree **children, size_t size, size_t offset) {
|
||||
ts_tree * ts_tree_make_node(ts_symbol symbol, size_t child_count, size_t immediate_child_count, ts_tree **children) {
|
||||
ts_tree **immediate_children = children + child_count;
|
||||
for (size_t i = 0; i < immediate_child_count; i++)
|
||||
ts_tree_retain(immediate_children[i]);
|
||||
size_t size, offset;
|
||||
for (size_t i = 0; i < immediate_child_count; i++) {
|
||||
ts_tree *child = immediate_children[i];
|
||||
ts_tree_retain(child);
|
||||
if (i == 0) {
|
||||
offset = ts_tree_offset(child);
|
||||
size = ts_tree_size(child);
|
||||
} else {
|
||||
size += ts_tree_offset(child) + ts_tree_size(child);
|
||||
}
|
||||
}
|
||||
ts_tree *result = ts_tree_make(symbol, size, offset);
|
||||
result->data.children.count = child_count;
|
||||
result->data.children.immediate_count = immediate_child_count;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue