From da430d570ecdc3dd42ce5f07ea1ea54d82ac6680 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Thu, 10 May 2018 12:29:36 +0530 Subject: [PATCH] [init] --- .gitignore | 1 + index.js | 5 +++++ package.json | 29 +++++++++++++++++++++++++++++ structures/slider.js | 41 +++++++++++++++++++++++++++++++++++++++++ structures/window.js | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json create mode 100644 structures/slider.js create mode 100644 structures/window.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..f4903ee --- /dev/null +++ b/index.js @@ -0,0 +1,5 @@ +'use strict'; + +const Slider = require('./structures/slider'); + +module.exports = Slider; diff --git a/package.json b/package.json new file mode 100644 index 0000000..e86fb44 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "@codefeathers/window", + "version": "0.0.1", + "description": "A sliding window algorithm to insert a new array into an existing one.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/codefeathers/sliding-window.git" + }, + "keywords": [ + "sliding-window", + "insertion", + "algorithm", + "array", + "javascript" + ], + "author": "Muthu Kumar @MKRhere ", + "license": "MIT", + "bugs": { + "url": "https://github.com/codefeathers/sliding-window/issues" + }, + "homepage": "https://github.com/codefeathers/sliding-window#readme", + "dependencies": { + "@codefeathers/iseq": "^1.1.0" + } +} diff --git a/structures/slider.js b/structures/slider.js new file mode 100644 index 0000000..d1a52f6 --- /dev/null +++ b/structures/slider.js @@ -0,0 +1,41 @@ +'use strict'; + +const isEq = require('@codefeathers/iseq/isEq'); + +const Window = require('./window'); + +const wrap = (f, ...x) => (...y) => f(...x, ...y); + +class Slider { + constructor (options) { + this.windowSize = options.window; + this.prefer = options.prefer; + this.accepting = options.accepting; + } + + slide (to, from) { + const k = this.windowSize; + const host = new Window(...to); + const guest = new Window(...from); + const recurse = (window, i) => { + let x, y, z; + x = host.findIndex(wrap(isEq, window[0])); + let switcher = true; + if(x !== -1) + for (let j = 1; j < window.length; j++) + if(host.findIndexAfter(x, wrap(isEq, window[j])) === -1) { + switcher = false; + break; + } + if(switcher) return [ x, i ]; + const newWindow = new Window(...guest.slice(++i, k)); + if(newWindow.length !== 0 && newWindow.length === k) return recurse(newWindow, i); + return [ -1, -1 ]; + }; + const window = new Window(...guest.slice(0, k)); + const index = recurse(window, 0); + return index; + } +}; + +module.exports = Slider; \ No newline at end of file diff --git a/structures/window.js b/structures/window.js new file mode 100644 index 0000000..7ea9991 --- /dev/null +++ b/structures/window.js @@ -0,0 +1,36 @@ +class Window extends Array { + + constructor (...args) { + if(args.length === 1) return args; + return super(...args); + } + + 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; + } + + empty () { + this.recurse(i => this.splice(i)); + return this; + } +}; + +module.exports = Window; \ No newline at end of file