Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pysegmenttree

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pysegmenttree

Segment Tree for python 3+

  • 0.2.0
  • PyPI
  • Socket score

Maintainers
1

pysegmenttree

GitHub license CI codecov

Segment tree is a data structure to perform efficient range queries over an array.

Properties of the segment tree with the size N.

OperationTime complexity
buildO(N)
queryO(Log[N])
updateO(Log[N])

Key features

  • Implements classical data structure to deal with interval queries.
  • Includes two classes IntSegmentTree and FloatSegmentTree implemented in pure C. They can boost performance up to 20x and are used by default for simple data types if possible.

Installation

$ pip install pysegmenttree

Basic usage

>> from pysegmenttree import stree

# Build the tree
# 'sum' function is used by default
>> tree = stree([5, 1, 9, 4, 5, 11])

# Find sum on the interval [1, 4)
>> tree.query(1, 4)
14

# Set element with index 3 to 6
>> tree.update(3, 6)
>> tree.query(1, 4)
16

Advanced usage

There are three predefined query functions available that can be used with int or float trees. Use them as follows:

>> from pysegmenttree import stree, QueryFunction
>> tree = stree([5, 1, 9, 4, 5, 11], func=QueryFunction.MIN)

# Find min on the interval [1, 4)
>> tree.query(1, 4)
1

Plain python functions are also suitable, but in this case c-extensions will not be used.

>> tree = stree([5, 1, 9, 4, 5, 11], func=min)
>> tree.query(1, 4)
1

Example with user-defined class.

>> from pysegmenttree import stree
>> from pysegmenttree.test_utils import Vec2D
# List of 2D vectors
>> tree = stree([Vec2D(0, 1), Vec2D(5, -2), Vec2D(-2, 3)], func=max)
# Find the vector of maximum length on the interval [0, 2)
>> tree.query(0, 2)

Vec2D(x=5, y=-2)

Docs

Docs are available here.

Perfomance

Three basic segment tree operations were benchmarked for three different types int, float and Vec2D. I included results for 3 other python segment trees libraries for comparison. All code related to benchmarking can be found in benchmarks subdirectory.

init

ParamValue
Tree size100 000

query

ParamValue
Tree size100 000
Queries performed10 000

update

ParamValue
Tree size100 000
Updates performed10 000

Development

Read more here.

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc