|
5 | 5 |
|
6 | 6 | import Foundation |
7 | 7 |
|
| 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 | + |
8 | 37 | public extension Array where Element: Equatable { |
9 | 38 |
|
10 | 39 | /// Returns a new array with all occurrences of the specified element removed. |
@@ -44,32 +73,6 @@ public extension Array where Element: Equatable { |
44 | 73 | let erasedObject = object as AnyObject? |
45 | 74 | return self.filter { $0 as AnyObject? !== erasedObject } |
46 | 75 | } |
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 | | - } |
73 | 76 | } |
74 | 77 |
|
75 | 78 | public extension Array where Element: Hashable { |
|
0 commit comments