skipset

package
v0.0.0-...-2605483 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package skipset implements a sorted set based on skip lists.

SkipSet is a collection type that stores unique elements in a skip list structure, providing ordered iteration and efficient set operations. All operations run in O(log n) average time. Internally, SkipSet is implemented using SkipMap with empty structs as values.

Time Complexity

  • Insert: O(log n) average
  • Delete: O(log n) average
  • Contains: O(log n) average
  • Union/Intersection/Difference: O(m log(m+n)) average where m, n are set sizes
  • Traversal: O(n)

Features

  • Ordered storage with sorted element iteration (IterAsc, IterDesc)
  • Efficient range queries with configurable boundaries (RangeAsc, RangeDesc)
  • Standard set operations: Union, Intersection, Difference, SymmetricDifference
  • Subset/superset checks: IsSubsetOf, IsSupersetOf
  • Generic type support for any cmp.Ordered element type
  • Simpler implementation compared to BTreeSet

Usage

SkipSet is simpler than BTreeSet and often faster for smaller datasets:

// For cmp.Ordered elements (recommended)
s := skipset.NewOrdered[string]()
s.Insert("apple")
s.Insert("banana")

// Check existence
if s.Contains("apple") {
    fmt.Println("Found apple")
}

Set Operations

SkipSet provides rich set operations:

set1 := skipset.NewOrdered[int]()
set1.Insert(1, 2, 3)

set2 := skipset.NewOrdered[int]()
set2.Insert(2, 3, 4)

union := set1.Union(set2)           // {1, 2, 3, 4}
intersection := set1.Intersection(set2) // {2, 3}
difference := set1.Difference(set2)    // {1}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SkipSet

type SkipSet[E any] struct {
	// contains filtered or unexported fields
}

func New

func New[E any](comparator func(E, E) int) *SkipSet[E]

New creates a new empty SkipSet using the specified key comparison function. Parameters:

  • comparator: Function for comparing keys, must not be nil. Should return a negative number when a < b, 0 when a == b, and a positive number when a > b.

Returns:

  • Pointer to the newly created SkipSet

func NewOrdered

func NewOrdered[E cmp.Ordered]() *SkipSet[E]

NewOrdered creates a new empty SkipSet for element types that support ordered comparison. This is a convenience function that uses cmp.Compare as the comparator. Type Parameters:

  • E: Element type, must implement the cmp.Ordered interface

Returns:

  • Pointer to the newly created SkipSet

func (*SkipSet[E]) Clear

func (ss *SkipSet[E]) Clear()

Clear removes all elements from the set, making it empty.

func (*SkipSet[E]) Clone

func (ss *SkipSet[E]) Clone() *SkipSet[E]

Clone creates a deep copy of the set. Returns:

  • A new SkipSet containing all the same elements

func (*SkipSet[E]) Contains

func (ss *SkipSet[E]) Contains(key E) bool

Contains checks if the set contains the specified element. Parameters:

  • key: The element to check

Returns:

  • true if the element exists, false otherwise

func (*SkipSet[E]) Difference

func (ss *SkipSet[E]) Difference(other *SkipSet[E]) *SkipSet[E]

Difference computes the difference between the current set and another set, returning a new set containing elements in the current set but not in the other set. Parameters:

Returns:

  • New SkipSet containing elements that exist in the current set but not in the other set

func (*SkipSet[E]) Equal

func (ss *SkipSet[E]) Equal(other *SkipSet[E]) bool

Equal checks if the current set is equal to another set (contains the same elements). Parameters:

Returns:

  • true if the two sets contain the same elements
  • false otherwise

func (*SkipSet[E]) Extend

func (ss *SkipSet[E]) Extend(it iter.Seq[E])

Extend inserts all elements from the iterator into the set.

Parameters:

  • it: An iterator yielding elements to insert.

func (*SkipSet[E]) First

func (ss *SkipSet[E]) First() (E, bool)

First returns the first (smallest) element in the set. Returns:

  • The element and true if the set is not empty
  • Zero value and false if the set is empty

func (*SkipSet[E]) Insert

func (ss *SkipSet[E]) Insert(key E) bool

Insert adds an element to the set. Parameters:

  • key: The element to add

