Skip to content

Commit 25d3f6f

Browse files
Implemented some helper functions
Partially implemented some of the easier functions from here servo/rust-smallvec#220 (comment)
1 parent 6c9eaa3 commit 25d3f6f

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ documentation = "https://cfallin.github.io/rust-smallset/smallset/"
88
license = "MIT"
99

1010
[dependencies]
11-
smallvec = "0.1"
11+
smallvec = "1.4.1"

src/lib.rs

+56-14
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
// Copyright (c) 2016 Chris Fallin <[email protected]>. Released under the MIT license.
55
//
66

7+
extern crate smallvec;
8+
9+
use std::borrow::Borrow;
710
use std::fmt;
11+
use std::hash::Hash;
812
use std::iter::{FromIterator, IntoIterator};
913
use std::slice::Iter;
1014

11-
extern crate smallvec;
1215
use smallvec::{Array, SmallVec};
1316

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

4650
impl<A: Array> SmallSet<A>
47-
where
48-
A::Item: PartialEq + Eq,
51+
where
52+
A::Item: PartialEq + Eq,
4953
{
5054
/// Creates a new, empty `SmallSet`.
5155
pub fn new() -> SmallSet<A> {
@@ -98,11 +102,48 @@ where
98102
pub fn clear(&mut self) {
99103
self.elements.clear();
100104
}
105+
106+
//
107+
pub fn get(&self, value: &A::Item) -> Option<&A::Item> {
108+
self.elements.iter().find(|x| (value).eq(&x))
109+
}
110+
111+
pub fn take(&mut self, value: &A::Item) -> Option<A::Item> {
112+
if let Some(pos) = self.elements.iter().position(|e| *e == *value) {
113+
let result = self.elements.remove(pos);
114+
Some(result)
115+
} else {
116+
None
117+
}
118+
}
119+
120+
// Adds a value to the set, replacing the existing value, if any, that is equal to the given one. Returns the replaced value.
121+
pub fn replace(&mut self, value: A::Item) -> Option<A::Item> {
122+
if let Some(pos) = self.elements.iter().position(|e| *e == value) {
123+
let result = self.elements.remove(pos);
124+
self.elements.insert(pos, value);
125+
Some(result)
126+
} else {
127+
None
128+
}
129+
}
130+
131+
pub fn drain<R>(&mut self, range: R) -> smallvec::Drain<A>
132+
where
133+
R: core::ops::RangeBounds<usize>,
134+
{
135+
self.elements.drain(range)
136+
}
137+
138+
pub fn retain<F>(&mut self, f: F)
139+
where F: FnMut(&mut A::Item) -> bool {
140+
self.elements.retain(f)
141+
}
101142
}
102143

103144
impl<A: Array> Clone for SmallSet<A>
104-
where
105-
A::Item: PartialEq + Eq + Clone,
145+
where
146+
A::Item: PartialEq + Eq + Clone,
106147
{
107148
fn clone(&self) -> SmallSet<A> {
108149
SmallSet {
@@ -112,21 +153,21 @@ where
112153
}
113154

114155
impl<A: Array> fmt::Debug for SmallSet<A>
115-
where
116-
A::Item: PartialEq + Eq + fmt::Debug,
156+
where
157+
A::Item: PartialEq + Eq + fmt::Debug,
117158
{
118159
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
119160
self.elements.fmt(f)
120161
}
121162
}
122163

123164
impl<A: Array> FromIterator<A::Item> for SmallSet<A>
124-
where
125-
A::Item: PartialEq + Eq,
165+
where
166+
A::Item: PartialEq + Eq,
126167
{
127168
fn from_iter<T>(iter: T) -> Self
128-
where
129-
T: IntoIterator<Item = A::Item>,
169+
where
170+
T: IntoIterator<Item=A::Item>,
130171
{
131172
SmallSet {
132173
elements: SmallVec::from_iter(iter),
@@ -136,9 +177,10 @@ where
136177

137178
#[cfg(test)]
138179
mod test {
139-
use super::*;
140180
use std::fmt::Write;
141181

182+
use super::*;
183+
142184
#[test]
143185
fn test_basic_set() {
144186
let mut s: SmallSet<[u32; 2]> = SmallSet::new();

0 commit comments

Comments
 (0)