Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting box-intersect function constructors to normal functions #8

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ logs
results

npm-debug.log
package-lock.json
node_modules/*
*.DS_Store
*.asm
Expand Down
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function boxIntersect(red, blue, visit, full) {
//Special case: 1d complete
sweep.init(n)
retval = sweep.sweepComplete(
d, visit,
d, visit,
0, n, redList, redIds,
0, n, redList, redIds)
} else {
Expand All @@ -76,7 +76,7 @@ function boxIntersect(red, blue, visit, full) {
if(d === 1) {
//Special case: 1d bipartite
retval = sweep.sweepBipartite(
d, visit,
d, visit,
0, n, redList, redIds,
0, m, blueList, blueIds)
} else {
Expand Down Expand Up @@ -120,7 +120,6 @@ function intersectBipartiteArray(x, y) {

//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)
Expand Down
253 changes: 124 additions & 129 deletions lib/brute.js
Original file line number Diff line number Diff line change
@@ -1,144 +1,139 @@
'use strict'

var DIMENSION = 'd'
var AXIS = 'ax'
var VISIT = 'vv'
var FLIP = 'fp'

var ELEM_SIZE = 'es'

var RED_START = 'rs'
var RED_END = 're'
var RED_BOXES = 'rb'
var RED_INDEX = 'ri'
var RED_PTR = 'rp'

var BLUE_START = 'bs'
var BLUE_END = 'be'
var BLUE_BOXES = 'bb'
var BLUE_INDEX = 'bi'
var BLUE_PTR = 'bp'

var RETVAL = 'rv'

var INNER_LABEL = 'Q'

var ARGS = [
DIMENSION,
AXIS,
VISIT,
RED_START,
RED_END,
RED_BOXES,
RED_INDEX,
BLUE_START,
BLUE_END,
BLUE_BOXES,
BLUE_INDEX
]

function generateBruteForce(redMajor, flip, full) {
var funcName = 'bruteForce' +
(redMajor ? 'Red' : 'Blue') +
(flip ? 'Flip' : '') +
(full ? 'Full' : '')

var code = ['function ', funcName, '(', ARGS.join(), '){',
'var ', ELEM_SIZE, '=2*', DIMENSION, ';']

var redLoop =
'for(var i=' + RED_START + ',' + RED_PTR + '=' + ELEM_SIZE + '*' + RED_START + ';' +
'i<' + RED_END +';' +
'++i,' + RED_PTR + '+=' + ELEM_SIZE + '){' +
'var x0=' + RED_BOXES + '[' + AXIS + '+' + RED_PTR + '],' +
'x1=' + RED_BOXES + '[' + AXIS + '+' + RED_PTR + '+' + DIMENSION + '],' +
'xi=' + RED_INDEX + '[i];'

var blueLoop =
'for(var j=' + BLUE_START + ',' + BLUE_PTR + '=' + ELEM_SIZE + '*' + BLUE_START + ';' +
'j<' + BLUE_END + ';' +
'++j,' + BLUE_PTR + '+=' + ELEM_SIZE + '){' +
'var y0=' + BLUE_BOXES + '[' + AXIS + '+' + BLUE_PTR + '],' +
(full ? 'y1=' + BLUE_BOXES + '[' + AXIS + '+' + BLUE_PTR + '+' + DIMENSION + '],' : '') +
'yi=' + BLUE_INDEX + '[j];'

if(redMajor) {
code.push(redLoop, INNER_LABEL, ':', blueLoop)
} else {
code.push(blueLoop, INNER_LABEL, ':', redLoop)
}

if(full) {
code.push('if(y1<x0||x1<y0)continue;')
} else if(flip) {
code.push('if(y0<=x0||x1<y0)continue;')
} else {
code.push('if(y0<x0||x1<y0)continue;')
function full() {
function bruteForceRedFull(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi) {
var es = 2 * d
for (var i = rs, rp = es * rs; i < re; ++i, rp += es) {
var x0 = rb[ax + rp], x1 = rb[ax + rp + d], xi = ri[i]
Q: for (var j = bs, bp = es * bs; j < be; ++j, bp += es) {
var y0 = bb[ax + bp], y1 = bb[ax + bp + d], yi = bi[j]
if (y1 < x0 || x1 < y0) continue
for (var k = ax + 1; k < d; ++k) {
var r0 = rb[k + rp], r1 = rb[k + d + rp], b0 = bb[k + bp], b1 = bb[k + d + bp]
if (r1 < b0 || b1 < r0) continue Q
}
var rv = vv(xi, yi)
if (rv !== void 0) return rv
}
}
}

code.push('for(var k='+AXIS+'+1;k<'+DIMENSION+';++k){'+
'var r0='+RED_BOXES+'[k+'+RED_PTR+'],'+
'r1='+RED_BOXES+'[k+'+DIMENSION+'+'+RED_PTR+'],'+
'b0='+BLUE_BOXES+'[k+'+BLUE_PTR+'],'+
'b1='+BLUE_BOXES+'[k+'+DIMENSION+'+'+BLUE_PTR+'];'+
'if(r1<b0||b1<r0)continue ' + INNER_LABEL + ';}' +
'var ' + RETVAL + '=' + VISIT + '(')

if(flip) {
code.push('yi,xi')
} else {
code.push('xi,yi')
function bruteForceBlueFull(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi) {
var es = 2 * d
for (var j = bs, bp = es * bs; j < be; ++j, bp += es) {
var y0 = bb[ax + bp], y1 = bb[ax + bp + d], yi = bi[j]
Q: for (var i = rs, rp = es * rs; i < re; ++i, rp += es) {
var x0 = rb[ax + rp], x1 = rb[ax + rp + d], xi = ri[i]
if (y1 < x0 || x1 < y0) continue
for (var k = ax + 1; k < d; ++k) {
var r0 = rb[k + rp], r1 = rb[k + d + rp], b0 = bb[k + bp], b1 = bb[k + d + bp]
if (r1 < b0 || b1 < r0) continue Q
}
var rv = vv(xi, yi)
if (rv !== void 0) return rv
}
}
}

code.push(');if(' + RETVAL + '!==void 0)return ' + RETVAL + ';}}}')

return {
name: funcName,
code: code.join('')
function bruteForceFull(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi) {
if (re - rs > be - bs) {
return bruteForceRedFull(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi)
}
else {
return bruteForceBlueFull(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi)
}
}
return bruteForceFull
}

function bruteForcePlanner(full) {
var funcName = 'bruteForce' + (full ? 'Full' : 'Partial')
var prefix = []
var fargs = ARGS.slice()
if(!full) {
fargs.splice(3, 0, FLIP)
function partial() {
function bruteForceRedFlip(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi) {
var es = 2 * d
for (var i = rs, rp = es * rs; i < re; ++i, rp += es) {
var x0 = rb[ax + rp], x1 = rb[ax + rp + d], xi = ri[i]
Q: for (var j = bs, bp = es * bs; j < be; ++j, bp += es) {
var y0 = bb[ax + bp], yi = bi[j]
if (y0 <= x0 || x1 < y0) continue
for (var k = ax + 1; k < d; ++k) {
var r0 = rb[k + rp], r1 = rb[k + d + rp], b0 = bb[k + bp], b1 = bb[k + d + bp]
if (r1 < b0 || b1 < r0) continue Q
}
var rv = vv(yi, xi)
if (rv !== void 0) return rv
}
}
}

var code = ['function ' + funcName + '(' + fargs.join() + '){']

function invoke(redMajor, flip) {
var res = generateBruteForce(redMajor, flip, full)
prefix.push(res.code)
code.push('return ' + res.name + '(' + ARGS.join() + ');')
function bruteForceRed(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi) {
var es = 2 * d
for (var i = rs, rp = es * rs; i < re; ++i, rp += es) {
var x0 = rb[ax + rp], x1 = rb[ax + rp + d], xi = ri[i]
Q: for (var j = bs, bp = es * bs; j < be; ++j, bp += es) {
var y0 = bb[ax + bp], yi = bi[j]
if (y0 < x0 || x1 < y0) continue
for (var k = ax + 1; k < d; ++k) {
var r0 = rb[k + rp], r1 = rb[k + d + rp], b0 = bb[k + bp], b1 = bb[k + d + bp]
if (r1 < b0 || b1 < r0) continue Q
}
var rv = vv(xi, yi)
if (rv !== void 0) return rv
}
}
}

code.push('if(' + RED_END + '-' + RED_START + '>' +
BLUE_END + '-' + BLUE_START + '){')

if(full) {
invoke(true, false)
code.push('}else{')
invoke(false, false)
} else {
code.push('if(' + FLIP + '){')
invoke(true, true)
code.push('}else{')
invoke(true, false)
code.push('}}else{if(' + FLIP + '){')
invoke(false, true)
code.push('}else{')
invoke(false, false)
code.push('}')
function bruteForceBlueFlip(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi) {
var es = 2 * d
for (var j = bs, bp = es * bs; j < be; ++j, bp += es) {
var y0 = bb[ax + bp], yi = bi[j]
Q: for (var i = rs, rp = es * rs; i < re; ++i, rp += es) {
var x0 = rb[ax + rp], x1 = rb[ax + rp + d], xi = ri[i]
if (y0 <= x0 || x1 < y0) continue
for (var k = ax + 1; k < d; ++k) {
var r0 = rb[k + rp], r1 = rb[k + d + rp], b0 = bb[k + bp], b1 = bb[k + d + bp]
if (r1 < b0 || b1 < r0) continue Q
}
var rv = vv(yi, xi)
if (rv !== void 0) return rv
}
}
}
code.push('}}return ' + funcName)

var codeStr = prefix.join('') + code.join('')
var proc = new Function(codeStr)
return proc()
function bruteForceBlue(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi) {
var es = 2 * d
for (var j = bs, bp = es * bs; j < be; ++j, bp += es) {
var y0 = bb[ax + bp], yi = bi[j]
Q: for (var i = rs, rp = es * rs; i < re; ++i, rp += es) {
var x0 = rb[ax + rp], x1 = rb[ax + rp + d], xi = ri[i]
if (y0 < x0 || x1 < y0) continue
for (var k = ax + 1; k < d; ++k) {
var r0 = rb[k + rp], r1 = rb[k + d + rp], b0 = bb[k + bp], b1 = bb[k + d + bp]
if (r1 < b0 || b1 < r0) continue Q
}
var rv = vv(xi, yi)
if (rv !== void 0) return rv
}
}
}
function bruteForcePartial(d, ax, vv, fp, rs, re, rb, ri, bs, be, bb, bi) {
if (re - rs > be - bs) {
if (fp) {
return bruteForceRedFlip(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi)
}
else {
return bruteForceRed(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi)
}
}
else {
if (fp) {
return bruteForceBlueFlip(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi)
}
else {
return bruteForceBlue(d, ax, vv, rs, re, rb, ri, bs, be, bb, bi)
}
}
}
return bruteForcePartial
}

function bruteForcePlanner(isFull) {
return isFull ? full() : partial()

}

exports.partial = bruteForcePlanner(false)
exports.full = bruteForcePlanner(true)
Loading