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
/*
 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
 * SPDX-FileCopyrightText: 2023 Inria
 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
 *
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
 */

use core::convert::Infallible;

use common_traits::UnsignedInt;

use crate::prelude::*;

/**

An implementation of [`WordRead`] and [`WordSeek`] for a slice.

The implementation depends on the `INF` parameter: if true, the reader will
behave as if the slice is infinitely extended with zeros.
If false, the reader will return an error when reading
beyond the end of the slice. You can create a zero-extended
reader with [`MemWordReader::new`] and a strict reader with
[`MemWordReader::new_strict`].

The zero-extended reader is usually much faster than the strict reader, but
it might loop infinitely when reading beyond the end of the slice.

# Examples

```rust
use dsi_bitstream::prelude::*;

let words: [u32; 2] = [1, 2];

let mut word_reader = MemWordReader::new(&words);
assert_eq!(1, word_reader.read_word().unwrap());
assert_eq!(2, word_reader.read_word().unwrap());
assert_eq!(0, word_reader.read_word().unwrap());
assert_eq!(0, word_reader.read_word().unwrap());

let mut word_reader = MemWordReader::new_strict(&words);
assert_eq!(1, word_reader.read_word().unwrap());
assert_eq!(2, word_reader.read_word().unwrap());
assert!(word_reader.read_word().is_err());
```
*/
#[derive(Debug, Clone, PartialEq)]
pub struct MemWordReader<W: UnsignedInt, B: AsRef<[W]>, const INF: bool = true> {
    data: B,
    word_index: usize,
    _marker: core::marker::PhantomData<W>,
}

impl<W: UnsignedInt, B: AsRef<[W]>> MemWordReader<W, B> {
    /// Create a new [`MemWordReader`] from a slice of data
    #[must_use]
    pub fn new(data: B) -> Self {
        Self {
            data,
            word_index: 0,
            _marker: Default::default(),
        }
    }

    pub fn into_inner(self) -> B {
        self.data
    }
}

impl<W: UnsignedInt, B: AsRef<[W]>> MemWordReader<W, B, false> {
    /// Create a new [`MemWordReader`] from a slice of data
    #[must_use]
    pub fn new_strict(data: B) -> Self {
        Self {
            data,
            word_index: 0,
            _marker: Default::default(),
        }
    }
}

impl<W: UnsignedInt, B: AsRef<[W]>> WordRead for MemWordReader<W, B, true> {
    type Error = Infallible;
    type Word = W;

    #[inline(always)]
    fn read_word(&mut self) -> Result<W, Infallible> {
        let res = self
            .data
            .as_ref()
            .get(self.word_index)
            .copied()
            .unwrap_or(Self::Word::ZERO);

        self.word_index += 1;
        Ok(res)
    }
}

impl<W: UnsignedInt, B: AsRef<[W]>> WordRead for MemWordReader<W, B, false> {
    type Error = std::io::Error;
    type Word = W;

    #[inline(always)]
    fn read_word(&mut self) -> Result<W, std::io::Error> {
        let res = self
            .data
            .as_ref()
            .get(self.word_index)
            .ok_or(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                "Unexpected end of slice",
            ))?;

        self.word_index += 1;
        Ok(*res)
    }
}

impl<W: UnsignedInt, B: AsRef<[W]>> WordSeek for MemWordReader<W, B, true> {
    type Error = Infallible;

    #[inline(always)]
    fn get_word_pos(&mut self) -> Result<u64, Infallible> {
        Ok(self.word_index as u64)
    }

    #[inline(always)]
    fn set_word_pos(&mut self, word_index: u64) -> Result<(), Infallible> {
        // This is dirty but it's infallible
        self.word_index = word_index.min(usize::MAX as u64) as usize;
        Ok(())
    }
}

impl<W: UnsignedInt, B: AsRef<[W]>> WordSeek for MemWordReader<W, B, false> {
    type Error = std::io::Error;

    #[inline(always)]
    fn get_word_pos(&mut self) -> Result<u64, std::io::Error> {
        Ok(self.word_index as u64)
    }
    #[inline(always)]
    fn set_word_pos(&mut self, word_index: u64) -> Result<(), std::io::Error> {
        return if word_index > self.data.as_ref().len() as u64 {
            Err(std::io::Error::new(
                std::io::ErrorKind::UnexpectedEof,
                format_args!(
                    "Position beyond end of slice: {} > {}",
                    word_index,
                    self.data.as_ref().len()
                )
                .to_string(),
            ))
        } else {
            self.word_index = word_index as usize;
            Ok(())
        };
    }
}

#[test]

fn test_eof_table_read() {
    use crate::codes::{DeltaReadParam, DeltaWrite};
    let mut words: [u64; 1] = [0];
    let mut writer = crate::prelude::BufBitWriter::<crate::prelude::LE, _>::new(
        MemWordWriterSlice::new(&mut words),
    );
    for _ in 0..16 {
        writer.write_delta(1).unwrap();
    }
    writer.flush().unwrap();

    let mut reader =
        crate::prelude::BufBitReader::<crate::prelude::LE, _>::new(MemWordReader::new(&words));
    for _ in 0..16 {
        // Here the last table read make peek_bits return Ok(None)
        assert_eq!(1, reader.read_delta_param::<true, true>().unwrap());
    }
}