@flatten-js/interval-tree
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -102,3 +102,3 @@ 'use strict'; | ||
*/ | ||
comparable_max(val1, val2) { | ||
static comparable_max(val1, val2) { | ||
return Math.max(val1, val2); | ||
@@ -113,3 +113,3 @@ } | ||
*/ | ||
comparable_less_than(val1, val2 ) { | ||
static comparable_less_than(val1, val2 ) { | ||
return val1 < val2; | ||
@@ -185,7 +185,7 @@ } | ||
if (this.right && this.right.max) { | ||
const comparable_max = this.item.key.comparable_max; | ||
const comparable_max = this.item.key.constructor.comparable_max; // static method | ||
this.max = comparable_max(this.max, this.right.max); | ||
} | ||
if (this.left && this.left.max) { | ||
const comparable_max = this.item.key.comparable_max; | ||
const comparable_max = this.item.key.constructor.comparable_max; // static method | ||
this.max = comparable_max(this.max, this.left.max); | ||
@@ -197,3 +197,3 @@ } | ||
not_intersect_left_subtree(search_node) { | ||
const comparable_less_than = this.item.key.comparable_less_than; | ||
const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method | ||
let high = this.left.max.high ? this.left.max.high : this.left.max; | ||
@@ -205,3 +205,3 @@ return comparable_less_than(high, search_node.item.key.low); | ||
not_intersect_right_subtree(search_node) { | ||
const comparable_less_than = this.item.key.comparable_less_than; | ||
const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method | ||
let low = this.right.max.low ? this.right.max.low : this.right.item.key.low; | ||
@@ -330,18 +330,10 @@ return comparable_less_than(search_node.item.key.high, low); | ||
* @param interval - search interval, or array [low, high] | ||
* @param outputMapperFn(value,key) - optional function that maps (value, key) to custom output | ||
* @returns {Array} | ||
*/ | ||
search(interval) { | ||
search(interval, outputMapperFn = (value, key) => value ? value : key.output()) { | ||
let search_node = new Node(interval); | ||
let resp_nodes = []; | ||
this.tree_search_interval(this.root, search_node, resp_nodes); | ||
let resp = []; | ||
resp_nodes.forEach((node) => { | ||
if (node.item.value) { // if there are values, return only values | ||
resp.push(node.item.value); | ||
} | ||
else { // otherwise, return keys | ||
resp.push(node.item.key.output()); | ||
} | ||
}, []); | ||
return resp; | ||
return resp_nodes.map(node => outputMapperFn(node.item.value, node.item.key)) | ||
} | ||
@@ -780,3 +772,4 @@ | ||
exports.Node = Node; | ||
exports.Interval = Interval; | ||
exports.default = IntervalTree; |
@@ -98,3 +98,3 @@ /** | ||
*/ | ||
comparable_max(val1, val2) { | ||
static comparable_max(val1, val2) { | ||
return Math.max(val1, val2); | ||
@@ -109,3 +109,3 @@ } | ||
*/ | ||
comparable_less_than(val1, val2 ) { | ||
static comparable_less_than(val1, val2 ) { | ||
return val1 < val2; | ||
@@ -181,7 +181,7 @@ } | ||
if (this.right && this.right.max) { | ||
const comparable_max = this.item.key.comparable_max; | ||
const comparable_max = this.item.key.constructor.comparable_max; // static method | ||
this.max = comparable_max(this.max, this.right.max); | ||
} | ||
if (this.left && this.left.max) { | ||
const comparable_max = this.item.key.comparable_max; | ||
const comparable_max = this.item.key.constructor.comparable_max; // static method | ||
this.max = comparable_max(this.max, this.left.max); | ||
@@ -193,3 +193,3 @@ } | ||
not_intersect_left_subtree(search_node) { | ||
const comparable_less_than = this.item.key.comparable_less_than; | ||
const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method | ||
let high = this.left.max.high ? this.left.max.high : this.left.max; | ||
@@ -201,3 +201,3 @@ return comparable_less_than(high, search_node.item.key.low); | ||
not_intersect_right_subtree(search_node) { | ||
const comparable_less_than = this.item.key.comparable_less_than; | ||
const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method | ||
let low = this.right.max.low ? this.right.max.low : this.right.item.key.low; | ||
@@ -326,18 +326,10 @@ return comparable_less_than(search_node.item.key.high, low); | ||
* @param interval - search interval, or array [low, high] | ||
* @param outputMapperFn(value,key) - optional function that maps (value, key) to custom output | ||
* @returns {Array} | ||
*/ | ||
search(interval) { | ||
search(interval, outputMapperFn = (value, key) => value ? value : key.output()) { | ||
let search_node = new Node(interval); | ||
let resp_nodes = []; | ||
this.tree_search_interval(this.root, search_node, resp_nodes); | ||
let resp = []; | ||
resp_nodes.forEach((node) => { | ||
if (node.item.value) { // if there are values, return only values | ||
resp.push(node.item.value); | ||
} | ||
else { // otherwise, return keys | ||
resp.push(node.item.key.output()); | ||
} | ||
}, []); | ||
return resp; | ||
return resp_nodes.map(node => outputMapperFn(node.item.value, node.item.key)) | ||
} | ||
@@ -777,2 +769,2 @@ | ||
export default IntervalTree; | ||
export { Interval }; | ||
export { Node, Interval }; |
@@ -104,3 +104,3 @@ (function (global, factory) { | ||
*/ | ||
comparable_max(val1, val2) { | ||
static comparable_max(val1, val2) { | ||
return Math.max(val1, val2); | ||
@@ -115,3 +115,3 @@ } | ||
*/ | ||
comparable_less_than(val1, val2 ) { | ||
static comparable_less_than(val1, val2 ) { | ||
return val1 < val2; | ||
@@ -187,7 +187,7 @@ } | ||
if (this.right && this.right.max) { | ||
const comparable_max = this.item.key.comparable_max; | ||
const comparable_max = this.item.key.constructor.comparable_max; // static method | ||
this.max = comparable_max(this.max, this.right.max); | ||
} | ||
if (this.left && this.left.max) { | ||
const comparable_max = this.item.key.comparable_max; | ||
const comparable_max = this.item.key.constructor.comparable_max; // static method | ||
this.max = comparable_max(this.max, this.left.max); | ||
@@ -199,3 +199,3 @@ } | ||
not_intersect_left_subtree(search_node) { | ||
const comparable_less_than = this.item.key.comparable_less_than; | ||
const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method | ||
let high = this.left.max.high ? this.left.max.high : this.left.max; | ||
@@ -207,3 +207,3 @@ return comparable_less_than(high, search_node.item.key.low); | ||
not_intersect_right_subtree(search_node) { | ||
const comparable_less_than = this.item.key.comparable_less_than; | ||
const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method | ||
let low = this.right.max.low ? this.right.max.low : this.right.item.key.low; | ||
@@ -332,18 +332,10 @@ return comparable_less_than(search_node.item.key.high, low); | ||
* @param interval - search interval, or array [low, high] | ||
* @param outputMapperFn(value,key) - optional function that maps (value, key) to custom output | ||
* @returns {Array} | ||
*/ | ||
search(interval) { | ||
search(interval, outputMapperFn = (value, key) => value ? value : key.output()) { | ||
let search_node = new Node(interval); | ||
let resp_nodes = []; | ||
this.tree_search_interval(this.root, search_node, resp_nodes); | ||
let resp = []; | ||
resp_nodes.forEach((node) => { | ||
if (node.item.value) { // if there are values, return only values | ||
resp.push(node.item.value); | ||
} | ||
else { // otherwise, return keys | ||
resp.push(node.item.key.output()); | ||
} | ||
}, []); | ||
return resp; | ||
return resp_nodes.map(node => outputMapperFn(node.item.value, node.item.key)) | ||
} | ||
@@ -782,2 +774,3 @@ | ||
exports.Node = Node; | ||
exports.Interval = Interval; | ||
@@ -784,0 +777,0 @@ exports.default = IntervalTree; |
@@ -23,4 +23,4 @@ // Type definitions for flatten-interval-tree library v1.0.2 | ||
comparable_max(arg1: Comparable, arg2: Comparable) : Comparable; | ||
comparable_less_than(arg1: Comparable, arg2: Comparable ) : boolean; | ||
static comparable_max(arg1: Comparable, arg2: Comparable) : Comparable; | ||
static comparable_less_than(arg1: Comparable, arg2: Comparable ) : boolean; | ||
} | ||
@@ -61,5 +61,5 @@ | ||
remove(key: Interval | number[], value?: Value) : Node; | ||
search(interval: Interval | number[]) : Array<Value>; // Array of value's or key.output()'s | ||
search(interval: Interval | number[], outputMapperFn?: (value: Value, key: Interval) => any) : Array<any>; | ||
forEach(callbackfn: (key: Interval, value: Value) => void, thisArg?: any ) : void; | ||
map(callbackFn: (value: Value, key?: Interval) => any, thisArg?: any ): IntervalTree; | ||
} |
@@ -1,3 +0,3 @@ | ||
import Interval from './src/classes/interval'; | ||
export {Interval}; | ||
export {default as Node} from './src/classes/node'; | ||
export {default as Interval} from './src/classes/interval'; | ||
export {default} from './src/classes/intervalTree.js'; |
{ | ||
"name": "@flatten-js/interval-tree", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "Interval search tree", | ||
@@ -30,3 +30,3 @@ "main": "dist/main.cjs.js", | ||
}, | ||
"homepage": "https://github.com/alexbol99/flatten-interval-tree#readme", | ||
"homepage": "https://github.com/alexbol99/flatten-interval-tree/tree/v0.3.0-beta#readme", | ||
"dependencies": {}, | ||
@@ -44,4 +44,5 @@ "devDependencies": { | ||
"minami": "^1.2.3", | ||
"mocha": "^5.2.0" | ||
"mocha": "^5.2.0", | ||
"rollup": "^1.6.0" | ||
} | ||
} |
@@ -89,3 +89,3 @@ # Interval Tree | ||
### Search(interval) | ||
### Search(interval<, outputMapperFn>) | ||
Returns array of values which keys intersected with given interval. <br/> | ||
@@ -95,3 +95,32 @@ ```javascript | ||
``` | ||
Optional *outputMapperFn(value, key)* enables to map search results into custom defined output. | ||
Example: | ||
```javascript | ||
const composers = [ | ||
{name: "Ludwig van Beethoven", period: [1770, 1827]}, | ||
{name: "Johann Sebastian Bach", period: [1685, 1750]}, | ||
{name: "Wolfgang Amadeus Mozart", period: [1756, 1791]}, | ||
{name: "Johannes Brahms", period: [1833, 1897]}, | ||
{name: "Richard Wagner", period: [1813, 1883]}, | ||
{name: "Claude Debussy", period: [1862, 1918]}, | ||
{name: "Pyotr Ilyich Tchaikovsky", period: [1840, 1893]}, | ||
{name: "Frédéric Chopin", period: [1810, 1849]}, | ||
{name: "Joseph Haydn", period: [1732, 1809]}, | ||
{name: "Antonio Vivaldi", period: [1678, 1741]} | ||
]; | ||
const tree = new IntervalTree(); | ||
for (let composer of composers) | ||
tree.insert(composer.period, composer.name); | ||
// Great composers who lived in 17th century | ||
const searchRes = tree.search( [1600,1700], | ||
(name, period) => {return `${name} (${period.low}-${period.high})`}); | ||
console.log(searchRes) | ||
// expected to be | ||
// [ 'Antonio Vivaldi (1678-1741)', 'Johann Sebastian Bach (1685-1750)' ] | ||
``` | ||
### Size | ||
@@ -98,0 +127,0 @@ Returns number of items stored in the tree (getter) |
@@ -98,3 +98,3 @@ /** | ||
*/ | ||
comparable_max(val1, val2) { | ||
static comparable_max(val1, val2) { | ||
return Math.max(val1, val2); | ||
@@ -109,3 +109,3 @@ } | ||
*/ | ||
comparable_less_than(val1, val2 ) { | ||
static comparable_less_than(val1, val2 ) { | ||
return val1 < val2; | ||
@@ -112,0 +112,0 @@ } |
@@ -123,18 +123,10 @@ /** | ||
* @param interval - search interval, or array [low, high] | ||
* @param outputMapperFn(value,key) - optional function that maps (value, key) to custom output | ||
* @returns {Array} | ||
*/ | ||
search(interval) { | ||
search(interval, outputMapperFn = (value, key) => value ? value : key.output()) { | ||
let search_node = new Node(interval); | ||
let resp_nodes = []; | ||
this.tree_search_interval(this.root, search_node, resp_nodes); | ||
let resp = []; | ||
resp_nodes.forEach((node) => { | ||
if (node.item.value) { // if there are values, return only values | ||
resp.push(node.item.value); | ||
} | ||
else { // otherwise, return keys | ||
resp.push(node.item.key.output()); | ||
} | ||
}, []); | ||
return resp; | ||
return resp_nodes.map(node => outputMapperFn(node.item.value, node.item.key)) | ||
} | ||
@@ -141,0 +133,0 @@ |
@@ -60,7 +60,7 @@ /** | ||
if (this.right && this.right.max) { | ||
const comparable_max = this.item.key.comparable_max; | ||
const comparable_max = this.item.key.constructor.comparable_max; // static method | ||
this.max = comparable_max(this.max, this.right.max); | ||
} | ||
if (this.left && this.left.max) { | ||
const comparable_max = this.item.key.comparable_max; | ||
const comparable_max = this.item.key.constructor.comparable_max; // static method | ||
this.max = comparable_max(this.max, this.left.max); | ||
@@ -72,3 +72,3 @@ } | ||
not_intersect_left_subtree(search_node) { | ||
const comparable_less_than = this.item.key.comparable_less_than; | ||
const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method | ||
let high = this.left.max.high ? this.left.max.high : this.left.max; | ||
@@ -80,3 +80,3 @@ return comparable_less_than(high, search_node.item.key.low); | ||
not_intersect_right_subtree(search_node) { | ||
const comparable_less_than = this.item.key.comparable_less_than; | ||
const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method | ||
let low = this.right.max.low ? this.right.max.low : this.right.item.key.low; | ||
@@ -83,0 +83,0 @@ return comparable_less_than(search_node.item.key.high, low); |
@@ -47,3 +47,3 @@ /** | ||
let ints = [[6,8],[1,4],[5,12],[1,1],[5,7]]; | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i) | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i); | ||
expect(tree.keys).to.deep.equal([[1,1],[1,4],[5,7],[5,12],[6,8]]); | ||
@@ -57,3 +57,3 @@ for (let i=0; i < ints.length; i++) { | ||
let ints = [[6,8],[1,4],[5,12],[1,1],[5,7]]; | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i) | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i); | ||
expect(tree.exist([2,4],"val")).to.be.false; // wrong interval | ||
@@ -65,3 +65,3 @@ expect(tree.exist([1,4],"val2")).to.be.false; // wrong value | ||
let ints = [[6,8],[1,4],[5,12],[1,1],[5,7]]; | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i) | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i); | ||
tree.remove([1,4],"val1"); | ||
@@ -76,3 +76,3 @@ | ||
let ints = [[6,8],[1,4],[5,12],[1,1],[5,7]]; | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i) | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i); | ||
tree.remove([6,8],"val0"); | ||
@@ -90,3 +90,3 @@ tree.remove([1,4],"val1"); | ||
let ints = [[6,8],[1,4],[5,12],[1,1],[5,7]]; | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i) | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i); | ||
@@ -120,6 +120,29 @@ let items = tree.items; | ||
}); | ||
it('May search interval and return array of custom transformed objects', function () { | ||
const composers = [ | ||
{name: "Ludwig van Beethoven", period: [1770,1827]}, | ||
{name: "Johann Sebastian Bach", period: [1685, 1750]}, | ||
{name: "Wolfgang Amadeus Mozart", period: [1756, 1791]}, | ||
{name: "Johannes Brahms", period: [1833, 1897]}, | ||
{name: "Richard Wagner", period: [1813, 1883]}, | ||
{name: "Claude Debussy", period: [1862, 1918]}, | ||
{name: "Pyotr Ilyich Tchaikovsky", period: [1840, 1893]}, | ||
{name: "Frédéric Chopin", period: [1810, 1849]}, | ||
{name: "Joseph Haydn", period: [1732, 1809]}, | ||
{name: "Antonio Vivaldi", period: [1678, 1741]} | ||
]; | ||
const tree = new IntervalTree(); | ||
for (let composer of composers) | ||
tree.insert(composer.period, composer.name); | ||
const searchRes = tree.search( [1600,1700], | ||
(name, period) => {return `${name} (${period.low}-${period.high})`}); | ||
expect(searchRes.length).to.equal(2); | ||
expect(searchRes[0]).to.equal("Antonio Vivaldi (1678-1741)"); | ||
expect(searchRes[1]).to.equal("Johann Sebastian Bach (1685-1750)"); | ||
}); | ||
it('May return empty array when search interval does not intersect any', function () { | ||
let tree = new IntervalTree(); | ||
let ints = [[6,8],[1,2],[7,12],[1,1],[5,7]]; | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i) | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i); | ||
expect(tree.search([3,4])).to.deep.equal([]); | ||
@@ -130,3 +153,3 @@ }); | ||
let ints = [[6,8],[1,2],[7,12],[1,1],[5,7]]; | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i) | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i); | ||
expect(tree.testRedBlackProperty()).to.equal(true); | ||
@@ -137,3 +160,3 @@ }); | ||
let ints = [[6,8],[1,2],[7,12],[1,1],[5,7]]; | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i) | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i); | ||
let height = (tree) => { | ||
@@ -147,3 +170,3 @@ return tree.testBlackHeightProperty(tree.root); | ||
let ints = [[6,8],[1,2],[7,12],[1,1],[5,7]]; | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i) | ||
for (let i=0; i < ints.length; i++) tree.insert(ints[i],"val"+i); | ||
let height = (tree) => { | ||
@@ -150,0 +173,0 @@ return tree.testBlackHeightProperty(tree.root); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1654146
174
12
3906
1