-
Notifications
You must be signed in to change notification settings - Fork 0
/
eetables.js
270 lines (246 loc) · 12.1 KB
/
eetables.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// eelnss.js
// (c) 20014-2019 Pawel Cesar Sanjuan Szklarz
// eelnss may be freely distributed under the MIT license.
(function (root) {
// Baseline setup, inspired in underscore.js
// --------------
// Establish the root object, `window` in the browser, or `exports` on the server.
var underscoreRef = root._ || require('underscore');
// Util functions
// ---------------
// Assertion util function.
function _assert(condition, message) {
if (!condition) {
throw message || "Assertion failed";
}
}
var tableIteratorModule = (function (_) {
// Table Iterator
// ================
// Provide a iteration api for tables with repeated values.
function _tableIterator(pullFunctionBuilder, contextValueSizes) {
_assert(_.isFunction(pullFunctionBuilder), "pullFunctionBuilder is not a function, bad use od table iterator api");
var iteratorFactory = function () {
var iterator = {};
iterator.spec = {
contextSize: contextValueSizes.length,
contextValueSizes: contextValueSizes,
extractionPoints: _findContextValuesExtractionPoints(contextValueSizes)
};
iterator.nestedNext = pullFunctionBuilder();
_assert(_.isFunction(iterator.nestedNext), "nestedNext is not a function, bad use od table iterator api");
iterator.loopOver = function (iterationHandler) {
var next = iterator.nestedNext();
while (next !== void(0)) {
var changedContextLevel = next[0];
var contextKeys = next[1];
var contextValues = next[2];
iterationHandler(changedContextLevel, contextKeys, contextValues);
next = iterator.nestedNext();
}
};
return iterator;
};
iteratorFactory.spec = {
contextSize: contextValueSizes.length,
contextValueSizes: contextValueSizes,
extractionPoints: _findContextValuesExtractionPoints(contextValueSizes)
};
return iteratorFactory;
}
// util function
// show the position on the table where context values start
// For:
// [c1,c2,c3,x1,x2,x3,y1,y2,z1,z2]
// | | | |
// result is [3,6,8,10]
function _findContextValuesExtractionPoints(contextValueSizes) {
var contextSize = contextValueSizes.length;
return _.reduce(contextValueSizes, function (curr, nextContextValueSize) {
var lastMark = _.last(curr);
curr.push(lastMark + nextContextValueSize);
return curr;
}, [contextSize]);
}
// find where the context key has changed in relation to the previousContextKeys
function _findChangeLevel(previousContextKeys, fullContextKeys) {
var fullContextKeysLength = fullContextKeys.length;
var changeLevel = fullContextKeysLength;
for (var i = 0; i < fullContextKeysLength; i++) {
if (previousContextKeys[i] !== fullContextKeys[i]) {
changeLevel = i;
break;
}
}
return changeLevel;
}
function _abstractSourceIterator(contextValueSizes, rowExtractorLinearIterator) {
var contextSize = contextValueSizes.length;
var whereContextLevelValuesStart = _findContextValuesExtractionPoints(contextValueSizes);
function pullFromSourceBuilder() {
var rowExtractor = rowExtractorLinearIterator();
var previousContextKeys = void(0);
return function () {
var row = rowExtractor.next();
if (row === void(0)) {
return void(0);
}
var fullContextKeys = row.slice(0, contextSize);
var pullResult;
if (previousContextKeys === void(0)) {
var fullValues = row.slice(contextSize);
pullResult = [0, fullContextKeys, fullValues];
} else {
var changeLevel = _findChangeLevel(previousContextKeys, fullContextKeys);
var changedContextKeys = fullContextKeys.slice(changeLevel);
var changedContextValues = row.slice(whereContextLevelValuesStart[changeLevel]);
pullResult = [changeLevel, changedContextKeys, changedContextValues];
}
previousContextKeys = fullContextKeys;
return pullResult;
};
}
return _tableIterator(pullFromSourceBuilder, contextValueSizes);
}
function _tableIteratorFromTable(contextValueSizes, table) {
var nrOfRows = table.length;
var tableRowExtractorBuilder = function () {
var indexCounter = 0;
return {
next: function () {
if (nrOfRows === indexCounter) {
return void(0);
}
return table[indexCounter++];
}
};
};
return _abstractSourceIterator(contextValueSizes, tableRowExtractorBuilder);
}
function _buildTable(iterator) {
var table = [];
var currentContextKeys;
var currentContextValues;
// Parameters are:
// contextLevel: start level where context keys have changed
// contextKeys: new values of context keys [C_m, C_m+1,...,C_n], where m = contextLevel
// contextValues: new values on contexts [v_m_1,v_m_2,..,v_(m+1)_1,....], where m = contextLevel
var valueExtractionPointsWithoutContext = _.map(iterator.spec.extractionPoints, function (point) {
return point - iterator.spec.contextSize;
});
var tableBuilder = function (contextLevel, contextKeys, contextValues) {
if (contextLevel === 0) {
currentContextKeys = contextKeys;
currentContextValues = contextValues;
} else {
var valueExtractionPoint = valueExtractionPointsWithoutContext[contextLevel];
currentContextKeys = _.first(currentContextKeys, contextLevel).concat(contextKeys);
currentContextValues = _.first(currentContextValues, valueExtractionPoint).concat(contextValues);
}
table.push(currentContextKeys.concat(currentContextValues));
};
iterator.loopOver(tableBuilder);
return table;
}
function _crossProductTableIterators() {
_assert(arguments.length > 1, "Pass at least 2 table iterators to build a cross product");
var args = Array.prototype.slice.call(arguments);
var head = args[0];
var tail = _.tail(args);
return _.reduce(tail, _crossProductTableIteratorsReduction, head);
function _crossProductTableIteratorsReduction(aIterator, bIterator) {
var aContextValuesSizes = aIterator.spec.contextValueSizes;
var bContextValuesSizes = bIterator.spec.contextValueSizes;
var crossIteratorExtractorBuilder = function () {
var AIterationInstance = aIterator();
var BIterationInstance = bIterator();
var AcontextValue = void(0);
var BcontextValue = void(0);
var iterationFinished = false;
var iterationCurrentStep = startIterations;
function noOpIteration() {
}
function startIterations() {
AcontextValue = AIterationInstance.nestedNext();
BcontextValue = BIterationInstance.nestedNext();
if (AcontextValue === void(0) || BcontextValue === void(0)) {
// there is a empty iterator in the cross product, so it is empty
iterationFinished = true;
iterationCurrentStep = noOpIteration;
return;
}
iterationCurrentStep = iterateOverB;
}
function iterateOverA() {
AcontextValue = AIterationInstance.nestedNext();
if (AcontextValue === void(0)) {
// A iterator finished
iterationFinished = true;
iterationCurrentStep = noOpIteration;
return;
}
BIterationInstance = bIterator();
BcontextValue = BIterationInstance.nestedNext();
}
function iterateOverB() {
BcontextValue = BIterationInstance.nestedNext();
if (BcontextValue === void(0)) {
// iteration over B finished, jump to A iteration
iterateOverA();
}
}
return {
next: function () {
iterationCurrentStep();
if (iterationFinished) {
return void(0);
}
return AcontextValue[1].concat(BcontextValue[1], AcontextValue[2], BcontextValue[2]);
}
};
};
return _abstractSourceIterator(aContextValuesSizes.concat(bContextValuesSizes), crossIteratorExtractorBuilder);
}
}
// because context is explicit and ordered, it is necessary to change the order of the columns
function _crossProductTables(tables, contextValuesSizes) {
_assert(tables.length > 1, "Pass at least 2 table iterators to build a cross product");
_assert(contextValuesSizes.length === tables.length, "Provide context sizes for each table");
var tableAndSize = _.zip(tables, contextValuesSizes);
var head = tableAndSize[0];
var tail = _.tail(tableAndSize);
return _.reduce(tail, _crossProductTablesReduction, head);
function _crossProductTablesReduction(a, b) {
var aTable = a[0];
var aContextValueSizes = a[1];
var aContextSize = aContextValueSizes.length;
var bTable = b[0];
var bContextValueSizes = b[1];
var bContextSize = bContextValueSizes.length;
return _.chain(aTable).map(function (aRow) {
return _.chain(bTable).map(function (bRow) {
var contextKeys = _.first(aRow, aContextSize).concat(_.first(bRow, bContextSize));
var values = aRow.slice(aContextSize).concat(bRow.slice(bContextSize));
return contextKeys.concat(values);
}).value();
}).flatten(true).value();
}
}
return {
tableIterator: _tableIterator,
tableIteratorFromTable: _tableIteratorFromTable,
tableIteratorBuilder: _abstractSourceIterator,
buildTableFromIterator: _buildTable,
crossProductTableIterators: _crossProductTableIterators,
crossProductTables: _crossProductTables
};
})(underscoreRef);
root.eetables = {
tableIterators: tableIteratorModule
};
if (typeof define === 'function' && define.amd) {
define('eetables', ['underscore'], function (_) {
return eetables;
});
}
})(this);