2019-11-07 10:55:15 -08:00
|
|
|
use crate::common::*;
|
|
|
|
|
2019-11-20 14:19:49 -08:00
|
|
|
use std::collections::btree_map;
|
|
|
|
|
2021-11-17 00:07:48 -08:00
|
|
|
#[derive(Debug, PartialEq, Serialize)]
|
|
|
|
#[serde(transparent)]
|
2019-11-07 10:55:15 -08:00
|
|
|
pub(crate) struct Table<'key, V: Keyed<'key>> {
|
|
|
|
map: BTreeMap<&'key str, V>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'key, V: Keyed<'key>> Table<'key, V> {
|
2019-11-21 07:39:32 -08:00
|
|
|
pub(crate) fn new() -> Table<'key, V> {
|
|
|
|
Table {
|
|
|
|
map: BTreeMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
pub(crate) fn insert(&mut self, value: V) {
|
|
|
|
self.map.insert(value.key(), value);
|
|
|
|
}
|
2019-11-20 14:19:49 -08:00
|
|
|
|
|
|
|
pub(crate) fn len(&self) -> usize {
|
|
|
|
self.map.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get(&self, key: &str) -> Option<&V> {
|
|
|
|
self.map.get(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn values(&self) -> btree_map::Values<&'key str, V> {
|
|
|
|
self.map.values()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn contains_key(&self, key: &str) -> bool {
|
|
|
|
self.map.contains_key(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn keys(&self) -> btree_map::Keys<&'key str, V> {
|
|
|
|
self.map.keys()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn iter(&self) -> btree_map::Iter<&'key str, V> {
|
|
|
|
self.map.iter()
|
|
|
|
}
|
2019-11-21 06:23:32 -08:00
|
|
|
|
|
|
|
pub(crate) fn pop(&mut self) -> Option<V> {
|
2021-09-16 07:51:45 -07:00
|
|
|
let key = self.map.keys().next().copied()?;
|
|
|
|
self.map.remove(key)
|
2019-11-21 06:23:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn remove(&mut self, key: &str) -> Option<V> {
|
|
|
|
self.map.remove(key)
|
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
|
2021-01-22 23:34:01 -08:00
|
|
|
impl<'key, V: Keyed<'key>> Default for Table<'key, V> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 10:55:15 -08:00
|
|
|
impl<'key, V: Keyed<'key>> FromIterator<V> for Table<'key, V> {
|
|
|
|
fn from_iter<I: IntoIterator<Item = V>>(iter: I) -> Self {
|
|
|
|
Table {
|
|
|
|
map: iter.into_iter().map(|value| (value.key(), value)).collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 14:19:49 -08:00
|
|
|
impl<'key, V: Keyed<'key>> Index<&'key str> for Table<'key, V> {
|
|
|
|
type Output = V;
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2019-11-20 14:19:49 -08:00
|
|
|
#[inline]
|
|
|
|
fn index(&self, key: &str) -> &V {
|
|
|
|
self.map.get(key).expect("no entry found for key")
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'key, V: Keyed<'key>> IntoIterator for Table<'key, V> {
|
2019-11-20 14:19:49 -08:00
|
|
|
type IntoIter = btree_map::IntoIter<&'key str, V>;
|
2020-02-10 20:07:06 -08:00
|
|
|
type Item = (&'key str, V);
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2019-11-20 14:19:49 -08:00
|
|
|
fn into_iter(self) -> btree_map::IntoIter<&'key str, V> {
|
2019-11-07 10:55:15 -08:00
|
|
|
self.map.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'table, V: Keyed<'table> + 'table> IntoIterator for &'table Table<'table, V> {
|
2019-11-20 14:19:49 -08:00
|
|
|
type IntoIter = btree_map::Iter<'table, &'table str, V>;
|
2020-02-10 20:07:06 -08:00
|
|
|
type Item = (&'table &'table str, &'table V);
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2019-11-20 14:19:49 -08:00
|
|
|
fn into_iter(self) -> btree_map::Iter<'table, &'table str, V> {
|
2019-11-07 10:55:15 -08:00
|
|
|
self.map.iter()
|
|
|
|
}
|
|
|
|
}
|