-
Notifications
You must be signed in to change notification settings - Fork 448
Add "sortedPrefix(_:by)" to Collection #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
5429d3b
Add partial sort algorithm
rakaramos 4362197
Add in place partial sorting
rockbruno f299df1
Guide docs
rockbruno 6cd2870
Use Indexes
rockbruno 63b2dd0
Merge pull request #1 from rakaramos/guide
rakaramos 88216e1
Add partial sort tests
rakaramos afe7111
Indent up to 80 columns
rakaramos 4652ae7
Fix heapify stopping before it should
rockbruno 37d494a
Update PartialSort.md
rockbruno 83d5f1e
Update PartialSort.md
rockbruno bf31ba1
Update PartialSort.swift
rockbruno acb3583
Cleaning up iterators logic
rockbruno 6227bd8
Update PartialSort.swift
rockbruno d4a2e6b
Cleaning docs
rockbruno 62ee6f2
Change implementation and name
rakaramos f674851
DocDocs
rockbruno 5bdea96
Merge remote-tracking branch 'origin/fix-algo' into docdocs
rockbruno dd15b5a
Docs
rockbruno 7ac3915
Merge pull request #3 from rakaramos/docdocs
rockbruno c68537f
Docs
rockbruno e8504fd
Optimize
rockbruno 36e9a39
Fix header and remove assert
rakaramos 1d22ef9
Add more tests (#4)
rakaramos 62096e1
Update PartialSortTests.swift
rockbruno d0c1ccd
Merge pull request #5 from rakaramos/rockbruno-patch-1
rockbruno 23bf863
Update Sources/Algorithms/PartialSort.swift
rockbruno 379609b
Update Sources/Algorithms/PartialSort.swift
rockbruno 435a38c
Update Sources/Algorithms/PartialSort.swift
rockbruno 70973a2
Documentation fixes
rockbruno 70a263c
Add tests for massive inputs
rockbruno 1d3dcaf
isLastElement
rockbruno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,89 @@ | ||
import Foundation | ||
|
||
private final class Heap<T> { | ||
rakaramos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
typealias Comparator = (T,T) -> Bool | ||
|
||
private var elements: [T] | ||
private let priority: Comparator | ||
|
||
init<S: Sequence>(elements: S, priority: @escaping Comparator) where S.Element == T { | ||
self.priority = priority | ||
self.elements = Array(elements) | ||
if elements.isEmpty == false { | ||
for i in stride(from: (count / 2) - 1, to: -1, by: -1) { | ||
siftDown(i) | ||
} | ||
} | ||
} | ||
|
||
private func leftChildIndex(of index: Int) -> Int { | ||
return (2 * index) + 1 | ||
} | ||
|
||
private func rightChild(of index: Int) -> Int { | ||
return (2 * index) + 2 | ||
} | ||
|
||
private func parentIndex(of index: Int) -> Int { | ||
return (index - 1) / 2 | ||
} | ||
|
||
private func isHigherPriority(_ a: Int, _ b: Int) -> Bool { | ||
return priority(elements[a], elements[b]) | ||
} | ||
|
||
private func highestPriorityIndex(of index: Int) -> Int { | ||
let left = highestPriorityIndex(of: index, and: leftChildIndex(of: index)) | ||
let right = highestPriorityIndex(of: index, and: rightChild(of: index)) | ||
return highestPriorityIndex(of: left, and: right) | ||
} | ||
|
||
private func highestPriorityIndex(of parent: Int, and child: Int) -> Int { | ||
guard child < elements.count else { | ||
return parent | ||
} | ||
guard isHigherPriority(child, parent) else { | ||
return parent | ||
} | ||
return child | ||
} | ||
|
||
func dequeue() -> T? { | ||
guard elements.count > 0 else { | ||
return nil | ||
} | ||
elements.swapAt(0, elements.count - 1) | ||
let element = elements.popLast() | ||
siftDown(0) | ||
return element | ||
} | ||
|
||
private func siftDown(_ i: Int) { | ||
let indexToSwap = highestPriorityIndex(of: i) | ||
guard indexToSwap != i else { | ||
return | ||
} | ||
elements.swapAt(indexToSwap, i) | ||
siftDown(indexToSwap) | ||
} | ||
} | ||
|
||
extension Collection { | ||
rakaramos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func partiallySorted(_ count: Int, by: @escaping (Element, Element) -> Bool) -> [Element] { | ||
rakaramos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert(count >= 0 && count < self.count, "Are you crazy?") | ||
let heap = Heap<Element>(elements: self, priority: by) | ||
return [Element](unsafeUninitializedCapacity: count) { buffer, initializedCount in | ||
for i in 0..<count { | ||
buffer[i] = heap.dequeue()! | ||
} | ||
initializedCount = count | ||
} | ||
} | ||
} | ||
|
||
extension Collection where Element: Comparable { | ||
func partiallySorted(_ count: Int) -> [Element] { | ||
return partiallySorted(count, by: <) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.