A memory efficient, garbage-collector-friendly, linked list for allocation of fixed-size, indexed ranges.
Using npm
:
$ npm install rangemap
Using 'bower´:
$ bower install rangemap
In your web project, include the file rangemap.js
.
In Node.js:
var RangeMap = require('rangemap').RangeMap;
var map = new RangeMap(0xffff);
In Webbrowser using an AMD loader:
var RangeMap = require('rangemap').RangeMap;
var map = new RangeMap(0xffff);
In Webbrowser without an AMD loader:
<script src="rangemap.js"></script>
<script>
var map = new RangeMap(0xffff);
</script>
A RangeMap
has the following methods, members.
startOrLength
The range start (ifend
is present) else length of the map (start is then set to 0).end
The range end.options
{Object}useAllocPool
{Boolean} Use an object pool when creating new Segment object instances. Default=falsedefragAfterClear
{Boolean} De-frags (merges) empty (null) ranges after clear is issued. Default=false
Represents the start position of the RangeMap
.
Represents the end position of the RangeMap
.
Represents the total number of indicies in the RangeMap
.
Represents the total number of unallocated indicies in the RangeMap
.
Represents the total number of Range objects in the RangeMap
.
Indicates if the object pool is should be used when creating new RangeMapSegment
object instances.
Indicates that segments should be de-fraged after the clear
method is issued.
start
{Number} The start of the range.end
{Number} The end of the range.
The RangeMap
class is automatically initialized in constructor, so no need to call init
on a newly created RangeMap. The init
can however after the destroy
method is called.
length
{Number} Number of inidices that should be allocatedvalue
{Object} A value associated with the range.- return {[Segment]} An Array with
RangeMapSegment
instance's representing the newly allocated range.
Allocates a new range of size length
. The alloc
method tries to keep the allocation as little fragmented as possible, but there is now guarantee that the ranges is in one sequence.
value
{Object} A value associated with the range.- return {Number} The number of unallocations that was made (null ranges are not included).
Deallocates all ranges of value
.
start
{Number} The start position.end
{Number} The end position.- return {Number} The number of unallocations that was made (null ranges are not included).
Deallocates all segments between start
and end
, with a value.
value
{Object} The value to validate against.start
{Number} The start position.end
{Number} The end position. Default=rangemap.end- return {Boolean}
true
if defined range matchesvalue
, elsefalse
.
Validates that specified value
is within defined range.
pos
{Number} The position in range- return {Object} The
value
of specifiedpos
.
Returns the value
associated with specified pos
.
value
{Number} Thevalue
to find.- return {Array} array with all matching range segments.
Finds all range segments of given value
.
Destroys the RangeMap instance and free's all range segments.
A RangeMapSegment
, which represents a seqment in a RangeMap
, has the following methods, members.
(static) Inidicates how many objects that should be stored in the allocation pool. Default=1000
Represents the start position of the RangeMapSegment
.
Represents the end position of the RangeMapSegment
.
Represents the total number of indicies in the RangeMapSegment
.
There are a number of unit-tests in the package. The easiest way is to run the test suite via npm
in the repository:
$ npm test
This library is compatible with all major browsers.
Please report any issue on github: https://github.com/jfd/rangemap-js/issues
RangeMap.js is licensed under the MIT license. See LICENSE in this repo for more information.
Copyright (c) 2013 Johan Dahlberg http://jfd.github.com