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
|
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use metrohash::MetroHash64;
use std::hash::{BuildHasher, Hasher};
use std::marker::PhantomData;
use typenum::{Unsigned, U64};
pub(crate) fn is_sorted<A, I>(l: I) -> bool
where
I: IntoIterator<Item = A>,
A: Ord,
{
let mut it = l.into_iter().peekable();
loop {
match (it.next(), it.peek()) {
(_, None) => return true,
(Some(ref a), Some(b)) if a > b => return false,
_ => (),
}
}
}
pub(crate) struct LolHasher<N: Unsigned = U64> {
state: u64,
shift: usize,
size: PhantomData<N>,
}
impl<N: Unsigned> LolHasher<N> {
fn feed_me(&mut self, byte: u8) {
self.state ^= u64::from(byte) << self.shift;
self.shift += 8;
if self.shift >= 64 {
self.shift = 0;
}
}
}
impl<N: Unsigned> Hasher for LolHasher<N> {
fn write(&mut self, bytes: &[u8]) {
for byte in bytes {
self.feed_me(*byte)
}
}
fn finish(&self) -> u64 {
if N::USIZE == 64 {
self.state
} else {
self.state & ((1 << N::USIZE) - 1)
}
}
}
impl<N: Unsigned> Default for LolHasher<N> {
fn default() -> Self {
LolHasher {
state: 0,
shift: 0,
size: PhantomData,
}
}
}
pub(crate) struct MetroHashBuilder {
seed: u64,
}
impl MetroHashBuilder {
pub(crate) fn new(seed: u64) -> Self {
MetroHashBuilder { seed }
}
pub(crate) fn seed(&self) -> u64 {
self.seed
}
}
impl BuildHasher for MetroHashBuilder {
type Hasher = MetroHash64;
fn build_hasher(&self) -> Self::Hasher {
MetroHash64::with_seed(self.seed)
}
}
|