A minimal implementation of a segment tree for storing run-length encoded arrays with updates.
var tree = require("segment-tree").zeros(10)
tree.set(1, 1)
tree.set(2, 1)
console.log(tree.length)
console.log(tree.pointers)
console.log(tree.values)npm install segment-tree
var SegmentTree = require("segment-tree")SegmentTree constructor
lengthis the size of the segment treepointersis a sorted list of pointersvaluesis a sorted list of values
The segment tree object has the same properties as the arguments.
Retrieves the value index
indexis the coordinate of the value to retrieve
Returns The value at index
Sets the value at index to v
indexis the index to updatevis the new value
Returns v
Returns a slice of the segment tree. Same semantics as Array.slice
beginis the start of the intervalendis the end of the interval
Returns A 1-level deep slice of the segment tree
Unpacks the segment tree into an array
arraygets the output. If not specified a new array is allocated
Returns array
Create an empty segment tree
sizeis the size of the new segment tree
Returns A new segment tree
Encodes an array into a segment tree
arrayis the array to encode
Returns A new segment tree encoding array
Encodes an array into a segment tree, except instead of using [] to access the array it uses .get()
arrayis the array to encode
Returns A new segment tree encoding array
(c) 2013 Mikola Lysenko. MIT License