Skip to content

Commit 8246ce8

Browse files
authored
Merge pull request #13697 from moiseev/static-func-ops-rrc
[stdlib] Free function + into static func +
2 parents 63d98e6 + fe48e8c commit 8246ce8

File tree

1 file changed

+111
-103
lines changed

1 file changed

+111
-103
lines changed

stdlib/public/core/RangeReplaceableCollection.swift

Lines changed: 111 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -902,117 +902,125 @@ extension RangeReplaceableCollection
902902
}
903903
}
904904

905-
/// Creates a new collection by concatenating the elements of a collection and
906-
/// a sequence.
907-
///
908-
/// The two arguments must have the same `Element` type. For example, you can
909-
/// concatenate the elements of an integer array and a `Range<Int>` instance.
910-
///
911-
/// let numbers = [1, 2, 3, 4]
912-
/// let moreNumbers = numbers + 5...10
913-
/// print(moreNumbers)
914-
/// // Prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
915-
///
916-
/// The resulting collection has the type of the argument on the left-hand
917-
/// side. In the example above, `moreNumbers` has the same type as `numbers`,
918-
/// which is `[Int]`.
919-
///
920-
/// - Parameters:
921-
/// - lhs: A range-replaceable collection.
922-
/// - rhs: A collection or finite sequence.
923-
@_inlineable
924-
public func + <C : RangeReplaceableCollection, S : Sequence>(lhs: C, rhs: S) -> C
925-
where S.Element == C.Element {
905+
extension Sequence {
906+
/// Creates a new collection by concatenating the elements of a collection and
907+
/// a sequence.
908+
///
909+
/// The two arguments must have the same `Element` type. For example, you can
910+
/// concatenate the elements of an integer array and a `Range<Int>` instance.
911+
///
912+
/// let numbers = [1, 2, 3, 4]
913+
/// let moreNumbers = numbers + 5...10
914+
/// print(moreNumbers)
915+
/// // Prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
916+
///
917+
/// The resulting collection has the type of the argument on the left-hand
918+
/// side. In the example above, `moreNumbers` has the same type as `numbers`,
919+
/// which is `[Int]`.
920+
///
921+
/// - Parameters:
922+
/// - lhs: A range-replaceable collection.
923+
/// - rhs: A collection or finite sequence.
924+
@_inlineable
925+
public static func + <
926+
Other : RangeReplaceableCollection
927+
>(lhs: Other, rhs: Self) -> Other
928+
where Element == Other.Element {
926929

927-
var lhs = lhs
928-
// FIXME: what if lhs is a reference type? This will mutate it.
929-
lhs.append(contentsOf: rhs)
930-
return lhs
931-
}
930+
var lhs = lhs
931+
// FIXME: what if lhs is a reference type? This will mutate it.
932+
lhs.append(contentsOf: rhs)
933+
return lhs
934+
}
932935

933-
/// Creates a new collection by concatenating the elements of a sequence and a
934-
/// collection.
935-
///
936-
/// The two arguments must have the same `Element` type. For example, you can
937-
/// concatenate the elements of a `Range<Int>` instance and an integer array.
938-
///
939-
/// let numbers = [7, 8, 9, 10]
940-
/// let moreNumbers = 1...6 + numbers
941-
/// print(moreNumbers)
942-
/// // Prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
943-
///
944-
/// The resulting collection has the type of argument on the right-hand side.
945-
/// In the example above, `moreNumbers` has the same type as `numbers`, which
946-
/// is `[Int]`.
947-
///
948-
/// - Parameters:
949-
/// - lhs: A collection or finite sequence.
950-
/// - rhs: A range-replaceable collection.
951-
@_inlineable
952-
public func + <C : RangeReplaceableCollection, S : Sequence>(lhs: S, rhs: C) -> C
953-
where S.Element == C.Element {
936+
/// Creates a new collection by concatenating the elements of a sequence and a
937+
/// collection.
938+
///
939+
/// The two arguments must have the same `Element` type. For example, you can
940+
/// concatenate the elements of a `Range<Int>` instance and an integer array.
941+
///
942+
/// let numbers = [7, 8, 9, 10]
943+
/// let moreNumbers = 1...6 + numbers
944+
/// print(moreNumbers)
945+
/// // Prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
946+
///
947+
/// The resulting collection has the type of argument on the right-hand side.
948+
/// In the example above, `moreNumbers` has the same type as `numbers`, which
949+
/// is `[Int]`.
950+
///
951+
/// - Parameters:
952+
/// - lhs: A collection or finite sequence.
953+
/// - rhs: A range-replaceable collection.
954+
@_inlineable
955+
public static func + <
956+
Other : RangeReplaceableCollection
957+
>(lhs: Self, rhs: Other) -> Other
958+
where Element == Other.Element {
959+
960+
var result = Other()
961+
result.reserveCapacity(rhs.count + numericCast(lhs.underestimatedCount))
962+
result.append(contentsOf: lhs)
963+
result.append(contentsOf: rhs)
964+
return result
965+
}
954966

955-
var result = C()
956-
result.reserveCapacity(rhs.count + numericCast(lhs.underestimatedCount))
957-
result.append(contentsOf: lhs)
958-
result.append(contentsOf: rhs)
959-
return result
967+
/// Appends the elements of a sequence to a range-replaceable collection.
968+
///
969+
/// Use this operator to append the elements of a sequence to the end of
970+
/// range-replaceable collection with same `Element` type. This example appends
971+
/// the elements of a `Range<Int>` instance to an array of integers.
972+
///
973+
/// var numbers = [1, 2, 3, 4, 5]
974+
/// numbers += 10...15
975+
/// print(numbers)
976+
/// // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]"
977+
///
978+
/// - Parameters:
979+
/// - lhs: The array to append to.
980+
/// - rhs: A collection or finite sequence.
981+
///
982+
/// - Complexity: O(*n*), where *n* is the length of the resulting array.
983+
@_inlineable
984+
public static func += <
985+
Other : RangeReplaceableCollection
986+
>(lhs: inout Other, rhs: Self)
987+
where Element == Other.Element {
988+
lhs.append(contentsOf: rhs)
989+
}
960990
}
961991

962-
/// Creates a new collection by concatenating the elements of two collections.
963-
///
964-
/// The two arguments must have the same `Element` type. For example, you can
965-
/// concatenate the elements of two integer arrays.
966-
///
967-
/// let lowerNumbers = [1, 2, 3, 4]
968-
/// let higherNumbers: ContiguousArray = [5, 6, 7, 8, 9, 10]
969-
/// let allNumbers = lowerNumbers + higherNumbers
970-
/// print(allNumbers)
971-
/// // Prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
972-
///
973-
/// The resulting collection has the type of the argument on the left-hand
974-
/// side. In the example above, `moreNumbers` has the same type as `numbers`,
975-
/// which is `[Int]`.
976-
///
977-
/// - Parameters:
978-
/// - lhs: A range-replaceable collection.
979-
/// - rhs: Another range-replaceable collection.
980-
@_inlineable
981-
public func +<
982-
RRC1 : RangeReplaceableCollection,
983-
RRC2 : RangeReplaceableCollection
984-
>(lhs: RRC1, rhs: RRC2) -> RRC1
985-
where RRC1.Element == RRC2.Element {
992+
extension RangeReplaceableCollection {
993+
/// Creates a new collection by concatenating the elements of two collections.
994+
///
995+
/// The two arguments must have the same `Element` type. For example, you can
996+
/// concatenate the elements of two integer arrays.
997+
///
998+
/// let lowerNumbers = [1, 2, 3, 4]
999+
/// let higherNumbers: ContiguousArray = [5, 6, 7, 8, 9, 10]
1000+
/// let allNumbers = lowerNumbers + higherNumbers
1001+
/// print(allNumbers)
1002+
/// // Prints "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
1003+
///
1004+
/// The resulting collection has the type of the argument on the left-hand
1005+
/// side. In the example above, `moreNumbers` has the same type as `numbers`,
1006+
/// which is `[Int]`.
1007+
///
1008+
/// - Parameters:
1009+
/// - lhs: A range-replaceable collection.
1010+
/// - rhs: Another range-replaceable collection.
1011+
@_inlineable
1012+
public static func +<
1013+
Other : RangeReplaceableCollection
1014+
>(lhs: Self, rhs: Other) -> Self
1015+
where Element == Other.Element {
9861016

987-
var lhs = lhs
988-
// FIXME: what if lhs is a reference type? This will mutate it.
989-
lhs.append(contentsOf: rhs)
990-
return lhs
1017+
var lhs = lhs
1018+
// FIXME: what if lhs is a reference type? This will mutate it.
1019+
lhs.append(contentsOf: rhs)
1020+
return lhs
1021+
}
9911022
}
9921023

993-
/// Appends the elements of a sequence to a range-replaceable collection.
994-
///
995-
/// Use this operator to append the elements of a sequence to the end of
996-
/// range-replaceable collection with same `Element` type. This example appends
997-
/// the elements of a `Range<Int>` instance to an array of integers.
998-
///
999-
/// var numbers = [1, 2, 3, 4, 5]
1000-
/// numbers += 10...15
1001-
/// print(numbers)
1002-
/// // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]"
1003-
///
1004-
/// - Parameters:
1005-
/// - lhs: The array to append to.
1006-
/// - rhs: A collection or finite sequence.
1007-
///
1008-
/// - Complexity: O(*n*), where *n* is the length of the resulting array.
1009-
@_inlineable
1010-
public func += <
1011-
R : RangeReplaceableCollection, S : Sequence
1012-
>(lhs: inout R, rhs: S)
1013-
where R.Element == S.Element {
1014-
lhs.append(contentsOf: rhs)
1015-
}
10161024

10171025
extension RangeReplaceableCollection {
10181026
/// Returns a new collection of the same type containing, in order, the

0 commit comments

Comments
 (0)