alexbol99 / flatten-interval-tree

Interval binary search tree
MIT License
41 stars 21 forks source link

Storing '0' as a key value will return a key pair when searching #16

Closed BryonLewis closed 3 years ago

BryonLewis commented 4 years ago

Not a big deal because you provide the ability to insert your own function. The default outputMapperFn tests for truthy on the value. So if you store a value of '0' or other falsey values you will get back an array of keys that intersect the range. https://github.com/alexbol99/flatten-interval-tree/blob/master/src/classes/intervalTree.js#L126 search(interval, outputMapperFn = (value, key) => value ? value : key.output()) { Wondering if this should test for value !== undefined ? value : key.output()? If the user is storing numbers that happen to have '0', intentional 'null' values or 'false' in the interval tree this will allow them to get the result if it exists without overriding the outputMapperFn.

subdavis commented 3 years ago

This naturally breaks anything related to search, so delete() produces a rather unexpected behavior.

const tree = new IntervalTree();
atree.insert([0, 0], 0);
atree.insert([0, 0], 1);
atree.search([0, 0])
// [ [ 0, 0 ], 1 ]
atree.remove([0, 0], 1);
atree.search([0, 0])
// [ 1 ]
alexbol99 commented 3 years ago

Hello @subdavis, @BryonLewis ,

Issue fixed in release v1.0.12. Now Interval Tree supports storing falsy values like 0, null, NaN, false, except of undefined Please check this release.

Best regards, Alex