|
1 | 1 | #include <assert.h> |
2 | 2 | #include <ctype.h> |
3 | | -#include <limits.h> |
| 3 | +#include <stdint.h> |
4 | 4 | #include <stdbool.h> |
5 | 5 | #include <string.h> |
6 | 6 | #include <stdio.h> |
7 | 7 | #include "./alloc.h" |
| 8 | +#include "./array.h" |
8 | 9 | #include "./atomic.h" |
9 | 10 | #include "./subtree.h" |
10 | 11 | #include "./length.h" |
@@ -618,21 +619,30 @@ void ts_subtree_release(SubtreePool *pool, Subtree self) { |
618 | 619 | } |
619 | 620 | } |
620 | 621 |
|
621 | | -int ts_subtree_compare(Subtree left, Subtree right) { |
622 | | - if (ts_subtree_symbol(left) < ts_subtree_symbol(right)) return -1; |
623 | | - if (ts_subtree_symbol(right) < ts_subtree_symbol(left)) return 1; |
624 | | - if (ts_subtree_child_count(left) < ts_subtree_child_count(right)) return -1; |
625 | | - if (ts_subtree_child_count(right) < ts_subtree_child_count(left)) return 1; |
626 | | - for (uint32_t i = 0, n = ts_subtree_child_count(left); i < n; i++) { |
627 | | - Subtree left_child = ts_subtree_children(left)[i]; |
628 | | - Subtree right_child = ts_subtree_children(right)[i]; |
629 | | - switch (ts_subtree_compare(left_child, right_child)) { |
630 | | - case -1: return -1; |
631 | | - case 1: return 1; |
632 | | - default: break; |
| 622 | +int ts_subtree_compare(Subtree left, Subtree right, SubtreePool *pool) { |
| 623 | + array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(left)); |
| 624 | + array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(right)); |
| 625 | + |
| 626 | + int result = 0; |
| 627 | + while (result == 0 && pool->tree_stack.size > 0) { |
| 628 | + right = ts_subtree_from_mut(array_pop(&pool->tree_stack)); |
| 629 | + left = ts_subtree_from_mut(array_pop(&pool->tree_stack)); |
| 630 | + |
| 631 | + if (ts_subtree_symbol(left) < ts_subtree_symbol(right)) result = -1; |
| 632 | + if (ts_subtree_symbol(right) < ts_subtree_symbol(left)) result = 1; |
| 633 | + if (ts_subtree_child_count(left) < ts_subtree_child_count(right)) result = -1; |
| 634 | + if (ts_subtree_child_count(right) < ts_subtree_child_count(left)) result = 1; |
| 635 | + |
| 636 | + for (uint32_t i = ts_subtree_child_count(left); i > 0; i--) { |
| 637 | + Subtree left_child = ts_subtree_children(left)[i - 1]; |
| 638 | + Subtree right_child = ts_subtree_children(right)[i - 1]; |
| 639 | + array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(left_child)); |
| 640 | + array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(right_child)); |
633 | 641 | } |
634 | 642 | } |
635 | | - return 0; |
| 643 | + |
| 644 | + array_clear(&pool->tree_stack); |
| 645 | + return result; |
636 | 646 | } |
637 | 647 |
|
638 | 648 | static inline void ts_subtree_set_has_changes(MutableSubtree *self) { |
|
0 commit comments