Skip to content

Commit 27ee37b

Browse files
committed
wip: generic over reference type for rng
1 parent c992482 commit 27ee37b

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub mod quickfind;
7070
pub mod quickunion;
7171
pub mod rng;
7272

73-
use core::borrow::{Borrow, BorrowMut};
7473
use core::marker::PhantomData;
7574
use core::ops::AddAssign;
7675

@@ -86,7 +85,11 @@ pub struct Fat;
8685
pub struct Thin;
8786

8887
pub struct Owned;
89-
pub struct Borrowed<T = Fat>(PhantomData<T>);
88+
pub struct Borrowed<P: PointerType + ?Sized = Fat>(PhantomData<P>);
89+
90+
pub trait PointerType {}
91+
impl PointerType for Fat {}
92+
impl PointerType for Thin {}
9093

9194
/// Any type that can be used to index internal buffer
9295
pub trait VertexType: Eq + Copy {

src/quickunion/constructors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Constructors and macros for QuickUnion
22
3-
use rand_core::RngCore;
43
use crate::{
5-
quickunion::heuristics::ByRandom, rng::PhantomRng, ByRank, BySize, QuickUnion, UnionFind, Unweighted, Owned
4+
quickunion::heuristics::ByRandom, rng::PhantomRng, ByRank, BySize, Owned, QuickUnion,
5+
UnionFind, Unweighted,
66
};
7+
use rand_core::RngCore;
78

89
/// Helper macro to generate the representative array
910
macro_rules! generate_representative {

src/quickunion/mod.rs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ mod by_random_tests {
5353
use rand_core::SeedableRng;
5454
use rand_hc::Hc128Rng;
5555

56-
use crate::{Borrowed, Fat, Owned, QuickUnion, Thin, UnionFind, quickunion::ByRandom};
56+
use crate::{quickunion::ByRandom, Borrowed, Fat, Owned, QuickUnion, Thin, UnionFind};
5757

5858
#[test]
5959
fn test_one_union() {
@@ -70,19 +70,49 @@ mod by_random_tests {
7070
fn test_size() {
7171
const N: usize = 128;
7272
assert_eq!(
73-
mem::size_of::<UnionFind::<'_, QuickUnion<ByRandom<Hc128Rng, Owned>, false>, u8, N>>(),
73+
mem::size_of::<UnionFind::<'_, QuickUnion<ByRandom<Hc128Rng, Owned>, false>, u8, N>>(
74+
),
7475
mem::size_of::<[u8; N]>()
7576
+ mem::size_of::<[usize; 0]>()
7677
+ mem::size_of::<Hc128Rng>()
7778
);
7879
}
7980

81+
#[test]
82+
fn test_size_borrowed_rng() {
83+
const N: usize = 128;
84+
assert_eq!(
85+
mem::size_of::<
86+
UnionFind::<'_, QuickUnion<ByRandom<&mut Hc128Rng, Owned>, false>, u8, N>,
87+
>(),
88+
mem::size_of::<[u8; N]>()
89+
+ mem::size_of::<[usize; 0]>()
90+
+ mem::size_of::<&Hc128Rng>()
91+
);
92+
}
93+
94+
#[test]
95+
fn borrowed_rng_ctor() {
96+
const N: usize = 10;
97+
let mut rng = Hc128Rng::seed_from_u64(67);
98+
99+
let mut uf =
100+
UnionFind::<'_, QuickUnion<ByRandom<&mut Hc128Rng, Owned>, false>, u8, N>::new(
101+
&mut rng,
102+
);
103+
104+
uf.union_sets(0, 1);
105+
106+
assert!(uf.connected(0, 1));
107+
}
108+
80109
#[test]
81110
fn test_size_padded() {
82111
const M: usize = 100;
83112

84113
assert_eq!(
85-
mem::size_of::<UnionFind::<'_, QuickUnion<ByRandom<Hc128Rng, Owned>, false>, u8, M>>(),
114+
mem::size_of::<UnionFind::<'_, QuickUnion<ByRandom<Hc128Rng, Owned>, false>, u8, M>>(
115+
),
86116
mem::size_of::<[u8; M]>()
87117
+ mem::size_of::<[usize; 0]>()
88118
+ mem::size_of::<Hc128Rng>()
@@ -94,7 +124,9 @@ mod by_random_tests {
94124
fn test_size_borrowed_thin() {
95125
const N: usize = 128;
96126
assert_eq!(
97-
mem::size_of::<UnionFind::<'_, QuickUnion<ByRandom<Hc128Rng, Borrowed<Thin>>, false>, u8, N>>(),
127+
mem::size_of::<
128+
UnionFind::<'_, QuickUnion<ByRandom<Hc128Rng, Borrowed<Thin>>, false>, u8, N>,
129+
>(),
98130
mem::size_of::<&[u8; N]>() + mem::size_of::<Hc128Rng>()
99131
);
100132
}
@@ -103,7 +135,9 @@ mod by_random_tests {
103135
fn test_size_borrowed_fat() {
104136
const N: usize = 128;
105137
assert_eq!(
106-
mem::size_of::<UnionFind::<'_, QuickUnion<ByRandom<Hc128Rng, Borrowed<Fat>>, false>, u8, N>>(),
138+
mem::size_of::<
139+
UnionFind::<'_, QuickUnion<ByRandom<Hc128Rng, Borrowed<Fat>>, false>, u8, N>,
140+
>(),
107141
mem::size_of::<&[u8]>() + mem::size_of::<Hc128Rng>()
108142
);
109143
}

src/rng.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ impl RngCore for PhantomRng {
2828
fn fill_bytes(&mut self, _dst: &mut [u8]) {
2929
unreachable!("Should not be called!")
3030
}
31-
}
31+
}

0 commit comments

Comments
 (0)