-
-
Notifications
You must be signed in to change notification settings - Fork 446
Implement index tracking to optimize list operations #8519
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
caa4679
Implement numerical index tracking to optimize adding to lists
UnderscoreTud 2b65ef5
Fix variable desync
UnderscoreTud 47e3226
Optimize deleting and getting the size of a list variable
UnderscoreTud 4006574
Implement a different approach to find an open index
UnderscoreTud 26e5e17
Implement a different approach to find an open index (keep track of o…
UnderscoreTud d946481
Make `#add` use `#handleInsert`
UnderscoreTud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/main/java/org/skriptlang/skript/util/IndexTrackingTreeMap.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package org.skriptlang.skript.util; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import org.jetbrains.annotations.VisibleForTesting; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| /** | ||
| * A {@link TreeMap} that supports automatically assigning the next available | ||
| * positive integer key, represented as a string. | ||
| * | ||
| * <p>In addition to arbitrary string keys, this map can be used with | ||
| * positive integer string keys such as {@code "1"}, {@code "2"}, and | ||
| * {@code "3"}. The {@link #add(Object)} method inserts a value using the | ||
| * next available integer key.</p> | ||
| * | ||
| * @param <V> the type of mapped values | ||
| */ | ||
| public class IndexTrackingTreeMap<V> extends TreeMap<String, V> { | ||
|
|
||
| @VisibleForTesting | ||
| final List<Integer> numericalIndices = new ArrayList<>(); | ||
|
|
||
| public IndexTrackingTreeMap() { | ||
| super(); | ||
| } | ||
|
|
||
| public IndexTrackingTreeMap(Comparator<? super String> comparator) { | ||
| super(comparator); | ||
| } | ||
|
|
||
| @Override | ||
| public V put(String key, V value) { | ||
| V previous = super.put(key, value); | ||
| if (previous == null && value != null && isInt(key)) { | ||
| try { | ||
| int index = Integer.parseInt(key); | ||
| int pos = Collections.binarySearch(numericalIndices, index); | ||
| numericalIndices.add(-pos - 1, index); | ||
| } catch (NumberFormatException ignore) {} | ||
| } else if (previous != null && value == null) { | ||
| handleRemove(key); | ||
| } | ||
| return previous; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the given value under the first available positive integer key. | ||
| * | ||
| * @param value the value to add, cannot be null | ||
| */ | ||
| public void add(V value) { | ||
| Preconditions.checkNotNull(value, "value"); | ||
| int index = nextOpenIndex(); | ||
| super.put(String.valueOf(index), value); | ||
| numericalIndices.add(index - 1, index); | ||
| } | ||
|
|
||
| @Override | ||
| public V remove(Object key) { | ||
| V value = super.remove(key); | ||
| if (value != null && key instanceof String index) | ||
| handleRemove(index); | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| super.clear(); | ||
| numericalIndices.clear(); | ||
| } | ||
|
|
||
| /** | ||
| * Finds the first available positive integer index that is not currently | ||
| * used as a key in this map. | ||
| * | ||
| * <p>This method inspects tracked numeric keys and returns the smallest | ||
| * missing index, starting at {@code 1}.</p> | ||
| * | ||
| * @return the next available positive integer index | ||
| */ | ||
| public int nextOpenIndex() { | ||
| int size = numericalIndices.size(); | ||
| if (size == 0) | ||
| return 1; | ||
|
|
||
| if (numericalIndices.getFirst() > 1) | ||
| return 1; | ||
|
|
||
| int left = 0; | ||
| int right = size - 1; | ||
|
|
||
| while (left <= right) { | ||
| int midpoint = left + (right - left >>> 1); | ||
|
|
||
| if (numericalIndices.get(midpoint) == midpoint + 1) { | ||
| left = midpoint + 1; | ||
| } else { | ||
| right = midpoint - 1; | ||
| } | ||
| } | ||
|
|
||
| return left + 1; | ||
| } | ||
|
|
||
| private void handleRemove(String index) { | ||
| if (!isInt(index)) | ||
| return; | ||
| int pos = Collections.binarySearch(numericalIndices, Integer.parseInt(index)); | ||
| if (pos >= 0) | ||
| numericalIndices.remove(pos); | ||
| } | ||
|
|
||
| private boolean isInt(String string) { | ||
| if (string == null || string.isBlank() || string.charAt(0) == '0') // Don't handle leading-zero integers | ||
| return false; | ||
| for (int i = 0; i < string.length(); i++) { | ||
| if (!isDigit(string.charAt(i))) | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private boolean isDigit(int codepoint) { | ||
| return codepoint >= '0' && codepoint <= '9'; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.