Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ split_embedded_sources(
NORMAL DictionaryCasting.swift
NORMAL DictionaryStorage.swift
NORMAL DictionaryVariant.swift
NORMAL DiscontiguousSlice.swift
NORMAL DropWhile.swift
NORMAL Dump.swift
EMBEDDED EmptyCollection.swift
Expand Down Expand Up @@ -129,6 +130,8 @@ split_embedded_sources(
EMBEDDED RandomAccessCollection.swift
EMBEDDED Range.swift
EMBEDDED RangeReplaceableCollection.swift
NORMAL RangeSet.swift
NORMAL RangeSetRanges.swift
NORMAL ReflectionMirror.swift
EMBEDDED Repeat.swift
NORMAL REPL.swift
Expand Down
70 changes: 69 additions & 1 deletion stdlib/public/core/CollectionAlgorithms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -209,6 +209,74 @@ extension BidirectionalCollection where Element: Equatable {
}
}

//===----------------------------------------------------------------------===//
// indices(where:) / indices(of:)
//===----------------------------------------------------------------------===//

#if !$Embedded
extension Collection {
/// Returns the indices of all the elements that match the given predicate.
///
/// For example, you can use this method to find all the places that a
/// vowel occurs in a string.
///
/// let str = "Fresh cheese in a breeze"
/// let vowels: Set<Character> = ["a", "e", "i", "o", "u"]
/// let allTheVowels = str.indices(where: { vowels.contains($0) })
/// // str[allTheVowels].count == 9
///
/// - Parameter predicate: A closure that takes an element as its argument
/// and returns a Boolean value that indicates whether the passed element
/// represents a match.
/// - Returns: A set of the indices of the elements for which `predicate`
/// returns `true`.
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@available(SwiftStdlib 5.11, *)
@inlinable
public func indices(
where predicate: (Element) throws -> Bool
) rethrows -> RangeSet<Index> {
var result: [Range<Index>] = []
var end = startIndex
while let begin = try self[end...].firstIndex(where: predicate) {
end = try self[begin...].prefix(while: predicate).endIndex
result.append(begin ..< end)

guard end < self.endIndex else {
break
}
self.formIndex(after: &end)
}

return RangeSet(_orderedRanges: result)
}
}

extension Collection where Element: Equatable {
/// Returns the indices of all the elements that are equal to the given
/// element.
///
/// For example, you can use this method to find all the places that a
/// particular letter occurs in a string.
///
/// let str = "Fresh cheese in a breeze"
/// let allTheEs = str.indices(of: "e")
/// // str[allTheEs].count == 7
///
/// - Parameter element: An element to look for in the collection.
/// - Returns: A set of the indices of the elements that are equal to
/// `element`.
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@available(SwiftStdlib 5.11, *)
@inlinable
public func indices(of element: Element) -> RangeSet<Index> {
indices(where: { $0 == element })
}
}
#endif

//===----------------------------------------------------------------------===//
// partition(by:)
//===----------------------------------------------------------------------===//
Expand Down
Loading