sequoia-wot 0.15.0

An implementation of OpenPGP's web of trust.
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
use std::clone::Clone;
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::collections::HashSet;
use std::fmt::Debug;
use std::hash::Hash;
use std::iter::FromIterator;

const TRACE: bool = false;

/// In order to use a BinaryHeap, the *values* need to be `Ord`.  This
/// means when comparing two `Element`s, we only compare their values,
/// not their keys.
///
/// We also want the keys to be `Ord` so we can efficiently dedup it.
#[derive(Debug)]
struct Element<K, V>
    where K: Ord + Hash + Clone + Debug,
          V: Ord + Debug,
{
    key: K,
    value: V,
}

impl<K, V> Ord for Element<K, V>
    where K: Ord + Hash + Clone + Debug,
          V: Ord + Debug,
{
    fn cmp(&self, other: &Self) -> Ordering {
        self.value.cmp(&other.value)
            .then(self.key.cmp(&other.key).reverse())
    }
}

impl<K, V> PartialOrd for Element<K, V>
    where K: Ord + Hash + Clone + Debug,
          V: Ord + Debug,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<K, V> PartialEq for Element<K, V>
    where K: Ord + Hash + Clone + Debug,
          V: Ord + Debug,
{
    fn eq(&self, other: &Self) -> bool {
        self.cmp(&other) == Ordering::Equal
    }
}

impl<K, V> Eq for Element<K, V>
    where K: Ord + Hash + Clone + Debug,
          V: Ord + Debug,
{
}

/// A dedupping max-priority queue.
///
/// This data structure implements a priority queue for key-value
/// pairs.  When an element is popped from the priority queue, the
/// element with the *largest* value is popped.  (If there are
/// multiple such values, one is returned.)
///
/// When inserting a key into the priority queue, if there is already
/// an element with the same key, then the larger value is kept.
pub(super) struct PriorityQueue<K, V>
    where K: Ord + Hash + Clone + Debug,
          V: Ord + Debug,
{
    // When pending is larger than this, then convert pending into a
    // BH.
    threshold: usize,

    // The elements in the queue, sorted by value.
    bh: BinaryHeap<Element<K, V>>,

    // Elements that we haven't added to the binary heap yet.
    pending: Vec<Element<K, V>>,

    // Keys that are present in bh or pending.  If None, then we have
    // a duplicate.
    have_keys: Option<HashSet<K>>,

    // We're tidy if:
    //
    //   - We only have elements in bh.
    //   - We only have elements in pending, and they are sorted and
    //     deduped.
    is_tidy: bool,
}

const THRESHOLD: usize = 16;

