-
Notifications
You must be signed in to change notification settings - Fork 448
Add sliding windows algorithm #20
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
Conversation
94d9ae0
to
6adf2cb
Compare
What do you think about When we were naming |
I would not be against the name change, it was not immediately obvious to me that The arguments you make are valid, so I would be happy to change it - but, before I do, It might be a good idea to see what other folks think 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.
Thanks for this contribution, @ollieatkinson! 👏
@kylemacomber I've given this some thought and renamed the label to |
In a general-purpose language that now has Windows support, I would be wary of a general-purpose API vending a type named |
That's a good thought, I'm liking the suggestion you've made! Thank you @xwu |
Any reason not to rename the function accordingly now, |
I'm in the process of renaming the type, function and updating the documentation now! |
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 great, @ollieatkinson!
Should probably add a link to |
Many thanks to @timvermeulen for helping me refine this collection algorithm.
Description
Break a collection into overlapping contiguous window subsequences where
elements are slices from the original collection.
The
slidingWindows(ofCount:)
method takes in a integer size and returns a collection of subsequences.Detailed Design
The
slidingWindows(ofCount:)
is added as a method on an extension ofCollection
If a size larger than the collection length is specified, an empty collection is returned. Due to this
behaviour the indexes must be calculated on initialisation as we have to be able to compare the
upperBound
and allowCollection
correctly calculateisEmpty
.The resulting
SlidingWindows
type is a collection, with conditional conformance to theBidirectionalCollection
, andRandomAccessCollection
when the base collectionconforms.
Complexity
The call to
slidingWindows(ofCount: k)
is O(1) if the collection conforms toRandomAccessCollection
, otherwise O(k). Access to the next windowis O(1).
Naming
The type
SlidingWindows
takes its name from the algorithm, similarly the method takesit's name from it too
slidingWindows(ofCount: k)
.The label on the method
ofCount
was chosen to create a consistent feel to the APIavailable in swift-algorithms repository. Inspiration was taken from
combinations(ofCount:)
andpermutations(ofCount:)
.Previously the name
windows
was considered but was deemed to potentially createambiguity with the Windows operating system.
Comparison with other languages
rust has
std::slice::Windows
which is a method available on slices. It has the same semantics asdescribed here.
Documentation
This documentation exists in
Guides/SlidingWindows.md
Impact
This is an additive change, no impact.
Checklist