@@ -27,33 +27,30 @@ use rustc_serialize;
27
27
///
28
28
/// The representation is dense, using one bit per possible element.
29
29
#[ derive( Eq , PartialEq ) ]
30
- pub struct IdxSetBuf < T : Idx > {
30
+ pub struct IdxSet < T : Idx > {
31
31
_pd : PhantomData < fn ( & T ) > ,
32
32
bits : Vec < Word > ,
33
33
}
34
34
35
- // FIXME: temporary
36
- pub type IdxSet < T > = IdxSetBuf < T > ;
37
-
38
- impl < T : Idx > Clone for IdxSetBuf < T > {
35
+ impl < T : Idx > Clone for IdxSet < T > {
39
36
fn clone ( & self ) -> Self {
40
- IdxSetBuf { _pd : PhantomData , bits : self . bits . clone ( ) }
37
+ IdxSet { _pd : PhantomData , bits : self . bits . clone ( ) }
41
38
}
42
39
}
43
40
44
- impl < T : Idx > rustc_serialize:: Encodable for IdxSetBuf < T > {
41
+ impl < T : Idx > rustc_serialize:: Encodable for IdxSet < T > {
45
42
fn encode < E : rustc_serialize:: Encoder > ( & self ,
46
43
encoder : & mut E )
47
44
-> Result < ( ) , E :: Error > {
48
45
self . bits . encode ( encoder)
49
46
}
50
47
}
51
48
52
- impl < T : Idx > rustc_serialize:: Decodable for IdxSetBuf < T > {
53
- fn decode < D : rustc_serialize:: Decoder > ( d : & mut D ) -> Result < IdxSetBuf < T > , D :: Error > {
49
+ impl < T : Idx > rustc_serialize:: Decodable for IdxSet < T > {
50
+ fn decode < D : rustc_serialize:: Decoder > ( d : & mut D ) -> Result < IdxSet < T > , D :: Error > {
54
51
let words: Vec < Word > = rustc_serialize:: Decodable :: decode ( d) ?;
55
52
56
- Ok ( IdxSetBuf {
53
+ Ok ( IdxSet {
57
54
_pd : PhantomData ,
58
55
bits : words,
59
56
} )
@@ -62,18 +59,18 @@ impl<T: Idx> rustc_serialize::Decodable for IdxSetBuf<T> {
62
59
63
60
const BITS_PER_WORD : usize = mem:: size_of :: < Word > ( ) * 8 ;
64
61
65
- impl < T : Idx > fmt:: Debug for IdxSetBuf < T > {
62
+ impl < T : Idx > fmt:: Debug for IdxSet < T > {
66
63
fn fmt ( & self , w : & mut fmt:: Formatter ) -> fmt:: Result {
67
64
w. debug_list ( )
68
65
. entries ( self . iter ( ) )
69
66
. finish ( )
70
67
}
71
68
}
72
69
73
- impl < T : Idx > IdxSetBuf < T > {
70
+ impl < T : Idx > IdxSet < T > {
74
71
fn new ( init : Word , universe_size : usize ) -> Self {
75
72
let num_words = ( universe_size + ( BITS_PER_WORD - 1 ) ) / BITS_PER_WORD ;
76
- IdxSetBuf {
73
+ IdxSet {
77
74
_pd : Default :: default ( ) ,
78
75
bits : vec ! [ init; num_words] ,
79
76
}
@@ -92,13 +89,13 @@ impl<T: Idx> IdxSetBuf<T> {
92
89
}
93
90
94
91
/// Duplicates as a hybrid set.
95
- pub fn to_hybrid ( & self ) -> HybridIdxSetBuf < T > {
92
+ pub fn to_hybrid ( & self ) -> HybridIdxSet < T > {
96
93
// This universe_size may be slightly larger than the one specified
97
94
// upon creation, due to rounding up to a whole word. That's ok.
98
95
let universe_size = self . bits . len ( ) * BITS_PER_WORD ;
99
96
100
97
// Note: we currently don't bother trying to make a Sparse set.
101
- HybridIdxSetBuf :: Dense ( self . to_owned ( ) , universe_size)
98
+ HybridIdxSet :: Dense ( self . to_owned ( ) , universe_size)
102
99
}
103
100
104
101
/// Removes all elements
@@ -171,20 +168,20 @@ impl<T: Idx> IdxSetBuf<T> {
171
168
bitwise ( self . words_mut ( ) , other. words ( ) , & Union )
172
169
}
173
170
174
- /// Like `union()`, but takes a `SparseIdxSetBuf ` argument.
175
- fn union_sparse ( & mut self , other : & SparseIdxSetBuf < T > ) -> bool {
171
+ /// Like `union()`, but takes a `SparseIdxSet ` argument.
172
+ fn union_sparse ( & mut self , other : & SparseIdxSet < T > ) -> bool {
176
173
let mut changed = false ;
177
174
for elem in other. iter ( ) {
178
175
changed |= self . add ( & elem) ;
179
176
}
180
177
changed
181
178
}
182
179
183
- /// Like `union()`, but takes a `HybridIdxSetBuf ` argument.
184
- pub fn union_hybrid ( & mut self , other : & HybridIdxSetBuf < T > ) -> bool {
180
+ /// Like `union()`, but takes a `HybridIdxSet ` argument.
181
+ pub fn union_hybrid ( & mut self , other : & HybridIdxSet < T > ) -> bool {
185
182
match other {
186
- HybridIdxSetBuf :: Sparse ( sparse, _) => self . union_sparse ( sparse) ,
187
- HybridIdxSetBuf :: Dense ( dense, _) => self . union ( dense) ,
183
+ HybridIdxSet :: Sparse ( sparse, _) => self . union_sparse ( sparse) ,
184
+ HybridIdxSet :: Dense ( dense, _) => self . union ( dense) ,
188
185
}
189
186
}
190
187
@@ -194,20 +191,20 @@ impl<T: Idx> IdxSetBuf<T> {
194
191
bitwise ( self . words_mut ( ) , other. words ( ) , & Subtract )
195
192
}
196
193
197
- /// Like `subtract()`, but takes a `SparseIdxSetBuf ` argument.
198
- fn subtract_sparse ( & mut self , other : & SparseIdxSetBuf < T > ) -> bool {
194
+ /// Like `subtract()`, but takes a `SparseIdxSet ` argument.
195
+ fn subtract_sparse ( & mut self , other : & SparseIdxSet < T > ) -> bool {
199
196
let mut changed = false ;
200
197
for elem in other. iter ( ) {
201
198
changed |= self . remove ( & elem) ;
202
199
}
203
200
changed
204
201
}
205
202
206
- /// Like `subtract()`, but takes a `HybridIdxSetBuf ` argument.
207
- pub fn subtract_hybrid ( & mut self , other : & HybridIdxSetBuf < T > ) -> bool {
203
+ /// Like `subtract()`, but takes a `HybridIdxSet ` argument.
204
+ pub fn subtract_hybrid ( & mut self , other : & HybridIdxSet < T > ) -> bool {
208
205
match other {
209
- HybridIdxSetBuf :: Sparse ( sparse, _) => self . subtract_sparse ( sparse) ,
210
- HybridIdxSetBuf :: Dense ( dense, _) => self . subtract ( dense) ,
206
+ HybridIdxSet :: Sparse ( sparse, _) => self . subtract_sparse ( sparse) ,
207
+ HybridIdxSet :: Dense ( dense, _) => self . subtract ( dense) ,
211
208
}
212
209
}
213
210
@@ -255,15 +252,15 @@ impl<'a, T: Idx> Iterator for Iter<'a, T> {
255
252
const SPARSE_MAX : usize = 8 ;
256
253
257
254
/// A sparse index set with a maximum of SPARSE_MAX elements. Used by
258
- /// HybridIdxSetBuf ; do not use directly.
255
+ /// HybridIdxSet ; do not use directly.
259
256
///
260
257
/// The elements are stored as an unsorted vector with no duplicates.
261
258
#[ derive( Clone , Debug ) ]
262
- pub struct SparseIdxSetBuf < T : Idx > ( ArrayVec < [ T ; SPARSE_MAX ] > ) ;
259
+ pub struct SparseIdxSet < T : Idx > ( ArrayVec < [ T ; SPARSE_MAX ] > ) ;
263
260
264
- impl < T : Idx > SparseIdxSetBuf < T > {
261
+ impl < T : Idx > SparseIdxSet < T > {
265
262
fn new ( ) -> Self {
266
- SparseIdxSetBuf ( ArrayVec :: new ( ) )
263
+ SparseIdxSet ( ArrayVec :: new ( ) )
267
264
}
268
265
269
266
fn len ( & self ) -> usize {
@@ -296,8 +293,8 @@ impl<T: Idx> SparseIdxSetBuf<T> {
296
293
}
297
294
}
298
295
299
- fn to_dense ( & self , universe_size : usize ) -> IdxSetBuf < T > {
300
- let mut dense = IdxSetBuf :: new_empty ( universe_size) ;
296
+ fn to_dense ( & self , universe_size : usize ) -> IdxSet < T > {
297
+ let mut dense = IdxSet :: new_empty ( universe_size) ;
301
298
for elem in self . 0 . iter ( ) {
302
299
dense. add ( elem) ;
303
300
}
@@ -323,97 +320,97 @@ impl<'a, T: Idx> Iterator for SparseIter<'a, T> {
323
320
}
324
321
}
325
322
326
- /// Like IdxSetBuf , but with a hybrid representation: sparse when there are few
323
+ /// Like IdxSet , but with a hybrid representation: sparse when there are few
327
324
/// elements in the set, but dense when there are many. It's especially
328
325
/// efficient for sets that typically have a small number of elements, but a
329
326
/// large `universe_size`, and are cleared frequently.
330
327
#[ derive( Clone , Debug ) ]
331
- pub enum HybridIdxSetBuf < T : Idx > {
332
- Sparse ( SparseIdxSetBuf < T > , usize ) ,
333
- Dense ( IdxSetBuf < T > , usize ) ,
328
+ pub enum HybridIdxSet < T : Idx > {
329
+ Sparse ( SparseIdxSet < T > , usize ) ,
330
+ Dense ( IdxSet < T > , usize ) ,
334
331
}
335
332
336
- impl < T : Idx > HybridIdxSetBuf < T > {
333
+ impl < T : Idx > HybridIdxSet < T > {
337
334
pub fn new_empty ( universe_size : usize ) -> Self {
338
- HybridIdxSetBuf :: Sparse ( SparseIdxSetBuf :: new ( ) , universe_size)
335
+ HybridIdxSet :: Sparse ( SparseIdxSet :: new ( ) , universe_size)
339
336
}
340
337
341
338
fn universe_size ( & mut self ) -> usize {
342
339
match * self {
343
- HybridIdxSetBuf :: Sparse ( _, size) => size,
344
- HybridIdxSetBuf :: Dense ( _, size) => size,
340
+ HybridIdxSet :: Sparse ( _, size) => size,
341
+ HybridIdxSet :: Dense ( _, size) => size,
345
342
}
346
343
}
347
344
348
345
pub fn clear ( & mut self ) {
349
346
let universe_size = self . universe_size ( ) ;
350
- * self = HybridIdxSetBuf :: new_empty ( universe_size) ;
347
+ * self = HybridIdxSet :: new_empty ( universe_size) ;
351
348
}
352
349
353
350
/// Returns true iff set `self` contains `elem`.
354
351
pub fn contains ( & self , elem : & T ) -> bool {
355
352
match self {
356
- HybridIdxSetBuf :: Sparse ( sparse, _) => sparse. contains ( elem) ,
357
- HybridIdxSetBuf :: Dense ( dense, _) => dense. contains ( elem) ,
353
+ HybridIdxSet :: Sparse ( sparse, _) => sparse. contains ( elem) ,
354
+ HybridIdxSet :: Dense ( dense, _) => dense. contains ( elem) ,
358
355
}
359
356
}
360
357
361
358
/// Adds `elem` to the set `self`.
362
359
pub fn add ( & mut self , elem : & T ) -> bool {
363
360
match self {
364
- HybridIdxSetBuf :: Sparse ( sparse, _) if sparse. len ( ) < SPARSE_MAX => {
361
+ HybridIdxSet :: Sparse ( sparse, _) if sparse. len ( ) < SPARSE_MAX => {
365
362
// The set is sparse and has space for `elem`.
366
363
sparse. add ( elem)
367
364
}
368
- HybridIdxSetBuf :: Sparse ( sparse, _) if sparse. contains ( elem) => {
365
+ HybridIdxSet :: Sparse ( sparse, _) if sparse. contains ( elem) => {
369
366
// The set is sparse and does not have space for `elem`, but
370
367
// that doesn't matter because `elem` is already present.
371
368
false
372
369
}
373
- HybridIdxSetBuf :: Sparse ( _, _) => {
370
+ HybridIdxSet :: Sparse ( _, _) => {
374
371
// The set is sparse and full. Convert to a dense set.
375
372
//
376
373
// FIXME: This code is awful, but I can't work out how else to
377
374
// appease the borrow checker.
378
- let dummy = HybridIdxSetBuf :: Sparse ( SparseIdxSetBuf :: new ( ) , 0 ) ;
375
+ let dummy = HybridIdxSet :: Sparse ( SparseIdxSet :: new ( ) , 0 ) ;
379
376
match mem:: replace ( self , dummy) {
380
- HybridIdxSetBuf :: Sparse ( sparse, universe_size) => {
377
+ HybridIdxSet :: Sparse ( sparse, universe_size) => {
381
378
let mut dense = sparse. to_dense ( universe_size) ;
382
379
let changed = dense. add ( elem) ;
383
380
assert ! ( changed) ;
384
- mem:: replace ( self , HybridIdxSetBuf :: Dense ( dense, universe_size) ) ;
381
+ mem:: replace ( self , HybridIdxSet :: Dense ( dense, universe_size) ) ;
385
382
changed
386
383
}
387
384
_ => panic ! ( "impossible" ) ,
388
385
}
389
386
}
390
387
391
- HybridIdxSetBuf :: Dense ( dense, _) => dense. add ( elem) ,
388
+ HybridIdxSet :: Dense ( dense, _) => dense. add ( elem) ,
392
389
}
393
390
}
394
391
395
392
/// Removes `elem` from the set `self`.
396
393
pub fn remove ( & mut self , elem : & T ) -> bool {
397
394
// Note: we currently don't bother going from Dense back to Sparse.
398
395
match self {
399
- HybridIdxSetBuf :: Sparse ( sparse, _) => sparse. remove ( elem) ,
400
- HybridIdxSetBuf :: Dense ( dense, _) => dense. remove ( elem) ,
396
+ HybridIdxSet :: Sparse ( sparse, _) => sparse. remove ( elem) ,
397
+ HybridIdxSet :: Dense ( dense, _) => dense. remove ( elem) ,
401
398
}
402
399
}
403
400
404
401
/// Converts to a dense set, consuming itself in the process.
405
- pub fn to_dense ( self ) -> IdxSetBuf < T > {
402
+ pub fn to_dense ( self ) -> IdxSet < T > {
406
403
match self {
407
- HybridIdxSetBuf :: Sparse ( sparse, universe_size) => sparse. to_dense ( universe_size) ,
408
- HybridIdxSetBuf :: Dense ( dense, _) => dense,
404
+ HybridIdxSet :: Sparse ( sparse, universe_size) => sparse. to_dense ( universe_size) ,
405
+ HybridIdxSet :: Dense ( dense, _) => dense,
409
406
}
410
407
}
411
408
412
409
/// Iteration order is unspecified.
413
410
pub fn iter ( & self ) -> HybridIter < T > {
414
411
match self {
415
- HybridIdxSetBuf :: Sparse ( sparse, _) => HybridIter :: Sparse ( sparse. iter ( ) ) ,
416
- HybridIdxSetBuf :: Dense ( dense, _) => HybridIter :: Dense ( dense. iter ( ) ) ,
412
+ HybridIdxSet :: Sparse ( sparse, _) => HybridIter :: Sparse ( sparse. iter ( ) ) ,
413
+ HybridIdxSet :: Dense ( dense, _) => HybridIter :: Dense ( dense. iter ( ) ) ,
417
414
}
418
415
}
419
416
}
@@ -439,7 +436,7 @@ fn test_trim_to() {
439
436
use std:: cmp;
440
437
441
438
for i in 0 ..256 {
442
- let mut idx_buf: IdxSetBuf < usize > = IdxSetBuf :: new_filled ( 128 ) ;
439
+ let mut idx_buf: IdxSet < usize > = IdxSet :: new_filled ( 128 ) ;
443
440
idx_buf. trim_to ( i) ;
444
441
445
442
let elems: Vec < usize > = idx_buf. iter ( ) . collect ( ) ;
@@ -452,7 +449,7 @@ fn test_trim_to() {
452
449
fn test_set_up_to ( ) {
453
450
for i in 0 ..128 {
454
451
for mut idx_buf in
455
- vec ! [ IdxSetBuf :: new_empty( 128 ) , IdxSetBuf :: new_filled( 128 ) ]
452
+ vec ! [ IdxSet :: new_empty( 128 ) , IdxSet :: new_filled( 128 ) ]
456
453
. into_iter ( )
457
454
{
458
455
idx_buf. set_up_to ( i) ;
@@ -467,7 +464,7 @@ fn test_set_up_to() {
467
464
#[ test]
468
465
fn test_new_filled ( ) {
469
466
for i in 0 ..128 {
470
- let idx_buf = IdxSetBuf :: new_filled ( i) ;
467
+ let idx_buf = IdxSet :: new_filled ( i) ;
471
468
let elems: Vec < usize > = idx_buf. iter ( ) . collect ( ) ;
472
469
let expected: Vec < usize > = ( 0 ..i) . collect ( ) ;
473
470
assert_eq ! ( elems, expected) ;
0 commit comments