-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgfoperation.cpp
162 lines (144 loc) · 3.87 KB
/
sgfoperation.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stack>
#include "sgftree.h"
int write_sgfbranch(char *filename, SGFNode *branch);
int check_two_sgfnodes(SGFNode *desnode, SGFNode *srcnode);
int merge_two_sgfnodes(SGFNode *desnode, SGFNode *srcnode);
void swap(int *x,int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void quicksort(int list[],int m,int n) {
int key,i,j,k;
if( m < n) {
k = (m + n) / 2;
swap(&list[m],&list[k]);
key = list[m];
i = m+1; j = n;
while(i <= j) {
while((i <= n) && (list[i] <= key)) i++;
while((j >= m) && (list[j] > key)) j--;
if( i < j) swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
quicksort(list,m,j-1);
quicksort(list,j+1,n);
}
}
int // whether they have same move or add-stone list
check_two_sgfnodes(SGFNode *desnode, SGFNode *srcnode) { // 0 not equal; 1 equal
SGFProperty *prop;
int desval[300], srcval[300];
int descount = 0, srccount = 0;
int tmpval;
prop = desnode->props;
while (prop) {
if (prop->name == SGFAB || prop->name == SGFAW ||
prop->name == SGFB || prop->name == SGFW) {
desval[descount] = prop->name << 16;
desval[descount] += prop->value[0] + prop->value[1] << 8;
descount++;
}
prop = prop->next;
}
prop = srcnode->props;
while (prop) {
if (prop->name == SGFAB || prop->name == SGFAW ||
prop->name == SGFB || prop->name == SGFW) {
srcval[srccount] = prop->name << 16;
srcval[srccount] += prop->value[0] + prop->value[1] << 8;
srccount++;
}
prop = prop->next;
}
if (descount == srccount) {
quicksort(desval, 0, descount - 1);
quicksort(srcval, 0, srccount - 1);
for (tmpval = 0; tmpval < descount; tmpval++) {
if (desval[tmpval] != srcval[tmpval])
return 0;
}
return 1;
}
else return 0;
}
int
merge_two_sgfnodes(SGFNode *desnode, SGFNode *srcnode) {
// no need check
SGFNode *deslist, *srclist, *parent;
parent = desnode->parent;
deslist = desnode;
srclist = srcnode;
}
int
merge_two_sgffiles(SGFTree *destree, SGFTree *srctree) {
SGFNode *desnode, *srcnode;
desnode = destree->root;
srcnode = srctree->root;
if (check_two_sgfnodes(desnode, srcnode)) {
if (desnode->child == NULL) {
desnode->child = srcnode->child;
}
else if (srcnode->child != NULL) {
merge_two_sgfnodes(desnode->child, srcnode->child);
}
}
else { // add additional root
SGFNode *newroot = (SGFNode *)malloc(sizeof(SGFNode));
newroot->child = desnode;
desnode->next = srcnode;
// ...
destree->root = newroot;
}
return 1;
}
int
write_sgfbranch(char *filename, SGFNode *branch) {
std::stack<SGFNode *> brsave;
SGFNode *current = branch;
while (current != NULL) { // trace branch
brsave.push(current);
current = current->parent;
}
FILE *wrl = fopen(filename, "w");
if (brsave.empty()) return 1; // empty tree
fprintf(wrl, "(");
unparse_root(wrl, brsave.top());
restore_property(brsave.top()->props);
brsave.pop();
while (!brsave.empty()) {
unparse_node(wrl, brsave.top());
restore_property(brsave.top()->props);
brsave.pop();
}
fprintf(wrl, "\n)");
fclose(wrl); //
}
int
split_sgffile(SGFTree *tree, const char *filename) {
char namebuff[256];
int namenum = 0;
SGFNode *current;
current = tree->root;
std::stack<SGFNode *> iterstk;
iterstk.push(tree->root);
while (!iterstk.empty()) {
current = iterstk.top();
iterstk.pop();
while (current != NULL) {
if (current->next != NULL)
iterstk.push(current->next);
if (current->child == NULL) {
sprintf(namebuff, "%s_%d.sgf", filename, namenum++);
//if (namenum < 800)
write_sgfbranch(namebuff, current);
}
current = current->child;
} // end current != NULL
} // end iteration stack
}