Browse Source

[init]

master
Muthu Kumar 7 years ago
commit
da430d570e
  1. 1
      .gitignore
  2. 5
      index.js
  3. 29
      package.json
  4. 41
      structures/slider.js
  5. 36
      structures/window.js

1
.gitignore

@ -0,0 +1 @@
node_modules

5
index.js

@ -0,0 +1,5 @@
'use strict';
const Slider = require('./structures/slider');
module.exports = Slider;

29
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 <https://mkr.pw>",
"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"
}
}

41
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;

36
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;
Loading…
Cancel
Save