use std::collections::HashMap;
use std::hash::Hash;
use super::Literal;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Map {
index: HashMap<Literal, usize>,
pair: Vec<(Literal, Option<Literal>)>,
}
impl Map {
pub fn new() -> Self {
Self {
index: HashMap::new(),
pair: Vec::new(),
}
}
pub fn len(&self) -> usize {
self.pair.len()
}
pub fn is_empty(&self) -> bool {
self.pair.is_empty()
}
pub fn insert(&mut self, key: Literal, value: Option<Literal>) -> Option<Option<Literal>> {
if let Some(index) = self.index.get(&key) {
let old_value = std::mem::replace(&mut self.pair[*index].1, value);
Some(old_value)
} else {
self.pair.push((key.clone(), value));
self.index.insert(key, self.pair.len() - 1);
None
}
}
pub fn get(&self, key: &Literal) -> Option<&Option<Literal>> {
self.index.get(key).map(|index| &self.pair[*index].1)
}
pub fn has_same_content(&self, other: &Map) -> bool {
if self.len() != other.len() {
return false;
}
for (key, value) in &self.pair {
match other.get(key) {
Some(other_value) if value == other_value => (),
_ => return false,
}
}
true
}
}
impl Default for Map {
fn default() -> Self {
Self::new()
}
}
impl Hash for Map {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
for (key, value) in &self.pair {
key.hash(state);
value.hash(state);
}
}
}
impl FromIterator<(Literal, Option<Literal>)> for Map {
fn from_iter<T: IntoIterator<Item = (Literal, Option<Literal>)>>(iter: T) -> Self {
let mut map = Map::new();
for (key, value) in iter {
map.insert(key, value);
}
map
}
}
impl IntoIterator for Map {
type Item = (Literal, Option<Literal>);
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.pair.into_iter()
}
}
impl<const N: usize> From<[(Literal, Option<Literal>); N]> for Map {
fn from(value: [(Literal, Option<Literal>); N]) -> Self {
value.iter().cloned().collect()
}
}