8000 fixed insertion sort for native arrays · plotly/ndarray-sort@b963223 · GitHub
[go: up one dir, main page]

Skip to content

Commit b963223

Browse files
committed
fixed insertion sort for native arrays
1 parent 9fdc0b3 commit b963223

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/compile_sort.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,13 @@ function createInsertionSort(order, axis, dtype) {
188188
code.push("} return " + funcName)
189189

190190
//Compile and link function
191-
var result = new Function("malloc", "free", code.join("\n"))
192-
return result(allocator[0], allocator[1])
191+
if(allocator) {
192+
var result = new Function("malloc", "free", code.join("\n"))
193+
return result(allocator[0], allocator[1])
194+
} else {
195+
var result = new Function(code.join("\n"))
196+
return result()
197+
}
193198
}
194199

195200

test/test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ require("tape")("ndarray-sort-1d", function(t) {
1616
var nd0 = ndarray(new Uint16Array(arr))
1717
var nd1 = ndarray(new Float32Array(10*arr.length)).step(10)
1818
var nd2 = ndarray(new Int32Array(arr)).step(-1)
19+
var nd3 = ndarray(new Array(arr.length))
1920
ops.assign(nd1, nd0)
21+
ops.assign(nd3, nd0)
2022

2123
ndsort(nd0)
2224
ndsort(nd1)
2325
ndsort(nd2)
26+
ndsort(nd3)
2427
arr.sort(function (a,b) { return a-b })
2528

2629
for(var i=0; i<n; ++i) {
2730
t.equals(nd0.get(i), arr[i], "flat")
2831
t.equals(nd1.get(i), arr[i], "skip")
2932
t.equals(nd2.get(i), arr[i], "reverse")
33+
t.equals(nd3.get(i), arr[i], "array")
3034
}
3135
}
3236

@@ -74,15 +78,18 @@ require("tape")("ndarray-sort-2d", function(t) {
7478
ops.assign(nd4, nd0)
7579

7680
var nd5 = ndarray(new Uint32Array(nr*nc), [nc,nr])
77-
7881
ops.assign(nd5, nd0.transpose(1,0))
7982

83+
var nd6 = ndarray(new Array(nr*nc), [nr,nc])
84+
ops.assign(nd6, nd0)
85+
8086
ndsort(nd0)
8187
ndsort(nd1)
8288
ndsort(nd2)
8389
ndsort(nd3)
8490
ndsort(nd4)
8591
ndsort(nd5,1)
92+
ndsort(nd6)
8693
arr.sort(compare1D)
8794

8895
for(var i=0; i<nr; ++i) {
@@ -93,6 +100,7 @@ require("tape")("ndarray-sort-2d", function(t) {
93100
t.equals(nd3.get(i,j), arr[i][j], "flipx")
94101
t.equals(nd4.get(i,j), arr[i][j], "flipy")
95102
t.equals(nd5.get(j,i), arr[i][j], "alt-axis")
103+
t.equals(nd6.get(i,j), arr[i][j], "array")
96104
}
97105
}
98106
}

0 commit comments

Comments
 (0)
0