grixy 0.2.0

2-dimensional grids
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Provides a single-bit, 2D grid data structure backed by a linear buffer.
//!
//! The main type, [`GridBits`], is highly generic, allowing it to act as a view over any buffer
//! that can be treated as a slice.
//!
//! ## Convenience Types
//!
//! For ease of use, several type aliases are provided for common use cases:
//! - [`VecBits`]: An owned grid backed by a `Vec<u8>`
//! - [`ArrayBits`]: An owned grid backed by a fixed-size array, `[u8; N]`.
//! - [`SliceBits`]: A read-only, borrowed view over an existing slice.
//! - [`SliceMutBits`]: A mutable, borrowed view over an existing slice.
//!
//! # Examples
//!
//! Creating an owned `VecGridBits` and accessing an element:
//! ```
//! use grixy::{core::{Pos, RowMajor}, buf::bits::VecBits};
//!
//! let grid = VecBits::<u8, RowMajor>::new(8, 1);
//! assert_eq!(grid.get(Pos::new(3, 0)), Some(false));
//! ```

mod ops;

use core::marker::PhantomData;

use ixy::index::{Layout, RowMajor};
pub use ops::BitOps;

use crate::{core::GridError, core::Pos};

#[cfg(feature = "alloc")]
extern crate alloc;

/// A 2-dimensional grid where every individual bit is treated as either `true` or `false`.
///
/// ## Layout
///
/// The grid is stored in a linear buffer, with elements accessed in an order defined by [`Layout`].
#[derive(Debug, Clone)]
pub struct GridBits<T, B, L>
where
    T: BitOps,
    B: AsRef<[T]>,
    L: Layout,
{
    buffer: B,
    width: usize,
    height: usize,
    _element: PhantomData<T>,
    _layout: PhantomData<L>,
}

impl<T, B> GridBits<T, B, RowMajor>
where
    T: BitOps,
    B: AsRef<[T]>,
{
    /// Creates a `GridBuf` using an existing data buffer, specifying the grid dimensions.
    ///
    /// The maximum width that can be used is determined by [`BitOps::MAX_WIDTH`].
    ///
    /// The data buffer is expected to be in [`RowMajor`] order.
    ///
    /// ## Errors
    ///
    /// Returns an error if the buffer size does not match the expected size.
    pub fn with_buffer_row_major(
        buffer: B,
        width: usize,
        height: usize,
    ) -> Result<Self, GridError> {
        Self::with_buffer(buffer, width, height)
    }

    /// Creates a new `GridBuf` using an existing data buffer, specifying the grid dimensions.
    ///
    /// The data buffer is expected to be in [`RowMajor`] order.
    ///
    /// ## Safety
    ///
    /// The caller must ensure that the buffer is large enough to hold `width * height` elements.
    pub unsafe fn with_buffer_row_major_unchecked(buffer: B, width: usize, height: usize) -> Self {
        unsafe { Self::with_buffer_unchecked(buffer, width, height) }
    }
}

