Abstract HashMap into HTable (#146)
It is likely that we'll change the HashMap implementation in the future, hence its best to hide away the HashMap to make sure we can easily replace it.next
parent
6ab73958f2
commit
74893c275e
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Created on Sun May 09 2021
|
||||
*
|
||||
* This file is a part of Skytable
|
||||
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
|
||||
* NoSQL database written by Sayan Nandan ("the Author") with the
|
||||
* vision to provide flexibility in data modelling without compromising
|
||||
* on performance, queryability or scalability.
|
||||
*
|
||||
* Copyright (c) 2021, Sayan Nandan <ohsayan@outlook.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Borrow;
|
||||
pub use std::collections::hash_map::Entry;
|
||||
use std::collections::hash_map::Keys;
|
||||
use std::collections::hash_map::Values;
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::iter::FromIterator;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct HTable<K, V>
|
||||
where
|
||||
K: Eq + Hash,
|
||||
{
|
||||
inner: HashMap<K, V>,
|
||||
}
|
||||
|
||||
impl<K, V> HTable<K, V>
|
||||
where
|
||||
K: Eq + Hash,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
HTable {
|
||||
inner: HashMap::new(),
|
||||
}
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
pub fn remove<Q>(&mut self, key: &Q) -> Option<(K, V)>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
self.inner.remove_entry(key)
|
||||
}
|
||||
pub fn contains_key<Q>(&self, key: &Q) -> bool
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
self.inner.contains_key(key)
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
self.inner.clear()
|
||||
}
|
||||
pub fn get<Q>(&self, key: &Q) -> Option<&V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Hash + Eq + ?Sized,
|
||||
{
|
||||
self.inner.get(key)
|
||||
}
|
||||
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
|
||||
self.inner.entry(key)
|
||||
}
|
||||
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
|
||||
self.inner.insert(k, v)
|
||||
}
|
||||
pub fn keys(&self) -> Keys<'_, K, V> {
|
||||
self.inner.keys()
|
||||
}
|
||||
pub fn values(&self) -> Values<'_, K, V> {
|
||||
self.inner.values()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Eq + Hash, V> IntoIterator for HTable<K, V> {
|
||||
type Item = (K, V);
|
||||
type IntoIter = std::collections::hash_map::IntoIter<K, V>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.inner.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> FromIterator<(K, V)> for HTable<K, V>
|
||||
where
|
||||
K: Eq + Hash,
|
||||
{
|
||||
fn from_iter<T>(iter: T) -> Self
|
||||
where
|
||||
T: IntoIterator<Item = (K, V)>,
|
||||
{
|
||||
HTable {
|
||||
inner: HashMap::from_iter(iter),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue