Skip to content

Commit

Permalink
fix: LinkedList semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
delehef committed Jan 17, 2024
1 parent c6bf4cf commit 4a15a36
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ public String toString() {

@Override
public void enter() {
this.lists.push(new CountedList<>());
this.lists.addFirst(new CountedList<>());
}

public void enter(int initialCapacity) {
this.lists.push(new CountedList<>(initialCapacity));
this.lists.addFirst(new CountedList<>(initialCapacity));
}

@Override
public void pop() {
if (this.lists.isEmpty()) {
throw new RuntimeException("asymmetric pop");
}
this.totalSize -= this.lists.pop().size();

this.totalSize -= this.lists.removeFirst().size();
}

@Override
Expand Down Expand Up @@ -117,7 +118,7 @@ public <T> T[] toArray(@NotNull T[] a) {
@Override
public boolean add(E e) {
this.totalSize++;
return this.lists.get(this.lists.size() - 1).add(e);
return this.lists.getFirst().add(e);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,22 @@ public class StackedSet<E extends ModuleOperation> implements StackedContainer,

@Override
public void enter() {
this.sets.push(new HashSet<>());
this.sets.addLast(new HashSet<>());
}

@Override
public void pop() {
Set<E> set = this.sets.pop();
Set<E> set = this.sets.removeLast();
for (E e : set) {
Integer count = occurrences.get(e);
if (count > 0) occurrences.put(e, count - 1);
else throw new IllegalStateException("asymmetric element removal !");
occurrences.computeIfPresent(
e,
(k, count) -> {
if (count > 0) {
return count - 1;
} else {
throw new IllegalStateException("asymmetric element removal !");
}
});
}
}

Expand Down

0 comments on commit 4a15a36

Please sign in to comment.