last() method for LinkedList can be more efficient#1456
Closed
davydotcom wants to merge 1 commit intoapache:GROOVY_2_5_Xfrom
Closed
last() method for LinkedList can be more efficient#1456davydotcom wants to merge 1 commit intoapache:GROOVY_2_5_Xfrom
davydotcom wants to merge 1 commit intoapache:GROOVY_2_5_Xfrom
Conversation
Contributor
|
+1 |
Member
|
Isn't this true for any implementer of |
Member
|
The actual implementation for public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}So |
asfgit
pushed a commit
that referenced
this pull request
Jan 15, 2021
asfgit
pushed a commit
that referenced
this pull request
Jan 15, 2021
Contributor
|
Merged. I also tweaked to make it for Deque as per Eric's suggestion. As noted by Eric, this may yield almost no benefit for LinkedList but can't be worse and may be significantly better for some Deque implementations. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A LinkedList has a method called getLast() which on a DoublyLinkedList is a direct reference. Using size and get() can take more overhead in testing. While this optimization is VERY small, it can have impacts on large list traversals