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
func product<A: Collection, B: Collection>(_ a: A, _ b: B) -> CartesianProductCollection<A, B> { | |
return CartesianProductCollection(a, b) | |
} | |
struct CartesianProductCollection<A: Collection, B: Collection> { | |
let a: A | |
let b: B | |
init(_ a: A, _ b: B) { | |
self.a = a |
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
extension Collection { | |
func split(between predicate: (Element, Element) throws -> Bool) rethrows -> [SubSequence] { | |
guard !isEmpty else { return [] } | |
var remainder = self[...] | |
var slices: [SubSequence] = [] | |
for ((_, left), (index, right)) in indexed().pairs() { | |
if try predicate(left, right) { | |
slices.append(remainder.remove(upTo: index)) |
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
use std::cmp::Ordering; | |
pub trait VecExt { | |
type T; | |
fn find_entry<F>(&mut self, f: F) -> Entry<'_, Self::T> | |
where | |
F: FnMut(&Self::T) -> bool; | |
fn first_entry_where<F>(&mut self, f: F) -> Entry<'_, Self::T> |
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
extension Sequence { | |
func min(_ n: Int, by areInIncreasingOrder: (Element, Element) -> Bool) -> [Element] { | |
var iterator = makeIterator() | |
guard let first = iterator.next() else { return [] } | |
return withoutActuallyEscaping(areInIncreasingOrder) { areInIncreasingOrder in | |
var heap = NonEmptyMaxHeap(root: first, by: areInIncreasingOrder) | |
var heapSize = 1 | |
while heapSize < n, let element = iterator.next() { |
OlderNewer