1+ use core:: hash:: { BuildHasherDefault , Hasher } ;
12use std:: collections:: { HashSet , VecDeque } ;
23
34/// A simple graph represented as an adjacency list
@@ -23,10 +24,54 @@ impl Graph {
2324 }
2425}
2526
27+ pub ( crate ) type BuildNoHashHasher = BuildHasherDefault < NoHashHasher > ;
28+
29+ #[ derive( Default ) ]
30+ pub ( crate ) struct NoHashHasher ( u64 ) ;
31+
32+ impl Hasher for NoHashHasher {
33+ fn finish ( & self ) -> u64 {
34+ self . 0
35+ }
36+ fn write ( & mut self , _: & [ u8 ] ) {
37+ unreachable ! ( "Should not be used" )
38+ }
39+ fn write_u8 ( & mut self , _: u8 ) {
40+ unreachable ! ( "Should not be used" )
41+ }
42+ fn write_u16 ( & mut self , _: u16 ) {
43+ unreachable ! ( "Should not be used" )
44+ }
45+ fn write_u32 ( & mut self , _: u32 ) {
46+ unreachable ! ( "Should not be used" )
47+ }
48+ fn write_u64 ( & mut self , _: u64 ) {
49+ unreachable ! ( "Should not be used" )
50+ }
51+ fn write_usize ( & mut self , n : usize ) {
52+ self . 0 = n as u64 ;
53+ }
54+ fn write_i8 ( & mut self , _: i8 ) {
55+ unreachable ! ( "Should not be used" )
56+ }
57+ fn write_i16 ( & mut self , _: i16 ) {
58+ unreachable ! ( "Should not be used" )
59+ }
60+ fn write_i32 ( & mut self , _: i32 ) {
61+ unreachable ! ( "Should not be used" )
62+ }
63+ fn write_i64 ( & mut self , _: i64 ) {
64+ unreachable ! ( "Should not be used" )
65+ }
66+ fn write_isize ( & mut self , _: isize ) {
67+ unreachable ! ( "Should not be used" )
68+ }
69+ }
70+
2671/// Naive BFS implementation using Vec as a queue (intentionally slow)
2772/// Returns the order in which nodes were visited
2873pub fn bfs_naive ( graph : & Graph , start : usize ) -> Vec < usize > {
29- let mut visited = HashSet :: new ( ) ;
74+ let mut visited = HashSet :: with_capacity_and_hasher ( 1024 , BuildNoHashHasher :: new ( ) ) ;
3075 let mut queue = VecDeque :: new ( ) ; // Using Vec instead of VecDeque - intentionally inefficient!
3176 let mut result = Vec :: new ( ) ;
3277
0 commit comments