Files
addr2line
adler
adler32
ahash
aho_corasick
angle
approx
backtrace
bitflags
blender
bytemuck
byteorder
case
cast_trait
cfg_if
chrono
color
color_quant
const_fn
crc32fast
crossbeam
crossbeam_channel
crossbeam_deque
crossbeam_epoch
crossbeam_queue
crossbeam_skiplist
crossbeam_utils
darling
darling_core
darling_macro
dds
deflate
densevec
derive_builder
derive_builder_core
dot
downcast_rs
dual_quat
either
erased_serde
failure
failure_derive
fixedbitset
float_cmp
fnv
freeimage
freeimage_sys
freetype
freetype_gl_sys
freetype_sys
freetypegl
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
fxhash
generational_arena
generic_array
getrandom
gif
gimli
glfw
glfw_sys
glin
glin_derive
glsl
half
harfbuzz
harfbuzz_ft_sys
harfbuzz_sys
hashbrown
human_sort
ident_case
image
indexmap
instant
itertools
itoa
jpeg_decoder
lazy_static
libc
libm
lock_api
log
lut_parser
matrixmultiply
memchr
memoffset
meshopt
miniz_oxide
monotonic_clock
mopa
mutiny_derive
na
nalgebra
base
geometry
linalg
ncollide3d
bounding_volume
interpolation
partitioning
pipeline
procedural
query
algorithms
closest_points
contact
distance
nonlinear_time_of_impact
point
proximity
ray
time_of_impact
visitors
shape
transformation
utils
nom
num_complex
num_cpus
num_integer
num_iter
num_rational
num_traits
numext_constructor
numext_fixed_uint
numext_fixed_uint_core
numext_fixed_uint_hack
object
once_cell
parking_lot
parking_lot_core
pathfinding
pennereq
petgraph
pin_project_lite
pin_utils
png
polygon2
ppv_lite86
proc_macro2
proc_macro_crate
proc_macro_hack
proc_macro_nested
quote
rand
rand_chacha
rand_core
rand_distr
raw_window_handle
rawpointer
rayon
rayon_core
rect_packer
regex
regex_syntax
retain_mut
rin
rin_app
rin_blender
rin_core
rin_gl
rin_graphics
rin_gui
rin_material
rin_math
rin_postpo
rin_scene
rin_util
rin_window
rinblender
rinecs
rinecs_derive
rinecs_derive_utils
ringui_derive
rustc_demangle
rusty_pool
ryu
scopeguard
seitan
seitan_derive
semver
semver_parser
serde
serde_derive
serde_json
shaderdata_derive
simba
slab
slice_of_array
slotmap
smallvec
std140_data
streaming_iterator
strsim
syn
synstructure
thiserror
thiserror_impl
thread_local
tiff
time
toml
typenum
unchecked_unwrap
unicode_xid
vec2
vec3
weezl
x11
zlib_sys
  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
pub const WINDOW_SIZE: usize = 32768;
pub const WINDOW_MASK: usize = WINDOW_SIZE - 1;
#[cfg(test)]
pub const HASH_BYTES: usize = 3;
const HASH_SHIFT: u16 = 5;
const HASH_MASK: u16 = WINDOW_MASK as u16;

/// Helper struct to let us allocate both head and prev in the same block.
struct Tables {
    /// Starts of hash chains (in prev)
    pub head: [u16; WINDOW_SIZE],
    /// Link to previous occurence of this hash value
    pub prev: [u16; WINDOW_SIZE],
}

impl Default for Tables {
    #[inline]
    fn default() -> Tables {
        Tables {
            head: [0; WINDOW_SIZE],
            prev: [0; WINDOW_SIZE],
        }
    }
}

impl Tables {
    #[inline]
    fn fill_prev(&mut self) {
        self.prev.copy_from_slice(&self.head);
    }
}

/// Create and box the hash chains.
fn create_tables() -> Box<Tables> {
    // Using default here is a trick to get around the lack of box syntax on stable rust.
    //
    // Box::new([0u16,n]) ends up creating an temporary array on the stack which is not optimised
    // but using default, which simply calls `box value` internally allows us to get around this.
    //
    // We could use vec instead, but using a boxed array helps the compiler optimise
    // away bounds checks as `n & WINDOW_MASK < WINDOW_SIZE` will always be true.
    let mut t: Box<Tables> = Box::default();

    for (n, b) in t.head.iter_mut().enumerate() {
        *b = n as u16;
    }

    t.fill_prev();

    t
}

/// Returns a new hash value based on the previous value and the next byte
#[inline]
pub fn update_hash(current_hash: u16, to_insert: u8) -> u16 {
    update_hash_conf(current_hash, to_insert, HASH_SHIFT, HASH_MASK)
}

