-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathsort_traces.js
144 lines (117 loc) · 3.28 KB
/
sort_traces.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
'use strict';
function zipArrays(arrays) {
var zipped = [];
arrays[0].forEach(function(e, i) {
var row = [];
arrays.forEach(function(arr) {
row.push(arr[i]);
});
zipped.push(row);
});
return zipped;
}
function sortObjecstByKey(a, b, key) {
if(a[key] === b[key]) return 0;
if(a[key] < b[key]) return -1;
return 1;
}
function matrixToObjectList(matrix, cols) {
var zipped = zipArrays(matrix);
var objList = [];
zipped.forEach(function(row) {
var objRow = {};
cols.forEach(function(col, idx) {
objRow[col] = row[idx];
});
objRow.y = row[row.length - 1];
objList.push(objRow);
});
return objList;
}
exports.matrixToObjectList = matrixToObjectList;
function sortObjectList(cols, objList) {
var sortedObjectList = objList.map(function(e) {
return e;
});
cols.slice().reverse().forEach(function(key) {
sortedObjectList = sortedObjectList.sort(function(a, b) {
return sortObjecstByKey(a, b, key);
});
});
return sortedObjectList;
}
exports.sortObjectList = sortObjectList;
function objectListToList(objectList) {
var list = [];
objectList.forEach(function(item) {
list.push(Object.values(item));
});
return list;
}
exports.objectListToList = objectListToList;
function sortedMatrix(list, removeNull) {
var xs = [];
var y = [];
list.slice().forEach(function(item) {
var val = item.pop();
if(removeNull & item.includes(null)) {
return;
}
y.push(val);
xs.push(item);
});
return [xs, y];
}
exports.sortedMatrix = sortedMatrix;
function squareMatrix(matrix) {
var width = matrix[0].length;
var height = matrix.length;
if(width === height) {
return matrix;
}
var newMatrix = [];
if(width > height) {
for(var rw = 0; rw < height; rw++) {
newMatrix.push(matrix[rw].slice());
}
for(var i = height; i < width; i++) {
newMatrix.push(Array(width));
}
} else {
for(var row = 0; row < height; row++) {
var rowExpansion = Array(height - width);
var rowSlice = matrix[row].slice();
Array.prototype.push.apply(rowSlice, rowExpansion);
newMatrix.push(rowSlice);
}
}
return newMatrix;
}
exports.squareMatrix = squareMatrix;
function transpose(matrix) {
var height = matrix.length;
var width = matrix[0].length;
var squaredMatrix = squareMatrix(matrix);
var newMatrix = [];
// prevent inplace change and mantain the main diagonal
for(var rw = 0; rw < squaredMatrix.length; rw++) {
newMatrix.push(squaredMatrix[rw].slice());
}
for(var i = 0; i < newMatrix.length; i++) {
for(var j = 0; j < i; j++) {
newMatrix = newMatrix.slice();
var temp = newMatrix[i][j];
newMatrix[i][j] = newMatrix[j][i];
newMatrix[j][i] = temp;
}
}
if(width > height) {
for(var row = 0; row < newMatrix.length; row++) {
newMatrix[row] = newMatrix[row].slice(0, height);
}
} else {
newMatrix = newMatrix.slice(0, width);
}
return newMatrix;
}
exports.transpose = transpose;