Skip to content

Commit

Permalink
Merge PR#46 simple iterator implementation
Browse files Browse the repository at this point in the history
Add update index.d.ts and documentation
Build packages, upgrade version to minor 1.1.0
  • Loading branch information
alexbol99 committed Sep 10, 2023
1 parent 83da347 commit 6a99b44
Show file tree
Hide file tree
Showing 13 changed files with 1,722 additions and 1,938 deletions.
42 changes: 38 additions & 4 deletions dist/main.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,15 +392,34 @@ class IntervalTree {
this.tree_walk(this.root, (node) => visitor(node.item.key, node.item.value));
}

/** Value Mapper. Walk through every node and map node value to another value
* @param callback(value,key) - function to be called for each tree item
*/
/**
* Value Mapper. Walk through every node and map node value to another value
* @param callback(value,key) - function to be called for each tree item
*/
map(callback) {
const tree = new IntervalTree();
this.tree_walk(this.root, (node) => tree.insert(node.item.key, callback(node.item.value, node.item.key)));
return tree;
}

/**
* @param {Interval} interval - optional if the iterator is intended to start from the beginning or end
* @param outputMapperFn(value,key) - optional function that maps (value, key) to custom output
* @returns {Iterator}
*/
*iterate(interval, outputMapperFn = (value, key) => value === key ? key.output() : value) {
let node;
if (interval) {
node = this.tree_search_nearest_forward(this.root, new Node(interval));
} else if (this.root) {
node = this.local_minimum(this.root);
}
while (node) {
yield outputMapperFn(node.item.value, node.item.key);
node = this.tree_successor(node);
}
}

recalc_max(node) {
let node_current = node;
while (node_current.parent != null) {
Expand Down Expand Up @@ -633,6 +652,21 @@ class IntervalTree {
}
}

tree_search_nearest_forward(node, search_node) {
let best;
let curr = node;
while (curr && curr != this.nil_node) {
if (curr.less_than(search_node)) {
if (curr.intersect(search_node) && (!best || curr.less_than(best))) best = curr;
curr = curr.right;
} else {
if (!best || curr.less_than(best)) best = curr;
curr = curr.left;
}
}
return best || null;
}

// Original search_interval method; container res support push() insertion
// Search all intervals intersecting given one
tree_search_interval(node, search_node, res) {
Expand Down Expand Up @@ -830,7 +864,7 @@ class IntervalTree {
}
height += heightLeft;
return height;
};
}
}

exports.Interval = Interval;
Expand Down
42 changes: 38 additions & 4 deletions dist/main.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,34 @@ class IntervalTree {
this.tree_walk(this.root, (node) => visitor(node.item.key, node.item.value));
}

/** Value Mapper. Walk through every node and map node value to another value
* @param callback(value,key) - function to be called for each tree item
*/
/**
* Value Mapper. Walk through every node and map node value to another value
* @param callback(value,key) - function to be called for each tree item
*/
map(callback) {
const tree = new IntervalTree();
this.tree_walk(this.root, (node) => tree.insert(node.item.key, callback(node.item.value, node.item.key)));
return tree;
}

/**
* @param {Interval} interval - optional if the iterator is intended to start from the beginning or end
* @param outputMapperFn(value,key) - optional function that maps (value, key) to custom output
* @returns {Iterator}
*/
*iterate(interval, outputMapperFn = (value, key) => value === key ? key.output() : value) {
let node;
if (interval) {
node = this.tree_search_nearest_forward(this.root, new Node(interval));
} else if (this.root) {
node = this.local_minimum(this.root);
}
while (node) {
yield outputMapperFn(node.item.value, node.item.key);
node = this.tree_successor(node);
}
}

recalc_max(node) {
let node_current = node;
while (node_current.parent != null) {
Expand Down Expand Up @@ -629,6 +648,21 @@ class IntervalTree {
}
}

tree_search_nearest_forward(node, search_node) {
let best;
let curr = node;
while (curr && curr != this.nil_node) {
if (curr.less_than(search_node)) {
if (curr.intersect(search_node) && (!best || curr.less_than(best))) best = curr;
curr = curr.right;
} else {
if (!best || curr.less_than(best)) best = curr;
curr = curr.left;
}
}
return best || null;
}

// Original search_interval method; container res support push() insertion
// Search all intervals intersecting given one
tree_search_interval(node, search_node, res) {
Expand Down Expand Up @@ -826,7 +860,7 @@ class IntervalTree {
}
height += heightLeft;
return height;
};
}
}

export { Interval, Node, IntervalTree as default };
42 changes: 38 additions & 4 deletions dist/main.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,34 @@
this.tree_walk(this.root, (node) => visitor(node.item.key, node.item.value));
}

/** Value Mapper. Walk through every node and map node value to another value
* @param callback(value,key) - function to be called for each tree item
*/
/**
* Value Mapper. Walk through every node and map node value to another value
* @param callback(value,key) - function to be called for each tree item
*/
map(callback) {
const tree = new IntervalTree();
this.tree_walk(this.root, (node) => tree.insert(node.item.key, callback(node.item.value, node.item.key)));
return tree;
}