#[inline]
fn update_hash_conf(current_hash: u16, to_insert: u8, shift: u16, mask: u16) -> u16 {
    ((current_hash << shift) ^ (u16::from(to_insert))) & mask
}

#[inline]
fn reset_array(arr: &mut [u16; WINDOW_SIZE]) {
    for (n, b) in arr.iter_mut().enumerate() {
        *b = n as u16;
    }
}

pub struct ChainedHashTable {
    // Current running hash value of the last 3 bytes
    current_hash: u16,
    // Hash chains.
    c: Box<Tables>,
    // Used for testing
    // count: DebugCounter,
}

impl ChainedHashTable {
    pub fn new() -> ChainedHashTable {
        ChainedHashTable {
            current_hash: 0,
            c: create_tables(),
            //count: DebugCounter::default(),
        }
    }

    #[cfg(test)]
    pub fn from_starting_values(v1: u8, v2: u8) -> ChainedHashTable {
        let mut t = ChainedHashTable::new();
        t.current_hash = update_hash(t.current_hash, v1);
        t.current_hash = update_hash(t.current_hash, v2);
        t
    }

    /// Resets the hash value and hash chains
    pub fn reset(&mut self) {
        self.current_hash = 0;
        reset_array(&mut self.c.head);
        {
            let h = self.c.head;
            let mut c = self.c.prev;
            c[..].copy_from_slice(&h[..]);
        }
        /*if cfg!(debug_assertions) {
            self.count.reset();
        }*/
    }

    pub fn add_initial_hash_values(&mut self, v1: u8, v2: u8) {
        self.current_hash = update_hash(self.current_hash, v1);
        self.current_hash = update_hash(self.current_hash, v2);
    }

    /// Insert a byte into the hash table
    #[inline]
    pub fn add_hash_value(&mut self, position: usize, value: u8) {
        // Check that all bytes are input in order and at the correct positions.
        // Disabled for now as it breaks when sync flushing.
        /*debug_assert_eq!(
            position & WINDOW_MASK,
            self.count.get() as usize & WINDOW_MASK
        );*/
        debug_assert!(
            position < WINDOW_SIZE * 2,
            "Position is larger than 2 * window size! {}",
            position
        );
        // Storing the hash in a temporary variable here makes the compiler avoid the
        // bounds checks in this function.
        let new_hash = update_hash(self.current_hash, value);

        self.add_with_hash(position, new_hash);

        // Update the stored hash value with the new hash.
        self.current_hash = new_hash;
    }

    /// Directly set the current hash value
    #[inline]
    pub fn set_hash(&mut self, hash: u16) {
        self.current_hash = hash;
    }

    /// Update the tables directly, providing the hash.
    #[inline]
    pub fn add_with_hash(&mut self, position: usize, hash: u16) {
        /*if cfg!(debug_assertions) {
            self.count.add(1);
        }*/

        self.c.prev[position & WINDOW_MASK] = self.c.head[hash as usize];

        // Ignoring any bits over 16 here is deliberate, as we only concern ourselves about
        // where in the buffer (which is 64k bytes) we are referring to.
        self.c.head[hash as usize] = position as u16;
    }

    // Get the head of the hash chain for the current hash value
    #[cfg(test)]
    #[inline]
    pub fn current_head(&self) -> u16 {
        self.c.head[self.current_hash as usize]
    }

    #[inline]
    pub fn current_hash(&self) -> u16 {
        self.current_hash
    }

    #[inline]
    pub fn get_prev(&self, bytes: usize) -> u16 {
        self.c.prev[bytes & WINDOW_MASK]
    }

    #[cfg(test)]
    #[inline]
    pub fn farthest_next(&self, match_pos: usize, match_len: usize) -> usize {
        let to_check = match_len.saturating_sub(2);

        let mut n = 0;
        let mut smallest_prev = self.get_prev(match_pos);
        let mut smallest_pos = 0;
        while n < to_check {
            let prev = self.get_prev(match_pos + n);
            if prev < smallest_prev {
                smallest_prev = prev;
                smallest_pos = n;
            }
            n += 1;
        }
        smallest_pos
    }

    #[inline]
    fn slide_value(b: u16, pos: u16, bytes: u16) -> u16 {
        if b >= bytes {
            b - bytes
        } else {
            pos
        }
    }

    #[inline]
    fn slide_table(table: &mut [u16; WINDOW_SIZE], bytes: u16) {
        for (n, b) in table.iter_mut().enumerate() {
            *b = ChainedHashTable::slide_value(*b, n as u16, bytes);
        }
    }

