Skip to content
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

Improved ArrayList performance by removing size field. #255

Merged
merged 5 commits into from
Jul 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor ArrayList Insert() by slices.Insert()
  • Loading branch information
ksw2000 committed Jun 20, 2024
commit 8e464f3cef651fecec5f6266c00e61fae78e4fe0
8 changes: 3 additions & 5 deletions lists/arraylist/arraylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ func (list *List[T]) Swap(i, j int) {
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
func (list *List[T]) Insert(index int, values ...T) {

if !list.withinRange(index) {
// Append
if index == len(list.elements) {
ksw2000 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -139,10 +138,9 @@ func (list *List[T]) Insert(index int, values ...T) {
return
}

l := len(values)
list.growBy(l)
copy(list.elements[index+l:], list.elements[index:len(list.elements)-l])
copy(list.elements[index:], values)
l := len(list.elements)
list.growBy(len(values))
list.elements = slices.Insert(list.elements[:l], index, values...)
}

// Set the value at specified index
Expand Down
Loading