Git Product home page Git Product logo

fenwick-tree-rs's Introduction

fenwick-tree

crates.io Build

An implementation of the binary indexed tree (or Fenwick tree) data structure in Rust.

Overview

fenwick-tree provides a simple implementation of the Fenwick tree that can be used as a building block in some algorithms like weighted random.

The basic API is simple and consists of add and sum methods (both take O(log n) time). Here is a quick example:

use fenwick_tree::*;

let mut tree = FenwickTree::<i32>::with_len(5);

for i in 0..5 {
    tree.add(i, i as i32)?;
}

assert_eq!(tree.sum(..)?, 0 + 1 + 2 + 3 + 4);
assert_eq!(tree.sum(1..)?, 1 + 2 + 3 + 4);
assert_eq!(tree.sum(2..5)?, 2 + 3 + 4);
assert_eq!(tree.sum(3..=4)?, 3 + 4);

Panics

Both add and sum methods return Result and are not expected to panic. However, with_len constructs the backing vector by using vec![I::default(); len], and it actually may panic as in regular Rust code.

Learn more

For more details see documentation.

License

The package is licensed under the MIT license.

Contributing

There are no strict rules for contributing. Feel free to open an issue or submit a pull request.

fenwick-tree-rs's People

Contributors

joshua-light avatar vadim2404 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

vadim2404

fenwick-tree-rs's Issues

Ranges usage improvement

Currently, sum method accepts Range. This makes calling the method pretty nice and clean, although the caller is limited to x..y syntax. It'd be nice if sum could accept x..=y and other kinds of ranges specified in the reference.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.