graph_safe_compare 0.2.1

Equivalence predicate that can handle cyclic, shared, and very-deep graphs.
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
mod common
{
    pub mod rc_pair;
}

use {
    common::rc_pair::*,
    core::convert::identity,
};


/// Use `Arc` just because it's different than
/// `graph_safe_compare::generic::equiv_classes::premade::Rc`.  The multi-thread ability
/// provided by `Arc` is ignored.
mod custom_rc
{
    extern crate alloc;

    use {
        alloc::sync::Arc,
        core::{
            cell::Cell,
            ops::Deref,
        },
        graph_safe_compare::generic::equiv_classes::{
            self,
            Class,
        },
    };

    #[derive(Clone)]
    pub struct Rc(Arc<Cell<Class<Self>>>);

    impl Deref for Rc
    {
        type Target = Cell<Class<Self>>;

        fn deref(&self) -> &Self::Target
        {
            &*self.0
        }
    }

    impl equiv_classes::Rc for Rc
    {
        fn new(val: Cell<Class<Self>>) -> Self
        {
            Self(Arc::new(val))
        }
    }
}


/// Use `BTreeMap` just because it's different than
/// `graph_safe_compare::generic::equiv_classes::premade::HashMap`.  The ordering of keys
/// provided by `BTreeMap` is ignored.
mod custom_table
{
    extern crate alloc;

    use {
        super::{
            My,
            Node,
        },
        alloc::collections::BTreeMap,
        graph_safe_compare::generic::equiv_classes::Table,
    };

    #[derive(Default)]
    pub struct Map(BTreeMap<<My as Node>::Id, super::custom_rc::Rc>);

    impl Table for Map
    {
        type Node = My;
        type Rc = super::custom_rc::Rc;

        fn get(
            &self,
            k: &<Self::Node as Node>::Id,
        ) -> Option<&Self::Rc>
        {
            self.0.get(k)
        }

        fn insert(
            &mut self,
            k: <My as Node>::Id,
            v: Self::Rc,
        )
        {
            self.0.insert(k, v);
        }
    }
}


/// Use `LinkedList` just because it's different than
/// `graph_safe_compare::wide_safe::recursion::stack::RecurStack`.
///
/// Also, enables this integration test to be used when the `graph_safe_compare` crate is
/// built without the "std" feature enabled, and enables running the test cases of very-deep
/// shapes.
mod custom_recur_stack
{
    extern crate alloc;

    use {
        super::{
            other_recur_stack::OtherStack,
            My,
        },
        alloc::collections::LinkedList,
        graph_safe_compare::{
            basic::recursion::callstack::CallStack,
            generic::equiv::{
                self,
                CounterpartsResult,
                EdgesIter,
                Equiv,
                RecurMode,
            },
            Cmp,
            Node,
        },
    };

    #[derive(Default)]
    pub struct ListStack(LinkedList<EdgesIter<My>>);

    #[derive(Debug)]
    pub struct ExhaustedError;

    impl<P> RecurMode<P> for ListStack
    where
        P: equiv::Params<Node = My, RecurMode = Self>,
        ExhaustedError: Into<P::Error>,
    {
        type Error = ExhaustedError;

        fn recur(
            it: &mut Equiv<P>,
            edges_iter: EdgesIter<P::Node>,
        ) -> Result<<P::Node as Node>::Cmp, Self::Error>
        {
            if it.recur_mode.0.len() < 2_usize.pow(30) {
                it.recur_mode.0.push_front(edges_iter);
                Ok(Cmp::new_equiv())
            }
            else {
                Err(ExhaustedError)
            }
        }

        fn next(&mut self) -> Option<CounterpartsResult<P::Node>>
        {
            while let Some(edges_iter) = self.0.front_mut() {
                let next = edges_iter.next();
                if next.is_some() {
                    return next;
                }
                // Remove empty iterators from the stack, after returning `Some` - just to be
                // different and to have inefficient memory usage without "tail-call elimination".
                drop(self.0.pop_front());
            }
            None
        }

        fn reset(mut self) -> Self
        {
            self.0.clear();
            self
        }
    }

    impl From<CallStack> for ListStack
    {
        fn from(_: CallStack) -> Self
        {
            Self::default()
        }
    }

    impl From<OtherStack> for ListStack
    {
        fn from(_: OtherStack) -> Self
        {
            Self::default()
        }
    }
}

/// A different type, to test combining two custom `RecurMode` types.
mod other_recur_stack
{
    use {
        super::My,
        graph_safe_compare::{
            generic::equiv::{
                self,
                CounterpartsResult,
                EdgesIter,
                Equiv,
                RecurMode,
            },
            Cmp,
            Node,
        },
    };

    #[derive(Default)]
    pub struct OtherStack;

    pub enum OtherStackError<R>
    {
        Novel,
        Recur(R),
    }

