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
//! ## About
//! An IDX file format decoding library. (Currently WIP)
//! 
//! The main type is [`IDXDecoder`]. It implements Iterator whose Item
//! correspond to items of file format.
//! 
//! ### Type parameters
//! 
//! [`IDXDecoder`] takes three type parameters.
//! - `R`: Reader from which data is taken. Can be file, network stream etc.
//! - `T`: Type of items produced by Iterator. E.g. U8, I16, F32.
//!   
//!   All possible types can be found in [`types`](types/index.html) module
//! - `D`: Type-level integer of dimensions. Must be less than 256.
//!   
//!   If it's less than 128 use nalgebra's U* types.
//!   For value >=128 use typenum's consts.
//! 
//! ### Dimensions
//! 
//! For one-dimensional decoder returns simply items.
//! 
//! For more dimensions, output is a `Vec` of values containing a single item.
//! 
//! E.g. a 3-dimensional decoder where items are of size 4x4 will return `Vec`s
//! of length 16.
//! 
//! First dimension of decoder corresponds to amount of items left.
//! 
//! ## Caveats
//! 
//! Currently decoder only implements Iterator for 1 and 3 dimensions.
//! It's simply because I didn't implement other.
//! 
//! Crate also assumes that items are stored in big endian way, just like sizes.
//! 
//! If you found a bug or the crate is missing some functionality,
//! add an issue or send a pull request.
//! 
//! ## Example
//! ```ignore
//! let file = std::fs::File::open("data.idx")?;
//! let decode = idx_decoder::IDXDecoder::<_, idx_decoder::types::U8, nalgebra::U1>::new(file)?;
//! for item in decode {
//!     println!("Item: {}", item);
//! }
//! ```
//! 
//! ## Acknowledgement
//! This crate is implemented according to file format
//! found at <http://yann.lecun.com/exdb/mnist/>
//! 
//! [`IDXDecoder`]: struct.IDXDecoder.html

use std::{convert::TryInto, io::{self, Read}, marker::PhantomData};
use nalgebra::{self as na, VectorN, DimName, allocator::Allocator, DefaultAllocator};
// use typenum::{self as tn, type_operators::IsLess};
use thiserror::Error;

/// Types used by [`IDXDecoder`](struct.IDXDecoder.html) to specify iterator's output type
pub mod types {
    use std::{io::Read, mem::size_of};

    #[doc(hidden)]
    mod private { pub trait Sealed {} }
    use private::Sealed;

    /// Trait implemented by output types used by IDXDecoder's iterator
    /// 
    /// It can't be implemented outside this crate.
    pub trait Type: Sealed {
        const VALUE: u8;
        type TypeValue;
    }

    // implemented by types that can be read from reader using big endiann
    #[doc(hidden)]
    pub trait BEReadable<R>: Sized {
        fn read_self(r: &mut R) -> Option<Self>;
    }

    macro_rules! new_type_int {
        ( $( $vis:vis $name:ident : $tv:ty = $value:expr,)* ) => {
            $(
                $vis struct $name;
                impl Sealed for $name {}
                impl Type for $name {
                    type TypeValue = $tv;
                    const VALUE: u8 = $value;
                }

                impl<R: Read> BEReadable<R> for $tv {
                    fn read_self(r: &mut R) -> Option<Self> {
                        let mut buf = [0u8; size_of::<Self>()];
                        r.read_exact(&mut buf).ok()?;
                        Some(Self::from_be_bytes(buf))
                    }
                }
            )*
        }
    }

    macro_rules! new_type_f {
        ( $( $vis:vis $name:ident : $uint:ty as $tv:ty = $value:expr,)* ) => {
            $(
                $vis struct $name;
                impl Sealed for $name {}
                impl Type for $name {
                    type TypeValue = $tv;
                    const VALUE: u8 = $value;
                }

                impl<R: Read> BEReadable<R> for $tv {
                    fn read_self(r: &mut R) -> Option<Self> {
                        let mut buf = [0u8; size_of::<Self>()];
                        r.read_exact(&mut buf).ok()?;
                        Some(Self::from_bits(<$uint>::from_be_bytes(buf)))
                    }
                }
            )*
        }
    }

    new_type_int!(
        pub U8: u8 = 0x08,
        pub I8: i8 = 0x09,
        pub I16: i16 = 0x0b,
        pub I32: i32 = 0x0c,
    );
    new_type_f!(
        pub F32: u32 as f32 = 0x0d,
        pub F64: u64 as f64 = 0x0e,
    );
}

use types::*;

/// The decoder. Check [crate level docs](index.html) for more informations
pub struct IDXDecoder<R, T: Type, D: DimName>
where
    DefaultAllocator: Allocator<u32, D>
{
    reader: R,
    output_type: PhantomData<fn() -> T>,
    dimensions: VectorN<u32, D>,
}

