bathroom 0.0.1

Implementation of the Bathroom Model hash table algorithm
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use std::borrow::Borrow;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::mem;

/// A bucket containing a key-value pair
#[derive(Debug, Clone)]
struct Bucket<K, V> {
    /// The key in the key-value pair
    key: K,
    /// The value associated with the key
    value: V,
    /// Flag indicating whether this entry has been deleted (tombstone)
    deleted: bool, // Tombstone flag for deletion
}

/// A hash table with adaptive probing strategy.
///
/// This implementation uses a dynamic step size adjustment based on occupancy patterns,
/// leading to more efficient lookups compared to traditional probing methods.
///
/// Note: This implementation is not thread-safe. For concurrent access, use `ConcurrentElasticMap`.
#[derive(Debug, Clone)]
pub struct ElasticHashMap<K, V> {
    /// The buckets storing the key-value pairs
    buckets: Vec<Option<Bucket<K, V>>>,
    /// Current number of elements in the hash table
    size: usize,
    /// Threshold for load factor before resizing - stored as percentage (0-100)
    load_factor_threshold: usize,
    /// Occupancy threshold for step size adjustment
    occupancy_threshold: usize,
    /// Minimum step size for probing
    min_step_size: usize,
    /// Maximum step size for probing (to avoid extremely large jumps)
    max_step_size: usize,
}

impl<K, V> Default for ElasticHashMap<K, V>
where
    K: Eq + Hash + Clone,
    V: Clone,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<K, V> Extend<(K, V)> for ElasticHashMap<K, V>
where
    K: Eq + Hash + Clone,
    V: Clone,
{
    fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
        for (k, v) in iter {
            self.insert(k, v);
        }
    }
}

