Skip to content

Commit

Permalink
TreePVector.subList() makes fewer calls to TreePVector(), IntTreePMap…
Browse files Browse the repository at this point in the history
…(), and IntTreePMap.withKeysChangedAbove()
  • Loading branch information
hrldcpr committed Aug 3, 2019
1 parent a3ab9b9 commit f58c25f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Changed
- Faster `TreePVector.subList()` makes fewer calls to `TreePVector()`, `IntTreePMap()`, and `IntTreePMap.withKeysChangedAbove()` suggested by [@Groostav](https://github.com/Groostav)] in [[#74](https://github.com/hrldcpr/pcollections/issues/74)
- Reformat with [google-java-format](https://github.com/google/google-java-format)

## [3.0.4] - 2019-07-24
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/pcollections/IntTreePMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ private IntTreePMap<V> withRoot(final IntTree<V> root) {
}

//// UNINHERITED METHODS OF IntTreePMap ////
/**
* @param <V>
* @param start
* @param end
* @return this map but with all keys start<=k<end removed
*/
public IntTreePMap<V> minusRange(final int start, final int end) {
IntTree<V> root = this.root;
for (int i = start; i < end; i++) {
root = root.minus(i);
}
return withRoot(root);
}

IntTreePMap<V> withKeysChangedAbove(final int key, final int delta) {
// TODO check preconditions of changeKeysAbove()
// TODO make public?
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/org/pcollections/TreePVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,14 @@ public Iterator<E> iterator() {

@Override
public TreePVector<E> subList(int start, int end) {
int size = size();
final int size = size();
if (start < 0 || end > size || start > end) throw new IndexOutOfBoundsException();
if (start == 0 && end == size) return this;
if (start == end) return empty();

TreePVector<E> v = this;
while (end < size) { // remove from end
v = v.minus(size - 1);
size--;
}
while (start > 0) { // remove from start
v = v.minus(0);
size--;
start--;
end--;
}
return v;
// remove from end, then remove before start:
return new TreePVector<E>(
this.map.minusRange(end, size).minusRange(0, start).withKeysChangedAbove(start, -start));
}

//// IMPLEMENTED METHODS OF PVector ////
Expand Down

0 comments on commit f58c25f

Please sign in to comment.