From e3e6400181616a68d9ea8cdfe34b36655d9fede7 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Sat, 12 May 2018 18:44:39 +0530 Subject: [PATCH] [slider] wip 0.1.0 --- package.json | 2 +- structures/deck.js | 26 -------------------------- structures/slider.js | 37 +++++++++++++++++++------------------ 3 files changed, 20 insertions(+), 45 deletions(-) delete mode 100644 structures/deck.js diff --git a/package.json b/package.json index e86fb44..0b06367 100644 --- a/package.json +++ b/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": { diff --git a/structures/deck.js b/structures/deck.js deleted file mode 100644 index 402040d..0000000 --- a/structures/deck.js +++ /dev/null @@ -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; \ No newline at end of file diff --git a/structures/slider.js b/structures/slider.js index a4df4d9..8b1ece3 100644 --- a/structures/slider.js +++ b/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); } };