-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathhash.rs
More file actions
229 lines (208 loc) · 7.23 KB
/
Copy pathhash.rs
File metadata and controls
229 lines (208 loc) · 7.23 KB
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::any::Any;
use std::hash::Hash;
use std::hash::Hasher;
use vortex_buffer::BitBuffer;
use vortex_buffer::Buffer;
use vortex_mask::Mask;
use crate::patches::Patches;
use crate::validity::Validity;
/// The equality mode for structural equality and hashing of arrays.
///
/// This configuration option defines how precise the hash/equals results are for the set of
/// data buffers backing the array.
#[derive(Clone, Copy, Debug)]
pub enum EqMode {
/// Data buffers are compared by their pointer and length only. This is the fastest option, but
/// may lead to false negatives if two arrays contain identical data but are backed by
/// different buffers.
Ptr,
/// Data buffers are compared by their full content. This is the slowest option, but guarantees
/// that two arrays with identical data will be considered equal.
Value,
}
/// A hash trait for arrays that represents structural equality with a configurable equality mode.
/// This trait is used primarily to implement common subtree elimination and other
/// array-based caching mechanisms.
///
/// The equality mode defines what level of structural equality is represented. See
/// [`EqMode`] for more details.
///
/// Note that where [`EqMode::Ptr`] is used, the hash is only valid for the lifetime of the
/// object.
pub trait ArrayHash {
fn array_hash<H: Hasher>(&self, state: &mut H, eq_mode: EqMode);
}
/// A dynamic version of [`ArrayHash`].
pub trait DynArrayDataHash: private::SealedHash {
fn dyn_array_hash(&self, state: &mut dyn Hasher, eq_mode: EqMode);
}
impl<T: ArrayHash + ?Sized> DynArrayDataHash for T {
fn dyn_array_hash(&self, mut state: &mut dyn Hasher, eq_mode: EqMode) {
ArrayHash::array_hash(self, &mut state, eq_mode);
}
}
/// An equality trait for arrays that represents structural equality with a configurable equality
/// mode. This trait is used primarily to implement common subtree elimination and other
/// array-based caching mechanisms.
///
/// The equality mode defines what level of structural equality is represented. See
/// [`EqMode`] for more details.
pub trait ArrayEq {
fn array_eq(&self, other: &Self, eq_mode: EqMode) -> bool;
}
/// A dynamic version of [`ArrayEq`].
pub trait DynArrayDataEq: private::SealedEq {
fn dyn_array_eq(&self, other: &dyn Any, eq_mode: EqMode) -> bool;
}
impl<T: ArrayEq + 'static> DynArrayDataEq for T {
fn dyn_array_eq(&self, other: &dyn Any, eq_mode: EqMode) -> bool {
other
.downcast_ref::<Self>()
.is_some_and(|other| ArrayEq::array_eq(self, other, eq_mode))
}
}
mod private {
use crate::ArrayEq;
use crate::ArrayHash;
pub trait SealedHash {}
impl<T: ArrayHash + ?Sized> SealedHash for T {}
pub trait SealedEq {}
impl<T: ArrayEq + ?Sized> SealedEq for T {}
}
impl<T: Hash> ArrayHash for Buffer<T> {
fn array_hash<H: Hasher>(&self, state: &mut H, eq_mode: EqMode) {
match eq_mode {
EqMode::Ptr => {
self.as_ptr().hash(state);
self.len().hash(state);
}
EqMode::Value => {
self.as_ref().hash(state);
}
}
}
}
impl<T: PartialEq> ArrayEq for Buffer<T> {
fn array_eq(&self, other: &Self, eq_mode: EqMode) -> bool {
match eq_mode {
EqMode::Ptr => self.as_ptr() == other.as_ptr() && self.len() == other.len(),
EqMode::Value => self.as_ref() == other.as_ref(),
}
}
}
impl ArrayHash for BitBuffer {
fn array_hash<H: Hasher>(&self, state: &mut H, eq_mode: EqMode) {
match eq_mode {
EqMode::Ptr => {
self.inner().as_ptr().hash(state);
self.offset().hash(state);
self.len().hash(state);
}
EqMode::Value => {
// NOTE(ngates): this is really rather expensive...
for chunk in self.chunks().iter_padded() {
chunk.hash(state);
}
}
}
}
}
impl ArrayEq for BitBuffer {
fn array_eq(&self, other: &Self, eq_mode: EqMode) -> bool {
match eq_mode {
EqMode::Ptr => {
self.inner().as_ptr() == other.inner().as_ptr()
&& self.offset() == other.offset()
&& self.len() == other.len()
}
EqMode::Value => self.eq(other),
}
}
}
impl<T: ArrayHash> ArrayHash for Option<T> {
fn array_hash<H: Hasher>(&self, state: &mut H, eq_mode: EqMode) {
match self {
Some(value) => {
true.hash(state);
value.array_hash(state, eq_mode);
}
None => {
false.hash(state);
}
}
}
}
impl<T: ArrayEq> ArrayEq for Option<T> {
fn array_eq(&self, other: &Self, eq_mode: EqMode) -> bool {
match (self, other) {
(Some(v1), Some(v2)) => v1.array_eq(v2, eq_mode),
(None, None) => true,
_ => false,
}
}
}
impl ArrayHash for Mask {
fn array_hash<H: Hasher>(&self, state: &mut H, eq_mode: EqMode) {
std::mem::discriminant(self).hash(state);
match self {
Mask::AllTrue(len) => {
len.hash(state);
}
Mask::AllFalse(len) => {
len.hash(state);
}
Mask::Values(values) => {
values.bit_buffer().array_hash(state, eq_mode);
}
}
}
}
impl ArrayEq for Mask {
fn array_eq(&self, other: &Self, eq_mode: EqMode) -> bool {
match (self, other) {
(Mask::AllTrue(len1), Mask::AllTrue(len2)) => len1 == len2,
(Mask::AllFalse(len1), Mask::AllFalse(len2)) => len1 == len2,
(Mask::Values(buf1), Mask::Values(buf2)) => {
buf1.bit_buffer().array_eq(buf2.bit_buffer(), eq_mode)
}
_ => false,
}
}
}
impl ArrayHash for Validity {
fn array_hash<H: Hasher>(&self, state: &mut H, eq_mode: EqMode) {
std::mem::discriminant(self).hash(state);
if let Validity::Array(array) = self {
array.array_hash(state, eq_mode);
}
}
}
impl ArrayEq for Validity {
fn array_eq(&self, other: &Self, eq_mode: EqMode) -> bool {
match (self, other) {
(Validity::AllValid, Validity::AllValid) => true,
(Validity::AllInvalid, Validity::AllInvalid) => true,
(Validity::NonNullable, Validity::NonNullable) => true,
(Validity::Array(arr1), Validity::Array(arr2)) => arr1.array_eq(arr2, eq_mode),
_ => false,
}
}
}
impl ArrayHash for Patches {
fn array_hash<H: Hasher>(&self, state: &mut H, eq_mode: EqMode) {
self.array_len().hash(state);
self.offset().hash(state);
self.indices().array_hash(state, eq_mode);
self.values().array_hash(state, eq_mode);
}
}
impl ArrayEq for Patches {
fn array_eq(&self, other: &Self, eq_mode: EqMode) -> bool {
self.array_len() == other.array_len()
&& self.offset() == other.offset()
&& self.indices().array_eq(other.indices(), eq_mode)
&& self.values().array_eq(other.values(), eq_mode)
}
}