-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] Update complexity docs for seq/collection algorithms #17254
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
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.
I like the way you're standardizing the language, but I found a few places where I think you used the wrong name.
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length | ||
/// of the collection. | ||
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the number of | ||
/// elements to remove. |
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.
Shouldn't the n be k under the rule you stated?
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 review, @brentdax! Fixed these.
/// - Complexity: O(*n*), where *n* is the number of elements to drop. | ||
/// - Complexity: O(1) if the collection conforms to | ||
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the number of | ||
/// elements to drop. |
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.
n or k?
/// - Complexity: O(*n*), where *n* is equal to `maxLength`. | ||
/// - Complexity: O(1) if the collection conforms to | ||
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is equal to | ||
/// `maxLength`. |
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.
n or k?
stdlib/public/core/Collection.swift
Outdated
/// the beginning of the collection. | ||
/// - Complexity: O(1) if the collection conforms to | ||
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the number of | ||
/// elements to drop from the beginning of the collection. |
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.
n or k?
stdlib/public/core/Collection.swift
Outdated
/// - Complexity: O(*n*), where *n* is the length of the collection. | ||
/// - Complexity: O(1) if the collection conforms to | ||
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the number of | ||
/// elements to drop from the beginning of the collection. |
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.
n or k?
stdlib/public/core/Collection.swift
Outdated
/// | ||
/// - Complexity: O(1) if the collection conforms to | ||
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the number of | ||
/// elements to select from the beginning of the collection. |
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.
n or k?
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.
Unlikely I'll have time to finish before I get back from vacation in a week
stdlib/public/core/Array.swift
Outdated
/// - Complexity: Amortized O(1) over many additions. If the array uses a | ||
/// bridged `NSArray` instance as its storage, the efficiency is | ||
/// unspecified. | ||
/// - Complexity: O(1) on average, over many additions to the same array. |
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.
"addition" seems like the wrong word. Why not just "append?"
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.
"appends" is odd as a noun… To use append, we'd need to write something like "append operations" or "calls to append(_:)
", neither of which seem better. What strikes you as wrong about "additions"?
stdlib/public/core/Array.swift
Outdated
/// unspecified. | ||
/// - Complexity: O(1) on average, over many additions to the same array. | ||
/// If the array uses a bridged `NSArray` instance as its storage, the | ||
/// complexity is unspecified. |
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.
I wonder if this isn't also amortized O(1). Isn't it just as though the array started with no spare capacity?
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.
That's true — you'd get one copy to native storage, and then you're amortized O(1) from there on out.
stdlib/public/core/Array.swift
Outdated
@@ -1163,7 +1163,7 @@ extension Array: RangeReplaceableCollection, ArrayProtocol { | |||
/// | |||
/// - Parameter newElements: The elements to append to the array. | |||
/// | |||
/// - Complexity: O(*n*), where *n* is the length of the resulting array. | |||
/// - Complexity: O(*m*), where *m* is the length of `newElements`. |
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.
Did we not decide that this was amortized O(m)? I don't remember…
stdlib/public/core/Array.swift
Outdated
/// - Complexity: O(*n*), where *n* is the length of the array. | ||
/// - Complexity: O(*n*), where *n* is the length of the array. If the | ||
/// call to this method appends `newElement` to the array, the | ||
/// complexity is O(1) on average, over many additions to the same |
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 not say, "equivalent to append(newElement)
if i == count
"?
Otherwise s/addition/append/
stdlib/public/core/Array.swift
Outdated
/// - Complexity: O(*n* + *m*), where *n* is length of the array and | ||
/// *m* is the length of `newElements`. If the call to this method simply | ||
/// appends the contents of `newElements` to the array, the complexity | ||
/// is O(*m*). |
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.
Another opportunity to use equivalence.
"Calls to append(_:)" seems better to me. "Additions" sounds like applications of the + operator when I read it.
…Sent from my iPad
On Jun 15, 2018, at 5:56 PM, Nate Cook ***@***.***> wrote:
@natecook1000 commented on this pull request.
In stdlib/public/core/Array.swift:
> @@ -1138,9 +1138,9 @@ extension Array: RangeReplaceableCollection, ArrayProtocol {
///
/// - Parameter newElement: The element to append to the array.
///
- /// - Complexity: Amortized O(1) over many additions. If the array uses a
- /// bridged `NSArray` instance as its storage, the efficiency is
- /// unspecified.
+ /// - Complexity: O(1) on average, over many additions to the same array.
"appends" is odd as a noun… To use append, we'd need to write something like "append operations" or "calls to append(_:)", neither of which seem better. What strikes you as wrong about "additions"?
—
You are receiving this because your review was requested.
Reply to this email directly, view it on GitHub, or mute the thread.
|
74fed4d
to
f90f75a
Compare
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length | ||
/// of the collection. | ||
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the number of | ||
/// elements to remove. | ||
@inlinable // protocol-only | ||
public mutating func removeLast(_ n: Int) { |
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.
I don't think we should change the doc in this way without also changing the parameter name to k
.
/// - Complexity: O(*n*), where *n* is the number of elements to drop. | ||
/// - Complexity: O(1) if the collection conforms to | ||
/// `RandomAccessCollection`; otherwise, O(*k*), where *k* is the number of | ||
/// elements to drop. | ||
@inlinable // protocol-only | ||
public func dropLast(_ n: Int) -> SubSequence { |
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.
I don't think we should change the doc in this way without also changing the parameter name to k.
/// The value passed as `n` must not offset `i` beyond the bounds of the | ||
/// collection. | ||
/// The value passed as `distance` must not offset `i` beyond the bounds of | ||
/// the collection. |
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.
a. Shouldn't this be listed as a precondition?
b. The phrasing is a bit weird, since the value passed doesn't itself offset anything. Can we do better?
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.
Agreed! I'm going to take this up on a different branch, since that's existing language and this branch keeps falling behind master.
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.
@natecook1000 Looks like this was never resolved; master still contains this language.
70631f0
to
5f53908
Compare
@swift-ci Please smoke test |
5f53908
to
e651dda
Compare
@swift-ci Please smoke test |
This corrects and standardizes the complexity documentation for Sequence and Collection methods. The use of constants is more consistent, with `n` equal to the length of the target collection, `m` equal to the length of a collection passed in as a parameter, and `k` equal to any other passed or calculated constant.
004f96a
to
bcab652
Compare
@swift-ci Please smoke test |
…ang#17254) * [stdlib] Update complexity docs for seq/collection algorithms This corrects and standardizes the complexity documentation for Sequence and Collection methods. The use of constants is more consistent, with `n` equal to the length of the target collection, `m` equal to the length of a collection passed in as a parameter, and `k` equal to any other passed or calculated constant. * Apply notes from @brentdax about complexity nomenclature * Change `n` to `distance` in `index(_:offsetBy:)` * Use equivalency language more places; sync across array types * Use k instead of n for parameter names * Slight changes to index(_:offsetBy:) discussion. * Update tests with new parameter names
#18247) * [stdlib] Update complexity docs for seq/collection algorithms This corrects and standardizes the complexity documentation for Sequence and Collection methods. The use of constants is more consistent, with `n` equal to the length of the target collection, `m` equal to the length of a collection passed in as a parameter, and `k` equal to any other passed or calculated constant. * Apply notes from @brentdax about complexity nomenclature * Change `n` to `distance` in `index(_:offsetBy:)` * Use equivalency language more places; sync across array types * Use k instead of n for parameter names * Slight changes to index(_:offsetBy:) discussion. * Update tests with new parameter names
This corrects and standardizes the complexity documentation for
Sequence
andCollection
methods. The use of constants is more consistent, with n equal to the length of the target collection, m equal to the length of a collection passed in as a parameter, and k equal to any other passedor calculated constant.