-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* finish safrole * add more todos * more check * fix * fix for rethrows
- Loading branch information
Showing
12 changed files
with
237 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
extension Array where Element: Comparable { | ||
public func isSorted(by comparer: (Element, Element) -> Bool = { $0 < $1 }) -> Bool { | ||
var previous: Element? | ||
for element in self { | ||
if let previous { | ||
if !comparer(previous, element), previous != element { | ||
return false | ||
} | ||
} | ||
previous = element | ||
} | ||
return true | ||
} | ||
|
||
/// Insert the elements of the given sequence to the array, in sorted order. | ||
/// | ||
/// - Parameter elements: The elements to insert. | ||
/// - Parameter comparer: The comparison function to use to determine the order of the elements. | ||
/// - Complexity: O(*n*), where *n* is the number of elements in the sequence. | ||
/// | ||
/// - Note: The elements of the sequence must be comparable. | ||
/// - Invariant: The array and elements must be sorted according to the given comparison function. | ||
public mutating func insertSorted(_ elements: any Sequence<Element>, by comparer: ((Element, Element) throws -> Bool)? = nil) rethrows { | ||
reserveCapacity(count + elements.underestimatedCount) | ||
let comparer = comparer ?? { $0 < $1 } | ||
var startIdx = 0 | ||
for element in elements { | ||
if let idx = try self[startIdx...].firstIndex(where: { try !comparer($0, element) }) { | ||
insert(element, at: idx) | ||
startIdx = idx + 1 | ||
} else { | ||
append(element) | ||
startIdx = endIndex | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Foundation | ||
import Testing | ||
|
||
@testable import Utils | ||
|
||
struct ArrayTests { | ||
@Test(arguments: [ | ||
[], [1], [2, 4], [5, 19, 34, 34, 56, 56], | ||
]) | ||
func isSorted(testCase: [Int]) { | ||
#expect(testCase.isSorted()) | ||
} | ||
|
||
@Test(arguments: [ | ||
[], [1], [4, 2], [56, 56, 34, 34, 19, 5] | ||
]) | ||
func isSortedBy(testCase: [Int]) { | ||
#expect(testCase.isSorted(by: >)) | ||
} | ||
|
||
@Test(arguments: [ | ||
[4, 2], [56, 56, 34, 35, 19, 5], [1, 3, 2] | ||
]) | ||
func notSorted(testCase: [Int]) { | ||
#expect(!testCase.isSorted()) | ||
} | ||
|
||
@Test(arguments: [ | ||
([], []), | ||
([], [1]), | ||
([1], []), | ||
([1, 2, 3], [1, 2, 3]), | ||
([1, 10, 20, 30], [2, 12, 22, 32, 42]), | ||
([1, 2, 3], [4, 5, 6]), | ||
([4, 5, 6], [1, 2, 3]), | ||
([1, 5, 10, 30], [6, 7, 8, 9, 10, 11]), | ||
]) | ||
func insertSorted(testCase: ([Int], [Int])) { | ||
var arr = testCase.0 | ||
arr.insertSorted(testCase.1) | ||
#expect(arr.isSorted()) | ||
#expect(arr == (testCase.0 + testCase.1).sorted()) | ||
} | ||
|
||
@Test(arguments: [ | ||
([3, 3, 2, 2, 1, 1], [3, 2, 1]), | ||
([6, 5, 4], [4, 3, 2]), | ||
([10, 5, 3, 2], [11, 10, 4, 3, 3, 2, 1]), | ||
]) | ||
func insertSortedBy(testCase: ([Int], [Int])) { | ||
var arr = testCase.0 | ||
arr.insertSorted(testCase.1, by: >) | ||
#expect(arr.isSorted(by: >)) | ||
#expect(arr == (testCase.0 + testCase.1).sorted(by: >)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters