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
use std::hash::Hash;
use std::ops::{Deref, DerefMut};
use std::cmp;
use super::NodeHasher;
pub trait Node {
type NodeId: Hash + PartialEq + Ord;
type HashCode: Ord;
fn node_id(&self) -> &Self::NodeId;
fn hash_code<H, U: Hash>(&self, hasher: &H, item: &U) -> Self::HashCode
where
H: NodeHasher<Self::NodeId>;
}
impl<'a> Node for &'a str {
type NodeId = Self;
type HashCode = u64;
fn node_id(&self) -> &Self::NodeId {
self
}
fn hash_code<H, U: Hash>(&self, hasher: &H, item: &U) -> Self::HashCode
where
H: NodeHasher<Self::NodeId>,
{
hasher.hash(self, item)
}
}
#[derive(Debug, Clone)]
pub struct IdNode<T>(T);
impl<T> IdNode<T> {
pub fn new(node: T) -> Self {
IdNode(node)
}
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> Deref for IdNode<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for IdNode<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> Node for IdNode<T>
where
T: Hash + PartialEq + Ord,
{
type NodeId = T;
type HashCode = u64;
fn node_id(&self) -> &Self::NodeId {
&self.0
}
fn hash_code<H, U: Hash>(&self, hasher: &H, item: &U) -> Self::HashCode
where
H: NodeHasher<Self::NodeId>,
{
hasher.hash(self.node_id(), item)
}
}
#[derive(Debug, Clone)]
pub struct KeyValueNode<K, V> {
pub key: K,
pub value: V,
}
impl<K, V> KeyValueNode<K, V> {
pub fn new(key: K, value: V) -> Self {
KeyValueNode {
key: key,
value: value,
}
}
}
impl<K, V> Node for KeyValueNode<K, V>
where
K: Hash + PartialEq + Ord,
{
type NodeId = K;
type HashCode = u64;
fn node_id(&self) -> &Self::NodeId {
&self.key
}
fn hash_code<H, U: Hash>(&self, hasher: &H, item: &U) -> Self::HashCode
where
H: NodeHasher<Self::NodeId>,
{
hasher.hash(self.node_id(), item)
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct SignPositiveF64(f64);
impl Eq for SignPositiveF64 {}
impl cmp::Ord for SignPositiveF64 {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.0.partial_cmp(&other.0).unwrap()
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Capacity(f64);
impl Capacity {
pub fn new(value: f64) -> Option<Self> {
if value.is_normal() && value.is_sign_positive() {
Some(Capacity(value))
} else {
None
}
}
pub fn value(&self) -> f64 {
self.0
}
}
#[derive(Debug, Clone)]
pub struct WeightedNode<N> {
pub node: N,
pub capacity: Capacity,
}
impl<N: Node> WeightedNode<N> {
pub fn new(node: N, capacity: Capacity) -> Self {
WeightedNode {
node: node,
capacity: capacity,
}
}
}
impl<N: Node> Node for WeightedNode<N> {
type NodeId = N::NodeId;
type HashCode = SignPositiveF64;
fn node_id(&self) -> &Self::NodeId {
self.node.node_id()
}
fn hash_code<H, U: Hash>(&self, hasher: &H, item: &U) -> Self::HashCode
where
H: NodeHasher<Self::NodeId>,
{
use std::u64::MAX;
let hash = hasher.hash(self.node_id(), item) as f64;
let distance = (hash / MAX as f64).ln();
SignPositiveF64(distance / self.capacity.0)
}
}
#[derive(Debug, Clone)]
pub struct WithHashCode<N: Node> {
pub node: N,
pub hash_code: Option<N::HashCode>,
}
impl<N: Node> WithHashCode<N> {
pub fn new(node: N) -> Self {
WithHashCode {
node: node,
hash_code: None,
}
}
}