-
Notifications
You must be signed in to change notification settings - Fork 87
Fix Deque.reserve. #2943
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
base: master
Are you sure you want to change the base?
Fix Deque.reserve. #2943
Conversation
kasperl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
lib/core/collections.toit
Outdated
| // performance, we can reimplement with the a more traditional | ||
| // implementation. | ||
| first_ := 0 | ||
| last_ := 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: It is a little bit weird that first is inclusive and last isn't, just from a naming perspective. Maybe start/end would be nicer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
lib/core/collections.toit
Outdated
| result = backing.remove-last | ||
| else: | ||
| result = backing[last - 1] | ||
| last_ = last - 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should probably null out the removed element in the backing store, like it is done for remove-first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch.
done.
| Returns the value that was removed. | ||
| */ | ||
| remove --at/int -> any: | ||
| last := size - 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a different last than last_.
| if at >= last >> 1: | ||
| return backing_.remove --at=(first_ + at) | ||
| if not 0 <= at <= last: throw "OUT_OF_BOUNDS" | ||
| if at >= (last >> 1): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. And refactored it to make it easier to follow.
lib/core/collections.toit
Outdated
| last := last_ | ||
| reserved := backing.size - last | ||
| sz := last - first | ||
| if first * 2 > last: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment here? Why do we multiply here and shift in the other place?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switched to shift for consistency.
done.
lib/core/collections.toit
Outdated
| backing.resize size | ||
| last := last_ | ||
| reserved := backing.size - last | ||
| sz := last - first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we compute this eagerly? Why isn't this called size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
somehow this file started to copy fields into locals (probably for efficiency reasons), and I have continued that "tradition".
It can't be called "size", as that would shadow the method, which isn't allowed.
tests/deque-test.toit
Outdated
| expect-equals "[1, 42]" d.stringify | ||
| expect-equals "[103, 3]" d2.stringify | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drop one newline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
No description provided.