    /// Like `CallStack` but with custom error.
    impl<P> RecurMode<P> for OtherStack
    where
        P: equiv::Params<Node = My, RecurMode = Self>,
        OtherStackError<P::Error>: Into<P::Error>,
    {
        type Error = OtherStackError<P::Error>;

        fn recur(
            it: &mut Equiv<P>,
            edges_iter: EdgesIter<P::Node>,
        ) -> Result<<P::Node as Node>::Cmp, Self::Error>
        {
            if true {
                for next in edges_iter {
                    match next {
                        Ok([a, b]) => match it.equiv_main(a, b) {
                            Ok(cmp) if cmp.is_equiv() => (),
                            result => return result.map_err(OtherStackError::Recur),
                        },
                        Err(cmp_amount_edges) => return Ok(cmp_amount_edges),
                    }
                }
                Ok(Cmp::new_equiv())
            }
            else {
                Err(OtherStackError::Novel)
            }
        }

        fn next(&mut self) -> Option<CounterpartsResult<P::Node>>
        {
            None
        }

        fn reset(self) -> Self
        {
            self
        }
    }
}


/// Use our own (dummy) PRNG to test not depending on any from the crate.
mod custom_rng
{
    use graph_safe_compare::cycle_safe::modes::interleave::random;

    #[derive(Default)]
    pub struct PseudoPseudoRNG(u128);

    impl random::NumberGenerator for PseudoPseudoRNG
    {
        fn rand_upto(
            &mut self,
            exclusive_end: std::num::NonZeroU16,
        ) -> u16
        {
            self.0 = self.0.wrapping_mul(42);
            self.0 = self.0.wrapping_add(987654321);
            self.0 as u16 % exclusive_end
        }
    }
}


/// Use our custom `Table`, `Rc`, `RecurMode`, and `NumberGenerator` types.
fn custom_equiv(
    a: My,
    b: My,
) -> bool
{
    use {
        custom_recur_stack::{
            ExhaustedError,
            ListStack,
        },
        custom_rng::PseudoPseudoRNG,
        graph_safe_compare::{
            cycle_safe::modes::interleave,
            generic::precheck_interleave::{
                self,
                InterleaveError,
                PrecheckError,
            },
        },
        other_recur_stack::{
            OtherStack,
            OtherStackError,
        },
    };

    struct InterleaveArgs;

    impl interleave::Params for InterleaveArgs
    {
        type Node = My;
        type RNG = PseudoPseudoRNG;
        type Table = custom_table::Map;

        // Use custom values for these constants, not their defaults.
        const FAST_LIMIT_MAX: u16 = Self::PRECHECK_LIMIT / 4;
        const PRECHECK_LIMIT: u16 = 2000;
        const SLOW_LIMIT: u16 = Self::PRECHECK_LIMIT / 2;
    }

    // Exercise the call-stack for the precheck since that is limited and will not overflow the
    // stack when the stack is already shallow, and use the list-stack for the interleave so great
    // depth is supported since an input could be very-deep.
    let precheck_on_callstack = {
        #[derive(Debug)]
        enum ExhaustedOrOtherError
        {
            ExhaustedList,
            OtherNovel,
        }

        impl From<OtherStackError<Self>> for PrecheckError<ExhaustedOrOtherError>
        {
            fn from(e: OtherStackError<Self>) -> Self
            {
                match e {
                    OtherStackError::Recur(e) => e,
                    OtherStackError::Novel => Self::RecurError(ExhaustedOrOtherError::OtherNovel),
                }
            }
        }

        impl From<ExhaustedError> for InterleaveError<ExhaustedOrOtherError>
        {
            fn from(_: ExhaustedError) -> Self
            {
                InterleaveError(ExhaustedOrOtherError::ExhaustedList)
            }
        }

        struct Args;

        impl precheck_interleave::Params<My> for Args
        {
            type Error = ExhaustedOrOtherError;
            type InterleaveParams = InterleaveArgs;
            type InterleaveRecurMode = ListStack;
            type PrecheckRecurMode = OtherStack;
        }

        precheck_interleave::equiv::<_, Args>(a.clone(), b.clone()).unwrap()
    };

    // Exercise our list-stack for the precheck.
    let precheck_on_liststack = {
        impl From<ExhaustedError> for PrecheckError<ExhaustedError>
        {
            fn from(e: ExhaustedError) -> Self
            {
                PrecheckError::RecurError(e)
            }
        }

        impl From<ExhaustedError> for InterleaveError<ExhaustedError>
        {
            fn from(e: ExhaustedError) -> Self
            {
                InterleaveError(e)
            }
        }

        struct Args;

        impl precheck_interleave::Params<My> for Args
        {
            type Error = ExhaustedError;
            type InterleaveParams = InterleaveArgs;
            type InterleaveRecurMode = ListStack;
            type PrecheckRecurMode = ListStack;
        }

        precheck_interleave::equiv::<_, Args>(a, b).unwrap()
    };

    assert_eq!(precheck_on_callstack, precheck_on_liststack);

    precheck_on_callstack == Ordering::Equal
}

mod eq_variation
{
    use super::*;

    tests_utils::eq_variation_mod_body!(
        custom_equiv,
        My,
        Rc<Datum>,
        identity,
        DatumAllocator::new
    );
}

tests_utils::eq_shapes_tests!(identity, DatumAllocator::new, eq_variation::MyEq::new,
                              #[cfg(all())], #[cfg(all())]);