Skip to content

[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

Merged
merged 7 commits into from
Jul 24, 2018

Conversation

natecook1000
Copy link
Member

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.

@natecook1000 natecook1000 requested a review from dabrahams June 15, 2018 20:52
Copy link
Contributor

@beccadax beccadax left a 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.
Copy link
Contributor

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?

Copy link
Member Author

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.
Copy link
Contributor

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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n or k?

/// 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.
Copy link
Contributor

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 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.
Copy link
Contributor

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(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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n or k?

Copy link
Contributor

@dabrahams dabrahams left a 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

/// - 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.
Copy link
Contributor

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?"

Copy link
Member Author

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"?

/// 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.
Copy link
Contributor

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?

Copy link
Member Author

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.

@@ -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`.
Copy link
Contributor

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…

/// - 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
Copy link
Contributor

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/

/// - 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*).
Copy link
Contributor

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.

@natecook1000 natecook1000 changed the title [stdlib] Update complexity docs for seq/collection algorithms [WIP] [stdlib] Update complexity docs for seq/collection algorithms Jun 16, 2018
@dabrahams
Copy link
Contributor

dabrahams commented Jun 16, 2018 via email

@natecook1000 natecook1000 force-pushed the nc-complexity branch 4 times, most recently from 74fed4d to f90f75a Compare July 11, 2018 15:21
/// `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) {
Copy link
Contributor

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 {
Copy link
Contributor

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.
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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.

@natecook1000 natecook1000 changed the title [WIP] [stdlib] Update complexity docs for seq/collection algorithms [stdlib] Update complexity docs for seq/collection algorithms Jul 17, 2018
@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@natecook1000
Copy link
Member Author

@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.
@natecook1000
Copy link
Member Author

@swift-ci Please smoke test

@natecook1000 natecook1000 merged commit 3d7dfc2 into swiftlang:master Jul 24, 2018
natecook1000 added a commit to natecook1000/swift that referenced this pull request Jul 26, 2018
…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
airspeedswift pushed a commit that referenced this pull request Jul 26, 2018
#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
@natecook1000 natecook1000 deleted the nc-complexity branch October 4, 2018 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants