approx_collections 4.0.0

Collections using approximate floating-point comparison
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
//! Common traits related to approximate equality.

use std::{
    cmp::Ordering,
    hash::{Hash, Hasher},
};

use crate::Precision;

macro_rules! impl_for_tuples {
    ($impl_macro:ident) => {
        $impl_macro!(T0; 0);
        $impl_macro!(T0, T1; 0, 1);
        $impl_macro!(T0, T1, T2; 0, 1, 2);
        $impl_macro!(T0, T1, T2, T3; 0, 1, 2, 3);
        $impl_macro!(T0, T1, T2, T3, T4; 0, 1, 2, 3, 4);
        $impl_macro!(T0, T1, T2, T3, T4, T5; 0, 1, 2, 3, 4, 5);
        $impl_macro!(T0, T1, T2, T3, T4, T5, T6; 0, 1, 2, 3, 4, 5, 6);
        $impl_macro!(T0, T1, T2, T3, T4, T5, T6, T7; 0, 1, 2, 3, 4, 5, 6, 7);
        $impl_macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8; 0, 1, 2, 3, 4, 5, 6, 7, 8);
        $impl_macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    };
}

/// Trait for types that can be approximately compared for equality with each
/// other.
pub trait ApproxEq: std::fmt::Debug {
    /// Returns whether `self` and `other` are approximately equal according to
    /// the precision.
    fn approx_eq(&self, other: &Self, prec: Precision) -> bool;
}
impl ApproxEq for f64 {
    fn approx_eq(&self, other: &Self, prec: Precision) -> bool {
        prec.f64_eq(*self, *other)
    }
}
impl ApproxEq for f32 {
    fn approx_eq(&self, other: &Self, prec: Precision) -> bool {
        prec.f32_eq(*self, *other)
    }
}
impl<T: ApproxEq> ApproxEq for [T] {
    fn approx_eq(&self, other: &Self, prec: Precision) -> bool {
        self.len() == other.len() && std::iter::zip(self, other).all(|(a, b)| a.approx_eq(b, prec))
    }
}
impl<T: ApproxEq, const N: usize> ApproxEq for [T; N] {
    fn approx_eq(&self, other: &Self, prec: Precision) -> bool {
        <[T]>::approx_eq(self, other, prec)
    }
}
impl<T: ApproxEq> ApproxEq for Vec<T> {
    fn approx_eq(&self, other: &Self, prec: Precision) -> bool {
        <[T]>::approx_eq(self, other, prec)
    }
}
impl<T: ApproxEq> ApproxEq for Box<T> {
    fn approx_eq(&self, other: &Self, prec: Precision) -> bool {
        T::approx_eq(self, other, prec)
    }
}
impl<T: ApproxEq + ?Sized> ApproxEq for &T {
    fn approx_eq(&self, other: &Self, prec: Precision) -> bool {
        T::approx_eq(self, other, prec)
    }
}
impl<T: ApproxEq> ApproxEq for Option<T> {
    fn approx_eq(&self, other: &Self, prec: Precision) -> bool {
        match (self, other) {
            (None, None) => true,
            (None, Some(_)) | (Some(_), None) => false,
            (Some(a), Some(b)) => a.approx_eq(b, prec),
        }
    }
}
macro_rules! impl_approx_eq_for_tuple {
    ($($generic_param:ident),+; $($index:tt),+) => {
        impl<$($generic_param: ApproxEq,)+> ApproxEq for ($($generic_param,)+) {
            fn approx_eq(&self, other: &Self, prec: Precision) -> bool {
                $(self.$index.approx_eq(&other.$index, prec))&&+
            }
        }
    };
}
impl_for_tuples!(impl_approx_eq_for_tuple);

