Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove only one instance of the duplicated atom #799

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions lib/src/space/grounding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,17 @@ impl GroundingSpace {

fn remove_internal(&mut self, atom: &Atom) -> bool {
let index_key = atom_to_trie_key(atom);
let indexes: Vec<usize> = self.index.get(&index_key).map(|i| *i).collect();
let mut indexes: Vec<usize> = indexes.into_iter()
.filter(|i| self.content[*i] == *atom).collect();
indexes.sort_by(|a, b| b.partial_cmp(a).unwrap());
let is_removed = indexes.len() > 0;
for i in indexes {
self.index.remove(&index_key, &i);
self.free.insert(i);
let index = self.index.get(&index_key)
.filter(|&&i| self.content[i] == *atom)
.next();
match index {
Some(&i) => {
self.index.remove(&index_key, &i);
self.free.insert(i);
true
},
None => false,
}
is_removed
}

/// Replaces `from` atom to `to` atom inside space. Doesn't add `to` when
Expand Down Expand Up @@ -431,6 +432,22 @@ mod test {
SpaceEvent::Remove(sym!("b"))]);
}

#[test]
fn remove_duplicated_atom() {
let mut space = GroundingSpace::new();
let observer = space.common.register_observer(SpaceEventCollector::new());

space.add(expr!("a"));
space.add(expr!("a"));
space.add(expr!("a"));
assert_eq!(space.remove(&expr!("a")), true);

assert_eq_no_order!(space, vec![expr!("a"), expr!("a")]);
assert_eq!(observer.borrow().events, vec![SpaceEvent::Add(sym!("a")),
SpaceEvent::Add(sym!("a")), SpaceEvent::Add(sym!("a")),
SpaceEvent::Remove(sym!("a"))]);
}

#[test]
fn remove_atom_not_found() {
let mut space = GroundingSpace::new();
Expand Down
Loading