Skip to content

Commit 2f2c5b6

Browse files
authored
Merge pull request #61379 from lorentey/add-docs-for-customContainsEquatableElement
[stdlib] Add docs for Sequence._customContainsEquatableElement
2 parents 47d76d2 + b799866 commit 2f2c5b6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

stdlib/public/core/Sequence.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,33 @@ public protocol Sequence<Element> {
355355
/// In this case, see the documentation of `Collection.underestimatedCount`.
356356
var underestimatedCount: Int { get }
357357

358+
/// Sequences whose `Element` is `Equatable` and that are able to quickly
359+
/// check if they contain a particular value can implement this requirement
360+
/// to speed up the standard `contains` method.
361+
///
362+
/// The default implementation returns nil, indicating that `contains` should
363+
/// fall back to the standard linear search algorithm.
364+
///
365+
/// `Sequence` and `Collection` algorithms other than `contains` itself may
366+
/// adapt their behavior based on whether or not this function returns nil.
367+
/// For example, a generic algorithm that needs to do containment checks for
368+
/// many different values may decide not to copy items into a temporary `Set`
369+
/// if it sees that the sequence implements this method. Therefore, sequences
370+
/// should only implement this method if they can do it in better than linear
371+
/// time.
372+
///
373+
/// For sequences that are destructively consumed by iteration, calling this
374+
/// method must not consume any elements. (Such sequences usually leave this
375+
/// method with its default, `nil`-returning implementation, which trivially
376+
/// satisfies this requirement.)
377+
///
378+
/// - Returns: `nil` if containment cannot be verified in better than linear
379+
/// time; otherwise, the method returns a boolean value indicating whether
380+
/// or not the item is an element of this sequence.
381+
///
382+
/// - Complexity: If this function returns `nil`, it must do so in constant
383+
/// (O(1)) time. If this returns non-`nil`, then it must have better than linear
384+
/// (O(*n*)) complexity.
358385
func _customContainsEquatableElement(
359386
_ element: Element
360387
) -> Bool?

0 commit comments

Comments
 (0)