pipecircuit 0.0.11

Correct. Robust. Fast. Portable.
Documentation
use super::rows::SelectionRows;
use crate::storage::warp_table::secondary_index::SlotSet;
use crate::storage::warp_table::{WarpTable, WarpTableRow};

pub(super) enum SelectionMembership<'table> {
    Empty,
    Borrowed(&'table SlotSet),
    Owned(SlotSet),
}

#[must_use]
pub struct Selection<'table, Row: WarpTableRow, Brand = Row> {
    pub(super) table: &'table WarpTable<Row, Brand>,
    pub(super) membership: SelectionMembership<'table>,
}

impl<'table, Row: WarpTableRow, Brand> Selection<'table, Row, Brand> {
    pub(super) const fn empty(table: &'table WarpTable<Row, Brand>) -> Self {
        Self {
            table,
            membership: SelectionMembership::Empty,
        }
    }

    pub(super) const fn borrowed(
        table: &'table WarpTable<Row, Brand>,
        slots: &'table SlotSet,
    ) -> Self {
        Self {
            table,
            membership: SelectionMembership::Borrowed(slots),
        }
    }

    pub(super) const fn owned(table: &'table WarpTable<Row, Brand>, slots: SlotSet) -> Self {
        Self {
            table,
            membership: SelectionMembership::Owned(slots),
        }
    }

    pub fn len(&self) -> u64 {
        match &self.membership {
            SelectionMembership::Empty => 0,
            SelectionMembership::Borrowed(slots) => slots.len(),
            SelectionMembership::Owned(slots) => slots.len(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn rows(self) -> SelectionRows<'table, Row, Brand>
    where
        Row::Indexes: super::rows::StableSlotResolver,
    {
        SelectionRows::new(self.table, self.membership)
    }
}