@@ -325,6 +325,64 @@ where Bound: Strideable, Bound.Stride: SignedInteger
325325 // The first and last elements are the same because each element is unique.
326326 return _customIndexOfEquatableElement ( element)
327327 }
328+
329+ /// Returns a Boolean value indicating whether the given range is contained
330+ /// within this closed range.
331+ ///
332+ /// The given range is contained within this closed range if the elements of
333+ /// the range are all contained within this closed range.
334+ ///
335+ /// let range = 0...10
336+ /// range.contains(5..<7) // true
337+ /// range.contains(5..<10) // true
338+ /// range.contains(5..<12) // false
339+ ///
340+ /// // Note that `5..<11` contains 5, 6, 7, 8, 9, and 10.
341+ /// range.contains(5..<11) // true
342+ ///
343+ /// Additionally, passing any empty range as `other` results in the value
344+ /// `true`, even if the empty range's bounds are outside the bounds of this
345+ /// closed range.
346+ ///
347+ /// range.contains(3..<3) // true
348+ /// range.contains(20..<20) // true
349+ ///
350+ /// - Parameter other: A range to check for containment within this closed
351+ /// range.
352+ /// - Returns: `true` if `other` is empty or wholly contained within this
353+ /// closed range; otherwise, `false`.
354+ ///
355+ /// - Complexity: O(1)
356+ @_alwaysEmitIntoClient
357+ public func contains( _ other: Range < Bound > ) -> Bool {
358+ if other. isEmpty { return true }
359+ let otherInclusiveUpper = other. upperBound. advanced ( by: - 1 )
360+ return lowerBound <= other. lowerBound && upperBound >= otherInclusiveUpper
361+ }
362+ }
363+
364+ extension ClosedRange {
365+ /// Returns a Boolean value indicating whether the given closed range is
366+ /// contained within this closed range.
367+ ///
368+ /// The given closed range is contained within this range if its bounds are
369+ /// contained within this closed range.
370+ ///
371+ /// let range = 0...10
372+ /// range.contains(2...5) // true
373+ /// range.contains(2...10) // true
374+ /// range.contains(2...12) // false
375+ ///
376+ /// - Parameter other: A closed range to check for containment within this
377+ /// closed range.
378+ /// - Returns: `true` if `other` is wholly contained within this closed range;
379+ /// otherwise, `false`.
380+ ///
381+ /// - Complexity: O(1)
382+ @_alwaysEmitIntoClient
383+ public func contains( _ other: ClosedRange < Bound > ) -> Bool {
384+ lowerBound <= other. lowerBound && upperBound >= other. upperBound
385+ }
328386}
329387
330388extension Comparable {
@@ -459,6 +517,18 @@ extension ClosedRange where Bound: Strideable, Bound.Stride: SignedInteger {
459517}
460518
461519extension ClosedRange {
520+ /// Returns a Boolean value indicating whether this range and the given closed
521+ /// range contain an element in common.
522+ ///
523+ /// This example shows two overlapping ranges:
524+ ///
525+ /// let x: Range = 0...20
526+ /// print(x.overlaps(10...1000))
527+ /// // Prints "true"
528+ ///
529+ /// - Parameter other: A range to check for elements in common.
530+ /// - Returns: `true` if this range and `other` have at least one element in
531+ /// common; otherwise, `false`.
462532 @inlinable
463533 public func overlaps( _ other: ClosedRange < Bound > ) -> Bool {
464534 // Disjoint iff the other range is completely before or after our range.
@@ -469,6 +539,25 @@ extension ClosedRange {
469539 return !isDisjoint
470540 }
471541
542+ /// Returns a Boolean value indicating whether this range and the given range
543+ /// contain an element in common.
544+ ///
545+ /// This example shows two overlapping ranges:
546+ ///
547+ /// let x: Range = 0...20
548+ /// print(x.overlaps(10..<1000))
549+ /// // Prints "true"
550+ ///
551+ /// Because a closed range includes its upper bound, the ranges in the
552+ /// following example overlap:
553+ ///
554+ /// let y = 20..<30
555+ /// print(x.overlaps(y))
556+ /// // Prints "true"
557+ ///
558+ /// - Parameter other: A range to check for elements in common.
559+ /// - Returns: `true` if this range and `other` have at least one element in
560+ /// common; otherwise, `false`.
472561 @inlinable
473562 public func overlaps( _ other: Range < Bound > ) -> Bool {
474563 return other. overlaps ( self )
0 commit comments