🎚 Window-sliding insertion algorithm in JavaScript
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0 KiB

7 years ago
'use strict';
const isEq = require('@codefeathers/iseq/isEq');
const Deck = require('./deck');
7 years ago
const wrap = (f, ...x) => (...y) => f(...x, ...y);
class Slider {
constructor (options) {
this.windowSize = options.size;
7 years ago
this.prefer = options.prefer;
this.accepting = options.accepting;
}
slide (master, compare) {
7 years ago
const k = this.windowSize;
const host = new Deck(...master);
const guest = new Deck(...compare);
const startSliding = (window, i) => {
const firstMatch = host.findIndex(wrap(isEq, window[0]));
if(firstMatch !== -1
&& window.every((item, j) => host.findIndexAfter(
firstMatch,
wrap(isEq, window[j])
)) !== -1
) return [ firstMatch, i ];
const newWindow = new Deck(...guest.slice(++i, k));
7 years ago
if(newWindow.length !== 0 && newWindow.length === k) return recurse(newWindow, i);
7 years ago
return [ -1, -1 ];
7 years ago
};
const window = new Deck(...guest.slice(0, k));
const index = startSliding(window, 0);
7 years ago
return index;
}
};
module.exports = Slider;