Skip to content

A small rust library to describe setlike objects

Notifications You must be signed in to change notification settings

emallson/setlike

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

setlike

A small rust library to describe setlike objects.

[dependencies]
setlike = {version = "0.1", git = "https://github.com/emallson/setlike.git"}

The fundamental purpose of this library is to provide a way to abstract over the most basic set operations: containment. Constructing unions, differences, and intersections has problems in the current rust type system (some of which will be resolved when we get impl Trait return types). However, being able to swap out set-like types is extremely useful because it allows me to quickly switch the set implementation I use to something that matches my workload.

Thus, Setlike is born! The entire crate is just the trait Setlike and implementations for HashSet, BTreeSet and BitSet.

Usage

Want a function that can take any set-like instance? Easy!

extern crate setlike;
use setlike::Setlike;
fn hug<S: Setlike<usize>>(a: &S, b:&S, val: usize) -> bool {
    b.contains(&val) && a.contains(&val)
}

Want something that you can also iterator over? Combine Setlike with an IntoIterator trait:

extern crate setlike;
use setlike::Setlike;

/// Fake counting the intersection size. Definitely not the most efficient way to do this if you know the exact set type.
fn intersection_size<'a, E: 'a, S, T>(a: &S, b: &S) -> usize
    where S: Setlike<E> + 'a,
          T: Setlike<E> + 'a,
          &'a S: IntoIterator<&'a E> {
    let mut count = 0;
    for el in &a {
        if b.contains(el) {
            count += 1;
        }
    }
    count
}

License

Copyright 2017 J. David Smith

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

About

A small rust library to describe setlike objects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages