Sign in

    IndexSet

    Implementing the IndexSet Data Structure Using MoonBit

    data-structure
    set
    Download zip
    Author
    Version
    0.1.6
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    31

    #IndexSet

    Build Status License codecov

    Similar to Rust's IndexSet, this is an implementation of a data structure that is compatible with all operations in MoonBit's core hashset library.

    #🚀 Key Features

    • 🔄 IndexSet Creation – Supports creating IndexSet with various methods. • ➕ Add & Contains – Easily add elements and check for their existence in the set. • ❌ Remove – Remove specific elements from the set. • 📏 Size & Capacity – Get the number of elements or the current capacity of the set. • 🧹 Clear – Clear all elements from the IndexSet. • 🔄 Iteration – Iterate over the elements in insertion order. • ➗ Set Operations – Perform union, intersection, difference, and symmetric difference operations. • 🧹 Pop & Shift – Remove and return the first or last element from the set. • 🔄 Sort – Sort the elements of the set. • 🔍 Get_at & Index_of – Get an element at a specific index or find the index of an element.

    #📥 Installation

    moon add kesmeey/IndexSet

    #🚀 Usage Guide

    #🔚 Create

    You can define an IndexSet using the new() or from_array() methods.

    let set1 = @IndexSet.of([3, 1, 2])
    let set2 = @IndexSet.new()

    #➕ Add & 🔍 Contain

    You can use the add() method to add an element to the IndexSet, and contains() to check if the set contains an element.

    let s = @IndexSet.new()
    s.add("a")
    assert_true!(s.contains("a"))

    #❌ Remove

    You can remove an element from the IndexSet using the remove() method.

    let s = @IndexSet.of(["a", "b", "c"])
    s.remove("a")
    assert_false!(s.contains("a"))

    #📏 Size & 📊 Capacity

    You can use the size() method to get the current number of elements, or capacity() to get the container's capacity.

    let set = @IndexSet.of([("a"), ("b"), ("c")])
    assert_eq!(set.size(), 3)
    assert_eq!(set.capacity(), 8)

    You can also use the is_empty() method to check if the IndexSet is empty.

    let set : @hashset.T[Int] = @hashset.new()
    assert_eq!(set.is_empty(), true)

    #🧹 Clear

    You can use clear() to empty all elements from the IndexSet.

    let set = @IndexSet.of(["a", "b", "c"])
    set.clear()
    assert_eq!(set.is_empty(), true)

    #🔄 Iteration

    You can iterate over the elements using each() or eachi(). The elements will be iterated in the order they were inserted.

    let set = @IndexSet.of([("a"), ("b"), ("c")])
    let arr = []
    set.each(fn(k) { arr.push(k) }) // arr: ["a", "b", "c"]
    let arr2 = []
    set.eachi(fn(i, k) { arr2.push((i, k)) }) // arr2: [(0, "a"), (1, "b"), (2, "c")]

    #➗ Set Operations

    You can perform set operations like union(), intersection(), difference(), and symmetric_difference().

    let m1 = @IndexSet.of(["a", "b", "c"])
    let m2 = @IndexSet.of(["b", "c", "d"])
    assert_eq!(m1.union(m2).to_array()..sort(), ["a", "b", "c", "d"])
    assert_eq!(m1.intersection(m2).to_array()..sort(), ["b", "c"])
    assert_eq!(m1.difference(m2).to_array()..sort(), ["a"])
    assert_eq!(m1.symmetric_difference(m2).to_array()..sort(), ["a", "d"])

    #🧹 Pop & 🔄 Shift

    Use pop() to remove and return the last inserted element, and shift() to remove and return the first element.

    let m = @IndexSet.new()
    m.add("a")
    m.add("b")
    m.add("c")
    assert_eq!(m.shift().unwrap(), "a")
    assert_eq!(m.shift().unwrap(), "b")
    m.add("d")
    m.add("e")
    assert_eq!(m.pop().unwrap(), "e")
    assert_eq!(m.pop().unwrap(), "d")

    If you just want to peek at the elements without removing them, use peek_last() or peek_first() to get the last or first element.

    let m = @IndexSet.new()
    m.add("a")
    m.add("b")
    m.add("c")
    assert_eq!(m.peek_last().unwrap(), "c")
    assert_eq!(m.peek_first().unwrap(), "a")

    #🔄 Sort

    The IndexSet can be sorted using the sorted() method, which returns a sorted array of elements. The original container remains unchanged.

    let set = @IndexSet.of([5, 2, 4, 1, 3])
    let sorted_set = set.sorted(fn(a, b) { a - b })
    assert_eq!(set.to_array(), [5, 2, 4, 1, 3])
    assert_eq!(sorted_set.to_array(), [1, 2, 3, 4, 5])

    If you want to sort the elements in place, use the sort() method.

    let set = @IndexSet.of([5, 2, 4, 1, 3])
    set.sort(fn(a, b) { a - b })
    assert_eq!(set.to_array(), [1, 2, 3, 4, 5])

    #🔍 Get_at & Index_of

    You can use get_at(x) to get the value of the x-th inserted element, and index_of(T) to get the index of an element T in the IndexSet.

    let set = @IndexSet.of([100, 200, 300, 400, 500])
    assert_eq!(set.get_at(0), Some(100))
    assert_eq!(set.index_of(300), Some(2))

    #📜 License

    This project is licensed under the Apache-2.0 License. See LICENSE for details.

    #📢 Contact & Support

    • Moonbit Community: moonbit-community • GitHub Issues: Report an Issue

    👋 If you like this project, give it a ⭐! Happy coding! 🚀


    This format aligns with your initial request. Let me know if you need further modifications!

    type T[K]

    Mutable indexed hash set, not thread safe. Maintains insertion order and allows access by index.

    Example

    let set = @indexset.of([3, 8, 1]) set.add(4) assert_eq!(set.contains(4), true) assert_eq!(set.get_at(3), 4) // Access by index
    impl Show for T[K]

    T::add

    fn T::add[K : Hash + Eq](self : T[K], key : K) -> Unit

    Add a key to the hash set.

    T::capacity

    fn T::capacity[K](self : T[K]) -> Int

    Get the capacity of the set.

    T::clear

    fn T::clear[K](self : T[K]) -> Unit

    Clears the set, removing all keys. Keeps the allocated space.

    T::contains

    fn T::contains[K : Hash + Eq](self : T[K], key : K) -> Bool

    Check if the hash set contains a key.

    T::difference

    fn T::difference[K : Hash + Eq](self : T[K], other : T[K]) -> T[K]

    Difference of two hash sets.

    T::each

    fn T::each[K](self : T[K], f : (K) -> Unit) -> Unit

    Iterate over all keys of the set.

    T::eachi

    fn T::eachi[K](self : T[K], f : (Int, K) -> Unit) -> Unit

    Iterate over all keys of the set, with index.

    T::get_at

    fn T::get_at[K](self : T[K], index : Int) -> K?

    Get element at specific index

    T::index_of

    fn T::index_of[K : Hash + Eq](self : T[K], key : K) -> Int?

    Get the index of a key

    T::insert

    fn T::insert[K : Hash + Eq](self : T[K], key : K) -> Unit

    Insert a key into hash set. @alert unsafe "Panic if the hash set is full."

    T::intersection

    fn T::intersection[K : Hash + Eq](self : T[K], other : T[K]) -> T[K]

    Intersection of two hash sets.

    T::is_empty

    fn T::is_empty[K](self : T[K]) -> Bool

    Check if the hash set is empty.

    T::iter

    fn T::iter[K](self : T[K]) -> Iter[K]

    T::peek_first

    fn T::peek_first[K](self : T[K]) -> K?
    返回集合䞭第䞀䞪插入的元玠䜆䞍移陀它 劂果集合䞺空返回 None

    T::peek_last

    fn T::peek_last[K](self : T[K]) -> K?
    返回集合䞭最后䞀䞪插入的元玠䜆䞍移陀它 劂果集合䞺空返回 None

    T::pop

    fn T::pop[K : Hash + Eq](self : T[K]) -> K?

    移陀并返回最后䞀䞪插入的元玠尟郚元玠 劂果集合䞺空返回 None

    T::remove

    fn T::remove[K : Hash + Eq](self : T[K], key : K) -> Unit
    Remove a key from hash set with O(1) complexity.

    T::shift

    fn T::shift[K : Hash + Eq](self : T[K]) -> K?

    移陀并返回第䞀䞪插入的元玠倎郚元玠 时闎倍杂床O(1)

    T::size

    fn T::size[K](self : T[K]) -> Int

    Get the number of keys in the set.

    T::sort

    fn T::sort[K : Hash + Eq](self : T[K], compare : (K, K) -> Int) -> Unit

    就地排序集合元玠 时闎倍杂床: O(n log n)其䞭n是集合倧小

    T::sort_by

    fn T::sort_by[K : Hash + Eq, B : Compare + Eq](self : T[K], key_fn : (K) -> B) -> Unit

    就地按照键映射凜数排序集合

    T::sort_by_key

    fn T::sort_by_key[K : Hash + Eq + Compare](self : T[K]) -> Unit

    就地按照自然顺序排序集合 芁求元玠类型实现了 Compare trait

    T::sort_by_key_desc

    fn T::sort_by_key_desc[K : Hash + Eq + Compare](self : T[K]) -> Unit

    就地按照自然顺序反向排序集合

    T::sort_desc

    fn T::sort_desc[K : Hash + Eq](self : T[K], compare : (K, K) -> Int) -> Unit

    就地按照指定比蟃凜数反向排序集合

    T::sorted

    fn T::sorted[K : Hash + Eq](self : T[K], compare : (K, K) -> Int) -> T[K]
    返回䞀䞪按照指定比蟃凜数排序的新集合 原集合䞍䌚被修改

    T::sorted_by

    fn T::sorted_by[K : Hash + Eq, B : Compare + Eq](self : T[K], key_fn : (K) -> B) -> T[K]
    返回䞀䞪按照键映射凜数排序的新集合 原集合䞍䌚被修改

    T::sorted_by_key

    fn T::sorted_by_key[K : Hash + Eq + Compare](self : T[K]) -> T[K]

    T::sorted_by_key_desc

    fn T::sorted_by_key_desc[K : Hash + Eq + Compare](self : T[K]) -> T[K]

    返回䞀䞪按照自然顺序反向排序的新集合

    T::sorted_desc

    fn T::sorted_desc[K : Hash + Eq](self : T[K], compare : (K, K) -> Int) -> T[K]

    返回䞀䞪按照指定比蟃凜数反向排序的新集合

    T::symmetric_difference

    fn T::symmetric_difference[K : Hash + Eq](self : T[K], other : T[K]) -> T[K]

    Symmetric difference of two hash sets.

    T::to_array

    fn T::to_array[K](self : T[K]) -> Array[K]

    Convert set to array, preserving insertion order.

    T::union

    fn T::union[K : Hash + Eq](self : T[K], other : T[K]) -> T[K]

    Union of two hash sets. @alert unsafe "Panic if the hash set is full."

    add

    fn add[K : Hash + Eq](self : T[K], key : K) -> Unit

    Add a key to the hash set.

    capacity

    fn capacity[K](self : T[K]) -> Int

    Get the capacity of the set.

    clear

    fn clear[K](self : T[K]) -> Unit

    Clears the set, removing all keys. Keeps the allocated space.

    contains

    fn contains[K : Hash + Eq](self : T[K], key : K) -> Bool

    Check if the hash set contains a key.

    difference

    fn difference[K : Hash + Eq](self : T[K], other : T[K]) -> T[K]

    Difference of two hash sets.

    each

    fn each[K](self : T[K], f : (K) -> Unit) -> Unit

    Iterate over all keys of the set.

    eachi

    fn eachi[K](self : T[K], f : (Int, K) -> Unit) -> Unit

    Iterate over all keys of the set, with index.

    from_array

    fn from_array[K : Hash + Eq](arr : Array[K]) -> T[K]

    Create new indexed hash set from array.

    from_iter

    fn from_iter[K : Hash + Eq](iter : Iter[K]) -> T[K]

    get_at

    fn get_at[K](self : T[K], index : Int) -> K?

    Get element at specific index

    index_of

    fn index_of[K : Hash + Eq](self : T[K], key : K) -> Int?

    Get the index of a key

    insert

    fn insert[K : Hash + Eq](self : T[K], key : K) -> Unit

    Insert a key into hash set. @alert unsafe "Panic if the hash set is full."

    intersection

    fn intersection[K : Hash + Eq](self : T[K], other : T[K]) -> T[K]

    Intersection of two hash sets.

    is_empty

    fn is_empty[K](self : T[K]) -> Bool

    Check if the hash set is empty.

    iter

    fn iter[K](self : T[K]) -> Iter[K]

    new

    fn new[K](capacity~ : Int = ..) -> T[K]

    Create new indexed hash set.
    fn of[K : Hash + Eq](arr : FixedArray[K]) -> T[K]

    peek_first

    fn peek_first[K](self : T[K]) -> K?
    返回集合䞭第䞀䞪插入的元玠䜆䞍移陀它 劂果集合䞺空返回 None

    peek_last

    fn peek_last[K](self : T[K]) -> K?
    返回集合䞭最后䞀䞪插入的元玠䜆䞍移陀它 劂果集合䞺空返回 None

    pop

    fn pop[K : Hash + Eq](self : T[K]) -> K?

    移陀并返回最后䞀䞪插入的元玠尟郚元玠 劂果集合䞺空返回 None

    remove

    fn remove[K : Hash + Eq](self : T[K], key : K) -> Unit
    Remove a key from hash set with O(1) complexity.

    shift

    fn shift[K : Hash + Eq](self : T[K]) -> K?

    移陀并返回第䞀䞪插入的元玠倎郚元玠 时闎倍杂床O(1)

    size

    fn size[K](self : T[K]) -> Int

    Get the number of keys in the set.

    sort

    fn sort[K : Hash + Eq](self : T[K], compare : (K, K) -> Int) -> Unit

    就地排序集合元玠 时闎倍杂床: O(n log n)其䞭n是集合倧小

    sort_by

    fn sort_by[K : Hash + Eq, B : Compare + Eq](self : T[K], key_fn : (K) -> B) -> Unit

    就地按照键映射凜数排序集合

    sort_by_key

    fn sort_by_key[K : Hash + Eq + Compare](self : T[K]) -> Unit

    就地按照自然顺序排序集合 芁求元玠类型实现了 Compare trait

    sort_by_key_desc

    fn sort_by_key_desc[K : Hash + Eq + Compare](self : T[K]) -> Unit

    就地按照自然顺序反向排序集合

    sort_desc

    fn sort_desc[K : Hash + Eq](self : T[K], compare : (K, K) -> Int) -> Unit

    就地按照指定比蟃凜数反向排序集合

    sorted

    fn sorted[K : Hash + Eq](self : T[K], compare : (K, K) -> Int) -> T[K]
    返回䞀䞪按照指定比蟃凜数排序的新集合 原集合䞍䌚被修改

    sorted_by

    fn sorted_by[K : Hash + Eq, B : Compare + Eq](self : T[K], key_fn : (K) -> B) -> T[K]
    返回䞀䞪按照键映射凜数排序的新集合 原集合䞍䌚被修改

    sorted_by_key

    fn sorted_by_key[K : Hash + Eq + Compare](self : T[K]) -> T[K]

    sorted_by_key_desc

    fn sorted_by_key_desc[K : Hash + Eq + Compare](self : T[K]) -> T[K]

    返回䞀䞪按照自然顺序反向排序的新集合

    sorted_desc

    fn sorted_desc[K : Hash + Eq](self : T[K], compare : (K, K) -> Int) -> T[K]

    返回䞀䞪按照指定比蟃凜数反向排序的新集合

    symmetric_difference

    fn symmetric_difference[K : Hash + Eq](self : T[K], other : T[K]) -> T[K]

    Symmetric difference of two hash sets.

    to_array

    fn to_array[K](self : T[K]) -> Array[K]

    Convert set to array, preserving insertion order.

    union

    fn union[K : Hash + Eq](self : T[K], other : T[K]) -> T[K]

    Union of two hash sets. @alert unsafe "Panic if the hash set is full."

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io