Skip to content

Commit

Permalink
Implemented some helper functions
Browse files Browse the repository at this point in the history
Partially implemented some of the easier functions from here
servo/rust-smallvec#220 (comment)
  • Loading branch information
[email protected] authored and [email protected] committed Aug 7, 2020
1 parent 6c9eaa3 commit 25d3f6f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ documentation = "https://cfallin.github.io/rust-smallset/smallset/"
license = "MIT"

[dependencies]
smallvec = "0.1"
smallvec = "1.4.1"
70 changes: 56 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
// Copyright (c) 2016 Chris Fallin <[email protected]>. Released under the MIT license.
//

extern crate smallvec;

use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;
use std::iter::{FromIterator, IntoIterator};
use std::slice::Iter;

extern crate smallvec;
use smallvec::{Array, SmallVec};


/// A `SmallSet` is an unordered set of elements. It is designed to work best
/// for very small sets (no more than ten or so elements). In order to support
/// small sets very efficiently, it stores elements in a simple unordered array.
Expand All @@ -37,15 +41,15 @@ use smallvec::{Array, SmallVec};
/// assert!(s.contains(&1));
/// ```
pub struct SmallSet<A: Array>
where
A::Item: PartialEq + Eq,
where
A::Item: PartialEq + Eq,
{
elements: SmallVec<A>,
}

impl<A: Array> SmallSet<A>
where
A::Item: PartialEq + Eq,
where
A::Item: PartialEq + Eq,
{
/// Creates a new, empty `SmallSet`.
pub fn new() -> SmallSet<A> {
Expand Down Expand Up @@ -98,11 +102,48 @@ where
pub fn clear(&mut self) {
self.elements.clear();
}

//
pub fn get(&self, value: &A::Item) -> Option<&A::Item> {
self.elements.iter().find(|x| (value).eq(&x))
}

pub fn take(&mut self, value: &A::Item) -> Option<A::Item> {
if let Some(pos) = self.elements.iter().position(|e| *e == *value) {
let result = self.elements.remove(pos);
Some(result)
} else {
None
}
}

// Adds a value to the set, replacing the existing value, if any, that is equal to the given one. Returns the replaced value.
pub fn replace(&mut self, value: A::Item) -> Option<A::Item> {
if let Some(pos) = self.elements.iter().position(|e| *e == value) {
let result = self.elements.remove(pos);
self.elements.insert(pos, value);
Some(result)
} else {
None
}
}

pub fn drain<R>(&mut self, range: R) -> smallvec::Drain<A>
where
R: core::ops::RangeBounds<usize>,
{
self.elements.drain(range)
}

pub fn retain<F>(&mut self, f: F)
where F: FnMut(&mut A::Item) -> bool {
self.elements.retain(f)
}
}

impl<A: Array> Clone for SmallSet<A>
where
A::Item: PartialEq + Eq + Clone,
where
A::Item: PartialEq + Eq + Clone,
{
fn clone(&self) -> SmallSet<A> {
SmallSet {
Expand All @@ -112,21 +153,21 @@ where
}

impl<A: Array> fmt::Debug for SmallSet<A>
where
A::Item: PartialEq + Eq + fmt::Debug,
where
A::Item: PartialEq + Eq + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.elements.fmt(f)
}
}

impl<A: Array> FromIterator<A::Item> for SmallSet<A>
where
A::Item: PartialEq + Eq,
where
A::Item: PartialEq + Eq,
{
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = A::Item>,
where
T: IntoIterator<Item=A::Item>,
{
SmallSet {
elements: SmallVec::from_iter(iter),
Expand All @@ -136,9 +177,10 @@ where

#[cfg(test)]
mod test {
use super::*;
use std::fmt::Write;

use super::*;

#[test]
fn test_basic_set() {
let mut s: SmallSet<[u32; 2]> = SmallSet::new();
Expand Down

0 comments on commit 25d3f6f

Please sign in to comment.