-
Notifications
You must be signed in to change notification settings - Fork 416
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
Return isolated windows from Window* methods #655
Conversation
…d WindowRightDoNotExposeItsBuffer.
- ModifyWindowBeforeMoveNextDoNotAffectNextWindow - ModifyWindowAfterMoveNextDoNotAffectNextWindow - ModifyWindowBeforeMoveNextDoNotAffectPrevWindow
…wDoNotAffectPrevWindow
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.
Thanks for the fix. The only trouble with the correction is that it now eagerly allocates and prepares the next window, which is wasteful if the iteration never resumes. I like how it's done in F#:
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
// windowed : int -> seq<'T> -> seq<'T[]>
[<CompiledName("Windowed")>]
let windowed windowSize (source: seq<_>) =
checkNonNull "source" source
if windowSize <= 0 then invalidArgFmt "windowSize" "{0}\nwindowSize = {1}"
[|SR.GetString SR.inputMustBePositive; windowSize|]
seq {
let arr = Array.zeroCreateUnchecked windowSize
let r = ref (windowSize - 1)
let i = ref 0
use e = source.GetEnumerator()
while e.MoveNext() do
arr.[!i] <- e.Current
i := (!i + 1) % windowSize
if !r = 0 then
if windowSize < 32 then
yield Array.init windowSize (fun j -> arr.[(!i+j) % windowSize])
else
let result = Array.zeroCreateUnchecked windowSize
Array.Copy(arr, !i, result, 0, windowSize - !i)
Array.Copy(arr, 0, result, windowSize - !i, !i)
yield result
else r := (!r - 1)
}
A single working and circular window is maintained internally and it's re-aligned and copied to a new window when it's time to yield. Can we take inspiration from that?
@atifaziz I do a re-read of the code With current implementation of Window (and of WindowImplem in #656), at each iteration:
And when the sequence reach its end, all allocated array have been passed to the caller With your proposed implementation (has I understand it), at each iteration:
And when the sequence reach its end, one array has not been passed to the caller In any case a new array of size N is (and have to, since we give the data to the user) allocated. The only "we can do it later" I see is: yield return window;
newWindow[size - 1] = iter.Current; instead of newWindow[size - 1] = iter.Current;
yield return window; I will put it in #656 ;) |
How do you know you'll reach the end of the sequence. The caller is in control of asking the next window and may stop iteration earlier due to some condition. |
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.
Small nitpicks for now.
It's an hypothesis. |
Co-Authored-By: Atif Aziz <code@raboof.com>
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.
Thank you for this fix and the tests! 👍
This allow tests from #653 to pass and fix #652