impl<T, B, L> GridBits<T, B, L>
where
    T: BitOps,
    B: AsRef<[T]>,
    L: Layout,
{
    /// Creates a `GridBuf` using an existing data buffer, specifying the grid dimensions.
    ///
    /// The maximum width that can be used is determined by [`BitOps::MAX_WIDTH`].
    ///
    /// ## Errors
    ///
    /// Returns an error if the buffer size does not match the expected size.
    pub fn with_buffer(buffer: B, width: usize, height: usize) -> Result<Self, GridError> {
        let expected_size = width * height;
        if buffer.as_ref().len() * T::MAX_WIDTH < expected_size || width > T::MAX_WIDTH {
            return Err(GridError);
        }
        Ok(unsafe { Self::with_buffer_unchecked(buffer, width, height) })
    }

    /// Creates a new `GridBuf` using an existing data buffer, specifying the grid dimensions.
    ///
    /// ## Safety
    ///
    /// The caller must ensure that the buffer is large enough to hold `width * height` elements.
    pub unsafe fn with_buffer_unchecked(buffer: B, width: usize, height: usize) -> Self {
        debug_assert_eq!(
            buffer.as_ref().len() * T::MAX_WIDTH,
            width * height,
            "Buffer size does not match grid dimensions"
        );
        Self {
            buffer,
            width,
            height,
            _element: PhantomData,
            _layout: PhantomData,
        }
    }

    /// Returns a reference of the element at the specified position.'
    ///
    /// If the position is out of bounds, returns `None`.
    pub fn get(&self, pos: Pos) -> Option<bool> {
        if pos.x < self.width && pos.y < self.height {
            let index = L::to_1d(pos, self.width).index;
            let (byte_index, bit_index) = (index / T::MAX_WIDTH, index % T::MAX_WIDTH);
            self.buffer
                .as_ref()
                .get(byte_index)
                .map(|byte| (byte.to_usize() >> bit_index) & 1 != 0)
        } else {
            None
        }
    }

    /// Consumes the `GridBits`, returning the underlying buffer, width, and height.
    #[must_use]
    pub fn into_inner(self) -> (B, usize, usize) {
        (self.buffer, self.width, self.height)
    }

    /// Returns an iterator over the bits of the grid.
    ///
    /// The iterator yields all items in the grid in the order defined by the layout.
    pub fn iter(&self) -> impl Iterator<Item = bool> + '_ {
        self.buffer.as_ref().iter().flat_map(|byte| {
            (0..T::MAX_WIDTH).map(move |bit_index| (byte.to_usize() >> bit_index) & 1 != 0)
        })
    }
}

/// A 2-dimensional grid implemented by a fixed-size array buffer of bytes.
///
/// This is a convenience type for using arrays as the underlying buffer.
///
/// ## Layout
///
/// The grid is stored in a linear buffer, with elements accessed in an order defined by [`Layout`].
pub type ArrayBits<T, const N: usize, L> = GridBits<T, [T; N], L>;
impl<T, const N: usize, L> GridBits<T, [T; N], L>
where
    T: BitOps,
    L: Layout,
{
    /// Creates a new `GridBits` backed by a fixed-size array with the specified width and height.
    ///
    /// Each element is initialized to `false`.
    ///
    /// ## Panics
    ///
    /// Panics if the buffer size does not match the expected size.
    #[must_use]
    pub fn new(width: usize, height: usize) -> Self {
        Self::new_filled(width, height, false)
    }

    /// Creates a new `GridBits` backed by a fixed-size array with the specified width and height.
    ///
    /// Each element is initialized to the provided value.
    ///
    /// ## Panics
    ///
    /// Panics if the buffer size does not match the expected size.
    #[must_use]
    pub fn new_filled(width: usize, height: usize, value: bool) -> Self {
        let size = width * height;
        assert!(
            size.div_ceil(T::MAX_WIDTH) <= N,
            "Buffer size does not match grid dimensions"
        );
        let mut buffer = [T::from_usize(0); N];
        for i in 0..size {
            if value {
                buffer[i / T::MAX_WIDTH] |= T::from_usize(1 << (i % T::MAX_WIDTH));
            }
        }
        unsafe { Self::with_buffer_unchecked(buffer, width, height) }
    }
}

impl<T, B, L> GridBits<T, B, L>
where
    T: BitOps,
    B: AsRef<[T]> + AsMut<[T]>,
    L: Layout,
{
    /// Sets the bit at the specified position, if it is within bounds.
    pub fn set(&mut self, pos: Pos, value: bool) -> Option<()> {
        if pos.x < self.width && pos.y < self.height {
            let index = L::to_1d(pos, self.width).index;
            let (byte_index, bit_index) = (index / T::MAX_WIDTH, index % T::MAX_WIDTH);
            let byte = self.buffer.as_mut().get_mut(byte_index)?;
            if value {
                *byte |= T::from_usize(1 << bit_index);
            } else {
                *byte &= !T::from_usize(1 << bit_index);
            }
            Some(())
        } else {
            None
        }
    }
}

/// A 2-dimensional grid implemented by a slice buffer of bytes.
///
/// This is a convenience type for using slices as the underlying buffer.
///
/// ## Layout
///
/// The grid is stored in a linear buffer, with elements accessed in an order defined by [`Layout`].
pub type SliceBits<'a, T, L> = GridBits<T, &'a [T], L>;