impl<K, V> ElasticHashMap<K, V>
where
    K: Eq + Hash + Clone,
    V: Clone,
{
    /// Creates a new `ElasticHashMap` with the default initial capacity and parameters
    #[must_use]
    pub fn new() -> Self {
        Self::with_capacity(64)
    }

    /// Creates a new `ElasticHashMap` with the specified initial capacity
    pub fn with_capacity(capacity: usize) -> Self {
        // Ensure capacity is at least 1 and a power of 2
        let capacity = capacity.max(1).next_power_of_two();

        Self {
            buckets: vec![None; capacity],
            size: 0,
            load_factor_threshold: 75,
            occupancy_threshold: 2,
            min_step_size: 1,
            max_step_size: capacity / 4, // Limit step size to 1/4 of capacity
        }
    }

    /// Computes the hash for a key
    #[allow(clippy::unused_self)]
    fn hash<Q: ?Sized + Hash>(&self, key: &Q) -> u64 {
        let mut hasher = DefaultHasher::new();
        key.hash(&mut hasher);
        hasher.finish()
    }

    /// Gets the index in the buckets for a hash
    #[allow(clippy::cast_possible_truncation)]
    fn get_index<Q: ?Sized + Hash>(&self, key: &Q) -> usize {
        let hash = self.hash(key);
        (hash as usize) & (self.buckets.len().saturating_sub(1))
    }

    /// Insert a key-value pair into the hash table
    #[allow(clippy::arithmetic_side_effects, clippy::cast_precision_loss)]
    pub fn insert(&mut self, key: K, value: V) -> Option<V> {
        // Check if we need to resize
        if (self.size as f64) / (self.buckets.len() as f64)
            >= self.load_factor_threshold as f64 / 100.0
        {
            self.resize();
        }

        let index = self.get_index(&key);
        let result = self.insert_at(index, key, value);

        if result.is_none() {
            self.size = self.size.saturating_add(1);
        }

        result
    }

    /// Inserts a key-value pair starting from the specified index
    fn insert_at(&mut self, start_index: usize, key: K, value: V) -> Option<V> {
        let bucket_count = self.buckets.len();
        let mut index = start_index;
        let mut step_size = self.min_step_size;
        let mut consecutive_occupied: usize = 0;
        let mut first_tombstone = None;

        // Elastic probing loop
        for _ in 0..bucket_count {
            // Check if current bucket matches the key
            #[allow(clippy::question_mark, clippy::manual_let_else)]
            let bucket_ref = match self.buckets.get_mut(index) {
                Some(option) => option,
                None => return None,
            };
            match bucket_ref {
                // Found an empty slot
                None => {
                    // If we found a tombstone earlier, use that position instead
                    if let Some(tombstone_index) = first_tombstone {
                        if let Some(tomb_bucket) = self.buckets.get_mut(tombstone_index) {
                            *tomb_bucket = Some(Bucket { key, value, deleted: false });
                            return None;
                        }
                    } else {
                        *bucket_ref = Some(Bucket { key, value, deleted: false });
                        return None;
                    }
                    return None; // Fallback if get_mut fails, shouldn't happen
                }

                // Found a bucket with data
                Some(bucket) => {
                    // If this is a tombstone and we haven't recorded one yet
                    if bucket.deleted && first_tombstone.is_none() {
                        first_tombstone = Some(index);
                    }
                    // If the key matches and it's not deleted, update the value
                    else if !bucket.deleted && bucket.key == key {
                        let old_value = mem::replace(&mut bucket.value, value);
                        return Some(old_value);
                    }

                    // Dynamic step size adjustment
                    if bucket.deleted {
                        consecutive_occupied = 0;
                        // Decrease step size when finding deleted slots or empty spaces
                        step_size = (step_size / 2).max(self.min_step_size);
                    } else {
                        consecutive_occupied = consecutive_occupied.saturating_add(1);
                        if consecutive_occupied > self.occupancy_threshold {
                            // Increase step size exponentially when encountering many occupied slots
                            step_size = (step_size.saturating_mul(2)).min(self.max_step_size);
                        }
                    }
                }
            }

            // Compute next index with the current step size
            index = (index.saturating_add(step_size)) & (bucket_count.saturating_sub(1));
        }

        // If we get here, we've probed all slots and couldn't find a place
        // This should never happen if we resize properly
        // Return None to indicate insertion failure instead of panicking
        None
    }

    /// Retrieve a value for a given key
    pub fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let index = self.get_index(key);
        self.get_from(index, key)
    }

    /// Get a value starting from the specified index
    fn get_from<Q>(&self, start_index: usize, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let bucket_count = self.buckets.len();
        let mut index = start_index;
        let mut step_size = self.min_step_size;
        let mut consecutive_occupied: usize = 0;

        // Elastic probing loop (for retrieval)
        for _ in 0..bucket_count {
            match self.buckets.get(index) {
                // Empty slot or None bucket, the key is not in the table
                None | Some(None) => return None,

                // Found a bucket with data
                Some(Some(bucket)) => {
                    // If the key matches and it's not deleted, return the value
                    if !bucket.deleted && bucket.key.borrow() == key {
                        return Some(&bucket.value);
                    }

                    // Dynamic step size adjustment (similar to insert)
                    if bucket.deleted {
                        consecutive_occupied = 0;
                        // Decrease step size when finding deleted slots
                        step_size = (step_size / 2).max(self.min_step_size);
                    } else {
                        consecutive_occupied = consecutive_occupied.saturating_add(1);
                        if consecutive_occupied > self.occupancy_threshold {
                            // Increase step size exponentially when encountering many occupied slots
                            step_size = (step_size.saturating_mul(2)).min(self.max_step_size);
                        }
                    }
                }
            }

            // Compute next index with current step size
            index = (index.saturating_add(step_size)) & (bucket_count.saturating_sub(1));
        }

        None
    }

    /// Get a mutable reference to a value for a given key
    pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let index = self.get_index(key);
        self.get_mut_from(index, key)
    }

    /// Get a mutable reference to a value starting from the specified index
    fn get_mut_from<Q>(&mut self, start_index: usize, key: &Q) -> Option<&mut V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let bucket_count = self.buckets.len();
        let mut index = start_index;
        let mut step_size = self.min_step_size;
        let mut consecutive_occupied: usize = 0;

        // Elastic probing loop
        for _ in 0..bucket_count {
            // Check if current bucket matches the key
            #[allow(clippy::question_mark, clippy::manual_let_else)]
            let bucket_ref = match self.buckets.get_mut(index) {
                Some(option) => option,
                None => return None,
            };
            match bucket_ref {
                None => return None,
                Some(bucket) if !bucket.deleted && bucket.key.borrow() == key => {
                    // We found the key, return a mutable reference to its value
                    if let Some(Some(bucket_data)) = self.buckets.get_mut(index) {
                        return Some(&mut bucket_data.value);
                    }
                    return None; // This should never happen, but safer than unwrap()
                }
                Some(bucket) => {
                    // Dynamic step size adjustment
                    if bucket.deleted {
                        consecutive_occupied = 0;
                        // Decrease step size when finding deleted slots
                        step_size = (step_size / 2).max(self.min_step_size);
                    } else {
                        consecutive_occupied = consecutive_occupied.saturating_add(1);
                        if consecutive_occupied > self.occupancy_threshold {
                            step_size = (step_size.saturating_mul(2)).min(self.max_step_size);
                        }
                    }
                }
            }

            // Advance to next position
            index = (index.saturating_add(step_size)) & (bucket_count.saturating_sub(1));
        }

        None
    }

    /// Removes a key-value pair from the hash table
    pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let index = self.get_index(key);
        self.remove_from(index, key)
    }

    /// Removes a key-value pair starting from the specified index
    fn remove_from<Q>(&mut self, start_index: usize, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        let bucket_count = self.buckets.len();
        let mut index = start_index;
        let mut step_size = self.min_step_size;
        let mut consecutive_occupied: usize = 0;

        // Elastic probing loop (for removal)
        for _ in 0..bucket_count {
            match self.buckets.get_mut(index) {
                // Found an empty slot (and not a tombstone), the key is not in the table
                None | Some(None) => return None,

                // Found a bucket with data
                Some(Some(bucket)) => {
                    // If the key matches and it's not deleted, remove it
                    if !bucket.deleted && bucket.key.borrow() == key {
                        bucket.deleted = true;
                        self.size = self.size.saturating_sub(1);
                        return Some(bucket.value.clone());
                    }

                    // Dynamic step size adjustment
                    if bucket.deleted {
                        consecutive_occupied = 0;
                        // Decrease step size when finding deleted slots
                        step_size = (step_size / 2).max(self.min_step_size);
                    } else {
                        consecutive_occupied = consecutive_occupied.saturating_add(1);
                        if consecutive_occupied > self.occupancy_threshold {
                            // Increase step size exponentially when encountering many occupied slots
                            step_size = (step_size.saturating_mul(2)).min(self.max_step_size);
                        }
                    }
                }
            }

            // Compute next index with current step size
            index = (index.saturating_add(step_size)) & (bucket_count.saturating_sub(1));
        }

        None
    }

    /// Returns the number of elements in the hash table
    #[must_use]
    pub fn len(&self) -> usize {
        self.size
    }

    /// Returns true if the hash table is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.size == 0
    }

    /// Resizes the hash table when it gets too full
    fn resize(&mut self) {
        let new_capacity = self.buckets.len().saturating_mul(2);
        let mut new_table = Self {
            buckets: vec![None; new_capacity],
            size: 0,
            load_factor_threshold: self.load_factor_threshold,
            occupancy_threshold: self.occupancy_threshold,
            min_step_size: self.min_step_size,
            max_step_size: new_capacity / 4, // Update max step size for new capacity
        };

        // Move all non-deleted key-value pairs to the new table
        for bucket in self.buckets.iter().filter_map(|b| b.as_ref()) {
            if !bucket.deleted {
                new_table.insert(bucket.key.clone(), bucket.value.clone());
            }
        }

        // Replace the current table with the new one
        *self = new_table;
    }

    /// Provide a way to configure the occupancy threshold
    pub fn set_occupancy_threshold(&mut self, threshold: usize) {
        self.occupancy_threshold = threshold.max(1); // Ensure at least 1
    }

    /// Provide a way to configure the load factor threshold
    pub fn set_load_factor_threshold(&mut self, threshold: usize) {
        self.load_factor_threshold = threshold.clamp(1, 95); // Keep within reasonable range
    }

    /// Returns an iterator over the key-value pairs
    #[must_use]
    #[allow(clippy::iter_without_into_iter)]
    pub fn iter(&self) -> Iter<K, V> {
        Iter { buckets: &self.buckets, index: 0, _marker: PhantomData }
    }

    /// Clears the hash map, removing all key-value pairs
    pub fn clear(&mut self) {
        for bucket in &mut self.buckets {
            *bucket = None;
        }
        self.size = 0;
    }

    /// Returns the number of buckets in the hash map
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.buckets.len()
    }

    /// Returns the current load factor of the hash map
    #[must_use]
    #[allow(clippy::arithmetic_side_effects, clippy::cast_precision_loss)]
    pub fn load_factor(&self) -> f64 {
        self.size as f64 / self.buckets.len() as f64
    }
}

