-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusertest2_alg_listalg.js
217 lines (190 loc) · 7.41 KB
/
usertest2_alg_listalg.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var dumbalg = (function () {
"use strict";
var da = {
algname: 'DumbAlg',
um: [],
db: [],
umneedclustering: true,
hasdregs: -1
};
da.onDataSetChange = function () {
da.um = [];
da.umneedclustering = true;
da.hasdregs = -1;
};
da.onDataSetDocChange = function (dbrawitem) {
if (da.umneedclustering === true || da.um.length === 0) {
da.db = doClustering(dbrawitem);
da.umneedclustering = false;
da.hasdregs = -1;
} else {
da.db = da.doGroupingAndOrdering(dbrawitem);
}
};
da.umRecord = function (hasmovedx) {
// Adjust the grouping and ordering of predicates.
var gi, g, gt = [], ti, t, u = [], o = da.um, c = [[]], ocgi = -1, octi = -1, oterm, ucgi = -1, ucti = -1, uterm;
//if (hasmovedx) {
if(true) {
// Record the grouping and ordering from the UI
for (gi = 0; gi < da.db.length; gi++) {
g = da.db[gi];
gt = [];
for (ti = 0; ti < g.length; ti++) {
t = g[ti];
gt.push(t[0]);
}
u.push(gt);
}
// Merge the new data with the old usermodel
while (ucgi < u.length || ocgi < o.length) {
// Ready to start a new group in the output
if (ucti === -1 && octi === -1) {
// Only make a new group if the previous one is not empty (can happen where items in o are moved later in u)
if (c[c.length - 1].length > 0) {
c.push([]);
}
ucgi++;
uterm = undefined;
if (ucgi >= u.length) {
ucgi = u.length;
ucti = -1;
} else {
ucti = 0;
}
ocgi++;
oterm = undefined;
if (ocgi >= o.length) {
ocgi = o.length;
octi = -1;
} else {
octi = 0;
}
continue;
}
if (ucgi > -1 && ucti > -1) {
uterm = u[ucgi][ucti];
}
if (ocgi > -1 && octi > -1) {
oterm = o[ocgi][octi];
}
if (uterm === oterm) {
da.addTerm(c, uterm);
ucti++;
if (ucti >= u[ucgi].length) {
ucti = -1;
uterm = undefined;
}
octi++;
if (octi >= o[ocgi].length) {
octi = -1;
oterm = undefined;
}
} else if (octi > -1 && da.whichGroupForTerm(u, oterm) === -1) {
// If the old model o has something the new model u doesn't at all then output that now
da.addTerm(c, oterm);
octi++;
if (octi >= o[ocgi].length) {
octi = -1;
oterm = undefined;
}
} else if (ucti > -1) {
// Give preference to items from the new model u
da.addTerm(c, uterm);
ucti++;
if (ucti >= u[ucgi].length) {
ucti = -1;
uterm = undefined;
}
} else {
// Only add terms from the old model o where they not someplace in the new model u, otherwise just skip over
if (da.whichGroupForTerm(u, oterm) === -1) {
da.addTerm(c, oterm);
}
octi++;
if (octi >= o[ocgi].length) {
octi = -1;
oterm = undefined;
}
}
}
// Cleanup the last group because it'll be empty
c.pop();
da.um = c;
da.umneedclustering = false;
} else if (da.um.length === 0) {
da.umneedclustering = true;
}
};
da.whichGroupForTerm = function (um, term) {
var gi;
if (um.length > 0) {
// Look for group that contains the term
for (gi = 0; gi < um.length; gi++) {
if (um[gi].indexOf(term) > -1) {
return gi;
}
}
}
// No idea which group the term goes into
return -1;
};
da.addTerm = function (um, term) {
var r = da.whichGroupForTerm(um, term);
if (r === -1) {
um[um.length - 1].push(term);
}
return r;
};
da.doGroupingAndOrdering = function (t) {
var r = [], ti, thisitem, foundspot, fgi, fti, r1 = [], rg, rgi, rti;
da.hasdregs = -1;
r[da.um.length + 1] = []; // The dregpool
for (ti = 0; ti < t.length; ti++) {
thisitem = t[ti];
foundspot = false;
for (fgi = 0; fgi < da.um.length && !foundspot; fgi++) {
for (fti = 0; fti < da.um[fgi].length && !foundspot; fti++) {
if (thisitem[0] === da.um[fgi][fti]) {
if (r[fgi] === undefined) {
r[fgi] = [];
}
while (r[fgi][fti] !== undefined) {
fti++;
}
r[fgi][fti] = thisitem;
foundspot = true;
break;
}
}
}
if (!foundspot) {
r[da.um.length + 1].push(thisitem);
da.hasdregs = 1;
}
}
// Clear out the blanks
for (rgi = 0; rgi < r.length; rgi++) {
if (r[rgi] !== undefined) {
rg = [];
for (rti = 0; rti < r[rgi].length; rti++) {
if (r[rgi][rti] !== undefined) {
rg.push(r[rgi][rti]);
}
}
r1.push(rg);
}
}
// Point to the dregpool if necessary
if (da.hasdregs > -1) {
da.hasdregs = r1.length - 1;
}
// Cleanup last group if it is empty
if (r1[r1.length - 1].length === 0) {
r1.pop();
}
return r1;
};
//Return the module
return da;
}());