Skip to content
Permalink
Browse files

TreePVector.subList() makes fewer calls to TreePVector(), IntTreePMap…

…(), and IntTreePMap.withKeysChangedAbove()
  • Loading branch information...
hrldcpr committed Aug 3, 2019
1 parent a3ab9b9 commit f58c25fb264c5c3421b4703d26731a2d2578ea2c
Showing with 20 additions and 13 deletions.
  1. +1 −0 CHANGELOG.md
  2. +14 −0 src/main/java/org/pcollections/IntTreePMap.java
  3. +5 −13 src/main/java/org/pcollections/TreePVector.java
@@ -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
@@ -88,6 +88,20 @@ private IntTreePMap(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?
@@ -90,22 +90,14 @@ public E get(final int index) {

@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 ////

0 comments on commit f58c25f

Please sign in to comment.
You can’t perform that action at this time.