Browse Source

[slider] wip 0.1.0

master
Muthu Kumar 6 years ago
parent
commit
e3e6400181
  1. 2
      package.json
  2. 26
      structures/deck.js
  3. 37
      structures/slider.js

2
package.json

@ -1,6 +1,6 @@
{
"name": "@codefeathers/window",
"version": "0.0.1",
"version": "0.1.0",
"description": "A sliding window algorithm to insert a new array into an existing one.",
"main": "index.js",
"scripts": {

26
structures/deck.js

@ -1,26 +0,0 @@
class Deck extends Array {
recurse (fn) {
let index = 0;
const call = (arr, acc = []) => {
if(arr[0]) {
const res = fn(index++, arr[0]);
return call(arr.slice(1), [ res, ...acc ]);
}
return acc;
};
return call(this);
}
findIndexAfter(index, predicate) {
const self = this;
let i = index + 1;
while(i < self.length) {
if(predicate(self[i])) return i;
i++;
}
return -1;
}
};
module.exports = Deck;

37
structures/slider.js

@ -1,8 +1,6 @@
'use strict';
const isEq = require('@codefeathers/iseq/isEq');
const Deck = require('./deck');
const isEq = require('@codefeathers/iseq');
const wrap = (f, ...x) => (...y) => f(...x, ...y);
@ -13,32 +11,35 @@ class Slider {
this.accepting = options.accepting;
}
slide (master, compare) {
slide (host, guest) {
const k = this.windowSize;
const host = Deck.from(master);
const guest = Deck.from(compare);
const get = {
val: 0,
next: () => ++get.val,
}
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 = Deck.from(guest.slice(++i, k));
if(newWindow.length !== 0 && newWindow.length === k) return startSliding(newWindow, i);
if(firstMatch !== -1) {
get.val = firstMatch;
if(window.every((item, j) => {
if(j === 0) return true;
return isEq(item, host[get.next()]);
})) return [ firstMatch, i ];
}
const nextI = i + 1;
const newWindow = guest.slice(nextI, nextI + k);
if(newWindow.length === k) return startSliding(newWindow, nextI);
return [ -1, -1 ];
};
const window = Deck.from(guest.slice(0, k));
const index = startSliding(window, 0);
return index;
const window = guest.slice(0, k);
return startSliding(window, 0);
}
};

Loading…
Cancel
Save