Skip to content

Commit

Permalink
Remove only instance of the duplicated atom
Browse files Browse the repository at this point in the history
  • Loading branch information
vsbogd committed Nov 14, 2024
1 parent 3136f80 commit e320801
Showing 1 changed file with 26 additions and 9 deletions.
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

0 comments on commit e320801

Please sign in to comment.