impl<K, V> PriorityQueue<K, V>
    where K: Ord + Hash + Clone + Debug,
          V: Ord + Debug,
{
    pub fn new() -> Self {
        Self::with_threshold(THRESHOLD)
    }

    pub fn with_threshold(threshold: usize) -> Self {
        Self {
            threshold,

            bh: BinaryHeap::new(),
            pending: Vec::with_capacity(threshold),

            have_keys: Some(HashSet::new()),

            is_tidy: true,
        }
    }

    fn tidy(&mut self) {
        tracer!(TRACE, "PriorityQueue::tidy");

        if self.is_tidy {
            assert!(self.bh.is_empty()
                    || self.pending.len() == 0);
            assert!(self.have_keys.is_some());
            self.pending[..].windows(2).for_each(|v| {
                assert!(v[0] <= v[1]);
            });
            return;
        }

        t!("pre: bh: {} elements; pending:\n{}",
           self.bh.iter().count(),
           self.pending.iter().enumerate().map(|(i, e)| {
               format!("  {}. {:?}: {:?}", i, e.key, e.value)
           })
           .collect::<Vec<_>>()
           .join("\n"));

        // If there are no duplicates, it is safe to just merge
        // pending into bh.
        if self.have_keys.is_some()
            && (! self.bh.is_empty()
                || self.pending.len() > self.threshold)
        {
            t!("  No duplicates, merging pending into bh");
            self.bh.extend(self.pending.drain(..));
            self.is_tidy = true;
            return;
        }

        if self.have_keys.is_none() {
            t!("  Have duplicates (moving bh to pending).");
            if ! self.bh.is_empty() {
                let bh = std::mem::replace(&mut self.bh, BinaryHeap::new());
                self.pending.append(&mut bh.into_sorted_vec());
            }
        }

        // We need to dedup by key, not value.  But pending needs
        // to be sorted by value.  Since pending is probably
        // nearly sorted, we sort the keys in a separate vector.

        // Assume that the values are nearly sorted.
        let mut keys: Vec<(&K, usize)>
            = self.pending.iter().enumerate().map(|(i, e)| {
                (&e.key, i)
            })
            .collect();
        // Sort by the keys.
        keys.sort_by_key(|a| a.0);

        // Now dedup pending.  For a given key, we want to keep
        // the maximum value.
        keys.dedup_by(|a, b| {
            if a.0 == b.0 {
                // a will be remove.  Store the larger value in b.
                if self.pending[a.1].value > self.pending[b.1].value {
                    b.1 = a.1
                }
                true
            } else {
                false
            }
        });

        if keys.len() != self.pending.len() {
            // We deduped something.
            let mut retain = vec![ false; self.pending.len() ];
            for (_, i) in keys.into_iter() {
                retain[i] = true;
            }

            let mut i = 0;
            self.pending.retain(|_| (retain[i], i += 1).0);
        }

        self.pending.sort_by(|a, b| {
            a.value.cmp(&b.value)
                // Make it deterministic by also considering keys.  We
                // want the minimal key to be returned first.  Since
                // we return the maximum, negate the comparison.
                .then(a.key.cmp(&b.key).reverse())
        });

        self.have_keys
            = Some(HashSet::from_iter(
                self.pending.iter().map(|e| e.key.clone())));
        self.is_tidy = true;

        t!("pending (post):\n{}",
           self.pending.iter().enumerate().map(|(i, e)| {
               format!("  {}. {:?}: {:?}", i, e.key, e.value)
           })
           .collect::<Vec<_>>()
           .join("\n"));
    }

    pub fn push(&mut self, key: K, value: V) {
        tracer!(TRACE, "PriorityQueue::push");
        t!("<{:?}, {:?}>", key, value);

        if ! self.bh.is_empty() || self.pending.len() > 0 {
            // Have already have at least one element.  We're adding
            // another.  It's probably out of order; it could be a
            // duplicate.
            self.is_tidy = false;

            // Note that this key is in the queue.
            if let Some(ref mut have_keys) = self.have_keys {
                if have_keys.replace(key.clone()).is_some() {
                    // DUP!
                    t!("DUP!");
                    self.have_keys = None;
                }
            }
        } else {
            // bh and pending are empty.
            assert!(self.have_keys.as_ref().expect("some").is_empty());
            self.is_tidy = true;
            self.have_keys
                = Some(HashSet::from_iter(std::iter::once(key.clone())));
        }

        self.pending.push(Element { key, value });
    }

    pub fn pop(&mut self) -> Option<(K, V)> {
        tracer!(TRACE, "PriorityQueue::pop");

        self.tidy();
        if let Some((key, value)) = self.pending.pop()
            .or_else(|| self.bh.pop())
            .map(|e| (e.key, e.value))
        {
            t!(" => <{:?}, {:?}>", key, value);
            if let Some(ref mut have_keys) = self.have_keys {
                let was_present = have_keys.remove(&key);
                assert!(was_present);
            } else if self.bh.is_empty()
                && self.pending.len() == 1
            {
                // We don't have any elements left; we clearly don't
                // have any duplicates.
                self.have_keys = Some(HashSet::new());
            }

            Some((key, value))
        } else {
            t!(" => None");
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use quickcheck::quickcheck;

    const THRESHOLDS: &[usize] = &[ 1, 4, 16, 32 ];

    #[test]
    fn simple() {
        for &t in THRESHOLDS.iter() {
            let mut pq: PriorityQueue<isize, isize>
                = PriorityQueue::with_threshold(t);

            pq.push(0, 0);
            pq.push(1, 1);
            pq.push(2, 2);
            pq.push(3, 3);
            pq.push(4, 4);
            pq.push(5, 5);

            assert_eq!(pq.pop(), Some((5, 5)));
            assert_eq!(pq.pop(), Some((4, 4)));
            assert_eq!(pq.pop(), Some((3, 3)));
            assert_eq!(pq.pop(), Some((2, 2)));
            assert_eq!(pq.pop(), Some((1, 1)));
            assert_eq!(pq.pop(), Some((0, 0)));
            assert_eq!(pq.pop(), None);
            assert_eq!(pq.pop(), None);

            let mut pq: PriorityQueue<isize, isize>
                = PriorityQueue::with_threshold(t);

            pq.push(0, 0);
            pq.push(1, -1);
            pq.push(2, -2);
            pq.push(3, -3);
            pq.push(4, -4);
            pq.push(5, -5);

            assert_eq!(pq.pop(), Some((0, 0)));
            assert_eq!(pq.pop(), Some((1, -1)));
            assert_eq!(pq.pop(), Some((2, -2)));
            assert_eq!(pq.pop(), Some((3, -3)));
            assert_eq!(pq.pop(), Some((4, -4)));
            assert_eq!(pq.pop(), Some((5, -5)));
            assert_eq!(pq.pop(), None);
            assert_eq!(pq.pop(), None);

            let mut pq: PriorityQueue<isize, isize>
                = PriorityQueue::with_threshold(t);

            pq.push(0, 0);
            pq.push(1, 1);
            pq.push(5, 5);
            pq.push(2, 2);
            pq.push(4, 4);
            pq.push(3, 3);

            assert_eq!(pq.pop(), Some((5, 5)));
            assert_eq!(pq.pop(), Some((4, 4)));
            assert_eq!(pq.pop(), Some((3, 3)));
            assert_eq!(pq.pop(), Some((2, 2)));
            assert_eq!(pq.pop(), Some((1, 1)));
            assert_eq!(pq.pop(), Some((0, 0)));
            assert_eq!(pq.pop(), None);
            assert_eq!(pq.pop(), None);

            let mut pq: PriorityQueue<isize, isize>
                = PriorityQueue::with_threshold(t);
            assert_eq!(pq.pop(), None);

            pq.push(0, 0);
            pq.push(0, 0);
            assert_eq!(pq.pop(), Some((0, 0)));
            assert_eq!(pq.pop(), None);

            let mut pq: PriorityQueue<isize, isize>
                = PriorityQueue::with_threshold(t);
            assert_eq!(pq.pop(), None);

            pq.push(0, 0);
            pq.push(0, 0);
            assert_eq!(pq.pop(), Some((0, 0)));
            pq.push(0, 0);
            assert_eq!(pq.pop(), Some((0, 0)));
            assert_eq!(pq.pop(), None);
        }
    }

    #[test]
    fn duplicates() {
        let mut pq: PriorityQueue<isize, isize> = PriorityQueue::new();

        // Push different keys with the same value.
        for i in 0..20 {
            pq.push(i, 0);
        }
        // Push the same keys with their own value.  This should
        // overwrite the old keys.
        for i in 0..20 {
            pq.push(i, i);
        }

        // Push different keys with the same value.
        for i in 0..20 {
            pq.push(i, 0);
        }

        for i in (0..20).rev() {
            assert_eq!(pq.pop(), Some((i, i)));
        }
        assert_eq!(pq.pop(), None);
        assert_eq!(pq.pop(), None);
    }

    #[test]
    fn push_pop() {
        let mut pq: PriorityQueue<isize, isize> = PriorityQueue::new();

        // Push different keys with the same value.
        for i in 0..10 {
            pq.push(i, 0);
        }
        // Push the same keys with their own value.  This should
        // overwrite the old keys.
        for i in (0..10).rev() {
            pq.push(i, i);
            assert_eq!(pq.pop(), Some((i, i)));
        }
        assert_eq!(pq.pop(), None);
        assert_eq!(pq.pop(), None);
    }

    // Use a u8 so we have a change of a few duplicates.
    fn pq(e: Vec<(u8, u8)>, threshold: usize) -> bool {
        tracer!(TRACE, "pq");
        t!("\n\nGot {} elements; threshold: {}", e.len(), threshold);
        t!("elements: {:?}", e);

        let mut expected = e.clone();

        // Sort by keys.
        expected.sort_by(|a, b| {
            a.0.cmp(&b.0)
        });
        // Dedup keys.
        expected.dedup_by(|a, b| {
            if a.0 == b.0 {
                if a.1 > b.1 {
                    b.1 = a.1;
                }

                true
            } else {
                false
            }
        });
        // Sort by value (largest first) then by key.
        expected.sort_by(|a, b| {
            a.1.cmp(&b.1).reverse()
                .then(a.0.cmp(&b.0))
        });

        let mut pq: PriorityQueue<u8, u8>
            = PriorityQueue::with_threshold(threshold);

        // Add everything to the priority queue.  Every third
        // push, do a pop.  Then add those again in the next
        // round.
        let mut popped = e.clone();
        for _i in 0..5 {
            let topush = popped;
            popped = Vec::new();
            for (j, (k, v)) in topush.iter().enumerate() {
                pq.push(*k, *v);

                if j % 3 == 1 || j % 7 == 1 {
                    // Pop one.
                    let (k, v) = pq.pop().unwrap();
                    assert!(e.contains(&(k, v)));
                    popped.push((k, v));
                }
            }
        }
        for (k, v) in popped.into_iter() {
            pq.push(k, v);
        }


        let mut got = Vec::new();
        while let Some((k, v)) = pq.pop() {
            got.push((k, v));
        }

        t!("       e: {:?}", e);
        t!("expected: {:?}", expected);
        t!("     got: {:?}", got);

        if got == expected {
            true
        } else {
            t!("BAD");
            false
        }
    }

    quickcheck! {
        fn pq1(e: Vec<(u8, u8)>) -> bool {
            pq(e, 1)
        }
    }
    quickcheck! {
        fn pq4(e: Vec<(u8, u8)>) -> bool {
            pq(e, 4)
        }
    }
    quickcheck! {
        fn pq16(e: Vec<(u8, u8)>) -> bool {
            pq(e, 16)
        }
    }
    quickcheck! {
        fn pq64(e: Vec<(u8, u8)>) -> bool {
            pq(e, 64)
        }
    }

    #[test]
    fn extra() {
        pq([(75, 0), (75, 0)].to_vec(), 1);
    }
}