/// Trait for types that can be approximately compared to some zero value.
pub trait ApproxEqZero {
    /// Returns whether `self` is approximately zero according to the precision.
    ///
    /// This should have the same behavior as [`ApproxEq::approx_eq()`] with
    /// zero as one of the arguments, but may be more optimized.
    fn approx_eq_zero(&self, prec: Precision) -> bool;
}
impl ApproxEqZero for f64 {
    fn approx_eq_zero(&self, prec: Precision) -> bool {
        prec.f64_eq_zero(*self)
    }
}
impl ApproxEqZero for f32 {
    fn approx_eq_zero(&self, prec: Precision) -> bool {
        prec.f64_eq_zero(*self as f64)
    }
}
impl<T: ApproxEqZero> ApproxEqZero for [T] {
    fn approx_eq_zero(&self, prec: Precision) -> bool {
        self.iter().all(|x| x.approx_eq_zero(prec))
    }
}
impl<T: ApproxEqZero, const N: usize> ApproxEqZero for [T; N] {
    fn approx_eq_zero(&self, prec: Precision) -> bool {
        <[T]>::approx_eq_zero(self, prec)
    }
}
impl<T: ApproxEqZero> ApproxEqZero for Vec<T> {
    fn approx_eq_zero(&self, prec: Precision) -> bool {
        <[T]>::approx_eq_zero(self, prec)
    }
}
impl<T: ApproxEqZero> ApproxEqZero for Box<T> {
    fn approx_eq_zero(&self, prec: Precision) -> bool {
        T::approx_eq_zero(self, prec)
    }
}
impl<T: ApproxEqZero + ?Sized> ApproxEqZero for &T {
    fn approx_eq_zero(&self, prec: Precision) -> bool {
        T::approx_eq_zero(self, prec)
    }
}
macro_rules! impl_approx_eq_zero_for_tuple {
    ($($generic_param:ident),+; $($index:tt),+) => {
        impl<$($generic_param: ApproxEqZero,)+> ApproxEqZero for ($($generic_param,)+) {
            fn approx_eq_zero(&self, prec: Precision) -> bool {
                $(self.$index.approx_eq_zero(prec))&&+
            }
        }
    };
}
impl_for_tuples!(impl_approx_eq_zero_for_tuple);

/// Trait for types that can be approximately ordered with each other.
///
/// This ordering should be total.
pub trait ApproxOrd: ApproxEq {
    /// Returns the ordering relation between `self` and `other` according to
    /// the precision.
    fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering;
}
impl ApproxOrd for f64 {
    fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering {
        match self.approx_eq(other, prec) {
            true => Ordering::Equal,
            false => self.total_cmp(other),
        }
    }
}
impl ApproxOrd for f32 {
    fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering {
        match self.approx_eq(other, prec) {
            true => Ordering::Equal,
            false => self.total_cmp(other),
        }
    }
}
impl<T: ApproxOrd> ApproxOrd for [T] {
    fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering {
        std::iter::zip(self, other)
            .map(|(a, b)| a.approx_cmp(b, prec))
            .find(|&ord| ord != Ordering::Equal)
            .unwrap_or_else(|| self.len().cmp(&other.len()))
    }
}
impl<T: ApproxOrd, const N: usize> ApproxOrd for [T; N] {
    fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering {
        <[T]>::approx_cmp(self, other, prec)
    }
}
impl<T: ApproxOrd> ApproxOrd for Vec<T> {
    fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering {
        <[T]>::approx_cmp(self, other, prec)
    }
}
impl<T: ApproxOrd> ApproxOrd for Box<T> {
    fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering {
        T::approx_cmp(self, other, prec)
    }
}
impl<T: ApproxOrd + ?Sized> ApproxOrd for &T {
    fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering {
        T::approx_cmp(self, other, prec)
    }
}
impl<T: ApproxOrd> ApproxOrd for Option<T> {
    fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering {
        match (self, other) {
            (None, None) => Ordering::Equal,
            (None, Some(_)) => Ordering::Less,
            (Some(_), None) => Ordering::Greater,
            (Some(a), Some(b)) => a.approx_cmp(b, prec),
        }
    }
}
macro_rules! impl_approx_ord_for_tuple {
    ($($generic_param:ident),+; $($index:tt),+) => {
        impl<$($generic_param: ApproxOrd,)+> ApproxOrd for ($($generic_param,)+) {
            fn approx_cmp(&self, other: &Self, prec: Precision) -> Ordering {
                $(
                    match self.$index.approx_cmp(&other.$index, prec) {
                        Ordering::Equal => (),
                        nonequal => return nonequal,
                    }
                )+
                Ordering::Equal
            }
        }
    };
}
impl_for_tuples!(impl_approx_ord_for_tuple);

