mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-29 04:44:21 +00:00
* Add scan function * Add initial conditional conformances to Collection and BidirectionalCollection * Rename scan to reductions * Add information to the readme about the reductions name * Add eager API * Remove Reductions' conformance to BidirectionalCollection * Implement Reductions subscript such that it occurs in constant time * Add a variant which includes the given initial result * Add a variant which takes no initial result value * Add excluding label to show the initial result is not included * Test lazy implementations * Remove reductions(including:_:) * Add conformance to LazySequenceProtocol and LazyCollectionProtocol when the base sequence conforms * Fix implementation of lazy reductions * Add test cases for sequences with one element * Tidy up tests * Improve ergonomics of no initial value eager reductions call and provide it as an extension on Sequence * Add lazy InclusiveReductions sequence * Rename Reductions to ExclusiveReductions * Add Collection implementation for InclusiveReductions * Update guide * Add links for C++ implementations * Add conformance to Collection for ExclusiveReductions * Improve ergonomics of ExclusiveReductions' index representation * Improve ergonomics of InclusiveReductions' index representation by sharing the Index implementation * Tidy up internal function * Add exclusive eager version of reductions(into:_:) * Use new lazy assertion functions * Correct the complexity claims for reductions * Separate the index types This will allow for future changes to either implementation without causing a breakage for the other. * Update guide to reflect that arrays are returned directly * Add scan as deprecated methods * Add lazy overload of reductions(into:_:) * Add the reductions(into:_:) functions * More succinctly introduce reductions * Add a note about the deprecated scan functions * Update documentation * Add @inlinable and @usableFromInline * Copy documentation to all variants of reductions * Test the value after the function is complete * Just use the += operator * Add an example for reductions(into:_:) * Improve the documentation of the return value * Update complexity note * Use the reduce documentation for inspiration for the discussion of reductions
94 lines
3.4 KiB
Swift
94 lines
3.4 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Algorithms open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import XCTest
|
|
import Algorithms
|
|
|
|
final class ReductionsTests: XCTestCase {
|
|
|
|
struct TestError: Error {}
|
|
|
|
// MARK: - Exclusive Reductions
|
|
|
|
func testExclusiveLazy() {
|
|
XCTAssertEqualSequences((1...).prefix(4).lazy.reductions(0, +), [0, 1, 3, 6, 10])
|
|
XCTAssertEqualSequences((1...).prefix(1).lazy.reductions(0, +), [0, 1])
|
|
XCTAssertEqualSequences((1...).prefix(0).lazy.reductions(0, +), [0])
|
|
|
|
XCTAssertEqualCollections([1, 2, 3, 4].lazy.reductions(0, +), [0, 1, 3, 6, 10])
|
|
XCTAssertEqualCollections([1].lazy.reductions(0, +), [0, 1])
|
|
XCTAssertEqualCollections(EmptyCollection<Int>().lazy.reductions(0, +), [0])
|
|
|
|
var value = 0
|
|
XCTAssertEqual([1, 2, 3, 4].lazy.reductions(into: &value, +=), [0, 1, 3, 6, 10])
|
|
XCTAssertEqual(value, 10)
|
|
|
|
value = 0
|
|
XCTAssertEqual([1].lazy.reductions(into: &value, +=), [0, 1])
|
|
XCTAssertEqual(value, 1)
|
|
|
|
value = 0
|
|
XCTAssertEqual(EmptyCollection<Int>().lazy.reductions(into: &value, +=), [0])
|
|
XCTAssertEqual(value, 0)
|
|
|
|
XCTAssertLazySequence((1...).prefix(1).lazy.reductions(0, +))
|
|
XCTAssertLazySequence([1].lazy.reductions(0, +))
|
|
XCTAssertLazyCollection([1].lazy.reductions(0, +))
|
|
}
|
|
|
|
func testExclusiveEager() {
|
|
XCTAssertEqual([1, 2, 3, 4].reductions(0, +), [0, 1, 3, 6, 10])
|
|
XCTAssertEqual([1].reductions(0, +), [0, 1])
|
|
XCTAssertEqual(EmptyCollection<Int>().reductions(0, +), [0])
|
|
|
|
var value = 0
|
|
XCTAssertEqual([1, 2, 3, 4].reductions(into: &value, +=), [0, 1, 3, 6, 10])
|
|
XCTAssertEqual(value, 10)
|
|
|
|
value = 0
|
|
XCTAssertEqual([1].reductions(into: &value, +=), [0, 1])
|
|
XCTAssertEqual(value, 1)
|
|
|
|
value = 0
|
|
XCTAssertEqual(EmptyCollection<Int>().reductions(into: &value, +=), [0])
|
|
XCTAssertEqual(value, 0)
|
|
|
|
XCTAssertNoThrow(try [].reductions(0) { _, _ in throw TestError() })
|
|
XCTAssertThrowsError(try [1].reductions(0) { _, _ in throw TestError() })
|
|
}
|
|
|
|
// MARK: - Inclusive Reductions
|
|
|
|
func testInclusiveLazy() {
|
|
XCTAssertEqualSequences((1...).prefix(4).lazy.reductions(+), [1, 3, 6, 10])
|
|
XCTAssertEqualSequences((1...).prefix(1).lazy.reductions(+), [1])
|
|
XCTAssertEqualSequences((1...).prefix(0).lazy.reductions(+), [])
|
|
|
|
XCTAssertEqualCollections([1, 2, 3, 4].lazy.reductions(+), [1, 3, 6, 10])
|
|
XCTAssertEqualCollections([1].lazy.reductions(+), [1])
|
|
XCTAssertEqualCollections(EmptyCollection<Int>().lazy.reductions(+), [])
|
|
|
|
XCTAssertLazySequence((1...).prefix(1).lazy.reductions(+))
|
|
XCTAssertLazySequence([1].lazy.reductions(+))
|
|
XCTAssertLazyCollection([1].lazy.reductions(+))
|
|
}
|
|
|
|
func testInclusiveEager() {
|
|
XCTAssertEqual([1, 2, 3, 4].reductions(+), [1, 3, 6, 10])
|
|
XCTAssertEqual([1].reductions(+), [1])
|
|
XCTAssertEqual(EmptyCollection<Int>().reductions(+), [])
|
|
|
|
XCTAssertNoThrow(try [].reductions { _, _ in throw TestError() })
|
|
XCTAssertNoThrow(try [1].reductions { _, _ in throw TestError() })
|
|
XCTAssertThrowsError(try [1, 1].reductions { _, _ in throw TestError() })
|
|
}
|
|
}
|