    pub fn slide(&mut self, bytes: usize) {
        /*if cfg!(debug_assertions) && bytes != WINDOW_SIZE {
            // This should only happen in tests in this file.
            self.count.reset();
        }*/
        ChainedHashTable::slide_table(&mut self.c.head, bytes as u16);
        ChainedHashTable::slide_table(&mut self.c.prev, bytes as u16);
    }
}

#[cfg(test)]
pub fn filled_hash_table(data: &[u8]) -> ChainedHashTable {
    assert!(data.len() <= (WINDOW_SIZE * 2) + 2);
    let mut hash_table = ChainedHashTable::from_starting_values(data[0], data[1]);
    for (n, b) in data[2..].iter().enumerate() {
        hash_table.add_hash_value(n, *b);
    }
    hash_table
}

#[cfg(test)]
mod test {
    use super::{filled_hash_table, ChainedHashTable};

    #[test]
    fn chained_hash() {
        use std::str;

        let test_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do \
                           eiusmod tempor. rum. incididunt ut labore et dolore magna aliqua. Ut \
                           enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi \
                           ut aliquip ex ea commodo consequat. rum. Duis aute irure dolor in \
                           reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla \
                           pariatur. Excepteur sint occaecat cupidatat non proident, sunt in \
                           culpa qui officia deserunt mollit anim id est laborum.";

        let test_data = test_string.as_bytes();

        let current_bytes = &test_data[test_data.len() - super::HASH_BYTES..test_data.len()];

        let num_iters = test_string
            .matches(str::from_utf8(current_bytes).unwrap())
            .count();

        let hash_table = filled_hash_table(test_data);

        // Test that the positions in the chain are valid
        let mut prev_value = hash_table.get_prev(hash_table.current_head() as usize) as usize;
        let mut count = 0;
        let mut current = hash_table.current_head() as usize;
        while current != prev_value {
            count += 1;
            current = prev_value;
            prev_value = hash_table.get_prev(prev_value) as usize;
        }
        // There should be at least as many occurences of the hash of the checked bytes as the
        // numbers of occurences of the checked bytes themselves. As the hashes are not large enough
        // to store 8 * 3 = 24 bits, there could be more with different input data.
        assert!(count >= num_iters);
    }

    #[test]
    fn table_unique() {
        let mut test_data = Vec::new();
        test_data.extend(0u8..255);
        test_data.extend(255u8..0);
        let hash_table = filled_hash_table(&test_data);
        let prev_pos = hash_table.get_prev(hash_table.current_head() as usize);
        // Since all sequences in the input are unique, there shouldn't be any previous values.
        assert_eq!(prev_pos, hash_table.current_hash());
    }

    #[test]
    fn table_slide() {
        use std::fs::File;
        use std::io::Read;

        let window_size = super::WINDOW_SIZE;
        let window_size16 = super::WINDOW_SIZE as u16;

        let mut input = Vec::new();

        let mut f = File::open("tests/pg11.txt").unwrap();

        f.read_to_end(&mut input).unwrap();

        let mut hash_table = filled_hash_table(&input[..window_size + 2]);

        for (n, b) in input[2..window_size + 2].iter().enumerate() {
            hash_table.add_hash_value(n + window_size, *b);
        }

        hash_table.slide(window_size);

        {
            let max_head = hash_table.c.head.iter().max().unwrap();
            // After sliding there should be no hashes referring to values
            // higher than the window size
            assert!(*max_head < window_size16);
            assert!(*max_head > 0);
            let pos = hash_table.get_prev(hash_table.current_head() as usize);
            // There should be a previous occurence since we inserted the data 3 times
            assert!(pos < window_size16);
            assert!(pos > 0);
        }

        for (n, b) in input[2..(window_size / 2)].iter().enumerate() {
            hash_table.add_hash_value(n + window_size, *b);
        }

        // There should hashes referring to values in the upper part of the input window
        // at this point
        let max_prev = hash_table.c.prev.iter().max().unwrap();
        assert!(*max_prev > window_size16);

        let mut pos = hash_table.current_head();
        // There should be a previous occurence since we inserted the data 3 times
        assert!(pos > window_size16);
        let end_byte = input[(window_size / 2) - 1 - 2];
        let mut iterations = 0;
        while pos > window_size16 && iterations < 5000 {
            assert_eq!(input[pos as usize & window_size - 1], end_byte);

            pos = hash_table.get_prev(pos as usize);
            iterations += 1;
        }
    }

    #[test]
    /// Ensure that the initial hash values are correct.
    fn initial_chains() {
        let t = ChainedHashTable::new();
        for (n, &b) in t.c.head.iter().enumerate() {
            assert_eq!(n, b as usize);
        }
        for (n, &b) in t.c.prev.iter().enumerate() {
            assert_eq!(n, b as usize);
        }
    }
}