|
| 1 | +#![cfg_attr(feature = "guest", no_std)] |
| 2 | + |
| 3 | +extern crate alloc; |
| 4 | + |
| 5 | +use core::option::Option::None; |
| 6 | + |
| 7 | +/// Fast hash function implementation using wyhash64 algorithm. |
| 8 | +/// |
| 9 | +/// This function provides a high-quality, fast hash suitable for hashmap operations. |
| 10 | +/// It uses the wyhash64 algorithm which offers good distribution properties and |
| 11 | +/// performance characteristics. |
| 12 | +fn wyhash64(mut x: u64) -> u64 { |
| 13 | + x ^= x >> 32; |
| 14 | + x = x.wrapping_mul(0xd6e8feb86659fd93); |
| 15 | + x ^= x >> 32; |
| 16 | + x = x.wrapping_mul(0xd6e8feb86659fd93); |
| 17 | + x |
| 18 | +} |
| 19 | + |
| 20 | +#[jolt::provable(stack_size = 10000, memory_size = 10000000)] |
| 21 | +pub fn btreemap(n: u32) -> u128 { |
| 22 | + use alloc::collections::BTreeMap; |
| 23 | + |
| 24 | + let mut map = BTreeMap::new(); |
| 25 | + let mut inserted_keys = alloc::vec::Vec::with_capacity(n as usize); |
| 26 | + |
| 27 | + // Phase 1: Insert N entries with high-entropy keys |
| 28 | + for i in 0..n { |
| 29 | + let key = wyhash64(i as u64); // Use u64 directly, not usize |
| 30 | + inserted_keys.push(key); |
| 31 | + map.insert(key, i as u64); |
| 32 | + } |
| 33 | + |
| 34 | + // Phase 2: Delete 25% of the inserted keys to trigger rebalancing |
| 35 | + let delete_count = n / 4; |
| 36 | + for i in 0..delete_count { |
| 37 | + let key = inserted_keys[i as usize]; |
| 38 | + map.remove(&key); |
| 39 | + } |
| 40 | + |
| 41 | + // Phase 3: Insert N/2 new entries with new hashed keys |
| 42 | + for i in 0..(n / 2) { |
| 43 | + let key = wyhash64((i + n * 2) as u64); // Non-overlapping seed |
| 44 | + map.insert(key, (i + n) as u64); |
| 45 | + } |
| 46 | + |
| 47 | + // Phase 4: Range scan over middle 25% of key space |
| 48 | + let mut range_sum = 0u64; |
| 49 | + if let Some((&min_key, _)) = map.first_key_value() { |
| 50 | + if let Some((&max_key, _)) = map.last_key_value() { |
| 51 | + let range_size = (max_key - min_key) / 4; |
| 52 | + let start = min_key + range_size; |
| 53 | + let end = start + range_size; |
| 54 | + |
| 55 | + for (_, value) in map.range(start..end) { |
| 56 | + range_sum = range_sum.wrapping_add(*value); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Combine size and range sum into a single return value |
| 62 | + (map.len() as u128).wrapping_add(range_sum as u128) |
| 63 | +} |
0 commit comments