diet

Discrete Interval Encoding Trees

diet
data-structure
moon add CAIMEOX/diet@0.1.3
Download zip
Author
Version
0.1.3
License
Apache-2.0
Last updated
9 months ago
Downloads
32
README

#DIET: Discrete Interval Encoding Trees for MoonBit

The DIET (Discrete Interval Encoding Tree) library provides an efficient set representation for continuous ranges of values. This data structure is particularly useful when you need to represent large sets of sequential data while maintaining memory efficiency.

#Overview

A DIET represents sets as intervals rather than individual elements. For example, instead of storing [1, 2, 3, 4, 5] as five separate values, it stores them as a single interval (1, 5). This approach provides significant memory savings when working with large, dense sets of ordered data.

#Usage Examples

  • Creating a DIET:

// Create an empty set
let empty = @diet.empty()

// Create a set with a single element
let singleton = @diet.singleton('a')

// Create a set with an interval
let digits = @diet.interval('0', '9')

// Create from an array
let letters = @diet.of(['a', 'b', 'c', 'd', 'e'])

  • Set operations:

// Union of two sets
let combined = @diet.union(set1, set2)

// Intersection of two sets
let common = @diet.intersection(set1, set2)

// Difference between sets
let difference = @diet.difference(set1, set2)

// Complement of a set
let complement = @diet.complement(set)

  • Element Operations:

// Add an element
let with_element = set.add(5)

// Remove an element
let without_element = set.remove(5)

// Check if an element exists
let exists = set.contains(5)

More other operations are available, including checking if a set is empty, getting the size of a set, and iterating over elements. Check the mooncakes DIET documentation for a complete list of methods.

#Supported Types

The library includes built-in support for:

  • Char - for character ranges
  • Int - for integer ranges You can extend support to custom types by implementing the BoundedEnum trait.

#
BoundedEnum

pub(open) trait BoundedEnum : Compare {
pred(Self) -> Self
succ(Self) -> Self
lower_bound() -> Self
upper_bound() -> Self
}

impl BoundedEnum for Char
impl BoundedEnum for Int

#
Tree

#alias(T)
type Tree[T]

impl Compare for Tree[N]
impl Default for Tree[N]
impl Eq for Tree[N]
impl Hash for Tree[N]

#
Tree::add

fn[N : BoundedEnum + Compare + Eq] Tree::add(self : Tree[N], n : N) -> Tree[N]

Adds a single value to a discrete interval encoding tree.

#
Tree::complement

fn[N : BoundedEnum + Compare + Eq] Tree::complement(self : Tree[N]) -> Tree[N]

Computes the complement of a discrete interval encoding tree.

#
Tree::contains

fn[N : BoundedEnum + Compare + Eq] Tree::contains(self : Tree[N], x : N) -> Bool

Determines if a discrete interval encoding tree contains a specific value.

#
Tree::fold_ranges

fn[N, A] Tree::fold_ranges(self : Tree[N], init~ : A, f : (A, N, N) -> A) -> A

Applies a function to each interval in the tree and accumulates the results.

#
Tree::intersection

fn[N : BoundedEnum + Compare + Eq] Tree::intersection(self : Tree[N], t2 : Tree[N]) -> Tree[N]

Computes the intersection of two discrete interval encoding trees.

#
Tree::is_empty

fn[N] Tree::is_empty(self : Tree[N]) -> Bool

Checks if a tree is empty.

#
Tree::iter

fn[N : BoundedEnum + Compare + Eq] Tree::iter(self : Tree[N]) -> Iter[N]
Create a new iterator over the elements represent by the tree

#
Tree::iter_intervals

fn[N] Tree::iter_intervals(self : Tree[N]) -> Iter[(N, N)]
Create a new iterator over intervals

#
Tree::remove

fn[N : BoundedEnum + Compare + Eq] Tree::remove(self : Tree[N], n : N) -> Tree[N]

Removes a single value from a discrete interval encoding tree.

#
Tree::slice

fn[N : BoundedEnum + Compare + Eq] Tree::slice(self : Tree[N], min? : N, max? : N) -> Tree[N]
Returns a tree containing all elements within the specified range.

#
Tree::slice_after

fn[N : BoundedEnum + Compare + Eq] Tree::slice_after(self : Tree[N], x : N) -> Tree[N]
Returns a tree containing all elements strictly greater than x.

#
Tree::slice_before

fn[N : BoundedEnum + Compare + Eq] Tree::slice_before(self : Tree[N], x : N) -> Tree[N]
Returns a tree containing all elements strictly less than x.

#
Tree::slice_from

fn[N : BoundedEnum + Compare + Eq] Tree::slice_from(self : Tree[N], x : N) -> Tree[N]
Returns a tree containing all elements greater than or equal to x.

#
Tree::slice_until

fn[N : BoundedEnum + Compare + Eq] Tree::slice_until(self : Tree[N], x : N) -> Tree[N]
Returns a tree containing all elements less than or equal to x.

#
Tree::union

fn[N : BoundedEnum + Compare + Eq] Tree::union(self : Tree[N], t2 : Tree[N]) -> Tree[N]

Computes the union of two discrete interval encoding trees.

#
difference

fn[N : BoundedEnum + Compare + Eq] difference(t1 : Tree[N], t2 : Tree[N]) -> Tree[N]

Computes the difference between two discrete interval encoding trees.

#
empty

fn[N] empty() -> Tree[N]
Create an empty tree

#
interval

fn[N] interval(min : N, max : N) -> Tree[N]
Create a tree with a single interval

#
is_subset

fn[N : BoundedEnum + Compare + Eq] is_subset(s1 : Tree[N], s2 : Tree[N]) -> Bool

Checks if one discrete interval encoding tree is a subset of another.
fn[N : BoundedEnum + Compare + Eq] of(array : Array[N]) -> Tree[N]
Converts an array of elements into a tree

#
of_view

fn[N : BoundedEnum + Compare + Eq] of_view(view : ArrayView[N]) -> Tree[N]
Converts an array view into a tree

#
singleton

fn[N] singleton(x : N) -> Tree[N]
Create a tree with a single element

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io