/// Error type return by `IDXDecoder::new`
#[derive(Debug, Error)]
pub enum IDXError {
    #[error("Wrong magic, first two bytes should be zero")]
    WrongMagic,
    #[error("Wrong type, expected {0}, got {1}")]
    WrongType(u8, u8),
    #[error("Wrong number of dimensions, expected {0}, got {1}")]
    WrongDimensions(u8, u8),
    #[error("Too many dimensions, must be less than 256")]
    TooManyDimensons,
    #[error("{0}")]
    IOError(#[from] io::Error),
}

impl<R: Read, T: Type, D: DimName> IDXDecoder<R, T, D>
where
    // D: IsLess<tn::consts::U256>,
    DefaultAllocator: Allocator<u32, D>
{
    /// Returns error in case provided types aren't valid 
    pub fn new(mut reader: R) -> Result<Self, IDXError> {
        // Read magic and check if it's valid
        let mut buf = [0u8; 4];
        reader.read_exact(&mut buf)?;
        if buf[0] != 0 || buf[1] != 0 { Err(IDXError::WrongMagic)? }
        if buf[2] != T::VALUE { Err(IDXError::WrongType(T::VALUE, buf[2]))? }
        let dims: u8 = D::dim().try_into().or(Err(IDXError::TooManyDimensons))?;
        if buf[3] != dims { Err(IDXError::WrongDimensions(dims, buf[3]))? }

        // Read dimensions
        // To simplify code we treat amount of items as first dimension
        let mut dimensions: VectorN<u32, D> = na::zero();
        for d in dimensions.iter_mut() {
            let mut buf = [0u8; 4];
            reader.read_exact(&mut buf)?;
            *d = u32::from_be_bytes(buf);
        }
        Ok(IDXDecoder { reader, output_type: PhantomData, dimensions })
    }

    /// Size of return values.
    /// 
    /// First dimension of decoder corresponds to amount of items left.
    pub fn dimensions(&self) -> VectorN<u32, D> {
        self.dimensions.clone()
    }
}

impl<R: Read, T: Type> Iterator for IDXDecoder<R, T, na::U1>
where
    DefaultAllocator: Allocator<u32, na::U1>,
    T::TypeValue: BEReadable<R>,
{
    type Item = T::TypeValue;
    fn next(&mut self) -> Option<Self::Item> {
        if self.dimensions[0] > 0 {
            self.dimensions[0] -= 1;
            T::TypeValue::read_self(&mut self.reader)
        } else {
            None
        }
    }
}

impl<R: Read, T: Type> Iterator for IDXDecoder<R, T, na::U3>
where
    DefaultAllocator: Allocator<u32, na::U3>,
    T::TypeValue: Default + Clone + BEReadable<R>,
{
    type Item = Vec<T::TypeValue>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.dimensions[0] > 0 {
            self.dimensions[0] -= 1;
            let as_usize = |n: u32| -> Option<usize> { n.try_into().ok() };
            let len = as_usize(self.dimensions[1])?.checked_mul(as_usize(self.dimensions[2])?)?;
            let mut items = vec![Default::default(); len];
            for item in items.iter_mut() {
                *item = T::TypeValue::read_self(&mut self.reader)?
            }
            Some(items)
        } else {
            None
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, self.dimensions[0].try_into().ok())
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn example_1d() {
        const DATA: &[u8] = &[
            // magic, type u8, 1 dim
            0, 0, 8, 1,
            // len as big endiann u32
            0, 0, 0, 3,
            // items
            1, 2, 3];
        let reader = std::io::Cursor::new(DATA);
        let mut decoder = IDXDecoder::<_, U8, nalgebra::U1>::new(reader)
            .expect("Decoder creation error");
        assert_eq!(decoder.next(), Some(1));
        assert_eq!(decoder.next(), Some(2));
        assert_eq!(decoder.next(), Some(3));
        assert_eq!(decoder.next(), None);
    }

    #[test]
    fn example_3d() {
        const DATA: &[u8] = &[
            // magic, type u8, 1 dim
            0, 0, 8, 3,
            // lens as big endiann u32: 3 matrices of 2x2
            0, 0, 0, 3,
            0, 0, 0, 2,
            0, 0, 0, 2,
            // items
            1, 2, 3, 4,
            5, 6, 7, 8,
            9, 10, 11, 12];
        let reader = std::io::Cursor::new(DATA);
        let mut decoder = IDXDecoder::<_, U8, nalgebra::U3>::new(reader)
            .expect("Decoder creation error");
        assert_eq!(decoder.next(), Some(vec![1, 2, 3, 4]));
        assert_eq!(decoder.next(), Some(vec![5, 6, 7, 8]));
        assert_eq!(decoder.next(), Some(vec![9, 10, 11, 12]));
        assert_eq!(decoder.next(), None);
    }
}