Skip to main content

egui_table_kit/
highlights.rs

1//! Highlighting layers powered by roaring bitmaps.
2
3use roaring::RoaringBitmap;
4use serde::{Deserialize, Serialize};
5
6/// Highlighting structure mapping row indices to one of up to 10 color-tag groups.
7#[derive(Clone, Debug, Default, Serialize, Deserialize)]
8pub struct Highlights(pub [RoaringBitmap; 10]);
9
10impl Highlights {
11    /// Accesses the highlight color index assigned to the row (if any).
12    #[must_use]
13    pub fn get_usize(&self, index: usize) -> Option<u8> {
14        self.get(index as u32)
15    }
16
17    /// Adds a row index to a specific highlight color group.
18    pub fn insert(&mut self, typ: u8, index: u32) -> bool {
19        self.0
20            .get_mut(typ as usize)
21            .is_some_and(|bmp| bmp.insert(index))
22    }
23
24    /// Accesses the highlight color group index matching a specific row index.
25    #[must_use]
26    pub fn get(&self, index: u32) -> Option<u8> {
27        self.0.iter().enumerate().find_map(|(i, bmp)| {
28            if bmp.contains(index) {
29                Some(i as u8)
30            } else {
31                None
32            }
33        })
34    }
35
36    /// Unions a selection map into a specific highlight group, removing them from other groups.
37    pub fn insert_map(&mut self, typ: u8, map: &RoaringBitmap) {
38        self.0.iter_mut().enumerate().for_each(|(i, bmp)| {
39            if i as u8 == typ {
40                *bmp |= map;
41            } else {
42                *bmp -= map;
43            }
44        });
45    }
46
47    /// Removes a collection of row indices from all color highlight groups.
48    pub fn remove_map(&mut self, map: &RoaringBitmap) {
49        for bmp in &mut self.0 {
50            *bmp -= map;
51        }
52    }
53}