/**
* @param {Interval} interval - optional if the iterator is intended to start from the beginning or end
* @param outputMapperFn(value,key) - optional function that maps (value, key) to custom output
* @returns {Iterator}
*/
*iterate(interval, outputMapperFn = (value, key) => value === key ? key.output() : value) {
let node;
if (interval) {
node = this.tree_search_nearest_forward(this.root, new Node(interval));
} else if (this.root) {
node = this.local_minimum(this.root);
}
while (node) {
yield outputMapperFn(node.item.value, node.item.key);
node = this.tree_successor(node);
}
}

recalc_max(node) {
let node_current = node;
while (node_current.parent != null) {
Expand Down Expand Up @@ -635,6 +654,21 @@
}
}

tree_search_nearest_forward(node, search_node) {
let best;
let curr = node;
while (curr && curr != this.nil_node) {
if (curr.less_than(search_node)) {
if (curr.intersect(search_node) && (!best || curr.less_than(best))) best = curr;
curr = curr.right;
} else {
if (!best || curr.less_than(best)) best = curr;
curr = curr.left;
}
}
return best || null;
}

// Original search_interval method; container res support push() insertion
// Search all intervals intersecting given one
tree_search_interval(node, search_node, res) {
Expand Down Expand Up @@ -832,7 +866,7 @@
}
height += heightLeft;
return height;
};
}
}

exports.Interval = Interval;
Expand Down
19 changes: 15 additions & 4 deletions docs/Interval.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<label for="nav-trigger" class="overlay"></label>

<nav>
<li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Classes</li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Interval.html">Interval</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#.comparable_less_than">comparable_less_than</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#.comparable_max">comparable_max</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#clone">clone</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#equal_to">equal_to</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#intersect">intersect</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#less_than">less_than</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#merge">merge</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#not_intersect">not_intersect</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#output">output</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="IntervalTree.html">IntervalTree</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#clear">clear</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#exist">exist</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#forEach">forEach</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#insert">insert</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#intersect_any">intersect_any</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#isEmpty">isEmpty</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#map">map</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#remove">remove</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#search">search</a></span></li>
<li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Classes</li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Interval.html">Interval</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#.comparable_less_than">comparable_less_than</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#.comparable_max">comparable_max</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#clone">clone</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#equal_to">equal_to</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#intersect">intersect</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#less_than">less_than</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#merge">merge</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#not_intersect">not_intersect</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Interval.html#output">output</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="IntervalTree.html">IntervalTree</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#clear">clear</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#exist">exist</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#forEach">forEach</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#insert">insert</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#intersect_any">intersect_any</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#isEmpty">isEmpty</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#iterate">iterate</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#map">map</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#remove">remove</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="IntervalTree.html#search">search</a></span></li>
</nav>

<div id="main">
Expand All @@ -45,7 +45,17 @@ <h2>
Interval
</h2>

<div class="class-description">Interval is a pair of numbers or a pair of any comparable objects on which may be defined predicates*equal*, *less* and method *max(p1, p1)* that returns maximum in a pair.When interval is an object rather than pair of numbers, this object should have properties *low*, *high*, *max*and implement methods *less_than(), equal_to(), intersect(), not_intersect(), clone(), output()*.Two static methods *comparable_max(), comparable_less_than()* define how to compare values in pair. <br/>This interface is described in typescript definition file *index.d.ts*Axis aligned rectangle is an example of such interval.We may look at rectangle as an interval between its low left and top right corners.See **Box** class in [flatten-js](https://github.com/alexbol99/flatten-js) library as the exampleof Interval interface implementation</div>
<div class="class-description">Interval is a pair of numbers or a pair of any comparable objects on which may be defined predicates
*equal*, *less* and method *max(p1, p1)* that returns maximum in a pair.
When interval is an object rather than pair of numbers, this object should have properties *low*, *high*, *max*
and implement methods *less_than(), equal_to(), intersect(), not_intersect(), clone(), output()*.
Two static methods *comparable_max(), comparable_less_than()* define how to compare values in pair. <br/>
This interface is described in typescript definition file *index.d.ts*

Axis aligned rectangle is an example of such interval.
We may look at rectangle as an interval between its low left and top right corners.
See **Box** class in [flatten-js](https://github.com/alexbol99/flatten-js) library as the example
of Interval interface implementation</div>


</header>
Expand All @@ -67,7 +77,8 @@ <h4 class="name" id="Interval"><span class="type-signature"></span>new Interval<


<div class="description">
Accept two comparable values and creates new instance of intervalPredicate Interval.comparable_less(low, high) supposed to return true on these values
Accept two comparable values and creates new instance of interval
Predicate Interval.comparable_less(low, high) supposed to return true on these values
</div>


Expand Down Expand Up @@ -1600,7 +1611,7 @@ <h4 class="name" id="output"><span class="type-signature"></span>output<span cla
<br class="clear">

<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Sat Jul 30 2022 12:04:03 GMT+0300 (Israel Daylight Time) using the Minami theme.
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Sun Sep 10 2023 14:11:35 GMT+0300 (Israel Daylight Time) using the Minami theme.
</footer>

<script>prettyPrint();</script>
Expand Down
Loading

0 comments on commit 6a99b44

Please sign in to comment.