npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

s-tree

v0.1.1

Published

Segment Tree data structure for storing intervals or segments. It allows querying which of the stored segments contain a given point.

Downloads

9

Readme

s-tree

Node.js module implementation of Segment Tree data structure for storing intervals, or segments. It allows querying which of the stored segments contain a given point.

Example

var stree = require('s-tree');

stree(function(tree) {
  
  tree
    .push(5, 10, 'foo')
    .push(8, 14, 'bar')
    .push(12, 16)
  .build();

  tree.query({ start: 10, end: 12 }, function(intervals) {
    ...
  });

});

Install

npm install s-tree

API

push(start, end, [data])

Store a interval (start, end) with a optional data parameter.

  • start Start point of interval (number)
  • end End point of interval (number)
  • data Optinal key value (e.g. id, label or even an object) for future reference (object)

Returns object which is a reference to the tree itself` (Chain Pattern)

Example:

var stree = require('s-tree');

stree(function(tree) {
  ...
  tree.push(5, 10);
  tree.push(5, 10, 'foo');
  ...
});

build()

Build the segment tree data structure.

Returns object which is a reference to the tree itself` (Chain Pattern)

var stree = require('s-tree');

stree(function(tree) {
  ...
  tree.build();
  ...
});

query({ start, end, [endpoints], [concurrency] }, callback)

Query for intervals which overlap a given endpoint (start, end).

  • options:
    • start Start point of interval (number)
    • end End point of interval (number)
    • endpoints optional parameter to indicate whether or not endpoints should be included on interval comparison (boolean, default: true)
    • concurrency optional parameter to indicate whether or not the query should look only for concurrent intervals (boolean, default: false)
  • callback The callback function which provides the queried intervals (e.g. function(intervals) {...})

Returns number of intervals

Example:

var stree = require('s-tree');

stree(function(tree) {
  ...
  tree.query({ start: 5, end: 10 }, function(intervals) {
    ...
  });
  tree.query({ start: 5, end: 10, endpoints: false }, function(intervals) {
    ...
  });
  tree.query({ start: 5, end: 10, concurrency: true }, function(intervals) {
    ...
  });

  var numberOfIntrevals = tree.query({ start: 5, end: 10 });
  ...
});

query({ start, end, [endpoints], [concurrency] }, callback)

Query for array of intervals which overlap the given endpoints ([start], [end]).

  • options:
    • start Start points of intervals (array)
    • end End points of intervals (array)
    • endpoints optional parameter to indicate whether or not endpoints should be included on interval comparison (boolean, default: true)
    • concurrency optional parameter to indicate whether or not the query should look only for concurrent intervals (boolean, default: false)
  • callback The callback function which provides the queried intervals (e.g. function(intervals) {...})

Returns number of intervals

Example:

var stree = require('s-tree');

stree(function(tree) {
  ...
  tree.query({ start: [5, 10], end: [8, 14] }, function(intervals) {
    ...
  });
  tree.query({ start: [5, 10], end: [8, 14], endpoints: false }, function(intervals) {
    ...
  });
  tree.query({ start: [5, 10], end: [8, 14], concurrency: true }, function(intervals) {
    ...
  });

  var numberOfIntrevals = tree.query({ start: [5, 10], end: [8, 14] });
  ...
});

query({ point, [endpoints], [concurrency] }, callback)

Query for intervals which overlaps a given point (point).

  • options:
    • point Point where intervals overlap (number)
    • endpoints optional parameter to indicate whether or not endpoints should be included on interval comparison (boolean, default: true)
    • concurrency optional parameter to indicate whether or not the query should look only for concurrent intervals (boolean, default: false)
  • callback The callback function which provides the queried intervals (e.g. function(intervals) {...})

Returns number of intervals

Example:

var stree = require('s-tree');

stree(function(tree) {
  ...
  tree.query({ point: 5 }, function(intervals) {
    ...
  });
  tree.query({ point: 5, endpoints: false }, function(intervals) {
    ...
  });
  tree.query({ point: 5, concurrency: true }, function(intervals) {
    ...
  });

  var numberOfIntrevals = tree.query({ point: 5 });
  ...
});

query({ points, [endpoints], [concurrency] }, callback)

Query for intervals which overlaps the given points (points).

  • options:
    • points Points where intervals overlap (array)
    • endpoints optional parameter to indicate whether or not endpoints should be included on interval comparison (boolean, default: true)
    • concurrency optional parameter to indicate whether or not the query should look only for concurrent intervals (boolean, default: false)
  • callback The callback function which provides the queried intervals (e.g. function(intervals) {...})

Returns number of intervals

Example:

var stree = require('s-tree');

stree(function(tree) {
  ...
  tree.query({ points: [5, 10, 25] }, function(intervals) {
    ...
  });
  tree.query({ points: [5, 10, 25], endpoints: false }, function(intervals) {
    ...
  });
  tree.query({ points: [5, 10, 25], concurrency: true }, function(intervals) {
    ...
  });

  var numberOfIntrevals = tree.query({ points: [5, 10, 25] });
  ...
});

clear()

Remove intervals from the data structure.

Returns object which is a reference to the tree itself` (Chain Pattern)

Example:

var stree = require('s-tree');

stree(function(tree) {
  ...
  tree.clear();
  ...
});

hasIntervals()

Indicates whether or not there are intervals in the data structure.

Returns Boolean

Example:

var stree = require('s-tree');

stree(function(tree) {
  ...
  if (tree.hasIntervals()) {...};
  ...
});