Skip to content

Commit 9f1c172

Browse files
committed
Document new data types and methods
Rename some types and methods to make names more logical.
1 parent 5fc75f8 commit 9f1c172

File tree

3 files changed

+194
-109
lines changed

3 files changed

+194
-109
lines changed

lib/src/space/grounding/index/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,29 @@ impl<'a> Iterator for AtomIter<'a> {
143143
}
144144
}
145145

146+
/// Iterator over results of the query to the index.
146147
pub type QueryResult = Box<dyn Iterator<Item=Bindings>>;
147148

149+
/// Atom index implementation, parameterized by [DuplicationStrategy].
148150
#[derive(Default, Debug, Clone, PartialEq, Eq)]
149151
pub struct AtomIndex<D: DuplicationStrategy = NoDuplication> {
150152
trie: AtomTrie<D>,
151153
}
152154

153155
impl AtomIndex {
156+
/// New atom index instance using default [NoDuplication] duplication strategy.
154157
pub fn new() -> Self {
155158
Default::default()
156159
}
157160
}
158161

159162
impl<D: DuplicationStrategy> AtomIndex<D> {
163+
/// New atom index instance passing specific [DuplicationStrategy].
160164
pub fn with_strategy(_strategy: D) -> Self {
161165
Default::default()
162166
}
163167

168+
/// Insert atom into index.
164169
pub fn insert(&mut self, atom: Atom) {
165170
let key = AtomIter::from_atom(atom)
166171
.map(|token| Self::atom_token_to_insert_index_key(token));
@@ -176,6 +181,7 @@ impl<D: DuplicationStrategy> AtomIndex<D> {
176181
}
177182
}
178183

184+
/// Query atoms which can be unified with `atom` from index.
179185
pub fn query(&self, atom: &Atom) -> QueryResult {
180186
let key = AtomIter::from_ref(&atom)
181187
.map(|token| Self::atom_token_to_query_index_key(token));
@@ -191,16 +197,19 @@ impl<D: DuplicationStrategy> AtomIndex<D> {
191197
}
192198
}
193199

200+
/// Remove specific atom from index.
194201
pub fn remove(&mut self, atom: &Atom) -> bool {
195202
let key = AtomIter::from_ref(&atom)
196203
.map(|token| Self::atom_token_to_query_index_key(token));
197204
self.trie.remove(key)
198205
}
199206

207+
/// Iterate via atoms in index.
200208
pub fn iter(&self) -> Box<dyn Iterator<Item=Cow<'_, Atom>> + '_> {
201209
self.trie.unpack_atoms()
202210
}
203211

212+
/// Returns [true] if index has no atoms.
204213
pub fn is_empty(&self) -> bool {
205214
self.trie.is_empty()
206215
}

lib/src/space/grounding/index/storage.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ use std::hash::{Hash, Hasher};
77
use std::hash::DefaultHasher;
88
use std::fmt::{Debug, Display, Formatter};
99

10+
/// Storage for a hashable atoms.
1011
#[derive(Default, Debug, Clone, PartialEq, Eq)]
1112
pub struct AtomStorage {
1213
next_id: usize,
1314
atoms: BiMap<HashableAtom, usize>,
1415
}
1516

1617
impl AtomStorage {
18+
/// New storage instance.
1719
#[cfg(test)]
1820
pub fn new() -> Self {
1921
Default::default()
2022
}
2123

24+
/// Insert atom into a storage. Returns `Ok(<id>)` if atom is hashable or
25+
/// `Err(<atom>)` otherwise. Returns `Ok(<previous id>)` if atoms is already
26+
/// in a storage.
2227
pub fn insert(&mut self, atom: Atom) -> Result<usize, Atom> {
2328
if Self::is_hashable(&atom) {
2429
Ok(self.insert_internal(atom))
@@ -55,10 +60,12 @@ impl AtomStorage {
5560
id
5661
}
5762

63+
/// Gets atom from the storage if any.
5864
pub fn get_atom(&self, id: usize) -> Option<&Atom> {
5965
self.atoms.get_by_right(&id).map(|h| h.as_atom())
6066
}
6167

68+
/// Gets id of the atom in the storage if any.
6269
pub fn get_id(&self, atom: &Atom) -> Option<usize> {
6370
if Self::is_hashable(atom) {
6471
self.atoms.get_by_left(&HashableAtom::Query(atom)).map(|id| *id)
@@ -77,6 +84,7 @@ impl AtomStorage {
7784
}
7885
}
7986

87+
/// Returns number of atoms in the storage.
8088
pub fn count(&self) -> usize {
8189
self.atoms.left_values().count()
8290
}

0 commit comments

Comments
 (0)