| 1 |
|
| #include <string.h> |
| 2 |
|
| #include <stdio.h> |
| 3 |
|
| #include <stdlib.h> |
| 4 |
|
| #include <assert.h> |
| 5 |
|
| #include <ctype.h> |
| 6 |
|
| |
| 7 |
|
| typedef struct Suffix_Tree_Node Suffix_Tree_Node; |
| 8 |
|
| |
| 9 |
|
| typedef struct Active_Point Active_Point; |
| 10 |
|
| |
| 11 |
|
| struct Active_Point |
| 12 |
|
| { |
| 13 |
|
| int m_node_id; |
| 14 |
|
| int m_begin_idx; |
| 15 |
|
| int m_end_idx; |
| 16 |
|
| }; |
| 17 |
|
| |
| 18 |
|
| struct Suffix_Tree_Node |
| 19 |
|
| { |
| 20 |
|
| int m_begin_char_idx; /* inclusive */ |
| 21 |
|
| int m_end_char_idx; /* inclusive */ |
| 22 |
|
| int m_parent; |
| 23 |
|
| int m_id; |
| 24 |
|
| int m_child; |
| 25 |
|
| int m_sibling; |
| 26 |
|
| int m_in_s1; |
| 27 |
|
| int m_in_s2; |
| 28 |
|
| }; |
| 29 |
|
| |
| 30 |
|
| typedef struct Suffix_Tree Suffix_Tree; |
| 31 |
|
| |
| 32 |
|
| struct Suffix_Tree |
| 33 |
|
| { |
| 34 |
|
| Suffix_Tree_Node *m_nodes; |
| 35 |
|
| int m_hash_base; |
| 36 |
|
| int m_strlen; |
| 37 |
|
| int m_size; |
| 38 |
|
| char *m_str; |
| 39 |
|
| int *m_suffix; |
| 40 |
|
| Suffix_Tree_Node m_head; |
| 41 |
|
| }; |
| 42 |
|
| |
| 43 |
0 |
| static void ctor_node(Suffix_Tree_Node *node, |
| 44 |
|
| int begin_idx, int end_idx, int parent, int id) |
| 45 |
|
| { |
| 46 |
0 |
| node->m_begin_char_idx = begin_idx; |
| 47 |
0 |
| node->m_end_char_idx = end_idx; |
| 48 |
0 |
| node->m_parent = parent; |
| 49 |
0 |
| node->m_id = id; |
| 50 |
0 |
| node->m_child = -1; |
| 51 |
0 |
| node->m_sibling = -1; |
| 52 |
0 |
| } |
| 53 |
|
| |
| 54 |
0 |
| static void suffix_tree_cleanup(Suffix_Tree *t) |
| 55 |
|
| { |
| 56 |
0 |
| free(t->m_nodes); |
| 57 |
0 |
| free(t->m_suffix); |
| 58 |
0 |
| t->m_hash_base = -1; |
| 59 |
0 |
| } |
| 60 |
|
| |
| 61 |
0 |
| static void ctor_Active_Point(Active_Point *ap, int node_id, |
| 62 |
|
| int begin_idx, int end_idx) |
| 63 |
|
| { |
| 64 |
0 |
| ap->m_node_id = node_id; |
| 65 |
0 |
| ap->m_begin_idx = begin_idx; |
| 66 |
0 |
| ap->m_end_idx = end_idx; |
| 67 |
0 |
| } |
| 68 |
|
| |
| 69 |
0 |
| static int implicit(Active_Point *ap) |
| 70 |
|
| { |
| 71 |
0 |
| return ap->m_begin_idx <= ap->m_end_idx; |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
|
| /* |
| 75 |
|
| static int explicit(Active_Point *ap) |
| 76 |
|
| { |
| 77 |
|
| return ap->m_begin_idx > ap->m_end_idx; |
| 78 |
|
| } |
| 79 |
|
| */ |
| 80 |
|
| |
| 81 |
0 |
| static int hash(Suffix_Tree *t, int parent_id, int chr) |
| 82 |
|
| { |
| 83 |
0 |
| int x = ((parent_id << 8) + chr) % t->m_hash_base; |
| 84 |
0 |
▶ | if (x < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x < 0) { |
0 |
0 |
0 |
|
| 85 |
0 |
| x += t->m_hash_base; |
| 86 |
|
| } |
| 87 |
0 |
| return x; |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
0 |
| static int ap_span(Active_Point *ap) |
| 91 |
|
| { |
| 92 |
0 |
| return ap->m_end_idx - ap->m_begin_idx; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
0 |
| static int edge_span(Suffix_Tree_Node *node) |
| 96 |
|
| { |
| 97 |
0 |
| return node->m_end_char_idx - node->m_begin_char_idx; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
0 |
| static char ap_begin_char(Suffix_Tree *t, Active_Point *ap) |
| 101 |
|
| { |
| 102 |
0 |
| return t->m_str[ap->m_begin_idx]; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
0 |
| static char ap_end_char(Suffix_Tree *t, Active_Point *ap) |
| 106 |
|
| { |
| 107 |
0 |
| return t->m_str[ap->m_end_idx]; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
0 |
| static char ap_any_char(Suffix_Tree *t, Active_Point *ap, int any) |
| 111 |
|
| { |
| 112 |
0 |
| return t->m_str[ap->m_begin_idx + any]; |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
0 |
| static char node_begin_char(Suffix_Tree *t, Suffix_Tree_Node *node) |
| 116 |
|
| { |
| 117 |
0 |
| return t->m_str[node->m_begin_char_idx]; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
0 |
| static char node_end_char(Suffix_Tree *t, Suffix_Tree_Node *node) |
| 121 |
|
| { |
| 122 |
0 |
| return t->m_str[node->m_end_char_idx]; |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
0 |
| static char node_any_char(Suffix_Tree *t, Suffix_Tree_Node *node, int any) |
| 126 |
|
| { |
| 127 |
0 |
| return t->m_str[node->m_begin_char_idx + any]; |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
0 |
| static char node_contains(Suffix_Tree_Node *node, int pos) |
| 131 |
|
| { |
| 132 |
0 |
▶ | return node->m_begin_char_idx <= pos && pos <= node->m_end_char_idx; |
Branch: 2 branches
| Branch | True | False | Total |
| return node->m_begin_char_idx <= pos && pos <= node->m_end_char_idx; |
0 |
0 |
0 |
| return node->m_begin_char_idx <= pos && pos <= node->m_end_char_idx; |
0 |
0 |
0 |
|
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
|
| /* find a node(edge) which is a child of 'parent_id' and begin with 'chr' */ |
| 136 |
0 |
| static int find_unused_node(Suffix_Tree *t, int parent_id, int chr) |
| 137 |
|
| { |
| 138 |
0 |
| int i = hash(t, parent_id, chr); |
| 139 |
0 |
| while (1) { |
| 140 |
0 |
| Suffix_Tree_Node *node = t->m_nodes + i; |
| 141 |
0 |
▶ | if (0 > node->m_id) { /* unused slot */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (0 > node->m_id) { /* unused slot */ |
0 |
0 |
0 |
|
| 142 |
0 |
| return i; |
| 143 |
|
| } |
| 144 |
0 |
| ++i; |
| 145 |
0 |
| i = i % t->m_hash_base; |
| 146 |
0 |
▶ | if (i < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < 0) { |
0 |
0 |
0 |
|
| 147 |
0 |
| i += t->m_hash_base; |
| 148 |
|
| } |
| 149 |
|
| } |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
|
| /* find a node(edge) which is a child of 'parent_id' and begin with 'chr' */ |
| 153 |
0 |
| static int find_node(Suffix_Tree *t, int parent_id, int chr) |
| 154 |
|
| { |
| 155 |
0 |
| int i = hash(t, parent_id, chr); |
| 156 |
0 |
| while (1) { |
| 157 |
0 |
| Suffix_Tree_Node *node = t->m_nodes + i; |
| 158 |
0 |
▶ | if (-1 == node->m_id) { /* unused slot */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (-1 == node->m_id) { /* unused slot */ |
0 |
0 |
0 |
|
| 159 |
0 |
| return i; |
| 160 |
|
| } |
| 161 |
0 |
▶ | if (node->m_parent == parent_id && |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->m_parent == parent_id && |
0 |
0 |
0 |
|
| 162 |
0 |
▶ | node_begin_char(t, node) == chr) { |
Branch: 1 branch
| Branch | True | False | Total |
| node_begin_char(t, node) == chr) { |
0 |
0 |
0 |
|
| 163 |
0 |
| return i; |
| 164 |
|
| } |
| 165 |
|
| /* printf("%d \n", i); */ |
| 166 |
0 |
| ++i; |
| 167 |
0 |
| i = i % t->m_hash_base; |
| 168 |
0 |
▶ | if (i < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < 0) { |
0 |
0 |
0 |
|
| 169 |
0 |
| i += t->m_hash_base; |
| 170 |
|
| } |
| 171 |
|
| } |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
0 |
| static void increment(int *i, int base) |
| 175 |
|
| { |
| 176 |
0 |
| ++*i; |
| 177 |
0 |
| *i = *i % base; |
| 178 |
0 |
▶ | if (*i < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*i < 0) { |
0 |
0 |
0 |
|
| 179 |
0 |
| *i += base; |
| 180 |
|
| } |
| 181 |
0 |
| } |
| 182 |
|
| |
| 183 |
0 |
| static int new_node(Suffix_Tree *t, int begin_idx, int end_idx, int parent) |
| 184 |
|
| { |
| 185 |
|
| int i; |
| 186 |
0 |
| t->m_size++; |
| 187 |
0 |
| i = find_unused_node(t, parent, t->m_str[begin_idx]); |
| 188 |
|
| { |
| 189 |
0 |
| Suffix_Tree_Node *node = t->m_nodes + i; |
| 190 |
0 |
▶ | assert (0 > node->m_id); /* unused or removed */ |
Branch: 1 branch
| Branch | True | False | Total |
| assert (0 > node->m_id); /* unused or removed */ |
0 |
0 |
0 |
|
| 191 |
0 |
| ctor_node(node, begin_idx, end_idx, parent, t->m_size); |
| 192 |
|
| } |
| 193 |
0 |
| return i; |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
0 |
| static int split_edge(Suffix_Tree *t, Active_Point *ap) |
| 197 |
|
| { |
| 198 |
|
| int node_idx, i, nid; |
| 199 |
|
| Suffix_Tree_Node *node, *tmp_node; |
| 200 |
0 |
▶ | assert(ap); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ap); |
0 |
0 |
0 |
|
| 201 |
0 |
▶ | assert(implicit(ap)); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(implicit(ap)); |
0 |
0 |
0 |
|
| 202 |
0 |
| node_idx = find_node(t, ap->m_node_id, ap_begin_char(t, ap)); |
| 203 |
0 |
| node = t->m_nodes + node_idx; |
| 204 |
|
| |
| 205 |
0 |
▶ | assert(node->m_id != -1); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(node->m_id != -1); |
0 |
0 |
0 |
|
| 206 |
0 |
▶ | assert(edge_span(node) >= ap_span(ap)); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(edge_span(node) >= ap_span(ap)); |
0 |
0 |
0 |
|
| 207 |
|
| |
| 208 |
0 |
▶ | assert(ap_span(ap) > 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ap_span(ap) > 0); |
0 |
0 |
0 |
|
| 209 |
0 |
▶ | assert(ap_end_char(t, ap) != node_any_char(t, node, ap_span(ap))); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ap_end_char(t, ap) != node_any_char(t, node, ap_span(ap))); |
0 |
0 |
0 |
|
| 210 |
0 |
▶ | assert(ap_any_char(t, ap, ap_span(ap)-1) == node_any_char(t, node, ap_span(ap)-1)); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ap_any_char(t, ap, ap_span(ap)-1) == node_any_char(t, node, ap_span(ap)-1)); |
0 |
0 |
0 |
|
| 211 |
|
| |
| 212 |
|
| /* match up to [ap_span(ap) - 1], see last two assertions |
| 213 |
|
| * we want to reuse the existing node because parent and the first char |
| 214 |
|
| * does not change, so what we create below is actually the existing node |
| 215 |
|
| * We have to adjust the node "node" |
| 216 |
|
| */ |
| 217 |
0 |
| i = new_node(t, node->m_begin_char_idx + ap_span(ap), |
| 218 |
|
| node->m_end_char_idx, |
| 219 |
0 |
| t->m_size+1); /* parent is the newly created id */ |
| 220 |
|
| |
| 221 |
|
| /* swap node id because the new node should be parent of the existing node. */ |
| 222 |
0 |
| tmp_node = t->m_nodes + i; |
| 223 |
0 |
| tmp_node->m_id = node->m_id; |
| 224 |
|
| |
| 225 |
0 |
| node->m_id = t->m_size; |
| 226 |
0 |
| node->m_end_char_idx = node->m_begin_char_idx + ap_span(ap) - 1; |
| 227 |
0 |
| return t->m_size; |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
0 |
| static void canonize(Suffix_Tree *t, Active_Point *ap) |
| 231 |
|
| { |
| 232 |
0 |
▶ | while (ap_span(ap) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ap_span(ap) > 0) { |
0 |
0 |
0 |
|
| 233 |
|
| int edge_len; |
| 234 |
0 |
| int nid = find_node(t, ap->m_node_id, ap_begin_char(t, ap)); |
| 235 |
0 |
| Suffix_Tree_Node *node = t->m_nodes + nid; |
| 236 |
0 |
▶ | if ( node->m_id <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( node->m_id <= 0) { |
0 |
0 |
0 |
|
| 237 |
0 |
| return; |
| 238 |
|
| } |
| 239 |
0 |
| edge_len = edge_span(node); |
| 240 |
0 |
▶ | if (edge_len > ap_span(ap) - 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (edge_len > ap_span(ap) - 1) { |
0 |
0 |
0 |
|
| 241 |
0 |
| return; |
| 242 |
|
| } |
| 243 |
0 |
| ap->m_node_id = node->m_id; |
| 244 |
0 |
| ap->m_begin_idx += edge_len + 1; |
| 245 |
|
| } |
| 246 |
|
| } |
| 247 |
|
| |
| 248 |
0 |
| static void follow_suffix_link(Suffix_Tree *t, Active_Point *ap) |
| 249 |
|
| { |
| 250 |
0 |
▶ | if (ap->m_node_id) { /* not root */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (ap->m_node_id) { /* not root */ |
0 |
0 |
0 |
|
| 251 |
0 |
| ap->m_node_id = t->m_suffix[ap->m_node_id]; |
| 252 |
|
| } else { /* root */ |
| 253 |
0 |
| ap->m_begin_idx++; |
| 254 |
|
| } |
| 255 |
0 |
| canonize(t, ap); |
| 256 |
0 |
| } |
| 257 |
|
| |
| 258 |
0 |
| static void update(Suffix_Tree *t, Active_Point *ap) |
| 259 |
|
| { |
| 260 |
0 |
| int last_parent = -1; |
| 261 |
0 |
| while (1) { |
| 262 |
0 |
| int node_idx = find_node(t, ap->m_node_id, ap_begin_char(t, ap)); |
| 263 |
0 |
| Suffix_Tree_Node *node = t->m_nodes + node_idx; |
| 264 |
0 |
▶ | assert(ap_span(ap) >= 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ap_span(ap) >= 0); |
0 |
0 |
0 |
|
| 265 |
|
| |
| 266 |
0 |
▶ | if (node->m_id < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->m_id < 0) { |
0 |
0 |
0 |
|
| 267 |
0 |
▶ | assert(ap_span(ap) == 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ap_span(ap) == 0); |
0 |
0 |
0 |
|
| 268 |
|
| /* new node */ |
| 269 |
0 |
| new_node(t, ap->m_end_idx, t->m_strlen - 1, ap->m_node_id); |
| 270 |
0 |
▶ | if (last_parent > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_parent > 0) { |
0 |
0 |
0 |
|
| 271 |
0 |
▶ | assert(t->m_suffix[last_parent] == ap->m_node_id |
Branch: 2 branches
| Branch | True | False | Total |
| assert(t->m_suffix[last_parent] == ap->m_node_id |
0 |
0 |
0 |
| assert(t->m_suffix[last_parent] == ap->m_node_id |
0 |
0 |
0 |
|
| 272 |
|
| || t->m_suffix[last_parent] == -1); |
| 273 |
0 |
| t->m_suffix[last_parent] = ap->m_node_id; |
| 274 |
|
| } |
| 275 |
0 |
| last_parent = ap->m_node_id; |
| 276 |
0 |
| follow_suffix_link(t, ap); |
| 277 |
0 |
▶ | if (ap_span(ap) < 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ap_span(ap) < 0) break; |
0 |
0 |
0 |
|
| 278 |
|
| } else { |
| 279 |
0 |
▶ | assert(edge_span(node) >= ap_span(ap) - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(edge_span(node) >= ap_span(ap) - 1); |
0 |
0 |
0 |
|
| 280 |
0 |
| if (edge_span(node) == ap_span(ap) - 1) { |
| 281 |
|
| |
| 282 |
|
| } |
| 283 |
0 |
▶ | if (node_any_char(t, node, ap_span(ap)) == ap_end_char(t, ap)) { /* match */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (node_any_char(t, node, ap_span(ap)) == ap_end_char(t, ap)) { /* match */ |
0 |
0 |
0 |
|
| 284 |
0 |
▶ | if (last_parent > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_parent > 0) { |
0 |
0 |
0 |
|
| 285 |
|
| /* suffix link: last_parent -> current's parent */ |
| 286 |
0 |
| t->m_suffix[last_parent] = node->m_parent; |
| 287 |
|
| } |
| 288 |
0 |
| break; |
| 289 |
|
| } else { /* last char in active point not match */ |
| 290 |
|
| int parent; |
| 291 |
0 |
▶ | assert(ap_span(ap) > 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ap_span(ap) > 0); |
0 |
0 |
0 |
|
| 292 |
0 |
▶ | assert(ap_any_char(t, ap, ap_span(ap) - 1) == |
Branch: 1 branch
| Branch | True | False | Total |
| assert(ap_any_char(t, ap, ap_span(ap) - 1) == |
0 |
0 |
0 |
|
| 293 |
|
| node_any_char(t, node, ap_span(ap) - 1)); |
| 294 |
|
| |
| 295 |
0 |
▶ | assert(implicit(ap)); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(implicit(ap)); |
0 |
0 |
0 |
|
| 296 |
0 |
| parent = split_edge(t, ap); |
| 297 |
|
| |
| 298 |
0 |
| new_node(t, ap->m_end_idx, t->m_strlen-1, parent); |
| 299 |
0 |
▶ | if (last_parent > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_parent > 0) { |
0 |
0 |
0 |
|
| 300 |
0 |
▶ | assert(t->m_suffix[last_parent] == -1); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(t->m_suffix[last_parent] == -1); |
0 |
0 |
0 |
|
| 301 |
0 |
| t->m_suffix[last_parent] = parent; |
| 302 |
|
| } |
| 303 |
0 |
| last_parent = parent; |
| 304 |
0 |
| follow_suffix_link(t, ap); |
| 305 |
|
| } |
| 306 |
|
| } |
| 307 |
|
| } |
| 308 |
0 |
| } |
| 309 |
|
| |
| 310 |
0 |
| static void print(Suffix_Tree *t) |
| 311 |
|
| { |
| 312 |
|
| int i, j; |
| 313 |
0 |
▶ | for (i = 0; i < t->m_hash_base; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->m_hash_base; i++) { |
0 |
0 |
0 |
|
| 314 |
0 |
| Suffix_Tree_Node *node = t->m_nodes + i; |
| 315 |
0 |
▶ | if (node->m_id > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->m_id > 0) { |
0 |
0 |
0 |
|
| 316 |
0 |
| printf("%4d%4d%4d%4d%4d ", node->m_parent, node->m_id, |
| 317 |
0 |
| node->m_begin_char_idx, node->m_end_char_idx, t->m_suffix[node->m_id]); |
| 318 |
0 |
| for (j = node->m_begin_char_idx; |
| 319 |
0 |
▶ | j <= node->m_end_char_idx; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| j <= node->m_end_char_idx; j++) { |
0 |
0 |
0 |
|
| 320 |
0 |
| printf("%c", t->m_str[j]); |
| 321 |
|
| } |
| 322 |
0 |
| printf("\n"); |
| 323 |
|
| } |
| 324 |
|
| } |
| 325 |
0 |
| } |
| 326 |
|
| |
| 327 |
0 |
| static void print_ap(Active_Point *ap) |
| 328 |
|
| { |
| 329 |
0 |
| printf("%d %d %d\n", ap->m_node_id, ap->m_begin_idx, ap->m_end_idx); |
| 330 |
0 |
| } |
| 331 |
|
| |
| 332 |
0 |
| static void suffix_tree_init(Suffix_Tree *t, char *str) |
| 333 |
|
| { |
| 334 |
0 |
| int size = strlen(str) + 1; |
| 335 |
|
| int i; |
| 336 |
0 |
| t->m_strlen = size; |
| 337 |
0 |
| size *= 2; |
| 338 |
0 |
| t->m_hash_base = size+1; |
| 339 |
0 |
| t->m_size = 0; |
| 340 |
0 |
| t->m_nodes = (Suffix_Tree_Node*) |
| 341 |
0 |
| malloc(sizeof(Suffix_Tree_Node)*t->m_hash_base); |
| 342 |
0 |
| t->m_str = str; |
| 343 |
0 |
| t->m_suffix = (int*)malloc(sizeof(int) * t->m_hash_base); |
| 344 |
|
| |
| 345 |
0 |
▶ | for (i = 0; i < t->m_hash_base; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->m_hash_base; i++) { |
0 |
0 |
0 |
|
| 346 |
0 |
| ctor_node(t->m_nodes + i, -1, -1, -1, -1); |
| 347 |
0 |
| t->m_suffix[i] = -1; |
| 348 |
|
| } |
| 349 |
|
| { |
| 350 |
|
| Active_Point ap; |
| 351 |
0 |
| ctor_Active_Point(&ap, 0, 0, 0); |
| 352 |
0 |
▶ | for (;ap.m_end_idx < t->m_strlen; ap.m_end_idx++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (;ap.m_end_idx < t->m_strlen; ap.m_end_idx++) { |
0 |
0 |
0 |
|
| 353 |
0 |
| canonize(t, &ap); |
| 354 |
0 |
| update(t, &ap); |
| 355 |
|
| /* print(t); |
| 356 |
|
| print_ap(&ap); |
| 357 |
|
| printf("-----------------------------------------------\n\n"); |
| 358 |
|
| */ |
| 359 |
|
| } |
| 360 |
|
| } |
| 361 |
0 |
| } |
| 362 |
|
| |
| 363 |
0 |
| static void suffix_tree_destroy(Suffix_Tree *t) |
| 364 |
|
| { |
| 365 |
0 |
| free(t->m_nodes); |
| 366 |
0 |
| free(t->m_suffix); |
| 367 |
0 |
| } |
| 368 |
|
| |
| 369 |
|
| #define END_STRING 0 |
| 370 |
|
| #define DOLLAR_SIGN -1 |
| 371 |
|
| |
| 372 |
0 |
| static void calc_lcs(Suffix_Tree *t, int s1_len, int id, int depth, |
| 373 |
|
| int *len, int *pos1, int *pos2) |
| 374 |
|
| { |
| 375 |
0 |
| Suffix_Tree_Node *node = t->m_nodes + id; |
| 376 |
0 |
▶ | assert(node->m_id == id && id >= 0); |
Branch: 2 branches
| Branch | True | False | Total |
| assert(node->m_id == id && id >= 0); |
0 |
0 |
0 |
| assert(node->m_id == id && id >= 0); |
0 |
0 |
0 |
|
| 377 |
0 |
| if (edge_span(node) >= 0 && |
| 378 |
0 |
| node_contains(node, s1_len)) { |
| 379 |
0 |
▶ | assert(-1 == node->m_child); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(-1 == node->m_child); |
0 |
0 |
0 |
|
| 380 |
0 |
| } else if (edge_span(node) >= 0 && |
| 381 |
0 |
| node_end_char(t, node) == END_STRING) { |
| 382 |
0 |
▶ | assert(-1 == node->m_child); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(-1 == node->m_child); |
0 |
0 |
0 |
|
| 383 |
|
| } else { |
| 384 |
0 |
| int child = node->m_child; |
| 385 |
|
| int t1, t2; |
| 386 |
|
| /* assert(node->m_child > 0); */ |
| 387 |
|
| |
| 388 |
0 |
▶ | while (child > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (child > 0) { |
0 |
0 |
0 |
|
| 389 |
0 |
| Suffix_Tree_Node *nc = t->m_nodes + child; |
| 390 |
0 |
| calc_lcs(t, s1_len, child, depth + edge_span(node) + 1, len, pos1, pos2); |
| 391 |
0 |
| child = nc->m_sibling; |
| 392 |
0 |
▶ | if (nc->m_in_s1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->m_in_s1) { |
0 |
0 |
0 |
|
| 393 |
0 |
| t1 = nc->m_begin_char_idx; |
| 394 |
|
| } |
| 395 |
0 |
▶ | if (nc->m_in_s2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->m_in_s2) { |
0 |
0 |
0 |
|
| 396 |
0 |
| t2 = nc->m_begin_char_idx; |
| 397 |
|
| } |
| 398 |
|
| } |
| 399 |
0 |
▶ | if (node->m_in_s1 && node->m_in_s2 && *len < depth + edge_span(node) + 1) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (node->m_in_s1 && node->m_in_s2 && *len < depth + edge_span(node) + 1) { |
0 |
0 |
0 |
| if (node->m_in_s1 && node->m_in_s2 && *len < depth + edge_span(node) + 1) { |
0 |
0 |
0 |
| if (node->m_in_s1 && node->m_in_s2 && *len < depth + edge_span(node) + 1) { |
0 |
0 |
0 |
|
| 400 |
0 |
| *len = depth + edge_span(node) + 1; |
| 401 |
0 |
| *pos1 = t1; |
| 402 |
0 |
| *pos2 = t2; |
| 403 |
|
| } |
| 404 |
|
| } |
| 405 |
0 |
▶ | assert(node->m_in_s1 || node->m_in_s2); |
Branch: 2 branches
| Branch | True | False | Total |
| assert(node->m_in_s1 || node->m_in_s2); |
0 |
0 |
0 |
| assert(node->m_in_s1 || node->m_in_s2); |
0 |
0 |
0 |
|
| 406 |
0 |
| } |
| 407 |
|
| |
| 408 |
0 |
| static void traverse_mark(Suffix_Tree *t, int s1_len, int id) |
| 409 |
|
| { |
| 410 |
0 |
| Suffix_Tree_Node *node = t->m_nodes + id; |
| 411 |
0 |
▶ | assert(node->m_id == id && id >= 0); |
Branch: 2 branches
| Branch | True | False | Total |
| assert(node->m_id == id && id >= 0); |
0 |
0 |
0 |
| assert(node->m_id == id && id >= 0); |
0 |
0 |
0 |
|
| 412 |
0 |
| node->m_in_s1 = 0; |
| 413 |
0 |
| node->m_in_s2 = 0; |
| 414 |
0 |
| if (edge_span(node) >= 0 && |
| 415 |
0 |
| node_contains(node, s1_len)) { |
| 416 |
0 |
▶ | assert(-1 == node->m_child); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(-1 == node->m_child); |
0 |
0 |
0 |
|
| 417 |
0 |
| node->m_in_s1 = 1; |
| 418 |
0 |
| } else if (edge_span(node) >= 0 && |
| 419 |
0 |
| node_end_char(t, node) == END_STRING) { |
| 420 |
0 |
▶ | assert(-1 == node->m_child); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(-1 == node->m_child); |
0 |
0 |
0 |
|
| 421 |
0 |
| node->m_in_s2 = 1; |
| 422 |
|
| } else { |
| 423 |
0 |
| int child = node->m_child; |
| 424 |
|
| /* assert(node->m_child > 0); */ |
| 425 |
0 |
▶ | while (child > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (child > 0) { |
0 |
0 |
0 |
|
| 426 |
0 |
| Suffix_Tree_Node *nc = t->m_nodes + child; |
| 427 |
0 |
| traverse_mark(t, s1_len, child); |
| 428 |
0 |
| child = nc->m_sibling; |
| 429 |
0 |
▶ | if (nc->m_in_s1) node->m_in_s1 = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->m_in_s1) node->m_in_s1 = 1; |
0 |
0 |
0 |
|
| 430 |
0 |
▶ | if (nc->m_in_s2) node->m_in_s2 = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nc->m_in_s2) node->m_in_s2 = 1; |
0 |
0 |
0 |
|
| 431 |
|
| } |
| 432 |
|
| } |
| 433 |
0 |
▶ | assert(node->m_in_s1 || node->m_in_s2); |
Branch: 2 branches
| Branch | True | False | Total |
| assert(node->m_in_s1 || node->m_in_s2); |
0 |
0 |
0 |
| assert(node->m_in_s1 || node->m_in_s2); |
0 |
0 |
0 |
|
| 434 |
0 |
| } |
| 435 |
|
| |
| 436 |
0 |
| static void lcs(int *pos1, int *pos2, int *len, |
| 437 |
|
| char const *s1, int s1_len, |
| 438 |
|
| char const *s2, int s2_len) |
| 439 |
|
| { |
| 440 |
|
| char *buff = |
| 441 |
0 |
| (char *)malloc(sizeof(const char) * (s1_len + s2_len + 2)); |
| 442 |
|
| Suffix_Tree t; |
| 443 |
0 |
| strncpy((char*)buff, (char*)s1, s1_len); |
| 444 |
0 |
| buff[s1_len] = DOLLAR_SIGN; /* as '$' */ |
| 445 |
0 |
| strncpy(buff + s1_len + 1, s2, s2_len); |
| 446 |
0 |
| buff[s1_len + s2_len + 1] = 0; |
| 447 |
0 |
| suffix_tree_init(&t, buff); |
| 448 |
|
| { |
| 449 |
|
| /* construct child and sibling */ |
| 450 |
|
| int i; |
| 451 |
|
| /* first move node to their proper destination based on their id */ |
| 452 |
0 |
▶ | for (i = 0; i < t.m_hash_base; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t.m_hash_base; i++) { |
0 |
0 |
0 |
|
| 453 |
0 |
| Suffix_Tree_Node *node = t.m_nodes + i; |
| 454 |
0 |
▶ | while (node->m_id > 0 && node->m_id != i) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (node->m_id > 0 && node->m_id != i) { |
0 |
0 |
0 |
| while (node->m_id > 0 && node->m_id != i) { |
0 |
0 |
0 |
|
| 455 |
0 |
| Suffix_Tree_Node tmp = t.m_nodes[node->m_id]; |
| 456 |
0 |
| t.m_nodes[node->m_id] = *node; |
| 457 |
0 |
| *node = tmp; |
| 458 |
|
| } |
| 459 |
|
| } |
| 460 |
|
| |
| 461 |
|
| /* set up root (node 0) */ |
| 462 |
0 |
| ctor_node(t.m_nodes, 0, -1, -1, 0); |
| 463 |
|
| |
| 464 |
|
| /* construct the tree */ |
| 465 |
0 |
▶ | for (i = 1; i < t.m_hash_base; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < t.m_hash_base; i++) { |
0 |
0 |
0 |
|
| 466 |
0 |
| Suffix_Tree_Node *node = t.m_nodes + i; |
| 467 |
|
| Suffix_Tree_Node *parent; |
| 468 |
0 |
▶ | if (node->m_id <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->m_id <= 0) { |
0 |
0 |
0 |
|
| 469 |
0 |
| break; |
| 470 |
|
| } |
| 471 |
0 |
| parent = t.m_nodes + node->m_parent; |
| 472 |
0 |
| node->m_sibling = parent->m_child; |
| 473 |
0 |
| parent->m_child = node->m_id; |
| 474 |
|
| } |
| 475 |
|
| /* post order traversal */ |
| 476 |
|
| { |
| 477 |
0 |
| traverse_mark(&t, s1_len, 0); |
| 478 |
0 |
| calc_lcs(&t, s1_len, 0, 0, len, pos1, pos2); |
| 479 |
0 |
▶ | if (*len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*len > 0) { |
0 |
0 |
0 |
|
| 480 |
0 |
| *pos1 -= *len; |
| 481 |
0 |
| *pos2 -= s1_len + 1 + *len; |
| 482 |
0 |
▶ | assert(*pos1 >= 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(*pos1 >= 0); |
0 |
0 |
0 |
|
| 483 |
0 |
▶ | assert(*pos2 >= 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(*pos2 >= 0); |
0 |
0 |
0 |
|
| 484 |
|
| } |
| 485 |
|
| } |
| 486 |
|
| } |
| 487 |
0 |
| suffix_tree_destroy(&t); |
| 488 |
0 |
| free(buff); |
| 489 |
0 |
| } |
| 490 |
|
| |
| 491 |
|
| /* 1. count the number of lines ("\n"). |
| 492 |
|
| * 2. record the begin and end position of each line, ignoring |
| 493 |
|
| * leading and trailing white spaces, including "\n" |
| 494 |
|
| * 3. |
| 495 |
|
| */ |
| 496 |
|
| #define BEGIN_LINE 0 |
| 497 |
|
| |
| 498 |
0 |
| static void normalize(char **ostr, int **line_attrs, char *istr, int len) |
| 499 |
|
| { |
| 500 |
0 |
| int i, num_lines = 0; |
| 501 |
0 |
| int state = BEGIN_LINE; |
| 502 |
0 |
| int trailing_ws = 0; |
| 503 |
|
| char *tmp_str; |
| 504 |
|
| |
| 505 |
0 |
| tmp_str = *ostr = (char *)malloc(sizeof(char) * (len+1)); |
| 506 |
0 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
0 |
0 |
0 |
|
| 507 |
0 |
▶ | if ('\n' == istr[i]) num_lines++; |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == istr[i]) num_lines++; |
0 |
0 |
0 |
|
| 508 |
|
| } |
| 509 |
0 |
| num_lines++; |
| 510 |
0 |
| *line_attrs = (int*)malloc(sizeof(int) * (num_lines + 1)); |
| 511 |
0 |
| **line_attrs = num_lines; |
| 512 |
0 |
| num_lines = 1; |
| 513 |
0 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
0 |
0 |
0 |
|
| 514 |
0 |
▶ | if ('\n' == istr[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == istr[i]) { |
0 |
0 |
0 |
|
| 515 |
0 |
| tmp_str -= trailing_ws; |
| 516 |
0 |
| (*line_attrs)[num_lines] = tmp_str - *ostr; |
| 517 |
0 |
| num_lines++; |
| 518 |
0 |
| trailing_ws = 0; |
| 519 |
0 |
| state = BEGIN_LINE; |
| 520 |
|
| } else { |
| 521 |
0 |
▶ | if (isspace(istr[i])) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (isspace(istr[i])) { |
0 |
0 |
0 |
|
| 522 |
0 |
▶ | if (BEGIN_LINE == state) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (BEGIN_LINE == state) continue; |
0 |
0 |
0 |
|
| 523 |
|
| else { |
| 524 |
0 |
| trailing_ws++; |
| 525 |
|
| } |
| 526 |
0 |
▶ | } else if (BEGIN_LINE == state) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (BEGIN_LINE == state) { |
0 |
0 |
0 |
|
| 527 |
0 |
| trailing_ws = 0; |
| 528 |
0 |
| state = !BEGIN_LINE; |
| 529 |
|
| } else { |
| 530 |
0 |
| trailing_ws = 0; |
| 531 |
|
| } |
| 532 |
0 |
| *tmp_str = istr[i]; |
| 533 |
0 |
| tmp_str++; |
| 534 |
|
| } |
| 535 |
|
| } |
| 536 |
|
| |
| 537 |
0 |
| tmp_str -= trailing_ws; |
| 538 |
0 |
| (*line_attrs)[num_lines] = tmp_str - *ostr; |
| 539 |
0 |
| *tmp_str = 0; /* terminate the string */ |
| 540 |
0 |
| } |
| 541 |
|
| |
| 542 |
|
| typedef struct equal_segment equal_segment; |
| 543 |
|
| |
| 544 |
|
| struct equal_segment |
| 545 |
|
| { |
| 546 |
|
| int m_begin1; |
| 547 |
|
| int m_end1; |
| 548 |
|
| int m_begin2; |
| 549 |
|
| int m_end2; |
| 550 |
|
| int m_begin_line_num1; |
| 551 |
|
| int m_end_line_num1; |
| 552 |
|
| int m_begin_line_num2; |
| 553 |
|
| int m_end_line_num2; |
| 554 |
|
| equal_segment *m_next; |
| 555 |
|
| }; |
| 556 |
|
| |
| 557 |
0 |
| static void equal_segment_ctor(equal_segment *seg, |
| 558 |
|
| int b1, int e1, int b2, int e2, equal_segment *next) |
| 559 |
|
| { |
| 560 |
0 |
| seg->m_begin1 = b1; |
| 561 |
0 |
| seg->m_end1 = e1; |
| 562 |
0 |
| seg->m_begin2 = b2; |
| 563 |
0 |
| seg->m_end2 = e2; |
| 564 |
0 |
| seg->m_next = next; |
| 565 |
0 |
| seg->m_begin_line_num1 = -1; |
| 566 |
0 |
| seg->m_end_line_num1 = -1; |
| 567 |
0 |
| seg->m_begin_line_num2 = -1; |
| 568 |
0 |
| seg->m_end_line_num2 = -1; |
| 569 |
0 |
| } |
| 570 |
|
| |
| 571 |
0 |
| static void diff(equal_segment **segs, char const *orig_s1, |
| 572 |
|
| char const *s1, int len1, |
| 573 |
|
| char const *orig_s2, char const *s2, int len2) |
| 574 |
|
| { |
| 575 |
0 |
| int pos1, pos2, len = 0; |
| 576 |
|
| equal_segment *tmp_seg; |
| 577 |
|
| |
| 578 |
0 |
| lcs(&pos1, &pos2, &len, s1, len1, s2, len2); |
| 579 |
|
| |
| 580 |
0 |
▶ | if (len <= 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len <= 3) { |
0 |
0 |
0 |
|
| 581 |
0 |
| return; |
| 582 |
|
| } |
| 583 |
|
| |
| 584 |
|
| /* traversal order: right, left, root */ |
| 585 |
0 |
▶ | if (len1 - pos1 - len >= 4 && len2 - pos2 - len >= 4) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len1 - pos1 - len >= 4 && len2 - pos2 - len >= 4) { |
0 |
0 |
0 |
| if (len1 - pos1 - len >= 4 && len2 - pos2 - len >= 4) { |
0 |
0 |
0 |
|
| 586 |
0 |
| diff(segs, orig_s1, s1 + pos1 + len, len1 - pos1 - len, |
| 587 |
0 |
| orig_s2, s2 + pos2 + len, len2 - pos2 - len); |
| 588 |
|
| } |
| 589 |
0 |
| tmp_seg = (equal_segment*)malloc(sizeof(equal_segment)); |
| 590 |
0 |
| equal_segment_ctor(tmp_seg, s1 - orig_s1 + pos1, |
| 591 |
0 |
| s1 - orig_s1 + pos1 + len, |
| 592 |
0 |
| s2 - orig_s2 + pos2, |
| 593 |
0 |
| s2 - orig_s2 + pos2 + len, |
| 594 |
|
| *segs); |
| 595 |
0 |
| *segs = tmp_seg; |
| 596 |
0 |
▶ | if (pos1 >= 4 && pos2 >= 4) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pos1 >= 4 && pos2 >= 4) { |
0 |
0 |
0 |
| if (pos1 >= 4 && pos2 >= 4) { |
0 |
0 |
0 |
|
| 597 |
0 |
| diff(segs, orig_s1, s1, pos1, orig_s2, s2, pos2); |
| 598 |
|
| } |
| 599 |
|
| } |
| 600 |
|
| |
| 601 |
0 |
| static void adjust(equal_segment *equal_segs, const char *s1, const char *s2) |
| 602 |
|
| { |
| 603 |
0 |
| int i, num_chars = 0; |
| 604 |
0 |
| int state = BEGIN_LINE; |
| 605 |
0 |
| int trailing_ws = 0; |
| 606 |
|
| int tmp_begin; |
| 607 |
0 |
| int line_num = 0, tmp_line_num; |
| 608 |
0 |
| equal_segment *segs = equal_segs; |
| 609 |
|
| |
| 610 |
0 |
▶ | for (i = 0; 0 == i || s1[i-1]; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; 0 == i || s1[i-1]; i++) { |
0 |
0 |
0 |
| for (i = 0; 0 == i || s1[i-1]; i++) { |
0 |
0 |
0 |
|
| 611 |
0 |
▶ | if (!segs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!segs) { |
0 |
0 |
0 |
|
| 612 |
0 |
| break; |
| 613 |
|
| } |
| 614 |
0 |
▶ | if (segs->m_begin1 == num_chars) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (segs->m_begin1 == num_chars) { |
0 |
0 |
0 |
|
| 615 |
0 |
| tmp_begin = i; |
| 616 |
0 |
| tmp_line_num = line_num; |
| 617 |
0 |
▶ | while (tmp_begin > 0 && isspace(s1[tmp_begin-1])) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (tmp_begin > 0 && isspace(s1[tmp_begin-1])) { |
0 |
0 |
0 |
| while (tmp_begin > 0 && isspace(s1[tmp_begin-1])) { |
0 |
0 |
0 |
|
| 618 |
0 |
▶ | if ('\n' == s1[tmp_begin-1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == s1[tmp_begin-1]) { |
0 |
0 |
0 |
|
| 619 |
0 |
| tmp_line_num--; |
| 620 |
|
| } |
| 621 |
0 |
| tmp_begin--; |
| 622 |
|
| } |
| 623 |
|
| } |
| 624 |
0 |
▶ | if (segs->m_end1 <= num_chars && '\n' != s1[i]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (segs->m_end1 <= num_chars && '\n' != s1[i]) { |
0 |
0 |
0 |
| if (segs->m_end1 <= num_chars && '\n' != s1[i]) { |
0 |
0 |
0 |
|
| 625 |
0 |
| segs->m_begin1 = tmp_begin; |
| 626 |
0 |
| segs->m_begin_line_num1 = tmp_line_num; |
| 627 |
0 |
| segs->m_end1 = i; |
| 628 |
0 |
| segs->m_end_line_num1 = line_num; |
| 629 |
0 |
▶ | while (s1[segs->m_end1] && isspace(s1[segs->m_end1])) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (s1[segs->m_end1] && isspace(s1[segs->m_end1])) { |
0 |
0 |
0 |
| while (s1[segs->m_end1] && isspace(s1[segs->m_end1])) { |
0 |
0 |
0 |
|
| 630 |
0 |
▶ | if ('\n' == s1[segs->m_end1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == s1[segs->m_end1]) { |
0 |
0 |
0 |
|
| 631 |
0 |
| segs->m_end_line_num1++; |
| 632 |
|
| } |
| 633 |
0 |
| segs->m_end1++; |
| 634 |
|
| } |
| 635 |
0 |
| segs = segs->m_next; |
| 636 |
0 |
▶ | if (!segs) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!segs) break; |
0 |
0 |
0 |
|
| 637 |
0 |
▶ | if (segs->m_begin1 == num_chars) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (segs->m_begin1 == num_chars) { |
0 |
0 |
0 |
|
| 638 |
0 |
| tmp_begin = i; |
| 639 |
0 |
| tmp_line_num = line_num; |
| 640 |
0 |
▶ | while (tmp_begin > 0 && isspace(s1[tmp_begin-1])) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (tmp_begin > 0 && isspace(s1[tmp_begin-1])) { |
0 |
0 |
0 |
| while (tmp_begin > 0 && isspace(s1[tmp_begin-1])) { |
0 |
0 |
0 |
|
| 641 |
0 |
▶ | if ('\n' == s1[tmp_begin-1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == s1[tmp_begin-1]) { |
0 |
0 |
0 |
|
| 642 |
0 |
| tmp_line_num--; |
| 643 |
|
| } |
| 644 |
0 |
| tmp_begin--; |
| 645 |
|
| } |
| 646 |
|
| } |
| 647 |
|
| } |
| 648 |
0 |
▶ | if ('\n' == s1[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == s1[i]) { |
0 |
0 |
0 |
|
| 649 |
0 |
| line_num++; |
| 650 |
0 |
| num_chars -= trailing_ws; |
| 651 |
0 |
| trailing_ws = 0; |
| 652 |
0 |
| state = BEGIN_LINE; |
| 653 |
|
| } else { |
| 654 |
0 |
▶ | if (isspace(s1[i])) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (isspace(s1[i])) { |
0 |
0 |
0 |
|
| 655 |
0 |
▶ | if (BEGIN_LINE == state) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (BEGIN_LINE == state) continue; |
0 |
0 |
0 |
|
| 656 |
|
| else { |
| 657 |
0 |
▶ | while (isspace(s1[i]) && '\n' != s1[i]) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (isspace(s1[i]) && '\n' != s1[i]) { |
0 |
0 |
0 |
| while (isspace(s1[i]) && '\n' != s1[i]) { |
0 |
0 |
0 |
|
| 658 |
0 |
| trailing_ws++; |
| 659 |
0 |
| num_chars++; |
| 660 |
0 |
| i++; |
| 661 |
|
| } |
| 662 |
0 |
▶ | if (!isspace(s1[i]) || '\n' == s1[i]) i--; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!isspace(s1[i]) || '\n' == s1[i]) i--; |
0 |
0 |
0 |
| if (!isspace(s1[i]) || '\n' == s1[i]) i--; |
0 |
0 |
0 |
|
| 663 |
0 |
| continue; |
| 664 |
|
| } |
| 665 |
0 |
▶ | } else if (BEGIN_LINE == state) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (BEGIN_LINE == state) { |
0 |
0 |
0 |
|
| 666 |
0 |
| trailing_ws = 0; |
| 667 |
0 |
| state = !BEGIN_LINE; |
| 668 |
|
| } else { |
| 669 |
0 |
| trailing_ws = 0; |
| 670 |
|
| } |
| 671 |
0 |
| num_chars++; |
| 672 |
|
| } |
| 673 |
|
| } |
| 674 |
|
| |
| 675 |
0 |
| trailing_ws = 0; |
| 676 |
0 |
| line_num = 0; |
| 677 |
0 |
| num_chars = 0; |
| 678 |
0 |
| segs = equal_segs; |
| 679 |
0 |
| state = BEGIN_LINE; |
| 680 |
0 |
▶ | for (i = 0; 0==i || s2[i-1]; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; 0==i || s2[i-1]; i++) { |
0 |
0 |
0 |
| for (i = 0; 0==i || s2[i-1]; i++) { |
0 |
0 |
0 |
|
| 681 |
0 |
▶ | if (!segs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!segs) { |
0 |
0 |
0 |
|
| 682 |
0 |
| break; |
| 683 |
|
| } |
| 684 |
0 |
▶ | if (segs->m_begin2 == num_chars) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (segs->m_begin2 == num_chars) { |
0 |
0 |
0 |
|
| 685 |
0 |
| tmp_begin = i; |
| 686 |
0 |
| tmp_line_num = line_num; |
| 687 |
0 |
▶ | while (tmp_begin > 0 && isspace(s2[tmp_begin-1])) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (tmp_begin > 0 && isspace(s2[tmp_begin-1])) { |
0 |
0 |
0 |
| while (tmp_begin > 0 && isspace(s2[tmp_begin-1])) { |
0 |
0 |
0 |
|
| 688 |
0 |
▶ | if ('\n' == s2[tmp_begin-1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == s2[tmp_begin-1]) { |
0 |
0 |
0 |
|
| 689 |
0 |
| tmp_line_num--; |
| 690 |
|
| } |
| 691 |
0 |
| tmp_begin--; |
| 692 |
|
| } |
| 693 |
|
| } |
| 694 |
0 |
▶ | if (segs->m_end2 <= num_chars && '\n' != s2[i]) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (segs->m_end2 <= num_chars && '\n' != s2[i]) { |
0 |
0 |
0 |
| if (segs->m_end2 <= num_chars && '\n' != s2[i]) { |
0 |
0 |
0 |
|
| 695 |
0 |
| segs->m_begin2 = tmp_begin; |
| 696 |
0 |
| segs->m_begin_line_num2 = tmp_line_num; |
| 697 |
0 |
| segs->m_end2 = i; |
| 698 |
0 |
| segs->m_end_line_num2 = line_num; |
| 699 |
0 |
▶ | while (s2[segs->m_end2] && isspace(s2[segs->m_end2])) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (s2[segs->m_end2] && isspace(s2[segs->m_end2])) { |
0 |
0 |
0 |
| while (s2[segs->m_end2] && isspace(s2[segs->m_end2])) { |
0 |
0 |
0 |
|
| 700 |
0 |
▶ | if ('\n' == s2[segs->m_end2]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == s2[segs->m_end2]) { |
0 |
0 |
0 |
|
| 701 |
0 |
| segs->m_end_line_num2++; |
| 702 |
|
| } |
| 703 |
0 |
| segs->m_end2++; |
| 704 |
|
| } |
| 705 |
0 |
| segs = segs->m_next; |
| 706 |
0 |
▶ | if (!segs) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!segs) break; |
0 |
0 |
0 |
|
| 707 |
0 |
▶ | if (segs->m_begin2 == num_chars) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (segs->m_begin2 == num_chars) { |
0 |
0 |
0 |
|
| 708 |
0 |
| tmp_begin = i; |
| 709 |
0 |
| tmp_line_num = line_num; |
| 710 |
0 |
▶ | while (tmp_begin > 0 && isspace(s2[tmp_begin-1])) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (tmp_begin > 0 && isspace(s2[tmp_begin-1])) { |
0 |
0 |
0 |
| while (tmp_begin > 0 && isspace(s2[tmp_begin-1])) { |
0 |
0 |
0 |
|
| 711 |
0 |
▶ | if ('\n' == s2[tmp_begin-1]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == s2[tmp_begin-1]) { |
0 |
0 |
0 |
|
| 712 |
0 |
| tmp_line_num--; |
| 713 |
|
| } |
| 714 |
0 |
| tmp_begin--; |
| 715 |
|
| } |
| 716 |
|
| } |
| 717 |
|
| } |
| 718 |
0 |
▶ | if ('\n' == s2[i]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ('\n' == s2[i]) { |
0 |
0 |
0 |
|
| 719 |
0 |
| line_num++; |
| 720 |
0 |
| num_chars -= trailing_ws; |
| 721 |
|
| /* printf("white %d\n", trailing_ws); */ |
| 722 |
0 |
| trailing_ws = 0; |
| 723 |
0 |
| state = BEGIN_LINE; |
| 724 |
|
| } else { |
| 725 |
0 |
▶ | if (isspace(s2[i])) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (isspace(s2[i])) { |
0 |
0 |
0 |
|
| 726 |
0 |
▶ | if (BEGIN_LINE == state) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (BEGIN_LINE == state) continue; |
0 |
0 |
0 |
|
| 727 |
|
| else { |
| 728 |
0 |
▶ | while (isspace(s2[i]) && '\n' != s2[i]) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (isspace(s2[i]) && '\n' != s2[i]) { |
0 |
0 |
0 |
| while (isspace(s2[i]) && '\n' != s2[i]) { |
0 |
0 |
0 |
|
| 729 |
0 |
| trailing_ws++; |
| 730 |
0 |
| num_chars++; |
| 731 |
0 |
| i++; |
| 732 |
|
| } |
| 733 |
0 |
▶ | if (!isspace(s2[i]) || '\n' == s2[i]) i--; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!isspace(s2[i]) || '\n' == s2[i]) i--; |
0 |
0 |
0 |
| if (!isspace(s2[i]) || '\n' == s2[i]) i--; |
0 |
0 |
0 |
|
| 734 |
0 |
| continue; |
| 735 |
|
| } |
| 736 |
0 |
▶ | } else if (BEGIN_LINE == state) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (BEGIN_LINE == state) { |
0 |
0 |
0 |
|
| 737 |
0 |
| trailing_ws = 0; |
| 738 |
0 |
| state = !BEGIN_LINE; |
| 739 |
|
| } else { |
| 740 |
0 |
| trailing_ws = 0; |
| 741 |
|
| } |
| 742 |
|
| /* printf("\"%c\"\n", s2[i]); */ |
| 743 |
0 |
| num_chars++; |
| 744 |
|
| } |
| 745 |
|
| } |
| 746 |
0 |
| } |
| 747 |
|
| |
| 748 |
0 |
| char *ediff(char *s1, char *s2) |
| 749 |
|
| { |
| 750 |
0 |
| int *line_attrs1, *line_attrs2, ix = 0; |
| 751 |
|
| char *ostr1, *ostr2, *ret; |
| 752 |
|
| /* int pos1, pos2, len = 0; */ |
| 753 |
0 |
| equal_segment *equals = NULL, *tmp_seg; |
| 754 |
|
| |
| 755 |
0 |
| normalize(&ostr1, &line_attrs1, s1, strlen(s1)); |
| 756 |
0 |
| normalize(&ostr2, &line_attrs2, s2, strlen(s2)); |
| 757 |
|
| |
| 758 |
0 |
| diff(&equals, ostr1, ostr1, strlen(ostr1), ostr2, ostr2, strlen(ostr2)); |
| 759 |
|
| /* lcs(&pos1, &pos2, &len, ostr1, strlen(ostr1), ostr2, strlen(ostr2)); |
| 760 |
|
| */ |
| 761 |
0 |
| adjust(equals, s1, s2); |
| 762 |
0 |
| tmp_seg = equals; |
| 763 |
0 |
▶ | while (tmp_seg) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (tmp_seg) { |
0 |
0 |
0 |
|
| 764 |
0 |
| ix++; |
| 765 |
0 |
| tmp_seg = tmp_seg->m_next; |
| 766 |
|
| } |
| 767 |
|
| #define INT_LEN 11 |
| 768 |
0 |
| ret = (char*)malloc(sizeof(char) * INT_LEN * ix * 8 + 1); |
| 769 |
0 |
| ret[0] = 0; |
| 770 |
0 |
| tmp_seg = equals; |
| 771 |
0 |
▶ | while (tmp_seg) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (tmp_seg) { |
0 |
0 |
0 |
|
| 772 |
|
| char buff[4 * INT_LEN + 1]; |
| 773 |
0 |
▶ | if (tmp_seg->m_begin_line_num1 < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp_seg->m_begin_line_num1 < 0) { |
0 |
0 |
0 |
|
| 774 |
0 |
| tmp_seg->m_begin_line_num1 = line_attrs1[0]; |
| 775 |
|
| } |
| 776 |
0 |
▶ | if (tmp_seg->m_end_line_num1 < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp_seg->m_end_line_num1 < 0) { |
0 |
0 |
0 |
|
| 777 |
0 |
| tmp_seg->m_end_line_num1 = line_attrs1[0]; |
| 778 |
|
| } |
| 779 |
0 |
▶ | if (tmp_seg->m_begin_line_num2 < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp_seg->m_begin_line_num2 < 0) { |
0 |
0 |
0 |
|
| 780 |
0 |
| tmp_seg->m_begin_line_num2 = line_attrs2[0]; |
| 781 |
|
| } |
| 782 |
0 |
▶ | if (tmp_seg->m_end_line_num2 < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp_seg->m_end_line_num2 < 0) { |
0 |
0 |
0 |
|
| 783 |
0 |
| tmp_seg->m_end_line_num2 = line_attrs2[0]; |
| 784 |
|
| } |
| 785 |
0 |
| sprintf(buff, "%d %d %d %d %d %d %d %d ", |
| 786 |
|
| tmp_seg->m_begin1, tmp_seg->m_end1, |
| 787 |
|
| tmp_seg->m_begin_line_num1, tmp_seg->m_end_line_num1, |
| 788 |
|
| tmp_seg->m_begin2, tmp_seg->m_end2, |
| 789 |
|
| tmp_seg->m_begin_line_num2, tmp_seg->m_end_line_num2); |
| 790 |
0 |
| strcat(ret, buff); |
| 791 |
0 |
| tmp_seg = tmp_seg->m_next; |
| 792 |
|
| } |
| 793 |
|
| |
| 794 |
0 |
| tmp_seg = equals; |
| 795 |
0 |
▶ | while (tmp_seg) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (tmp_seg) { |
0 |
0 |
0 |
|
| 796 |
0 |
| equal_segment *next = tmp_seg->m_next; |
| 797 |
0 |
| free(tmp_seg); |
| 798 |
0 |
| tmp_seg = next; |
| 799 |
|
| } |
| 800 |
0 |
| free(ostr1); free(ostr2); free(line_attrs1); free(line_attrs2); |
| 801 |
0 |
| return ret; |
| 802 |
|
| } |
| 803 |
|
| |
| 804 |
0 |
| static void print_str(char *s, int len) |
| 805 |
|
| { |
| 806 |
|
| int i; |
| 807 |
0 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
0 |
0 |
0 |
|
| 808 |
0 |
| printf("%c", s[i]); |
| 809 |
|
| } |
| 810 |
0 |
| } |