/// A 2-dimensional grid implemented by a mutable slice buffer of bytes.
///
/// This is a convenience type for using mutable slices as the underlying buffer.
///
/// ## Layout
///
/// The grid is stored in a linear buffer, with elements accessed in an order defined by [`Layout`].
pub type SliceMutBits<'a, T, L> = GridBits<T, &'a mut [T], L>;

/// A 2-dimensional grid implemented by a vector buffer of bytes.
///
/// This is a convenience type for using `Vec` as the underlying buffer.
///
/// ## Layout
///
/// The grid is stored in a linear buffer, with elements accessed in an order defined by [`Layout`].
#[cfg(feature = "alloc")]
pub type VecBits<T, L> = GridBits<T, alloc::vec::Vec<T>, L>;

#[cfg(feature = "alloc")]
impl<T, L> GridBits<T, alloc::vec::Vec<T>, L>
where
    T: BitOps,
    L: Layout,
{
    /// Creates a new `GridBits` backed by a `Vec` with the specified width and height.
    ///
    /// Each element is initialized to `false`.
    #[must_use]
    pub fn new(width: usize, height: usize) -> Self {
        Self::new_filled(width, height, false)
    }

    /// Creates a new `GridBits` backed by a `Vec` with the specified width and height.
    ///
    /// Each element is initialized to the provided value.
    #[must_use]
    pub fn new_filled(width: usize, height: usize, value: bool) -> Self {
        let size = width * height;
        let mut buffer = alloc::vec![T::from_usize(0); size.div_ceil(T::MAX_WIDTH)];
        for i in 0..size {
            if value {
                buffer[i / T::MAX_WIDTH] |= T::from_usize(1 << (i % T::MAX_WIDTH));
            }
        }
        unsafe { Self::with_buffer_unchecked(buffer, width, height) }
    }
}

#[cfg(test)]
mod tests {
    extern crate alloc;

    use ixy::index::RowMajor;

    use crate::{
        buf::bits::{ArrayBits, SliceBits, SliceMutBits, VecBits},
        core::Pos,
    };

    #[test]
    fn impl_arr() {
        let data: [u8; 1] = [0b0000_0001];
        let grid = ArrayBits::<_, 1, RowMajor>::with_buffer(data, 8, 1).unwrap();

        assert_eq!(grid.get(Pos::new(0, 0)), Some(true));
        assert_eq!(grid.get(Pos::new(1, 0)), Some(false));
        assert_eq!(grid.get(Pos::new(8, 0)), None);
        assert_eq!(grid.get(Pos::new(0, 1)), None);
    }

    #[test]
    fn arr_new() {
        let grid = ArrayBits::<u8, 1, RowMajor>::new(8, 1);
        assert_eq!(grid.get(Pos::new(0, 0)), Some(false));
        assert_eq!(grid.get(Pos::new(7, 0)), Some(false));
        assert_eq!(grid.get(Pos::new(8, 0)), None);
        assert_eq!(grid.get(Pos::new(0, 1)), None);
    }

    #[test]
    fn arr_new_filled() {
        let grid = ArrayBits::<u8, 1, RowMajor>::new_filled(8, 1, true);
        assert_eq!(grid.get(Pos::new(0, 0)), Some(true));
        assert_eq!(grid.get(Pos::new(7, 0)), Some(true));
        assert_eq!(grid.get(Pos::new(8, 0)), None);
        assert_eq!(grid.get(Pos::new(0, 1)), None);
    }

    #[test]
    #[should_panic(expected = "Buffer size does not match grid dimensions")]
    fn arr_new_panics() {
        let _ = ArrayBits::<u8, 1, RowMajor>::new(9, 1);
    }

    #[test]
    fn impl_slice() {
        let data: [u8; 1] = [0b0000_0001];
        let grid = SliceBits::with_buffer_row_major(&data, 8, 1).unwrap();

        assert_eq!(grid.get(Pos::new(0, 0)), Some(true));
        assert_eq!(grid.get(Pos::new(1, 0)), Some(false));
        assert_eq!(grid.get(Pos::new(8, 0)), None);
        assert_eq!(grid.get(Pos::new(0, 1)), None);
    }

    #[test]
    fn impl_slice_mut() {
        let mut data: [u8; 1] = [0b0000_0001];
        let mut grid = SliceMutBits::with_buffer_row_major(&mut data, 8, 1).unwrap();

        assert_eq!(grid.get(Pos::new(0, 0)), Some(true));
        assert_eq!(grid.get(Pos::new(1, 0)), Some(false));
        assert_eq!(grid.get(Pos::new(8, 0)), None);
        assert_eq!(grid.get(Pos::new(0, 1)), None);

        grid.set(Pos::new(1, 0), true).unwrap();
        assert_eq!(grid.get(Pos::new(1, 0)), Some(true));
        grid.set(Pos::new(0, 0), false).unwrap();
        assert_eq!(grid.get(Pos::new(0, 0)), Some(false));

        assert_eq!(grid.set(Pos::new(8, 0), true), None);
    }

    #[test]
    fn vec_new() {
        let grid = VecBits::<u8, RowMajor>::new(8, 1);
        assert_eq!(grid.get(Pos::new(4, 0)), Some(false));
        assert_eq!(grid.get(Pos::new(7, 0)), Some(false));
        assert_eq!(grid.get(Pos::new(8, 0)), None);
        assert_eq!(grid.get(Pos::new(0, 1)), None);
    }

    #[test]
    fn vec_new_filled() {
        let grid = VecBits::<u8, RowMajor>::new_filled(8, 1, true);
        assert_eq!(grid.get(Pos::new(4, 0)), Some(true));
    }

    #[test]
    #[cfg(debug_assertions)]
    #[should_panic(expected = "Buffer size does not match grid dimensions")]
    fn with_buffer_unchecked_panics() {
        let data: alloc::vec::Vec<u8> = alloc::vec![0b0001_0001];
        // width * height = 6
        // but data.len() = 5
        let _ = unsafe { VecBits::<_, RowMajor>::with_buffer_unchecked(data, 2, 3) };
    }

    #[test]
    fn out_of_bounds() {
        let data: alloc::vec::Vec<u8> = alloc::vec![0b0001_0001];
        let grid = VecBits::with_buffer_row_major(data, 8, 2);
        assert!(grid.is_err());
    }

    #[test]
    fn into_inner() {
        let data: alloc::vec::Vec<u8> = alloc::vec![0b0001_0001];
        let grid = VecBits::with_buffer_row_major(data, 8, 1).unwrap();
        let (buffer, width, height) = grid.into_inner();
        assert_eq!(width, 8);
        assert_eq!(height, 1);
        assert_eq!(buffer.len(), 1);
        assert_eq!(buffer[0], 0b0001_0001);
    }

    #[test]
    fn iter() {
        let data: alloc::vec::Vec<u8> = alloc::vec![0b0001_0001];
        let grid = VecBits::with_buffer_row_major(data, 8, 1).unwrap();
        let mut iter = grid.iter();
        assert_eq!(iter.next(), Some(true));
        assert_eq!(iter.next(), Some(false));
        assert_eq!(iter.next(), Some(false));
        assert_eq!(iter.next(), Some(false));
        assert_eq!(iter.next(), Some(true));
        assert_eq!(iter.next(), Some(false));
        assert_eq!(iter.next(), Some(false));
        assert_eq!(iter.next(), Some(false));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn with_buffer_row_major_unchecked() {
        let data: alloc::vec::Vec<u8> = alloc::vec![0b0001_0001];
        let grid = unsafe { VecBits::<u8, RowMajor>::with_buffer_row_major_unchecked(data, 8, 1) };
        assert_eq!(grid.get(Pos::new(0, 0)), Some(true));
        assert_eq!(grid.get(Pos::new(1, 0)), Some(false));
        assert_eq!(grid.get(Pos::new(8, 0)), None);
        assert_eq!(grid.get(Pos::new(0, 1)), None);
    }

    #[test]
    fn with_buffer_width_exceeded_err() {
        let data: alloc::vec::Vec<u8> = alloc::vec![0b0001_0001];
        let grid = VecBits::with_buffer_row_major(data, 8, 2);
        assert!(grid.is_err());
    }
}