@@ -27,33 +27,30 @@ use rustc_serialize;
2727///
2828/// The representation is dense, using one bit per possible element.
2929#[ derive( Eq , PartialEq ) ]
30- pub struct IdxSetBuf < T : Idx > {
30+ pub struct IdxSet < T : Idx > {
3131 _pd : PhantomData < fn ( & T ) > ,
3232 bits : Vec < Word > ,
3333}
3434
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 > {
3936 fn clone ( & self ) -> Self {
40- IdxSetBuf { _pd : PhantomData , bits : self . bits . clone ( ) }
37+ IdxSet { _pd : PhantomData , bits : self . bits . clone ( ) }
4138 }
4239}
4340
44- impl < T : Idx > rustc_serialize:: Encodable for IdxSetBuf < T > {
41+ impl < T : Idx > rustc_serialize:: Encodable for IdxSet < T > {
4542 fn encode < E : rustc_serialize:: Encoder > ( & self ,
4643 encoder : & mut E )
4744 -> Result < ( ) , E :: Error > {
4845 self . bits . encode ( encoder)
4946 }
5047}
5148
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 > {
5451 let words: Vec < Word > = rustc_serialize:: Decodable :: decode ( d) ?;
5552
56- Ok ( IdxSetBuf {
53+ Ok ( IdxSet {
5754 _pd : PhantomData ,
5855 bits : words,
5956 } )
@@ -62,18 +59,18 @@ impl<T: Idx> rustc_serialize::Decodable for IdxSetBuf<T> {
6259
6360const BITS_PER_WORD : usize = mem:: size_of :: < Word > ( ) * 8 ;
6461
65- impl < T : Idx > fmt:: Debug for IdxSetBuf < T > {
62+ impl < T : Idx > fmt:: Debug for IdxSet < T > {
6663 fn fmt ( & self , w : & mut fmt:: Formatter ) -> fmt:: Result {
6764 w. debug_list ( )
6865 . entries ( self . iter ( ) )
6966 . finish ( )
7067 }
7168}
7269
73- impl < T : Idx > IdxSetBuf < T > {
70+ impl < T : Idx > IdxSet < T > {
7471 fn new ( init : Word , universe_size : usize ) -> Self {
7572 let num_words = ( universe_size + ( BITS_PER_WORD - 1 ) ) / BITS_PER_WORD ;
76- IdxSetBuf {
73+ IdxSet {
7774 _pd : Default :: default ( ) ,
7875 bits : vec ! [ init; num_words] ,
7976 }
@@ -92,13 +89,13 @@ impl<T: Idx> IdxSetBuf<T> {
9289 }
9390
9491 /// Duplicates as a hybrid set.
95- pub fn to_hybrid ( & self ) -> HybridIdxSetBuf < T > {
92+ pub fn to_hybrid ( & self ) -> HybridIdxSet < T > {
9693 // This universe_size may be slightly larger than the one specified
9794 // upon creation, due to rounding up to a whole word. That's ok.
9895 let universe_size = self . bits . len ( ) * BITS_PER_WORD ;
9996
10097 // 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)
10299 }
103100
104101 /// Removes all elements
@@ -171,20 +168,20 @@ impl<T: Idx> IdxSetBuf<T> {
171168 bitwise ( self . words_mut ( ) , other. words ( ) , & Union )
172169 }
173170
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 {
176173 let mut changed = false ;
177174 for elem in other. iter ( ) {
178175 changed |= self . add ( & elem) ;
179176 }
180177 changed
181178 }
182179
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 {
185182 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) ,
188185 }
189186 }
190187
@@ -194,20 +191,20 @@ impl<T: Idx> IdxSetBuf<T> {
194191 bitwise ( self . words_mut ( ) , other. words ( ) , & Subtract )
195192 }
196193
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 {
199196 let mut changed = false ;
200197 for elem in other. iter ( ) {
201198 changed |= self . remove ( & elem) ;
202199 }
203200 changed
204201 }
205202
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 {
208205 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) ,
211208 }
212209 }
213210
@@ -255,15 +252,15 @@ impl<'a, T: Idx> Iterator for Iter<'a, T> {
255252const SPARSE_MAX : usize = 8 ;
256253
257254/// 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.
259256///
260257/// The elements are stored as an unsorted vector with no duplicates.
261258#[ derive( Clone , Debug ) ]
262- pub struct SparseIdxSetBuf < T : Idx > ( ArrayVec < [ T ; SPARSE_MAX ] > ) ;
259+ pub struct SparseIdxSet < T : Idx > ( ArrayVec < [ T ; SPARSE_MAX ] > ) ;
263260
264- impl < T : Idx > SparseIdxSetBuf < T > {
261+ impl < T : Idx > SparseIdxSet < T > {
265262 fn new ( ) -> Self {
266- SparseIdxSetBuf ( ArrayVec :: new ( ) )
263+ SparseIdxSet ( ArrayVec :: new ( ) )
267264 }
268265
269266 fn len ( & self ) -> usize {
@@ -296,8 +293,8 @@ impl<T: Idx> SparseIdxSetBuf<T> {
296293 }
297294 }
298295
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) ;
301298 for elem in self . 0 . iter ( ) {
302299 dense. add ( elem) ;
303300 }
@@ -323,97 +320,97 @@ impl<'a, T: Idx> Iterator for SparseIter<'a, T> {
323320 }
324321}
325322
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
327324/// elements in the set, but dense when there are many. It's especially
328325/// efficient for sets that typically have a small number of elements, but a
329326/// large `universe_size`, and are cleared frequently.
330327#[ 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 ) ,
334331}
335332
336- impl < T : Idx > HybridIdxSetBuf < T > {
333+ impl < T : Idx > HybridIdxSet < T > {
337334 pub fn new_empty ( universe_size : usize ) -> Self {
338- HybridIdxSetBuf :: Sparse ( SparseIdxSetBuf :: new ( ) , universe_size)
335+ HybridIdxSet :: Sparse ( SparseIdxSet :: new ( ) , universe_size)
339336 }
340337
341338 fn universe_size ( & mut self ) -> usize {
342339 match * self {
343- HybridIdxSetBuf :: Sparse ( _, size) => size,
344- HybridIdxSetBuf :: Dense ( _, size) => size,
340+ HybridIdxSet :: Sparse ( _, size) => size,
341+ HybridIdxSet :: Dense ( _, size) => size,
345342 }
346343 }
347344
348345 pub fn clear ( & mut self ) {
349346 let universe_size = self . universe_size ( ) ;
350- * self = HybridIdxSetBuf :: new_empty ( universe_size) ;
347+ * self = HybridIdxSet :: new_empty ( universe_size) ;
351348 }
352349
353350 /// Returns true iff set `self` contains `elem`.
354351 pub fn contains ( & self , elem : & T ) -> bool {
355352 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) ,
358355 }
359356 }
360357
361358 /// Adds `elem` to the set `self`.
362359 pub fn add ( & mut self , elem : & T ) -> bool {
363360 match self {
364- HybridIdxSetBuf :: Sparse ( sparse, _) if sparse. len ( ) < SPARSE_MAX => {
361+ HybridIdxSet :: Sparse ( sparse, _) if sparse. len ( ) < SPARSE_MAX => {
365362 // The set is sparse and has space for `elem`.
366363 sparse. add ( elem)
367364 }
368- HybridIdxSetBuf :: Sparse ( sparse, _) if sparse. contains ( elem) => {
365+ HybridIdxSet :: Sparse ( sparse, _) if sparse. contains ( elem) => {
369366 // The set is sparse and does not have space for `elem`, but
370367 // that doesn't matter because `elem` is already present.
371368 false
372369 }
373- HybridIdxSetBuf :: Sparse ( _, _) => {
370+ HybridIdxSet :: Sparse ( _, _) => {
374371 // The set is sparse and full. Convert to a dense set.
375372 //
376373 // FIXME: This code is awful, but I can't work out how else to
377374 // appease the borrow checker.
378- let dummy = HybridIdxSetBuf :: Sparse ( SparseIdxSetBuf :: new ( ) , 0 ) ;
375+ let dummy = HybridIdxSet :: Sparse ( SparseIdxSet :: new ( ) , 0 ) ;
379376 match mem:: replace ( self , dummy) {
380- HybridIdxSetBuf :: Sparse ( sparse, universe_size) => {
377+ HybridIdxSet :: Sparse ( sparse, universe_size) => {
381378 let mut dense = sparse. to_dense ( universe_size) ;
382379 let changed = dense. add ( elem) ;
383380 assert ! ( changed) ;
384- mem:: replace ( self , HybridIdxSetBuf :: Dense ( dense, universe_size) ) ;
381+ mem:: replace ( self , HybridIdxSet :: Dense ( dense, universe_size) ) ;
385382 changed
386383 }
387384 _ => panic ! ( "impossible" ) ,
388385 }
389386 }
390387
391- HybridIdxSetBuf :: Dense ( dense, _) => dense. add ( elem) ,
388+ HybridIdxSet :: Dense ( dense, _) => dense. add ( elem) ,
392389 }
393390 }
394391
395392 /// Removes `elem` from the set `self`.
396393 pub fn remove ( & mut self , elem : & T ) -> bool {
397394 // Note: we currently don't bother going from Dense back to Sparse.
398395 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) ,
401398 }
402399 }
403400
404401 /// 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 > {
406403 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,
409406 }
410407 }
411408
412409 /// Iteration order is unspecified.
413410 pub fn iter ( & self ) -> HybridIter < T > {
414411 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 ( ) ) ,
417414 }
418415 }
419416}
@@ -439,7 +436,7 @@ fn test_trim_to() {
439436 use std:: cmp;
440437
441438 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 ) ;
443440 idx_buf. trim_to ( i) ;
444441
445442 let elems: Vec < usize > = idx_buf. iter ( ) . collect ( ) ;
@@ -452,7 +449,7 @@ fn test_trim_to() {
452449fn test_set_up_to ( ) {
453450 for i in 0 ..128 {
454451 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 ) ]
456453 . into_iter ( )
457454 {
458455 idx_buf. set_up_to ( i) ;
@@ -467,7 +464,7 @@ fn test_set_up_to() {
467464#[ test]
468465fn test_new_filled ( ) {
469466 for i in 0 ..128 {
470- let idx_buf = IdxSetBuf :: new_filled ( i) ;
467+ let idx_buf = IdxSet :: new_filled ( i) ;
471468 let elems: Vec < usize > = idx_buf. iter ( ) . collect ( ) ;
472469 let expected: Vec < usize > = ( 0 ..i) . collect ( ) ;
473470 assert_eq ! ( elems, expected) ;
0 commit comments