Skip to content

Commit 9fc062e

Browse files
committed
[stdlib] Add docs for Sequence._customContainsEquatableElement
1 parent c5268e2 commit 9fc062e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

stdlib/public/core/Sequence.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,31 @@ 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+
/// This method must not return non-nil on sequences that are destructively
374+
/// consumed by iteration.
375+
///
376+
/// - Returns: `nil` if containment cannot be verified in better than linear
377+
/// time; otherwise, the method returns a boolean value indicating whether
378+
/// or not the item is an element of this sequence.
379+
///
380+
/// - Complexity: If this function returns `nil`, it must do so in constant
381+
/// time. If this returns non-`nil`, then it must have better than linear
382+
/// complexity.
358383
func _customContainsEquatableElement(
359384
_ element: Element
360385
) -> Bool?

0 commit comments

Comments
 (0)