//===----------------------------------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// @public func minElement< R : Sequence where R.GeneratorType.Element : Comparable>(range: R) -> R.GeneratorType.Element { var g = range.generate() var result = g.next()! for e in GeneratorSequence(g) { if e < result { result = e } } return result } @public func maxElement< R : Sequence where R.GeneratorType.Element : Comparable>(range: R) -> R.GeneratorType.Element { var g = range.generate() var result = g.next()! for e in GeneratorSequence(g) { if e > result { result = e } } return result } // Returns the first index where value appears in domain or nil if // domain doesn't contain the value. O(countElements(domain)) @public func find< C: Collection where C.GeneratorType.Element : Equatable >(domain: C, value: C.GeneratorType.Element) -> C.IndexType? { for i in indices(domain) { if domain[i] == value { return i } } return nil } @public func insertionSort< C: MutableCollection where C.IndexType: BidirectionalIndex >( inout elements: C, range: Range, inout less: (C.GeneratorType.Element, C.GeneratorType.Element)->Bool ) { if range { let start = range.startIndex // Keep track of the end of the initial sequence of sorted // elements. var sortedEnd = start // One element is trivially already-sorted, thus pre-increment // Continue until the sorted elements cover the whole sequence while (++sortedEnd != range.endIndex) { // get the first unsorted element var x: C.GeneratorType.Element = elements[sortedEnd] // Look backwards for x's position in the sorted sequence, // moving elements forward to make room. var i = sortedEnd do { let predecessor: C.GeneratorType.Element = elements[i.predecessor()] // if x doesn't belong before y, we've found its position if !less(x, predecessor) { break } // Move y forward elements[i] = predecessor } while --i != start if i != sortedEnd { // Plop x into position elements[i] = x } } } } /// Partition a non empty range into two partially sorted regions and return /// the index of the pivot: /// [start..idx), pivot ,[idx..end) @public func partition< C: MutableCollection where C.IndexType: RandomAccessIndex >( inout elements: C, range: Range, inout less: (C.GeneratorType.Element, C.GeneratorType.Element)->Bool ) -> C.IndexType { _precondition( range.startIndex != range.endIndex, "Can't partition an empty range") // Variables i and j point to the next element to be visited. var i = range.startIndex var j = range.endIndex.predecessor() // The first element is the pivot. let pivot = elements[range.startIndex] i++ // Continue to swap until all elements were visited and placed in one // of the partitions. while i.distanceTo(j) >= 0 { while less(elements[i], pivot) { i++ if (i.distanceTo(j) < 0) { break } } while less(pivot, elements[j]) { j-- // We don't need to check if j is greater than zero because we placed // our pivot at startIndex and comparing with pivot ends this loop. } if i.distanceTo(j) >= 0 { swap(&elements[i], &elements[j]) i++ j-- } } // Swap the pivot in between the two partitions. swap(&elements[i.predecessor()], &elements[range.startIndex]) return i.predecessor() } @public func quickSort< C: MutableCollection where C.IndexType: RandomAccessIndex >( inout elements: C, range: Range, less: (C.GeneratorType.Element, C.GeneratorType.Element)->Bool ) { var comp = less _quickSort(&elements, range, &comp) } func _quickSort< C: MutableCollection where C.IndexType: RandomAccessIndex >( inout elements: C, range: Range, inout less: (C.GeneratorType.Element, C.GeneratorType.Element)->Bool ) { // Insertion sort is better at handling smaller regions. let cnt = count(range) if cnt < 20 { insertionSort(&elements, range, &less) return } // Partition and sort. let part_idx : C.IndexType = partition(&elements, range, &less) _quickSort(&elements, range.startIndex.. { static func compare(x: T, _ y: T) -> Bool { return x < y } } @public func sort< C: MutableCollection where C.IndexType: RandomAccessIndex >( inout collection: C, predecessor: (C.GeneratorType.Element, C.GeneratorType.Element) -> Bool ) { quickSort(&collection, indices(collection), predecessor) } @public func sort< C: MutableCollection where C.IndexType: RandomAccessIndex, C.GeneratorType.Element: Comparable >( inout collection: C ) { quickSort(&collection, indices(collection)) } @public func sort(inout array: T[], predecessor: (T, T) -> Bool) { return array.withMutableStorage { a in sort(&a, predecessor) return } } /// The functions below are a copy of the functions above except that /// they don't accept a predicate and they are hardcoded to use the less-than /// comparator. @public func sort(inout array: T[]) { return array.withMutableStorage { a in sort(&a) return } } @public func sorted< C: MutableCollection where C.IndexType: RandomAccessIndex >( source: C, predecessor: (C.GeneratorType.Element, C.GeneratorType.Element) -> Bool ) -> C { var result = source sort(&result, predecessor) return result } @public func sorted< C: MutableCollection where C.GeneratorType.Element: Comparable, C.IndexType: RandomAccessIndex >(source: C) -> C { var result = source sort(&result) return result } @public func sorted< S: Sequence >( source: S, predecessor: (S.GeneratorType.Element, S.GeneratorType.Element) -> Bool ) -> S.GeneratorType.Element[] { var result = Array(source) sort(&result, predecessor) return result } @public func sorted< S: Sequence where S.GeneratorType.Element: Comparable >( source: S ) -> S.GeneratorType.Element[] { var result = Array(source) sort(&result) return result } @public func insertionSort< C: MutableCollection where C.IndexType: RandomAccessIndex, C.GeneratorType.Element: Comparable>( inout elements: C, range: Range) { if range { let start = range.startIndex // Keep track of the end of the initial sequence of sorted // elements. var sortedEnd = start // One element is trivially already-sorted, thus pre-increment // Continue until the sorted elements cover the whole sequence while (++sortedEnd != range.endIndex) { // get the first unsorted element var x: C.GeneratorType.Element = elements[sortedEnd] // Look backwards for x's position in the sorted sequence, // moving elements forward to make room. var i = sortedEnd do { let predecessor: C.GeneratorType.Element = elements[i.predecessor()] // if x doesn't belong before y, we've found its position if !Less.compare(x, predecessor) { break } // Move y forward elements[i] = predecessor } while --i != start if i != sortedEnd { // Plop x into position elements[i] = x } } } } /// Partition a non empty range into two partially sorted regions and return /// the index of the pivot: /// [start..idx), pivot ,[idx..end) @public func partition< C: MutableCollection where C.GeneratorType.Element: Comparable , C.IndexType: RandomAccessIndex >( inout elements: C, range: Range) -> C.IndexType { // Variables i and j point to the next element to be visited. var i = range.startIndex var j = range.endIndex.predecessor() // The first element is the pivot. let pivot = elements[range.startIndex] i++ // Continue to swap until all elements were visited and placed in one // of the partitions. while i.distanceTo(j) >= 0 { while Less.compare(elements[i], pivot) { i++ if (i.distanceTo(j) < 0) { break } } while Less.compare(pivot, elements[j]) { // We don't need to check if j is greater than zero because we placed // our pivot at startIndex and comparing with pivot ends this loop. j-- } if i.distanceTo(j) >= 0 { swap(&elements[i], &elements[j]) i++ j-- } } // Swap the pivot in between the two partitions. swap(&elements[i.predecessor()], &elements[range.startIndex]) return i.predecessor() } @public func quickSort< C: MutableCollection where C.GeneratorType.Element: Comparable, C.IndexType: RandomAccessIndex >( inout elements: C, range: Range) { _quickSort(&elements, range) } func _quickSort< C: MutableCollection where C.GeneratorType.Element: Comparable, C.IndexType: RandomAccessIndex >( inout elements: C, range: Range ) { // Insertion sort is better at handling smaller regions. let cnt = count(range) if cnt < 20 { insertionSort(&elements, range) return } // Partition and sort. let part_idx : C.IndexType = partition(&elements, range) _quickSort(&elements, range.startIndex..(inout a : T, inout b : T) { // Semantically equivalent to (a, b) = (b, a). // Microoptimized to avoid retain/release traffic. let p1 = Builtin.addressof(&a) let p2 = Builtin.addressof(&b) // Take from P1. let tmp : T = Builtin.take(p1) // Transfer P2 into P1. Builtin.initialize(Builtin.take(p2) as T, p1) // Initialize P2. Builtin.initialize(tmp, p2) } @public func min(x: T, y: T) -> T { var r = x if y < x { r = y } return r } @public func min(x: T, y: T, z: T, rest: T...) -> T { var r = x if y < x { r = y } if z < r { r = z } for t in rest { if t < r { r = t } } return r } @public func max(x: T, y: T) -> T { var r = y if y < x { r = x } return r } @public func max(x: T, y: T, z: T, rest: T...) -> T { var r = y if y < x { r = x } if r < z { r = z } for t in rest { if t >= r { r = t } } return r } @public func split( seq: Seq, isSeparator: (Seq.GeneratorType.Element)->R, maxSplit: Int = Int.max, allowEmptySlices: Bool = false ) -> Seq.SliceType[] { var result = Array() // FIXME: could be simplified pending // (ternary operator not resolving some/none) var startIndex: Optional = allowEmptySlices ? .Some(seq.startIndex) : .None var splits = 0 for j in indices(seq) { if isSeparator(seq[j]) { if startIndex { var i = startIndex! result.append(seq[i..= maxSplit { break } if !allowEmptySlices { startIndex = .None } } } else { if !startIndex { startIndex = .Some(j) } } } switch startIndex { case .Some(var i): result.append(seq[i..(s0: S0, s1: S1) -> Bool { var g1 = s1.generate() for e0 in s0 { var e1 = g1.next() if !e1 { return true } if e0 != e1! { return false } } return g1.next() ? false : true } @public struct EnumerateGenerator : Generator, Sequence { @public typealias Element = (index: Int, element: Base.Element) var base: Base var count: Int init(_ base: Base) { self.base = base count = 0 } @public mutating func next() -> Element? { var b = base.next() if !b { return .None } return .Some((index: count++, element: b!)) } // Every Generator is also a single-pass Sequence @public typealias GeneratorType = EnumerateGenerator @public func generate() -> GeneratorType { return self } } @public func enumerate( seq: Seq ) -> EnumerateGenerator { return EnumerateGenerator(seq.generate()) } /// Return true iff `a1` and `a2` contain the same elements. @public func equal< S1 : Sequence, S2 : Sequence where S1.GeneratorType.Element == S2.GeneratorType.Element, S1.GeneratorType.Element : Equatable >(a1: S1, a2: S2) -> Bool { var g1 = a1.generate() var g2 = a2.generate() while true { var e1 = g1.next() var e2 = g2.next() if e1 && e2 { if e1! != e2! { return false } } else { return !e1 == !e2 } } } /// Return true iff `a1` and `a2` contain the same elements, using /// `pred` as equality `==` comparison. @public func equal< S1 : Sequence, S2 : Sequence where S1.GeneratorType.Element == S2.GeneratorType.Element >(a1: S1, a2: S2, predecessor: (S1.GeneratorType.Element, S1.GeneratorType.Element) -> Bool) -> Bool { var g1 = a1.generate() var g2 = a2.generate() while true { var e1 = g1.next() var e2 = g2.next() if e1 && e2 { if !predecessor(e1!, e2!) { return false } } else { return !e1 == !e2 } } } /// Return true iff a1 precedes a2 in a lexicographical ("dictionary") /// ordering, using "<" as the comparison between elements. @public func lexicographicalCompare< S1 : Sequence, S2 : Sequence where S1.GeneratorType.Element == S2.GeneratorType.Element, S1.GeneratorType.Element : Comparable>( a1: S1, a2: S2) -> Bool { var g1 = a1.generate() var g2 = a2.generate() while true { var e1_ = g1.next() var e2_ = g2.next() if let e1 = e1_ { if let e2 = e2_ { if e1 < e2 { return true } if e2 < e1 { return false } continue // equivalent } return false } return e2_.getLogicValue() } } /// Return true iff `a1` precedes `a2` in a lexicographical ("dictionary") /// ordering, using `less` as the comparison between elements. @public func lexicographicalCompare< S1 : Sequence, S2 : Sequence where S1.GeneratorType.Element == S2.GeneratorType.Element >( a1: S1, a2: S2, less: (S1.GeneratorType.Element,S1.GeneratorType.Element)->Bool ) -> Bool { var g1 = a1.generate() var g2 = a2.generate() while true { var e1_ = g1.next() var e2_ = g2.next() if let e1 = e1_ { if let e2 = e2_ { if less(e1, e2) { return true } if less(e2, e1) { return false } continue // equivalent } return false } return e2_.getLogicValue() } } /// Return `true` iff an element in `seq` satisfies `predicate`. @public func contains< S: Sequence, L: LogicValue >(seq: S, predicate: (S.GeneratorType.Element)->L) -> Bool { for a in seq { if predicate(a) { return true } } return false } /// Return `true` iff `x` is in `seq`. @public func contains< S: Sequence where S.GeneratorType.Element: Equatable >(seq: S, x: S.GeneratorType.Element) -> Bool { return contains(seq, { $0 == x }) } @public func reduce( sequence: S, initial: U, combine: (U, S.GeneratorType.Element)->U ) -> U { var result = initial for element in sequence { result = combine(result, element) } return result }