fenwick

Fenwick tree implementation in MoonBit

fenwick-tree
data-structure
segment-tree
moon add CAIMEOX/fenwick@0.1.1
Download zip
Author
Version
0.1.1
License
Apache-2.0
Last updated
10 months ago
Downloads
27
README

#Fenwick Tree & Segment Tree Library

A MoonBit library implementing efficient data structures for range query operations and point updates.

#Overview

This library provides two powerful data structures:

  1. Fenwick Tree (Binary Indexed Tree) - Efficient for prefix sums and point updates in O(log n) time
  2. Segment Tree

#Fenwick Tree

A Fenwick Tree (also known as Binary Indexed Tree) is a data structure that provides efficient methods for:

  • Computing prefix sums in an array
  • Updating values in the array
  • Querying sum of elements in a range

All operations run in O(log n) time complexity.

#Usage

// Create a new Fenwick Tree of size 10
let tree = @fenwick.FenwickTree::new(10)

// Update values (1-indexed)
tree.update(1, 5) // Add 5 to position 1
tree.update(3, 2) // Add 2 to position 3

// Get prefix sums
let sum = tree.prefix(3) // Sum of elements from 1 to 3

// Get range sums
let range_sum = tree.range(1, 3) // Sum of elements from 1 to 3

// Get/Set individual values
let val = tree.get(3) // Get value at position 3
tree.set(3, 10) // Set position 3 to value 10

#Segment Tree

A Segment Tree is a more versatile data structure that allows for:

  • Range queries in O(log n) time
  • Point updates in O(log n) time
  • More complex operations than Fenwick trees
  • Immutable data structure

#Usage

// Segment trees operate on ranges
let tree = @fenwick.SegTree::empty() // Create an empty segment tree

// Update and query operations
let updated_tree = tree.update(5, 10) // Add 10 at index 5
let value = updated_tree.get(5) // Get value at index 5
let new_tree = updated_tree.set(5, 20) // Set index 5 to value 20

#Bits Implementation

This library includes a custom infinite bits representation for efficient bit manipulation operations, powering both the Fenwick Tree and Segment Tree implementations.

#Bit Operations

  • Basic operations: increment, decrement, shift left/right
  • Logical operations: AND, OR, NOT
  • Bit testing and manipulation: set/clear bits, test odd/even
  • Specialized operations for tree traversal

#Tutorials

There is an article that describes in detail the design methodology of this library and how to derive fenwick trees from segment trees and infinite bit representations:

#
Bit

type Bit

impl Eq for Bit

#
Bit::from_enum

fn Bit::from_enum(self : Bit) -> Int

#
Bit::not

fn Bit::not(self : Bit) -> Bit

#
Bit::to_enum

fn Bit::to_enum(i : Int) -> Bit

#
Bits

type Bits

Infinite bit representation
impl Add for Bits
impl BitAnd for Bits
impl Eq for Bits
impl Neg for Bits
impl Show for Bits

#
Bits::active_parent_binary

fn Bits::active_parent_binary(self : Bits) -> Bits

Finds the active parent in a binary representation.

Parameters

  • self: The bits to analyze.

Returns

The active parent bits.

#
Bits::at_lsb

fn Bits::at_lsb(self : Bits, f : (Bits) -> Bits) -> Bits

Applies a function at the least significant bit position.

Parameters

  • self: The bits to operate on.
  • f: The function to apply at the least significant bit.

Returns

The resulting bits after applying the function.

#
Bits::clear

fn Bits::clear(self : Bits, idx : Int) -> Bits

#
Bits::dec

fn Bits::dec(self : Bits) -> Bits

#
Bits::even

fn Bits::even(self : Bits) -> Bool

#
Bits::from_bits

fn Bits::from_bits(self : Bits) -> Int

#
Bits::inc

fn Bits::inc(self : Bits) -> Bits

#
Bits::inv

fn Bits::inv(self : Bits) -> Bits

#
Bits::lsb

fn Bits::lsb(self : Bits) -> Bits

#
Bits::make

fn Bits::make(self : Bits, b : Bit) -> Bits

#
Bits::odd

fn Bits::odd(self : Bits) -> Bool

#
Bits::pat_match

fn Bits::pat_match(self : Bits) -> (Bits, Bit)

#
Bits::prev_segment_binary

fn Bits::prev_segment_binary(self : Bits) -> Bits

Finds the previous segment in a binary representation.

Parameters

  • self: The bits to analyze.

Returns

The previous segment bits.

#
Bits::set

fn Bits::set(self : Bits, idx : Int) -> Bits

#
Bits::set_to

fn Bits::set_to(self : Bits, idx : Int, b1 : Bit) -> Bits

#
Bits::shl

