GithubHelp home page GithubHelp logo

sparseset's Introduction

sparseset

Build Status Documentation

A Sparse Set implemention in rust.

A sparse set is a specialized data structure for representing a set of integers. It can be useful in some very narrow and specific cases, namely when the universe of possible values is very large but used very sparingly and the set is iterated often or cleared often.

In this implementation the SparseSet can hold an arbitrary value for every integer (key) in the set.

Use it with Cargo

[dependencies]
sparseset = "1.0.1"

Example

use sparseset::SparseSet;
let mut set = SparseSet::with_capacity(128);
set.insert(42, 3);
set.insert(77, 5);
set.insert(23, 8);

assert_eq!(*set.get(42).unwrap(), 3);

set.remove(42);
assert!(!set.get(42).is_some());

for entry in set {
    println!("- {} => {}", entry.key(), entry.value);
}

Performance

Note that SparseSet is incredibly inefficient in terms of space. The O(1) insertion time assumes space for the element is already allocated. Otherwise, a large key may require a massive reallocation, with no direct relation to the number of elements in the collection. SparseSet should only be seriously considered for small keys.

Runtime complexity

See how the runtime complexity of SparseSet compares to Hash and Btree maps:

get insert remove iterate clear
SparseSet O(1) O(1)* O(1) O(n) O(1) / O(n)*
HashMap O(1)~ O(1)~* O(1)~ N/A N/A
BTreeMap O(log n) O(log n) O(log n) N/A N/A
  • Clear is O(1) on simple types and O(n) on types whom implements Drop.
  • Iterating is really efficient, its iterating over a dense array. In fact, its even possible to get an (even mutable) slice of the entries in the set.

See http://research.swtch.com/sparse for more details.

sparseset's People

Contributors

bombela avatar erikwdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

sparseset's Issues

Sparse set cannot hold an arbitrary value

dense array is essential part of sparse set and cannot hold any arbitrary value. It must contain an value that verifies the index obtained from sparce array, because the index obtained from sparse array might be just garbage. Only in that case the contains method can be properly implemented.

This test indicates the problem:

#[test]
fn contains_reset() {
    let mut s = SparseSet::with_capacity(5);
    s.insert(1, ());

    s.clear();
    s.insert(5, ());
    assert!(s.contains(1) == false);
}

Implementation Bug?

It seems that the library has an implementation bug. When removing the last element in the dense array, an array out of bounds error is triggered. I think the line below is the issue:

if self.len() > 0 {

I think the correct condition in the if statement should be:
if dense_idx < self.len()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.