use super::types::{IndexMap, IndexSet};
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize, serde_derive::Serialize),
serde(default, rename_all = "snake_case")
)]
pub struct MemoryIndex {
pub(crate) dimension_index: IndexMap<usize, usize>,
pub(crate) content_index: IndexMap<Vec<usize>, usize>,
pub(crate) importance_index: IndexMap<usize, usize>,
}
impl MemoryIndex {
pub fn new() -> Self {
MemoryIndex {
dimension_index: IndexMap::new(),
content_index: IndexMap::new(),
importance_index: IndexMap::new(),
}
}
pub const fn dimension_index(&self) -> &IndexMap<usize, usize> {
&self.dimension_index
}
pub const fn dimension_index_mut(&mut self) -> &mut IndexMap<usize, usize> {
&mut self.dimension_index
}
pub const fn content_index(&self) -> &IndexMap<Vec<usize>, usize> {
&self.content_index
}
pub const fn content_index_mut(&mut self) -> &mut IndexMap<Vec<usize>, usize> {
&mut self.content_index
}
pub const fn importance_index(&self) -> &IndexMap<usize, usize> {
&self.importance_index
}
pub const fn importance_index_mut(&mut self) -> &mut IndexMap<usize, usize> {
&mut self.importance_index
}
pub fn set_dimension_index(&mut self, index: IndexMap<usize, usize>) -> &mut Self {
self.dimension_index = index;
self
}
pub fn set_content_index(&mut self, index: IndexMap<Vec<usize>, usize>) -> &mut Self {
self.content_index = index;
self
}
pub fn set_importance_index(&mut self, index: IndexMap<usize, usize>) -> &mut Self {
self.importance_index = index;
self
}
pub fn with_dimension_index(self, index: IndexMap<usize, usize>) -> Self {
Self {
dimension_index: index,
..self
}
}
pub fn with_content_index(self, index: IndexMap<Vec<usize>, usize>) -> Self {
Self {
content_index: index,
..self
}
}
pub fn with_importance_index(self, index: IndexMap<usize, usize>) -> Self {
Self {
importance_index: index,
..self
}
}
pub fn clear(&mut self) -> &mut Self {
self.dimension_index.clear();
self.content_index.clear();
self.importance_index.clear();
self
}
pub fn dimension_index_len(&self) -> usize {
self.dimension_index().len()
}
pub fn content_index_len(&self) -> usize {
self.content_index().len()
}
pub fn importance_index_len(&self) -> usize {
self.importance_index().len()
}
pub fn dimension_index_is_empty(&self) -> bool {
self.dimension_index().is_empty()
}
pub fn content_index_is_empty(&self) -> bool {
self.content_index().is_empty()
}
pub fn importance_index_is_empty(&self) -> bool {
self.importance_index().is_empty()
}
pub fn is_empty(&self) -> bool {
self.dimension_index_is_empty()
&& self.content_index_is_empty()
&& self.importance_index_is_empty()
}
pub fn total_indexed_features(&self) -> usize {
self.dimension_index_len() + self.content_index_len() + self.importance_index_len()
}
pub fn get_dimension<Q>(&self, key: &Q) -> Option<&IndexSet<usize>>
where
Q: Eq + core::hash::Hash,
usize: core::borrow::Borrow<Q>,
{
self.dimension_index().get(key)
}
pub fn get_dimension_mut<Q>(&mut self, key: &Q) -> Option<&mut IndexSet<usize>>
where
Q: Eq + core::hash::Hash,
usize: core::borrow::Borrow<Q>,
{
self.dimension_index_mut().get_mut(key)
}
pub fn get_content<Q>(&self, key: &Q) -> Option<&IndexSet<usize>>
where
Q: Eq + core::hash::Hash,
Vec<usize>: core::borrow::Borrow<Q>,
{
self.content_index().get(key)
}
pub fn get_content_mut<Q>(&mut self, key: &Q) -> Option<&mut IndexSet<usize>>
where
Q: Eq + core::hash::Hash,
Vec<usize>: core::borrow::Borrow<Q>,
{
self.content_index_mut().get_mut(key)
}
pub fn get_importance<Q>(&self, key: &Q) -> Option<&IndexSet<usize>>
where
Q: Eq + core::hash::Hash,
usize: core::borrow::Borrow<Q>,
{
self.importance_index().get(key)
}
pub fn get_importance_mut<Q>(&mut self, key: &Q) -> Option<&mut IndexSet<usize>>
where
Q: Eq + core::hash::Hash,
usize: core::borrow::Borrow<Q>,
{
self.importance_index_mut().get_mut(key)
}
}