Browse Source

[slider] wip 0.1.0

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

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "@codefeathers/window", "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.", "description": "A sliding window algorithm to insert a new array into an existing one.",
"main": "index.js", "main": "index.js",
"scripts": { "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;

35
structures/slider.js

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

Loading…
Cancel
Save