forked from cfallin/rust-smallset
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partially implemented some of the easier functions from here servo/rust-smallvec#220 (comment)
- Loading branch information
1 parent
6c9eaa3
commit 25d3f6f
Showing
2 changed files
with
57 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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> { | ||
|
@@ -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 { | ||
|
@@ -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), | ||
|
@@ -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(); | ||
|