pub trait ApproxCmpZero: ApproxEqZero {
    fn approx_cmp_zero(&self, prec: Precision) -> Ordering;
}
impl ApproxCmpZero for f32 {
    fn approx_cmp_zero(&self, prec: Precision) -> Ordering {
        (*self as f64).approx_cmp_zero(prec)
    }
}
impl ApproxCmpZero for f64 {
    fn approx_cmp_zero(&self, prec: Precision) -> Ordering {
        if self.approx_eq_zero(prec) {
            Ordering::Equal
        } else if self.is_sign_positive() {
            Ordering::Greater
        } else {
            Ordering::Less
        }
    }
}
impl<T: ApproxCmpZero> ApproxCmpZero for Box<T> {
    fn approx_cmp_zero(&self, prec: Precision) -> Ordering {
        T::approx_cmp_zero(self, prec)
    }
}
impl<T: ApproxCmpZero> ApproxCmpZero for &T {
    fn approx_cmp_zero(&self, prec: Precision) -> Ordering {
        T::approx_cmp_zero(self, prec)
    }
}
macro_rules! impl_approx_cmp_zero_for_tuple {
    ($($generic_param:ident),+; $($index:tt),+) => {
        impl<$($generic_param: ApproxCmpZero,)+> ApproxCmpZero for ($($generic_param,)+) {
            fn approx_cmp_zero(&self, prec: Precision) -> Ordering {
                $(
                    match self.$index.approx_cmp_zero(prec) {
                        Ordering::Equal => (),
                        nonequal => return nonequal,
                    }
                )+
                Ordering::Equal
            }
        }
    };
}
impl_for_tuples!(impl_approx_cmp_zero_for_tuple);

///Trait for types that can be interned (component-wise) in a [`crate::FloatPool`].
pub trait ApproxInternable {
    /// Interns every float in the object by calling `f`.
    fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F);
}

impl ApproxInternable for f64 {
    fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F) {
        f(self)
    }
}

impl ApproxInternable for f32 {
    fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F) {
        let mut x = *self as f64;
        f(&mut x);
        *self = x as f32;
    }
}

impl<T: ApproxInternable> ApproxInternable for [T] {
    fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F) {
        self.into_iter().for_each(|x| x.intern_floats(f));
    }
}

impl<T: ApproxInternable, const N: usize> ApproxInternable for [T; N] {
    fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F) {
        <[T]>::intern_floats(self, f);
    }
}

impl<T: ApproxInternable> ApproxInternable for Vec<T> {
    fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F) {
        <[T]>::intern_floats(self, f);
    }
}
impl<T: ApproxInternable> ApproxInternable for Box<T> {
    fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F) {
        T::intern_floats(self, f);
    }
}

impl<T: ApproxInternable> ApproxInternable for &mut T {
    fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F) {
        T::intern_floats(self, f);
    }
}

impl<T: ApproxInternable> ApproxInternable for Option<T> {
    fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F) {
        if let Some(inner) = self {
            inner.intern_floats(f);
        }
    }
}
/// Trait for types that can be stored in a [`crate::ApproxHashMap`].
pub trait ApproxHash: ApproxInternable {
    /// Returns whether `self` and `other` are exactly equal, assuming both have
    /// already been interned using `intern_floats()`.
    fn interned_eq(&self, other: &Self) -> bool;

