use core::hash::{Hash, Hasher};
use std::collections::HashMap;
use routers_network::Entry;
use serde::{Deserialize, Serialize};
const OVERTURE_NULL_SENTINEL: i64 = -1;
#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[repr(transparent)]
pub struct OvertureEntryId {
pub identifier: i64,
}
impl OvertureEntryId {
#[inline]
pub const fn new(identifier: i64) -> Self {
Self { identifier }
}
#[inline]
pub const fn null() -> Self {
Self {
identifier: OVERTURE_NULL_SENTINEL,
}
}
#[inline]
pub const fn is_null(&self) -> bool {
self.identifier == OVERTURE_NULL_SENTINEL
}
}
impl Entry for OvertureEntryId {
#[inline]
fn identifier(&self) -> i64 {
self.identifier
}
}
impl Default for OvertureEntryId {
fn default() -> Self {
OvertureEntryId::null()
}
}
impl From<i64> for OvertureEntryId {
fn from(value: i64) -> Self {
OvertureEntryId::new(value)
}
}
impl PartialEq for OvertureEntryId {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.identifier == other.identifier
}
}
impl Hash for OvertureEntryId {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.identifier.hash(state);
}
}
#[derive(Debug, Default)]
pub struct Interner {
map: HashMap<String, i64>,
next: i64,
}
impl Interner {
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn intern(&mut self, gers: &str) -> OvertureEntryId {
if let Some(&id) = self.map.get(gers) {
return OvertureEntryId::new(id);
}
let id = self.next;
self.next += 1;
self.map.insert(gers.to_owned(), id);
OvertureEntryId::new(id)
}
#[inline]
pub fn get(&self, gers: &str) -> Option<OvertureEntryId> {
self.map.get(gers).copied().map(OvertureEntryId::new)
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
}