Skip to content

Sparse sets with efficient traversal, joining, sorting, etc.

License

Notifications You must be signed in to change notification settings

jabolopes/go-sparseset

Repository files navigation

go-sparseset

PkgGoDev

This library provides an implementation of sparse sets and utilities to operate on sparse sets, including, efficient traversal, joining, sorting, etc.

Values are stored contiguously in memory therefore traversing a set is particularly fast because it takes advantage of the underlying CPU caching.

// Construction
set := sparseset.New[string](4096, 1<<20)
*set.Add(id) = value
...

// Properties.
length := set.Length()

// Removal.
set.Remove(id)

// Lookup.
if value, ok := set.Get(id); ok {
  // Do something with value...
} else {
  // Set does not contain id...
}

// Traversal.
for iterator := sparseset.Iterate(set); ; {
  key, value, ok := iterator.Next()
  if !ok {
    break
  }

  // Do something with key and value...
}

// Joining 2 sets.
for iterator := sparseset.Join(set1, set2); ; {
  key, value1, value2, ok := iterator.Next()
  if !ok {
    break
  }

  // Do something with key, value1, and value2...
}

// Joining 3 sets.
for iterator := sparseset.Join3(set1, set2, set3); ; {
  key, value1, value2, value3, ok := iterator.Next()
  if !ok {
    break
  }

  // Do something with key, value1, value2, and value3...
}

Releases

No releases published

Packages

No packages published

Languages