/// Iterator over the key-value pairs of the hash table
#[derive(Debug, Clone)]
pub struct Iter<'a, K, V> {
    /// Reference to the buckets in the hash map
    buckets: &'a [Option<Bucket<K, V>>],
    /// Current position in the iteration
    index: usize,
    /// Phantom data to hold the lifetime and type parameters
    _marker: PhantomData<&'a (K, V)>,
}

impl<'a, K, V> Iterator for Iter<'a, K, V> {
    type Item = (&'a K, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        while self.index < self.buckets.len() {
            if let Some(Some(bucket)) = self.buckets.get(self.index) {
                self.index = self.index.saturating_add(1);
                if !bucket.deleted {
                    return Some((&bucket.key, &bucket.value));
                }
            } else {
                self.index = self.index.saturating_add(1);
            }
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_insert_and_get() {
        let mut map = ElasticHashMap::new();
        assert_eq!(map.insert("key1".to_string(), 1), None);
        assert_eq!(map.insert("key2".to_string(), 2), None);
        assert_eq!(map.insert("key3".to_string(), 3), None);

        assert_eq!(map.get("key1"), Some(&1));
        assert_eq!(map.get("key2"), Some(&2));
        assert_eq!(map.get("key3"), Some(&3));
        assert_eq!(map.get("key4"), None);
    }

    #[test]
    fn test_update() {
        let mut map = ElasticHashMap::new();
        assert_eq!(map.insert("key1".to_string(), 1), None);
        assert_eq!(map.insert("key1".to_string(), 10), Some(1));
        assert_eq!(map.get("key1"), Some(&10));
    }

    #[test]
    fn test_remove() {
        let mut map = ElasticHashMap::new();
        map.insert("key1".to_string(), 1);
        map.insert("key2".to_string(), 2);

        assert_eq!(map.remove("key1"), Some(1));
        assert_eq!(map.get("key1"), None);
        assert_eq!(map.get("key2"), Some(&2));
        assert_eq!(map.remove("key1"), None);
    }

    #[test]
    fn test_resize() {
        let mut map = ElasticHashMap::with_capacity(4);
        map.set_load_factor_threshold(50);

        // Initial capacity is 4, so after 2 inserts (load factor > 50%), it should resize
        map.insert("key1".to_string(), 1);
        map.insert("key2".to_string(), 2);
        map.insert("key3".to_string(), 3); // This should trigger resize to capacity 8

        assert_eq!(map.get("key1"), Some(&1));
        assert_eq!(map.get("key2"), Some(&2));
        assert_eq!(map.get("key3"), Some(&3));
        assert_eq!(map.capacity(), 8);
    }

    #[test]
    fn test_len_and_is_empty() {
        let mut map = ElasticHashMap::new();
        assert!(map.is_empty());
        assert_eq!(map.len(), 0);

        map.insert("key1".to_string(), 1);
        assert!(!map.is_empty());
        assert_eq!(map.len(), 1);

        map.insert("key2".to_string(), 2);
        assert_eq!(map.len(), 2);

        map.remove("key1");
        assert_eq!(map.len(), 1);

        map.remove("key2");
        assert!(map.is_empty());
    }

    #[test]
    fn test_iter() {
        let mut map = ElasticHashMap::new();
        map.insert("key1".to_string(), 1);
        map.insert("key2".to_string(), 2);
        map.insert("key3".to_string(), 3);

        let mut count = 0;
        let mut sum = 0;
        for (_, &value) in map.iter() {
            count += 1;
            sum += value;
        }

        assert_eq!(count, 3);
        assert_eq!(sum, 6);
    }

    #[test]
    fn test_get_mut() {
        let mut map = ElasticHashMap::new();
        map.insert("key1".to_string(), 1);

        if let Some(value) = map.get_mut("key1") {
            *value += 10;
        }

        assert_eq!(map.get("key1"), Some(&11));
    }

    #[test]
    fn test_clear() {
        let mut map = ElasticHashMap::new();
        map.insert("key1".to_string(), 1);
        map.insert("key2".to_string(), 2);

        assert_eq!(map.len(), 2);

        map.clear();

        assert_eq!(map.len(), 0);
        assert!(map.is_empty());
        assert_eq!(map.get("key1"), None);
        assert_eq!(map.get("key2"), None);
    }

    #[test]
    fn test_with_high_load_factor() {
        let mut map = ElasticHashMap::with_capacity(16);
        map.set_load_factor_threshold(90);

        for i in 0..14 {
            map.insert(i.to_string(), i);
        }

        for i in 0..14 {
            assert_eq!(map.get(&i.to_string()), Some(&i));
        }

        // Check that the load factor is correctly reported
        assert!((map.load_factor() - 14.0 / 16.0).abs() < 0.01);
    }
}