@@ -23,31 +23,10 @@ use hash::Hash;
2323use prelude:: * ;
2424use to_bytes:: IterBytes ;
2525
26- pub trait SendMap < K : Eq Hash , V : Copy > {
27- // FIXME(#3148) ^^^^ once find_ref() works, we can drop V:copy
28-
29- fn insert ( & mut self , k : K , +v : V ) -> bool ;
30- fn remove ( & mut self , k : & K ) -> bool ;
31- fn pop ( & mut self , k : & K ) -> Option < V > ;
32- fn swap ( & mut self , k : K , +v : V ) -> Option < V > ;
33- fn consume ( & mut self , f : fn ( K , V ) ) ;
34- fn clear ( & mut self ) ;
35- pure fn len ( & const self ) -> uint ;
36- pure fn is_empty ( & const self ) -> bool ;
37- pure fn contains_key ( & const self , k : & K ) -> bool ;
38- pure fn each ( & self , blk : fn ( k : & K , v : & V ) -> bool ) ;
39- pure fn each_key_ref ( & self , blk : fn ( k : & K ) -> bool ) ;
40- pure fn each_value_ref ( & self , blk : fn ( v : & V ) -> bool ) ;
41- pure fn find ( & const self , k : & K ) -> Option < V > ;
42- pure fn get ( & const self , k : & K ) -> V ;
43- pure fn find_ref ( & self , k : & K ) -> Option < & self /V > ;
44- pure fn get_ref ( & self , k : & K ) -> & self /V ;
45- }
46-
4726/// Open addressing with linear probing.
4827pub mod linear {
4928 use iter:: BaseIter ;
50- use container:: Set ;
29+ use container:: { Container , Mutable , Map , Set } ;
5130 use cmp:: Eq ;
5231 use cmp;
5332 use hash:: Hash ;
@@ -279,7 +258,48 @@ pub mod linear {
279258 }
280259 }
281260
282- impl < K : Hash IterBytes Eq , V > LinearMap < K , V > {
261+ impl < K : Hash IterBytes Eq , V > LinearMap < K , V > : Container {
262+ pure fn len ( & self ) -> uint { self . size }
263+ pure fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
264+ }
265+
266+ impl < K : Hash IterBytes Eq , V > LinearMap < K , V > : Mutable {
267+ fn clear ( & mut self ) {
268+ for uint:: range( 0 , self . buckets. len( ) ) |idx| {
269+ self . buckets [ idx] = None ;
270+ }
271+ self . size = 0 ;
272+ }
273+ }
274+
275+ impl < K : Hash IterBytes Eq , V > LinearMap < K , V > : Map < K , V > {
276+ pure fn contains_key ( & self , k : & K ) -> bool {
277+ match self . bucket_for_key ( self . buckets , k) {
278+ FoundEntry ( _) => { true }
279+ TableFull | FoundHole ( _) => { false }
280+ }
281+ }
282+
283+ pure fn each ( & self , blk : fn ( k : & K , v : & V ) -> bool ) {
284+ for vec:: each( self . buckets) |slot| {
285+ let mut broke = false ;
286+ do slot. iter |bucket| {
287+ if !blk ( & bucket. key , & bucket. value ) {
288+ broke = true ; // FIXME(#3064) just write "break;"
289+ }
290+ }
291+ if broke { break ; }
292+ }
293+ }
294+
295+ pure fn each_key ( & self , blk : fn ( k : & K ) -> bool ) {
296+ self . each ( |k, _v| blk ( k) )
297+ }
298+
299+ pure fn each_value ( & self , blk : fn ( v : & V ) -> bool ) {
300+ self . each ( |_k, v| blk ( v) )
301+ }
302+
283303 fn insert ( & mut self , k : K , v : V ) -> bool {
284304 if self . size >= self . resize_at {
285305 // n.b.: We could also do this after searching, so
@@ -301,7 +321,9 @@ pub mod linear {
301321 None => false ,
302322 }
303323 }
324+ }
304325
326+ impl < K : Hash IterBytes Eq , V > LinearMap < K , V > {
305327 fn pop ( & mut self , k : & K ) -> Option < V > {
306328 let hash = k. hash_keyed ( self . k0 , self . k1 ) as uint ;
307329 self . pop_internal ( hash, k)
@@ -347,29 +369,6 @@ pub mod linear {
347369 }
348370 }
349371
350- fn clear ( & mut self ) {
351- for uint:: range( 0 , self . buckets. len( ) ) |idx| {
352- self . buckets [ idx] = None ;
353- }
354- self . size = 0 ;
355- }
356-
357- pure fn len ( & const self ) -> uint {
358- self . size
359- }
360-
361- pure fn is_empty ( & const self ) -> bool {
362- self . len ( ) == 0
363- }
364-
365- pure fn contains_key ( & const self ,
366- k : & K ) -> bool {
367- match self . bucket_for_key ( self . buckets , k) {
368- FoundEntry ( _) => { true }
369- TableFull | FoundHole ( _) => { false }
370- }
371- }
372-
373372 pure fn find_ref ( & self , k : & K ) -> Option < & self /V > {
374373 match self . bucket_for_key ( self . buckets , k) {
375374 FoundEntry ( idx) => {
@@ -396,26 +395,6 @@ pub mod linear {
396395 None => fail fmt ! ( "No entry found for key: %?" , k) ,
397396 }
398397 }
399-
400- pure fn each ( & self , blk : fn ( k : & K , v : & V ) -> bool ) {
401- for vec:: each( self . buckets) |slot| {
402- let mut broke = false ;
403- do slot. iter |bucket| {
404- if !blk ( & bucket. key , & bucket. value ) {
405- broke = true ; // FIXME(#3064) just write "break;"
406- }
407- }
408- if broke { break ; }
409- }
410- }
411-
412- pure fn each_key ( & self , blk : fn ( k : & K ) -> bool ) {
413- self . each ( |k, _v| blk ( k) )
414- }
415-
416- pure fn each_value ( & self , blk : fn ( v : & V ) -> bool ) {
417- self . each ( |_k, v| blk ( v) )
418- }
419398 }
420399
421400 impl < K : Hash IterBytes Eq , V : Copy > LinearMap < K , V > {
@@ -482,6 +461,15 @@ pub mod linear {
482461 }
483462 }
484463
464+ impl < T : Hash IterBytes Eq > LinearSet < T > : Container {
465+ pure fn len ( & self ) -> uint { self . map . len ( ) }
466+ pure fn is_empty ( & self ) -> bool { self . map . is_empty ( ) }
467+ }
468+
469+ impl < T : Hash IterBytes Eq > LinearSet < T > : Mutable {
470+ fn clear ( & mut self ) { self . map . clear ( ) }
471+ }
472+
485473 impl < T : Hash IterBytes Eq > LinearSet < T > : Set < T > {
486474 /// Return true if the set contains a value
487475 pure fn contains ( & self , value : & T ) -> bool {
@@ -500,12 +488,6 @@ pub mod linear {
500488 impl < T : Hash IterBytes Eq > LinearSet < T > {
501489 /// Create an empty LinearSet
502490 static fn new( ) -> LinearSet <T > { LinearSet { map: LinearMap ( ) } }
503-
504- /// Return the number of elements in the set
505- pure fn len ( & self ) -> uint { self . map . len ( ) }
506-
507- /// Return true if the set contains no elements
508- pure fn is_empty ( & self ) -> bool { self . map . is_empty ( ) }
509491 }
510492}
511493
0 commit comments