4
4
// Copyright (c) 2016 Chris Fallin <[email protected] >. Released under the MIT license.
5
5
//
6
6
7
+ extern crate smallvec;
8
+
9
+ use std:: borrow:: Borrow ;
7
10
use std:: fmt;
11
+ use std:: hash:: Hash ;
8
12
use std:: iter:: { FromIterator , IntoIterator } ;
9
13
use std:: slice:: Iter ;
10
14
11
- extern crate smallvec;
12
15
use smallvec:: { Array , SmallVec } ;
13
16
17
+
14
18
/// A `SmallSet` is an unordered set of elements. It is designed to work best
15
19
/// for very small sets (no more than ten or so elements). In order to support
16
20
/// small sets very efficiently, it stores elements in a simple unordered array.
@@ -37,15 +41,15 @@ use smallvec::{Array, SmallVec};
37
41
/// assert!(s.contains(&1));
38
42
/// ```
39
43
pub struct SmallSet < A : Array >
40
- where
41
- A :: Item : PartialEq + Eq ,
44
+ where
45
+ A :: Item : PartialEq + Eq ,
42
46
{
43
47
elements : SmallVec < A > ,
44
48
}
45
49
46
50
impl < A : Array > SmallSet < A >
47
- where
48
- A :: Item : PartialEq + Eq ,
51
+ where
52
+ A :: Item : PartialEq + Eq ,
49
53
{
50
54
/// Creates a new, empty `SmallSet`.
51
55
pub fn new ( ) -> SmallSet < A > {
@@ -98,11 +102,48 @@ where
98
102
pub fn clear ( & mut self ) {
99
103
self . elements . clear ( ) ;
100
104
}
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
+ }
101
142
}
102
143
103
144
impl < A : Array > Clone for SmallSet < A >
104
- where
105
- A :: Item : PartialEq + Eq + Clone ,
145
+ where
146
+ A :: Item : PartialEq + Eq + Clone ,
106
147
{
107
148
fn clone ( & self ) -> SmallSet < A > {
108
149
SmallSet {
@@ -112,21 +153,21 @@ where
112
153
}
113
154
114
155
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 ,
117
158
{
118
159
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
119
160
self . elements . fmt ( f)
120
161
}
121
162
}
122
163
123
164
impl < A : Array > FromIterator < A :: Item > for SmallSet < A >
124
- where
125
- A :: Item : PartialEq + Eq ,
165
+ where
166
+ A :: Item : PartialEq + Eq ,
126
167
{
127
168
fn from_iter < T > ( iter : T ) -> Self
128
- where
129
- T : IntoIterator < Item = A :: Item > ,
169
+ where
170
+ T : IntoIterator < Item = A :: Item > ,
130
171
{
131
172
SmallSet {
132
173
elements : SmallVec :: from_iter ( iter) ,
@@ -136,9 +177,10 @@ where
136
177
137
178
#[ cfg( test) ]
138
179
mod test {
139
- use super :: * ;
140
180
use std:: fmt:: Write ;
141
181
182
+ use super :: * ;
183
+
142
184
#[ test]
143
185
fn test_basic_set ( ) {
144
186
let mut s: SmallSet < [ u32 ; 2 ] > = SmallSet :: new ( ) ;
0 commit comments