Skip to content

Commit c960a35

Browse files
committed
hashmap slab test
1 parent 4b949e2 commit c960a35

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ comemo-macros = { workspace = true, optional = true }
4646
parking_lot = { workspace = true }
4747
siphasher = { workspace = true }
4848
bumpalo = "*"
49-
slab = "0.4"
5049

5150
[dev-dependencies]
5251
quickcheck = { workspace = true }

src/calltree.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,62 @@ use std::collections::HashMap;
22
use std::collections::hash_map::Entry;
33
use std::fmt::{self, Debug};
44
use std::hash::Hash;
5+
use std::ops::{Index, IndexMut};
56

6-
use slab::Slab;
7+
/// A simple allocator.
8+
struct Slab<T> {
9+
map: HashMap<usize, T>,
10+
counter: usize,
11+
}
12+
13+
impl<T> Slab<T> {
14+
fn new() -> Self {
15+
Self { map: HashMap::new(), counter: 0 }
16+
}
17+
18+
#[cfg(test)]
19+
fn len(&self) -> usize {
20+
self.map.len()
21+
}
22+
23+
fn insert(&mut self, value: T) -> usize {
24+
self.counter += 1;
25+
self.map.insert(self.counter, value);
26+
self.counter
27+
}
28+
29+
fn contains(&self, i: usize) -> bool {
30+
self.map.contains_key(&i)
31+
}
32+
33+
fn retain(&mut self, mut f: impl FnMut(usize, &mut T) -> bool) {
34+
self.map.retain(|i, v| f(*i, v));
35+
}
36+
37+
fn remove(&mut self, i: usize) {
38+
self.map.remove(&i);
39+
}
40+
}
41+
42+
impl<T> Index<usize> for Slab<T> {
43+
type Output = T;
44+
45+
fn index(&self, index: usize) -> &Self::Output {
46+
&self.map[&index]
47+
}
48+
}
49+
50+
impl<T> IndexMut<usize> for Slab<T> {
51+
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
52+
self.map.get_mut(&index).unwrap()
53+
}
54+
}
55+
56+
impl<T> Default for Slab<T> {
57+
fn default() -> Self {
58+
Self::new()
59+
}
60+
}
761

862
/// A deduplicated sequence of calls to tracked functions.
963
///

0 commit comments

Comments
 (0)