-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
138 lines (116 loc) · 2.86 KB
/
index.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
'use strict'
module.exports = boxIntersectWrapper
var pool = require('typedarray-pool')
var sweep = require('./lib/sweep')
var boxIntersectIter = require('./lib/intersect')
function boxEmpty(d, box) {
for(var j=0; j<d; ++j) {
if(!(box[j] <= box[j+d])) {
return true
}
}
return false
}
//Unpack boxes into a flat typed array, remove empty boxes
function convertBoxes(boxes, d, data, ids) {
var ptr = 0
var count = 0
for(var i=0, n=boxes.length; i<n; ++i) {
var b = boxes[i]
if(boxEmpty(d, b)) {
continue
}
for(var j=0; j<2*d; ++j) {
data[ptr++] = b[j]
}
ids[count++] = i
}
return count
}
//Perform type conversions, check bounds
function boxIntersect(red, blue, visit, full) {
var n = red.length
var m = blue.length
//If either array is empty, then we can skip this whole thing
if(n <= 0 || m <= 0) {
return
}
//Compute dimension, if it is 0 then we skip
var d = (red[0].length)>>>1
if(d <= 0) {
return
}
var retval
//Convert red boxes
var redList = pool.mallocDouble(2*d*n)
var redIds = pool.mallocInt32(n)
n = convertBoxes(red, d, redList, redIds)
if(n > 0) {
if(d === 1 && full) {
//Special case: 1d complete
sweep.init(n)
retval = sweep.sweepComplete(
d, visit,
0, n, redList, redIds,
0, n, redList, redIds)
} else {
//Convert blue boxes
var blueList = pool.mallocDouble(2*d*m)
var blueIds = pool.mallocInt32(m)
m = convertBoxes(blue, d, blueList, blueIds)
if(m > 0) {
sweep.init(n+m)
if(d === 1) {
//Special case: 1d bipartite
retval = sweep.sweepBipartite(
d, visit,
0, n, redList, redIds,
0, m, blueList, blueIds)
} else {
//General case: d>1
retval = boxIntersectIter(
d, visit, full,
n, redList, redIds,
m, blueList, blueIds)
}
pool.free(blueList)
pool.free(blueIds)
}
}
pool.free(redList)
pool.free(redIds)
}
return retval
}
var RESULT
function appendItem(i,j) {
RESULT.push([i,j])
}
function intersectFullArray(x) {
RESULT = []
boxIntersect(x, x, appendItem, true)
return RESULT
}
function intersectBipartiteArray(x, y) {
RESULT = []
boxIntersect(x, y, appendItem, false)
return RESULT
}
//User-friendly wrapper, handle full input and no-visitor cases
function boxIntersectWrapper(arg0, arg1, arg2) {
var result
switch(arguments.length) {
case 1:
return intersectFullArray(arg0)
case 2:
if(typeof arg1 === 'function') {
return boxIntersect(arg0, arg0, arg1, true)
} else {
return intersectBipartiteArray(arg0, arg1)
}
case 3:
return boxIntersect(arg0, arg1, arg2, false)
default:
throw new Error('box-intersect: Invalid arguments')
}
}