    /// Hashes the object, assuming it has already been interned using
    /// `intern_floats()`.
    fn interned_hash<H: Hasher>(&self, state: &mut H);
}
impl ApproxHash for f64 {
    fn interned_eq(&self, other: &Self) -> bool {
        self.to_bits() == other.to_bits()
    }

    fn interned_hash<H: Hasher>(&self, state: &mut H) {
        self.to_bits().hash(state);
    }
}
impl ApproxHash for f32 {
    fn interned_eq(&self, other: &Self) -> bool {
        self.to_bits() == other.to_bits()
    }

    fn interned_hash<H: Hasher>(&self, state: &mut H) {
        self.to_bits().hash(state);
    }
}
impl<T: ApproxHash> ApproxHash for [T] {
    fn interned_eq(&self, other: &Self) -> bool {
        self.len() == other.len() && std::iter::zip(self, other).all(|(a, b)| a.interned_eq(b))
    }

    fn interned_hash<H: Hasher>(&self, state: &mut H) {
        self.len().hash(state);
        self.iter().for_each(|x| x.interned_hash(state));
    }
}
impl<T: ApproxHash, const N: usize> ApproxHash for [T; N] {
    fn interned_eq(&self, other: &Self) -> bool {
        <[T]>::interned_eq(self, other)
    }

    fn interned_hash<H: Hasher>(&self, state: &mut H) {
        <[T]>::interned_hash(self, state);
    }
}
impl<T: ApproxHash> ApproxHash for Vec<T> {
    fn interned_eq(&self, other: &Self) -> bool {
        <[T]>::interned_eq(self, other)
    }

    fn interned_hash<H: Hasher>(&self, state: &mut H) {
        <[T]>::interned_hash(self, state);
    }
}
impl<T: ApproxHash> ApproxHash for Box<T> {
    fn interned_eq(&self, other: &Self) -> bool {
        T::interned_eq(self, other)
    }

    fn interned_hash<H: Hasher>(&self, state: &mut H) {
        T::interned_hash(self, state);
    }
}
impl<T: ApproxHash> ApproxHash for &mut T {
    fn interned_eq(&self, other: &Self) -> bool {
        T::interned_eq(self, other)
    }

    fn interned_hash<H: Hasher>(&self, state: &mut H) {
        T::interned_hash(self, state);
    }
}
impl<T: ApproxHash> ApproxHash for Option<T> {
    fn interned_eq(&self, other: &Self) -> bool {
        match (self, other) {
            (None, None) => true,
            (None, Some(_)) | (Some(_), None) => false,
            (Some(a), Some(b)) => a.interned_eq(b),
        }
    }

    fn interned_hash<H: Hasher>(&self, state: &mut H) {
        std::mem::discriminant(self).hash(state);
        if let Some(inner) = self {
            inner.interned_hash(state);
        }
    }
}
macro_rules! impl_approx_internable_for_tuple {
    ($($generic_param:ident),+; $($index:tt),+) => {
        impl<$($generic_param: ApproxInternable,)+> ApproxInternable for ($($generic_param,)+) {
            fn intern_floats<F: FnMut(&mut f64)>(&mut self, f: &mut F) {
                $(self.$index.intern_floats(f);)+
            }
        }
    };
}

macro_rules! impl_approx_hash_for_tuple {
    ($($generic_param:ident),+; $($index:tt),+) => {
        impl<$($generic_param: ApproxHash,)+> ApproxHash for ($($generic_param,)+) {
            fn interned_eq(&self, other: &Self) -> bool {
                $(self.$index.interned_eq(&other.$index))&&+
            }

            fn interned_hash<H: Hasher>(&self, state: &mut H) {
                $(self.$index.interned_hash(state);)+
            }
        }
    };
}
impl_for_tuples!(impl_approx_internable_for_tuple);
impl_for_tuples!(impl_approx_hash_for_tuple);