Returns:

  • true if the element was newly added (didn't exist before)
  • false if the element already existed

func (*SkipSet[E]) Intersection

func (ss *SkipSet[E]) Intersection(other *SkipSet[E]) *SkipSet[E]

Intersection computes the intersection of the current set and another set, returning a new set containing only elements present in both sets. Parameters:

Returns:

  • New SkipSet containing elements that exist in both the current set and the other set

func (*SkipSet[E]) IsDisjoint

func (ss *SkipSet[E]) IsDisjoint(other *SkipSet[E]) bool

IsDisjoint checks if the current set and another set are disjoint (have no elements in common). Parameters:

Returns:

  • true if the two sets have no common elements
  • false otherwise

func (*SkipSet[E]) IsEmpty

func (ss *SkipSet[E]) IsEmpty() bool

IsEmpty checks if the set is empty (contains no elements). Returns:

  • true if the set is empty, false otherwise

func (*SkipSet[E]) IsSubset

func (ss *SkipSet[E]) IsSubset(other *SkipSet[E]) bool

IsSubset checks if the current set is a subset of another set. Parameters:

Returns:

  • true if all elements of the current set are in the other set
  • false otherwise

func (*SkipSet[E]) IsSuperset

func (ss *SkipSet[E]) IsSuperset(other *SkipSet[E]) bool

IsSuperset checks if the current set is a superset of another set. Parameters:

Returns:

  • true if all elements of the other set are in the current set
  • false otherwise

func (*SkipSet[E]) IterAsc

func (ss *SkipSet[E]) IterAsc() iter.Seq[E]

IterAsc returns an iterator over all elements in ascending order.

Returns:

  • An iter.Seq[E] that yields elements in ascending order.

Time Complexity: O(n)

func (*SkipSet[E]) IterDesc

func (ss *SkipSet[E]) IterDesc() iter.Seq[E]

IterDesc returns an iterator over all elements in descending order.

Returns:

  • An iter.Seq[E] that yields elements in descending order.

Time Complexity: O(n)

func (*SkipSet[E]) Last

func (ss *SkipSet[E]) Last() (E, bool)

Last returns the last (largest) element in the set. Returns:

  • The element and true if the set is not empty
  • Zero value and false if the set is empty

func (*SkipSet[E]) Len

func (ss *SkipSet[E]) Len() int

Len returns the number of elements in the set. Returns:

  • The number of elements in the set

func (*SkipSet[E]) PopFirst

func (ss *SkipSet[E]) PopFirst() (E, bool)

PopFirst removes and returns the first (smallest) element in the set. Returns:

  • The removed element and true if the set is not empty
  • Zero value and false if the set is empty

func (*SkipSet[E]) PopLast

func (ss *SkipSet[E]) PopLast() (E, bool)

PopLast removes and returns the last (largest) element in the set. Returns:

  • The removed element and true if the set is not empty
  • Zero value and false if the set is empty

func (*SkipSet[E]) RangeAsc

func (ss *SkipSet[E]) RangeAsc(bounds bound.RangeBounds[E]) iter.Seq[E]

RangeAsc returns an iterator over elements within the given bounds in ascending order.

Parameters:

  • bounds: The range bounds specifying the lower and upper limits.

Returns:

  • An iter.Seq[E] that yields elements in ascending order within the bounds.

Time Complexity: O(log n + k) where k is the number of elements in the range.

func (*SkipSet[E]) RangeDesc

func (ss *SkipSet[E]) RangeDesc(bounds bound.RangeBounds[E]) iter.Seq[E]

RangeDesc returns an iterator over elements within the given bounds in descending order.

Parameters:

  • bounds: The range bounds specifying the lower and upper limits.

Returns:

  • An iter.Seq[E] that yields elements in descending order within the bounds.

Time Complexity: O(log n + k) where k is the number of elements in the range.

func (*SkipSet[E]) Remove

func (ss *SkipSet[E]) Remove(key E) bool

Remove removes the specified element from the set. Parameters:

  • key: The element to remove

Returns:

  • true if the element existed and was removed
  • false if the element didn't exist

func (*SkipSet[E]) SymmetricDifference

func (ss *SkipSet[E]) SymmetricDifference(other *SkipSet[E]) *SkipSet[E]

SymmetricDifference computes the symmetric difference between the current set and another set, returning a new set containing elements that are in either set but not in both. Parameters:

Returns:

  • New SkipSet containing elements that are in either set but not in both sets simultaneously

func (*SkipSet[E]) Union

func (ss *SkipSet[E]) Union(other *SkipSet[E]) *SkipSet[E]

Union computes the union of the current set and another set, returning a new set containing all unique elements. Parameters:

Returns:

  • New SkipSet containing all elements from the current set and the other set

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL