feat: impl Debug fmt for sparse set
This commit is contained in:
parent
ca715d1d67
commit
5a07fae912
1 changed files with 41 additions and 6 deletions
|
@ -30,6 +30,19 @@ impl<T> SparseSet<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T, PackedAlloc, SparseAlloc> core::fmt::Debug for SparseSet<T, PackedAlloc, SparseAlloc>
|
||||
where
|
||||
T: core::fmt::Debug,
|
||||
PackedAlloc: Allocator,
|
||||
SparseAlloc: Allocator,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_map()
|
||||
.entries(self.dense_to_id.iter().zip(self.dense.iter()))
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, PackedAlloc, SparseAlloc> SparseSet<T, PackedAlloc, SparseAlloc>
|
||||
where
|
||||
PackedAlloc: Allocator,
|
||||
|
@ -236,7 +249,10 @@ mod tests {
|
|||
|
||||
assert_eq!(sparse_set.remove(SPARSE_PAGESIZE + 2).unwrap(), 3);
|
||||
assert_eq!(sparse_set.sparse[1].as_ref().unwrap().1, 2);
|
||||
assert_eq!(sparse_set.keys(), [10, 11, 12, SPARSE_PAGESIZE, SPARSE_PAGESIZE + 1]);
|
||||
assert_eq!(
|
||||
sparse_set.keys(),
|
||||
[10, 11, 12, SPARSE_PAGESIZE, SPARSE_PAGESIZE + 1]
|
||||
);
|
||||
assert_eq!(sparse_set.values(), [1, 2, 2, 1, 2]);
|
||||
|
||||
assert_eq!(sparse_set.remove(SPARSE_PAGESIZE + 1).unwrap(), 2);
|
||||
|
@ -249,25 +265,44 @@ mod tests {
|
|||
assert_eq!(sparse_set.keys(), [10, 11, 12]);
|
||||
assert_eq!(sparse_set.values(), [1, 2, 2]);
|
||||
|
||||
|
||||
sparse_set.insert(SPARSE_PAGESIZE, 1);
|
||||
sparse_set.insert(SPARSE_PAGESIZE + 1, 2);
|
||||
sparse_set.insert(SPARSE_PAGESIZE + 2, 3);
|
||||
|
||||
assert_eq!(sparse_set.remove(10).unwrap(), 1);
|
||||
assert_eq!(sparse_set.sparse[0].as_ref().unwrap().1, 2);
|
||||
// swap-remove
|
||||
assert_eq!(sparse_set.keys(), [SPARSE_PAGESIZE + 2, 11, 12, SPARSE_PAGESIZE, SPARSE_PAGESIZE + 1]);
|
||||
// swap-remove
|
||||
assert_eq!(
|
||||
sparse_set.keys(),
|
||||
[
|
||||
SPARSE_PAGESIZE + 2,
|
||||
11,
|
||||
12,
|
||||
SPARSE_PAGESIZE,
|
||||
SPARSE_PAGESIZE + 1
|
||||
]
|
||||
);
|
||||
assert_eq!(sparse_set.values(), [3, 2, 2, 1, 2]);
|
||||
|
||||
assert_eq!(sparse_set.remove(11).unwrap(), 2);
|
||||
assert_eq!(sparse_set.sparse[0].as_ref().unwrap().1, 1);
|
||||
assert_eq!(sparse_set.keys(), [SPARSE_PAGESIZE + 2, SPARSE_PAGESIZE + 1, 12, SPARSE_PAGESIZE]);
|
||||
assert_eq!(
|
||||
sparse_set.keys(),
|
||||
[
|
||||
SPARSE_PAGESIZE + 2,
|
||||
SPARSE_PAGESIZE + 1,
|
||||
12,
|
||||
SPARSE_PAGESIZE
|
||||
]
|
||||
);
|
||||
assert_eq!(sparse_set.values(), [3, 2, 2, 1]);
|
||||
|
||||
assert_eq!(sparse_set.remove(12).unwrap(), 2);
|
||||
assert!(sparse_set.sparse[0].is_none());
|
||||
assert_eq!(sparse_set.keys(), [SPARSE_PAGESIZE + 2, SPARSE_PAGESIZE + 1, SPARSE_PAGESIZE]);
|
||||
assert_eq!(
|
||||
sparse_set.keys(),
|
||||
[SPARSE_PAGESIZE + 2, SPARSE_PAGESIZE + 1, SPARSE_PAGESIZE]
|
||||
);
|
||||
assert_eq!(sparse_set.values(), [3, 2, 1]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue