bitgrid 0.1.0

A compact, efficient bit grid structure
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611

#![cfg_attr(not(feature = "std"), no_std)]

//! A fast, packed bit grid structure for managing N-Dim boolean matrices with:
//! - constant-time `get`/`set` 
//! - efficient iteration over set bits,
//! - arbitrary word sizes (`u8`, `u16`, `u32`, `u64`, `u128`), 
//! - `no_std` compatible
//!
//! # Examples
//! ```
//! # use bitgrid::BitGrid2D;
//!
//! // Create a 4x4 grid
//! let mut grid = BitGrid2D::<u32>::new(4, 4);
//!
//! // Set some bits
//! grid.set(1, 2);
//! grid.set(3, 0);
//!
//! // Check bit states
//! assert!(grid.get(1, 2));
//! assert!(!grid.get(0, 0));
//!
//! // Iterate over set bits
//! let set_bits: Vec<_> = grid.iter().collect();
//! assert_eq!(set_bits, vec![[1, 2], [3, 0]]);
//! ```
//!
//! For N-Dim grids:
//! ```
//! # use bitgrid::BitGrid;
//!
//! // create a 10x10x10x10 grid
//! let mut space = BitGrid::<u64, 4>::new_n([10, 10, 10, 10]);
//! space.set_n([1, 2, 3, 4]);
//! assert!(space.get_n([1, 2, 3, 4]));
//! ```



extern crate alloc;

use alloc::{boxed::Box, vec::Vec};
use core::{fmt, ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Shl, Shr, Sub}};


/// Words are used as bit blocks in [`BitGrid`]
///
pub trait Word:
    Copy
    + Default
    + BitAnd<Output = Self>
    + BitAndAssign
    + BitOr<Output = Self>
    + BitOrAssign
    + BitXor<Output = Self>
    + BitXorAssign
    + Shl<u32, Output = Self>
    + Shr<u32, Output = Self>
    + Not<Output = Self>
    + Sub<Output = Self>
    + PartialEq
    + Eq
{
    /// number of bits in word
    const N_BITS: u32;

    /// Words with all bits set to `1`
    const FULL: Self;

    /// Words with all bits set to `0`
    const EMPTY: Self;

    /// Words with least significant bit set to `1`
    const ONE: Self;

    /// Returns the number of trailing zeros
    fn trailing_zeros(self) -> u32;
}


macro_rules! impl_wrd {
    ($ty:ty) => {
        impl Word for $ty {
            const N_BITS: u32 = core::mem::size_of::<$ty>() as u32 * 8;
            const FULL: $ty = <$ty>::MAX;
            const EMPTY: $ty = 0;
            const ONE: $ty = 1;

            #[inline(always)]
            fn trailing_zeros(self) -> u32 {
                self.trailing_zeros()
            }
        }
    }
}

impl_wrd!(u8);
impl_wrd!(u16);
impl_wrd!(u32);
impl_wrd!(u64);
impl_wrd!(u128);

/// N-dimensional bit grid with packed storage
///
pub struct BitGrid<W, const N: usize> {
    /// Packed bit storage
    pub data: Box<[W]>,
    /// Grid dimensions [dim0, dim1, ..., dimN-1]
    pub shape: [u32; N],
    /// Total number of bits in the grid
    pub count: u32,
}

/// 2D bit grid specialization
pub type BitGrid2D<W> = BitGrid<W, 2>;
/// 3D bit grid specialization
pub type BitGrid3D<W> = BitGrid<W, 3>;

impl<WRD: Word, const N: usize> BitGrid<WRD, N> {

    /// Creates a new bit grid with the specified dimensions and all bits cleared
    ///
    /// # Panics
    /// Panics if the total grid size would overflow `u32`
    ///
    /// # Examples
    /// ```
    /// use bitgrid::BitGrid;
    /// let grid = BitGrid::<u32, 3>::new_n([10, 10, 10]);
    /// ```
    #[inline]
    pub fn new_n(shape: [u32; N]) -> Self {
        let mut count = 1;
        for d in shape {
            count *= d;
        }

        let n_words = ((count + WRD::N_BITS - 1) / WRD::N_BITS) as usize;
        let data = alloc::vec![WRD::EMPTY; n_words].into_boxed_slice();
        BitGrid { data, shape, count }
    }

    #[inline]
    pub fn flat_index(&self, coords: [u32; N]) -> (usize, u32) {
        let mut index = 0;
        let mut stride = 1;

        for (i, &dim) in self.shape.iter().enumerate().rev() {
            let coord = coords[i];
            debug_assert!(
                coords[i] < dim,
                "Coordinate {} out of bounds for dimension {} (size {})",
                coords[i],
                i,
                dim
            );
            index += coord * stride;
            stride *= dim;
        }

        let word = (index / WRD::N_BITS) as usize;
        let bit = index % WRD::N_BITS;
        (word, bit)
    }

    /// Sets the bit at the given coordinates
    ///
    /// # Panics
    /// Panics if coordinates are out of bounds (debug builds only)
    ///
    /// # Examples
    /// ```
    /// use bitgrid::BitGrid;
    /// let mut grid = BitGrid::<u32, 2>::new_n([4, 4]);
    /// grid.set_n([1, 2]);
    /// ```
    #[inline]
    pub fn set_n(&mut self, coords: [u32; N]) {
        let (word, off) = self.flat_index(coords);
        self.data[word] |= WRD::ONE << off;
    }

    /// Clears the bit at the given coordinates
    ///
    /// # Panics
    /// Panics if coordinates are out of bounds (debug builds only)
    #[inline]
    pub fn clear_n(&mut self, coords: [u32; N]) {
        let (word, off) = self.flat_index(coords);
        self.data[word] &= !(WRD::ONE << off);
    }

    /// Toggles the bit at the given coordinates
    ///
    /// # Panics
    /// Panics if coordinates are out of bounds (debug builds only)
    pub fn flip_n(&mut self, coords: [u32; N]) {
        let (word, off) = self.flat_index(coords);
        self.data[word] ^= WRD::ONE << off;
    }

    /// Checks if the bit at given coordinates is set
    ///
    /// # Panics
    /// Panics if coordinates are out of bounds (debug builds only)
    ///
    /// # Examples
    /// ```
    /// use bitgrid::BitGrid;
    /// let mut grid = BitGrid::<u32, 2>::new_n([4, 4]);
    /// grid.set_n([1, 2]);
    /// assert!(grid.get_n([1, 2]));
    /// ```
    #[inline]
    pub fn get_n(&self, coords: [u32; N]) -> bool {
        let (word, off) = self.flat_index(coords);
        (self.data[word] & (WRD::ONE << off)) != WRD::EMPTY
    }

    /// Sets all bits in the grid to 1
    pub fn set_all(&mut self) {
        for w in &mut *self.data {
            *w = WRD::FULL;
        }
    }

    /// Sets all bits in the grid to 0
    pub fn clear_all(&mut self) {
        for w in &mut *self.data {
            *w = WRD::EMPTY;
        }
    }

    /// Returns an iterator over coordinates of all set bits
    ///
    /// The iterator yields coordinates in row-major order (last dimension changes fastest)
    ///
    /// # Examples
    /// ```
    /// # use bitgrid::BitGrid;
    /// 
    /// let mut grid = BitGrid::<u32, 2>::new_n([2, 2]);
    /// grid.set_n([0, 1]);
    /// grid.set_n([1, 0]);
    /// 
    /// let mut iter = grid.iter();
    /// assert_eq!(iter.next(), Some([0, 1]));
    /// assert_eq!(iter.next(), Some([1, 0]));
    /// assert_eq!(iter.next(), None);
    /// ```
    #[inline]
    pub fn iter(&self) -> BitIter<WRD, N> {
        if self.count == 0 {
            BitIter::default()
        } else {
            let last_i = (self.count - 1) / WRD::N_BITS;
            let valid  = self.count - last_i * WRD::N_BITS;
            let mask   = if valid == WRD::N_BITS { WRD::FULL } else {
                (WRD::ONE << valid) - WRD::ONE
            };
            let first  = self.data.get(0).cloned().unwrap_or(WRD::EMPTY)
                         & if last_i == 0 { mask } else { WRD::FULL };
            BitIter {
                data: &self.data,
                shape: self.shape,
                index: 0,
                curr_wrd: first,
            }
        }
    }

}

impl<WRD: Word> BitGrid<WRD, 2> {

    /// Creates a new 2D grid with specified dimensions
    pub fn new(x: u32, y: u32) -> Self {
        Self::new_n([x, y])
    }

    /// Gets the bit at (x, y)
    pub fn get(&self, x: u32, y: u32) -> bool {
        self.get_n([x, y])
    }

    /// Sets the bit at (x, y)
    pub fn set(&mut self, x: u32, y: u32) {
        self.set_n([x, y]);
    }

    /// Clears the bit at (x, y)
    pub fn clear(&mut self, x: u32, y: u32) {
        self.clear_n([x, y]);
    }

    /// Toggle the bit at (x, y)
    pub fn flip(&mut self, x: u32, y: u32) {
        self.flip_n([x, y]);
    }

    /// Grid width (x-dimension)
    pub fn width(&self) -> u32 {
        self.shape[0]
    }

    /// Grid height (y-dimension)
    pub fn height(&self) -> u32 {
        self.shape[1]
    }
}

impl<W: Word> fmt::Display for BitGrid<W, 2> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for y in 0..self.height() {
            for x in 0..self.width() {
                let ch = if self.get(x, y) { 'x' } else { 'o' };
                write!(f, "{}", ch)?;
            }
            if y + 1 < self.height() {
                writeln!(f)?;
            }
        }
        Ok(())
    }
}


impl<WRD: Word> BitGrid<WRD, 3> {

    /// Creates a new 3D grid with specified dimensions
    pub fn new(x: u32, y: u32, z: u32) -> Self {
        Self::new_n([x, y, z])
    }

    /// Gets the bit value at (x, y, z)
    pub fn get(&self, x: u32, y: u32, z: u32) -> bool {
        self.get_n([x, y, z])
    }

    /// Sets the bit at (x, y, z)
    pub fn set(&mut self, x: u32, y: u32, z: u32) {
        self.set_n([x, y, z]);
    }

    /// Clears the bit at (x, y, z)
    pub fn clear(&mut self, x: u32, y: u32, z: u32) {
        self.clear_n([x, y, z]);
    }

    /// Toggles the bit at (x, y, z)
    pub fn flip(&mut self, x: u32, y: u32, z: u32) {
        self.flip_n([x, y, z]);
    }

    /// Grid width (x-dimension)
    pub fn width(&self) -> u32 {
        self.shape[0]
    }

    /// Grid height (y-dimension)
    pub fn height(&self) -> u32 {
        self.shape[1]
    }

    /// Grid depth (z-dimension)
    pub fn depth(&self) -> u32 {
        self.shape[2]
    }
}


/// Iterator over set bits in an N-dimensional [`BitGrid`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BitIter<'a, W: Word, const N: usize> {
    /// Slice of words representing the bit field
    pub data: &'a [W],
    /// Dimensions of the grid [dim0, dim1, ..., dimN-1]
    pub shape: [u32; N],
    /// Current word index
    pub index: usize,
    /// Current word being scanned
    pub curr_wrd: W,
}

impl<'a, W: Word, const N: usize> Default for BitIter<'a, W, N> {
    fn default() -> Self {
        Self {
            data: &[],
            shape: [0; N],
            curr_wrd: W::EMPTY,
            index: 0,
        }
    }
}

impl<'a, W: Word, const N: usize> BitIter<'a, W, N> {
    /// Creates a new iterator for a grid with given shape and data
    pub fn new(shape: [u32; N], data: &'a [W]) -> Self {
        let first = data.first().copied().unwrap_or(W::EMPTY);
        Self {
            shape,
            data,
            index: 0,
            curr_wrd: first,
        }
    }

    /// Converts a flat bit index to N-dimensional coordinates
    ///
    fn crumple(&self, bit_index: u32) -> [u32; N] {
        let mut coords = [0; N];
        let mut remainder = bit_index;
        
        for i in (0..N).rev() {
            let stride = self.shape[i];
            coords[i] = remainder % stride;
            remainder /= stride;
        }
        
        coords
    }
}

impl<W: Word, const N: usize> Iterator for BitIter<'_, W, N> {
    type Item = [u32; N];

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        while self.curr_wrd == W::EMPTY {
            self.index += 1;
            if self.index >= self.data.len() {
                return None;
            }
            self.curr_wrd = self.data[self.index];
        }

        let tz = self.curr_wrd.trailing_zeros();
        let bit_index = (self.index as u32) * W::N_BITS + tz;
        self.curr_wrd &= self.curr_wrd - W::ONE;

        Some(self.crumple(bit_index))
    }
}




#[cfg(test)]
mod test {
    use super::{BitGrid, BitGrid2D, BitGrid3D, Word};

    extern crate std;
    use std::{vec, vec::Vec};

    fn collect_bits<W: Word>(grid: &BitGrid2D<W>) -> Vec<[u32; 2]> {
        grid.iter().collect()
    }

    #[test]
    fn test_new_and_empty() {
        let g32 = BitGrid2D::<u32>::new(5, 4);
        assert_eq!(g32.width(), 5);
        assert_eq!(g32.height(), 4);
        assert!(g32.iter().next().is_none());

        let g64 = BitGrid2D::<u64>::new(7, 3);
        assert_eq!(g64.width(), 7);
        assert_eq!(g64.height(), 3);
        assert!(g64.iter().next().is_none());
    }

    #[test]
    fn test_set_and_get() {
        let mut g = BitGrid2D::<u32>::new(4, 4);
        g.set(0, 0);
        g.set(3, 0);
        g.set(1, 2);
        g.set(2, 3);

        assert!(g.get(0, 0));
        assert!(g.get(3, 0));
        assert!(g.get(1, 2));
        assert!(g.get(2, 3));
        assert!(!g.get(1, 1));
        assert!(!g.get(0, 3));
    }

    #[test]
    fn test_iterator_basic() {
        let mut g = BitGrid2D::<u64>::new(3, 3);
        for i in 0..3 {
            g.set(i, i);
        }
        let mut coords = collect_bits(&g);
        coords.sort_unstable();
        let expected = vec![[0,0], [1,1], [2,2]];
        assert_eq!(coords, expected);
    }

    #[test]
    fn test_iterator_sparse() {
        let mut g = BitGrid2D::<u32>::new(6, 2);
        g.set(5, 0);
        g.set(0, 1);
        g.set(3, 1);
        let mut coords = collect_bits(&g);
        coords.sort_unstable();
        let expected = vec![[0,1], [3,1], [5,0]];
        assert_eq!(coords, expected);
    }

    #[test]
    fn test_full_last_word_masking() {
        let w = 10;
        let h = 1;
        let mut g = BitGrid2D::<u32>::new(w, h);
        for x in 0..w {
            g.set(x, 0);
        }
        let coords = collect_bits(&g);
        assert_eq!(coords.len() as u32, w);
        let mut expected: Vec<_> = (0..w).map(|x| [x,0]).collect();
        expected.sort_unstable();
        assert_eq!(coords, expected);
    }

    #[test]
    fn packed_grid() {
        let mut g = BitGrid2D::<u32>::new(4, 4);
        g.set(1, 1);
        g.set(3, 1);
        g.set(3, 3);

        assert!(g.get(1, 1));
        assert!(g.get(3, 1));
        assert!(g.get(3, 3));

        let bits: Vec<_> = g.iter().collect();
        assert_eq!(&bits, &[[1, 1], [3, 1], [3, 3]], "{bits:?}");
    }


    #[test]
    fn test_set_clear_get() {
        let mut g = BitGrid2D::<u32>::new(4, 4);
        g.set(1, 2);
        assert!(g.get(1, 2));
        
        g.clear(1, 2);
        assert!(!g.get(1, 2));
        
        g.flip(1, 2);
        assert!(g.get(1, 2));
    }

    #[test]
    fn test_iterator() {
        let mut g = BitGrid2D::<u64>::new(3, 3);
        g.set(0, 0);
        g.set(1, 1);
        g.set(2, 2);
        
        let mut coords = collect_bits(&g);
        coords.sort();
        assert_eq!(coords, vec![[0, 0], [1, 1], [2, 2]]);
    }

    #[test]
    fn test_edge_cases() {
        // Empty grid
        let g = BitGrid2D::<u32>::new(0, 0);
        assert_eq!(g.iter().count(), 0);
        
        // Single cell grid
        let mut g = BitGrid2D::<u32>::new(1, 1);
        g.set(0, 0);
        assert_eq!(g.iter().collect::<Vec<_>>(), vec![[0, 0]]);
    }

    #[test]
    fn test_full_grid() {
        let mut g = BitGrid2D::<u32>::new(32, 2);
        for y in 0..2 {
            for x in 0..32 {
                g.set(x, y);
            }
        }
        assert_eq!(g.iter().count(), 64);
    }

    #[test]
    fn test_3d_grid() {
        let mut space = BitGrid3D::<u32>::new(2, 2, 2);
        space.set(0, 0, 0);
        space.set(1, 1, 1);
        
        let mut coords = space.iter().collect::<Vec<_>>();
        coords.sort();
        assert_eq!(coords, vec![[0, 0, 0], [1, 1, 1]]);
    }

    #[test]
    #[should_panic]
    fn test_overflow_protection() {
        let _ = BitGrid2D::<u32>::new(u32::MAX, u32::MAX);
    }

}