Skip to content

Commit b142492

Browse files
committed
Fix Array/appending for non-equatable elements
1 parent 7c5f95c commit b142492

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

Sources/Extensions/Array/Array+Selectors.swift

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@
55

66
import Foundation
77

8+
public extension Array {
9+
10+
/// Returns a new array with the specified element appended to the end.
11+
///
12+
/// - Parameter element: The element to append.
13+
/// - Returns: A new array containing all elements of the original array followed by the appended element.
14+
/// - Complexity: `O(n + 1)`, where n is the length of the original array.
15+
func appending(_ element: Element) -> [Element] {
16+
var arr = [Element]()
17+
arr.reserveCapacity(self.count + 1)
18+
arr.append(contentsOf: self)
19+
arr.append(element)
20+
return arr
21+
}
22+
23+
/// Returns a new array with the specified elements appended to the end.
24+
///
25+
/// - Parameter element: The element to append.
26+
/// - Returns: A new array containing all elements of the original array followed by the appended elements.
27+
/// - Complexity: `O(n + m)`, where n is the length of the original array and m is the length of the contents array.
28+
func appending(contentsOf contents: [Element]) -> [Element] {
29+
var arr = [Element]()
30+
arr.reserveCapacity(self.count + contents.count)
31+
arr.append(contentsOf: self)
32+
arr.append(contentsOf: contents)
33+
return arr
34+
}
35+
}
36+
837
public extension Array where Element: Equatable {
938

1039
/// Returns a new array with all occurrences of the specified element removed.
@@ -44,32 +73,6 @@ public extension Array where Element: Equatable {
4473
let erasedObject = object as AnyObject?
4574
return self.filter { $0 as AnyObject? !== erasedObject }
4675
}
47-
48-
/// Returns a new array with the specified element appended to the end.
49-
///
50-
/// - Parameter element: The element to append.
51-
/// - Returns: A new array containing all elements of the original array followed by the appended element.
52-
/// - Complexity: `O(n + 1)`, where n is the length of the original array.
53-
func appending(_ element: Element) -> [Element] {
54-
var arr = [Element]()
55-
arr.reserveCapacity(self.count + 1)
56-
arr.append(contentsOf: self)
57-
arr.append(element)
58-
return arr
59-
}
60-
61-
/// Returns a new array with the specified elements appended to the end.
62-
///
63-
/// - Parameter element: The element to append.
64-
/// - Returns: A new array containing all elements of the original array followed by the appended elements.
65-
/// - Complexity: `O(n + m)`, where n is the length of the original array and m is the length of the contents array.
66-
func appending(contentsOf contents: [Element]) -> [Element] {
67-
var arr = [Element]()
68-
arr.reserveCapacity(self.count + contents.count)
69-
arr.append(contentsOf: self)
70-
arr.append(contentsOf: contents)
71-
return arr
72-
}
7376
}
7477

7578
public extension Array where Element: Hashable {

0 commit comments

Comments
 (0)