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
// Copyright (C) 2024 The Software Heritage developers
// See the AUTHORS file at the top-level directory of this distribution
// License: GNU General Public License version 3, or any later version
// See top-level LICENSE file for more information

//! Implementations of the last type parameter of [`SinglePhf`](crate::SinglePhf) and
//! [`PartitionedPhf`](crate::PartitionedPhf) ([`DictionaryDictionary`],
//! [`PartitionedCompact`], and [`EliasFano`])

use crate::hashing::Hash;
#[cfg(feature = "hash128")]
use crate::structs::hash128;
#[cfg(feature = "hash64")]
use crate::structs::hash64;

#[cfg(all(feature = "hash64", feature = "hash128"))]
#[allow(private_bounds)] // Users shouldn't be able to impl the Encoder trait
pub trait Encoder: BackendForEncoderByHash<hash64> + BackendForEncoderByHash<hash128> {
    /// Same value as the one passed as PTHash's CLI's -e argument
    const NAME: &'static str;
}
#[cfg(all(feature = "hash64", not(feature = "hash128")))]
#[allow(private_bounds)]
pub trait Encoder: BackendForEncoderByHash<hash64> {
    /// Same value as the one passed as PTHash's CLI's -e argument
    const NAME: &'static str;
}
#[cfg(all(not(feature = "hash64"), feature = "hash128"))]
#[allow(private_bounds)]
pub trait Encoder: BackendForEncoderByHash<hash128> {
    /// Same value as the one passed as PTHash's CLI's -e argument
    const NAME: &'static str;
}
// build.rs rejects both hash64 and hash128 being disabled

/// Type trickery to make [`Hash`] implementable
pub(crate) trait BackendForEncoderByHash<H: Hash> {
    #[cfg(feature = "minimal")]
    type MinimalSinglePhfBackend: crate::backends::BackendPhf<Hash = H, Encoder = Self>;
    #[cfg(feature = "nonminimal")]
    type NonminimalSinglePhfBackend: crate::backends::BackendPhf<Hash = H, Encoder = Self>;
    #[cfg(feature = "minimal")]
    type MinimalPartitionedPhfBackend: crate::backends::BackendPhf<Hash = H, Encoder = Self>;
    #[cfg(feature = "nonminimal")]
    type NonminimalPartitionedPhfBackend: crate::backends::BackendPhf<Hash = H, Encoder = Self>;
}

#[cfg(feature = "dictionary_dictionary")]
mod dictionary_dictionary {
    use super::*;

    /// Encoder known as "D-D" in the PTHash papers
    pub struct DictionaryDictionary;
    impl Encoder for DictionaryDictionary {
        const NAME: &'static str = "dictionary_dictionary";
    }

    #[cfg(feature = "hash64")]
    impl BackendForEncoderByHash<hash64> for DictionaryDictionary {
        #[cfg(feature = "minimal")]
        type MinimalSinglePhfBackend = crate::backends::singlephf_64_dictionary_dictionary_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalSinglePhfBackend =
            crate::backends::singlephf_64_dictionary_dictionary_nonminimal;
        #[cfg(feature = "minimal")]
        type MinimalPartitionedPhfBackend =
            crate::backends::partitionedphf_64_dictionary_dictionary_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalPartitionedPhfBackend =
            crate::backends::partitionedphf_64_dictionary_dictionary_nonminimal;
    }

    #[cfg(feature = "hash128")]
    impl BackendForEncoderByHash<hash128> for DictionaryDictionary {
        #[cfg(feature = "minimal")]
        type MinimalSinglePhfBackend = crate::backends::singlephf_128_dictionary_dictionary_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalSinglePhfBackend =
            crate::backends::singlephf_128_dictionary_dictionary_nonminimal;
        #[cfg(feature = "minimal")]
        type MinimalPartitionedPhfBackend =
            crate::backends::partitionedphf_128_dictionary_dictionary_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalPartitionedPhfBackend =
            crate::backends::partitionedphf_128_dictionary_dictionary_nonminimal;
    }
}

#[cfg(feature = "dictionary_dictionary")]
pub use dictionary_dictionary::*;

#[cfg(feature = "partitioned_compact")]
mod partitioned_compact {
    use super::*;

    /// Encoder known as "DC" in the PTHash papers
    pub struct PartitionedCompact;
    impl Encoder for PartitionedCompact {
        const NAME: &'static str = "partitioned_compact";
    }

    #[cfg(feature = "hash64")]
    impl BackendForEncoderByHash<hash64> for PartitionedCompact {
        #[cfg(feature = "minimal")]
        type MinimalSinglePhfBackend = crate::backends::singlephf_64_partitioned_compact_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalSinglePhfBackend =
            crate::backends::singlephf_64_partitioned_compact_nonminimal;
        #[cfg(feature = "minimal")]
        type MinimalPartitionedPhfBackend =
            crate::backends::partitionedphf_64_partitioned_compact_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalPartitionedPhfBackend =
            crate::backends::partitionedphf_64_partitioned_compact_nonminimal;
    }

    #[cfg(feature = "hash128")]
    impl BackendForEncoderByHash<hash128> for PartitionedCompact {
        #[cfg(feature = "minimal")]
        type MinimalSinglePhfBackend = crate::backends::singlephf_128_partitioned_compact_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalSinglePhfBackend =
            crate::backends::singlephf_128_partitioned_compact_nonminimal;
        #[cfg(feature = "minimal")]
        type MinimalPartitionedPhfBackend =
            crate::backends::partitionedphf_128_partitioned_compact_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalPartitionedPhfBackend =
            crate::backends::partitionedphf_128_partitioned_compact_nonminimal;
    }
}

#[cfg(feature = "partitioned_compact")]
pub use partitioned_compact::*;

#[cfg(feature = "elias_fano")]
mod elias_fano {
    use super::*;

    /// Encoder known as "EF" in the PTHash papers
    pub struct EliasFano;
    impl Encoder for EliasFano {
        const NAME: &'static str = "elias_fano";
    }

    #[cfg(feature = "hash64")]
    impl BackendForEncoderByHash<hash64> for EliasFano {
        #[cfg(feature = "minimal")]
        type MinimalSinglePhfBackend = crate::backends::singlephf_64_elias_fano_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalSinglePhfBackend = crate::backends::singlephf_64_elias_fano_nonminimal;
        #[cfg(feature = "minimal")]
        type MinimalPartitionedPhfBackend = crate::backends::partitionedphf_64_elias_fano_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalPartitionedPhfBackend =
            crate::backends::partitionedphf_64_elias_fano_nonminimal;
    }

    #[cfg(feature = "hash128")]
    impl BackendForEncoderByHash<hash128> for EliasFano {
        #[cfg(feature = "minimal")]
        type MinimalSinglePhfBackend = crate::backends::singlephf_128_elias_fano_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalSinglePhfBackend = crate::backends::singlephf_128_elias_fano_nonminimal;
        #[cfg(feature = "minimal")]
        type MinimalPartitionedPhfBackend = crate::backends::partitionedphf_128_elias_fano_minimal;
        #[cfg(feature = "nonminimal")]
        type NonminimalPartitionedPhfBackend =
            crate::backends::partitionedphf_128_elias_fano_nonminimal;
    }
}

#[cfg(feature = "elias_fano")]
pub use elias_fano::*;