use relate_core::tokens::WorkspaceCharge;
use relate_core::Result;
use crate::hash_seed::{id_set_with_capacity, seeded_set_with_capacity, IdSet, SeededSet};
use crate::workspace::{charge_set_delta, word_insert_delta, word_set_bytes};
const INLINE_WORDS: usize = 16;
const _: () = assert!(INLINE_WORDS <= u32::BITS as usize);
const PROMOTED_CAPACITY: usize = 32;
#[derive(Clone)]
enum Tier {
Empty,
Inline { len: u8, slots: [u64; INLINE_WORDS] },
Promoted(WordTable),
}
#[derive(Clone)]
enum WordTable {
Id(IdSet<u64>),
Value(SeededSet<u64>),
}
impl WordTable {
fn for_hash(hash: WordHash) -> Self {
match hash {
WordHash::Id => Self::Id(id_set_with_capacity(PROMOTED_CAPACITY)),
WordHash::Value => Self::Value(seeded_set_with_capacity(PROMOTED_CAPACITY)),
}
}
fn insert(&mut self, word: u64) -> bool {
match self {
Self::Id(set) => set.insert(word),
Self::Value(set) => set.insert(word),
}
}
fn remove(&mut self, word: u64) {
match self {
Self::Id(set) => set.remove(&word),
Self::Value(set) => set.remove(&word),
};
}
fn len(&self) -> usize {
match self {
Self::Id(set) => set.len(),
Self::Value(set) => set.len(),
}
}
}
fn promoted_table(hash: WordHash, words: &[u64]) -> WordTable {
let mut table = WordTable::for_hash(hash);
for &word in words {
table.insert(word);
}
table
}
#[derive(Clone, Copy)]
pub(crate) enum WordHash {
Id,
Value,
}
pub(crate) struct TieredWordSet {
tier: Tier,
hash: WordHash,
charge: WorkspaceCharge,
}
impl TieredWordSet {
pub(crate) fn new(hash: WordHash) -> Self {
Self {
tier: Tier::Empty,
hash,
charge: WorkspaceCharge::empty(),
}
}
pub(crate) fn insert(&mut self, word: u64, family: &'static str) -> Result<bool> {
match &mut self.tier {
Tier::Empty => {
charge_set_delta(&mut self.charge, family, || word_insert_delta(0))?;
let mut slots = [0; INLINE_WORDS];
slots[0] = word;
self.tier = Tier::Inline { len: 1, slots };
Ok(true)
}
Tier::Inline { len, slots } => {
if inline_contains(slots, *len, word) {
return Ok(false);
}
if usize::from(*len) < INLINE_WORDS {
let old_len = usize::from(*len);
charge_set_delta(&mut self.charge, family, || word_insert_delta(old_len))?;
slots[old_len] = word;
*len += 1;
return Ok(true);
}
let slots = *slots;
let mut table = promoted_table(self.hash, &slots);
let inserted = insert_table_word(&mut self.charge, family, &mut table, word);
self.tier = Tier::Promoted(table);
inserted
}
Tier::Promoted(table) => insert_table_word(&mut self.charge, family, table, word),
}
}
pub(crate) fn len(&self) -> usize {
match &self.tier {
Tier::Empty => 0,
Tier::Inline { len, .. } => usize::from(*len),
Tier::Promoted(table) => table.len(),
}
}
pub(crate) fn bytes(&self) -> u64 {
word_set_bytes(self.len())
}
#[cfg(test)]
pub(crate) fn promoted(&self) -> bool {
matches!(self.tier, Tier::Promoted(_))
}
pub(crate) fn promote_inline(&mut self) {
if let Tier::Inline { len, slots } = &mut self.tier {
let words = *slots;
let table = promoted_table(self.hash, &words[..usize::from(*len)]);
self.tier = Tier::Promoted(table);
}
}
pub(crate) fn clone_contents(&self) -> Self {
Self {
tier: self.tier.clone(),
hash: self.hash,
charge: WorkspaceCharge::empty(),
}
}
pub(crate) fn try_clone(&self) -> Result<Self> {
let mut copy = self.clone_contents();
copy.charge = WorkspaceCharge::retain_with(|| copy.bytes())?;
Ok(copy)
}
pub(crate) fn charged(&self) -> u64 {
self.charge.bytes()
}
}
fn insert_table_word(
charge: &mut WorkspaceCharge,
family: &'static str,
table: &mut WordTable,
word: u64,
) -> Result<bool> {
if !table.insert(word) {
return Ok(false);
}
let old_len = table.len() - 1;
charge_set_delta(charge, family, || word_insert_delta(old_len))
.inspect_err(|_| table.remove(word))?;
Ok(true)
}
#[inline]
fn inline_contains(slots: &[u64; INLINE_WORDS], len: u8, word: u64) -> bool {
let mut mask: u32 = 0;
for (lane, slot) in slots.iter().enumerate() {
mask |= u32::from(*slot == word) << lane;
}
mask & ((1u32 << len) - 1) != 0
}