blob: 50c7fef655ada77d46074f6b05a7fc79030e971a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rope.h"
struct rope_s {
size_t len;
char *str;
struct rope_s *left, *right, *parent;
};
rope* rope_new()
{
rope *tmp;
tmp = malloc(sizeof(rope));
assert(tmp);
tmp->len = 0;
tmp->str = NULL;
tmp->left = NULL;
tmp->right = NULL;
tmp->parent = NULL;
return tmp;
}
size_t rope_len(root)
rope *root;
{
if (!root)
return 0;
if (root->str)
return strlen(root->str);
return rope_len(root->left) + rope_len(root->right);
}
rope* str_to_rope(str)
char *str;
{
rope *tmp;
if (!str)
return NULL;
tmp = rope_new();
tmp->len = strlen(str);
tmp->str = strdup(str);
return tmp;
}
rope* rope_concat(node1, node2)
rope *node1, *node2;
{
rope *tmp;
if (!node1 || !node2)
return node1 ? node1 : node2;
tmp = rope_new();
tmp->len = rope_len(node1);
tmp->left = node1;
tmp->right = node2;
node1->parent = tmp;
node2->parent = tmp;
return tmp;
}
|