fn Bits::shl(self : Bits) -> Bits

#
Bits::shr

fn Bits::shr(self : Bits) -> Bits

#
Bits::to_bits

fn Bits::to_bits(n : Int) -> Bits

#
Bits::to_snoc

fn Bits::to_snoc(self : Bits) -> Bits

#
FenwickTree

type FenwickTree

A Fenwick Tree (also known as a Binary Indexed Tree) is a data structure that provides efficient methods for prefix sums and point updates in an array.

#
FenwickTree::get

fn FenwickTree::get(self : FenwickTree, i : Int) -> Int

Gets the value at a specific index.

Arguments

  • i: The index to query (1-indexed)

Returns

The value at position i

#
FenwickTree::new

fn FenwickTree::new(len : Int) -> FenwickTree

Creates a new Fenwick Tree with the specified length.

Arguments

  • len: The size of the Fenwick Tree

Returns

A new FenwickTree instance

#
FenwickTree::prefix

fn FenwickTree::prefix(self : FenwickTree, i : Int) -> Int

Computes the sum of elements from index 1 to i.

Arguments

  • i: The upper bound of the range (inclusive)

Returns

The sum of all elements in positions 1 to i

#
FenwickTree::range

fn FenwickTree::range(self : FenwickTree, i : Int, j : Int) -> Int

Computes the sum of elements from index i to j (inclusive).

Arguments

  • i: The lower bound of the range
  • j: The upper bound of the range

Returns

The sum of elements in positions i through j

#
FenwickTree::set

fn FenwickTree::set(self : FenwickTree, i : Int, value : Int) -> Unit

Sets the value at a specific index.

Arguments

  • i: The index to update (1-indexed)
  • value: The new value to set

#
FenwickTree::update

fn FenwickTree::update(self : FenwickTree, i : Int, delta : Int) -> Unit

Adds a value to the element at position i and updates the tree accordingly.

Arguments

  • i: The index to update (1-indexed)
  • delta: The value to add to the current value

#
SegTree

type SegTree

Represents a segment tree, a tree data structure used for storing information about intervals, or segments.
  • Empty: Represents an empty segment tree.
  • Branch(Int, Range, SegTree, SegTree): A node in the segment tree with a value, a range, and two children.
impl Eq for SegTree
impl Show for SegTree

#
SegTree::empty

fn SegTree::empty() -> SegTree

Creates an empty segment tree.

#
SegTree::get

fn SegTree::get(self : SegTree, i : Int) -> Int

Gets the value at a specific index in the segment tree.

Parameters

  • self: The segment tree to query.
  • i: The index to get the value from.

Returns

The value at the specified index.

#
SegTree::new

fn SegTree::new(rng : (Int, Int)) -> SegTree

Creates a new segment tree with the specified range.

#
SegTree::set

fn SegTree::set(self : SegTree, i : Int, v : Int) -> SegTree

Sets the value at a specific index in the segment tree.

Parameters

  • self: The segment tree to update.
  • i: The index to set.
  • v: The new value to set at the index.

Returns

A new segment tree with the updated value.

#
SegTree::update

fn SegTree::update(self : SegTree, i : Int, v : Int) -> SegTree

Updates the value at a specific index in the segment tree.

Parameters

  • self: The segment tree to update.
  • i: The index to update.
  • v: The value to add to the current value at the index.

Returns

A new segment tree with the updated value.
fn b(i : Int) ->
List
[Int]

Helper function to create a binary tree structure represented as a list of integers.

Parameters

  • i: The depth of the tree to generate.

Returns

A list representing the binary tree structure.

#
b2f

fn b2f(n : Int, se : Bits) -> Bits

Converts from binary representation back to the original form.

Parameters

  • n: The size parameter.
  • se: The bits to convert.

Returns

The converted bits.

#
f2b

fn f2b(n : Int, se : Bits) -> Bits

Converts from one binary representation to another using shifting.

Parameters

  • n: The size parameter.
  • se: The bits to convert.

Returns

The converted bits.

#
interleave

Interleaves two lists together, taking elements alternately.

Parameters

  • sel: The first list.
  • other: The second list.

Returns

A new list with elements interleaved from both input lists.

#
shift

fn shift(n : Int, se : Bits) -> Bits

Shifts bits based on a specific condition and position.

Parameters

  • n: The position to set in the bits.
  • se: The bits to shift.

Returns

The shifted bits.

#
unshift

fn unshift(n : Int, se : Bits) -> Bits

Reverses the shift operation on bits.

Parameters

  • n: The position to clear in the bits.
  • se: The bits to unshift.

Returns

The unshifted bits.

#
while_

fn[A] while_(p : (A) -> Bool, f : (A) -> A, x : A) -> A

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io