72 Commits
Author SHA1 Message Date
Nate Cook 0aa3733a77 Fix swift-format errors throughout (#266) 2025-11-04 00:16:21 -06:00
Nate Cook 66ae806bbe Enable CI format check (#253)
* Incorporate swift-format formatting fixes
* Resolve linting issues
* Doc edits, mostly to fix linter-identified issues
* Early exit improvements
* Indentation fixes
* Line lengths to 80 maximum
* Implement case binding rule, plus argument line break improvements
* Implement NoForceUnwrap rule
* Format the tests, as well
* Stop masquerading as XCTest
2025-03-05 09:11:17 -06:00
Alexander Momchilov 401145b7b8 Add ends(with:) (#224) 2025-01-14 12:42:37 -06:00
b1ackturtle cdc909b93d Fix compile errors for Swift 6 language mode (#230) 2025-01-14 12:02:42 -06:00
Takumi Muraishi 82ee335a48 Fix typo in TestUtilities.swift (#223) 2024-02-17 00:58:00 -06:00
Dave Abrahams 9dd88e5eaf CombinationsSequence: underestimatedCount == count (#220)
Not doing this makes it needlessly costly to create an Array of the
results.
2024-01-20 10:04:45 -06:00
Nate Cook 5c54f200e7 Update guide/docs/tests. 2023-11-14 13:00:34 -06:00
Alexander Momchilov 328631d60c Add grouped(by:) and keyed(by:) (#197)
* Add Grouped and Keyed

* Add `Key` to `combine` closure

* Split `keyed(by:)` into two overloads

* Rename uniquingKeysWith → resolvingConflictsWith

* Keep latest instead of throwing on collision
2023-11-07 14:29:10 -06:00
Nate Cook 0f81ef4a70 Chunked type / documentation cleanup (#200)
This change improves the documentation for `evenlyChunked(in:)` and
`chunks(ofCount:)` and renames `EvenChunksCollection` to
`EvenlyChunkedCollection` to follow the pattern of wrapper type
names matching their producing functions.
2023-07-26 13:15:35 -05:00
Tim VermeulenandTim Vermeulen de3efbc567 Evenly divide a collection into chunks (#96)
Adds `evenlyChunked(in:)` as a `Collection` method that divides a
collection into a specific number of chunks as evenly as possible.

Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>
2023-07-26 11:17:25 -05:00
Matt Zanchelli da475ea9af Clarify endIndex behavior of partitioningIndex(where:) in documentation (Resolves #170) (#171)
The returned index isn’t necessarily an index of an element. In the case where it returns `endIndex`, it’s not valid for subscripting the collection.
2021-12-16 07:10:17 -06:00
e2fa131d62 Add partitioned(by:) (#152)
* Add `partitioned(_:)`

`partitioned(_:)` works like `filter(_:)`, but also returns the excluded elements by returning a tuple of two `Array`s

* For collections with fewer than 8 elements, use the `Sequence`-based implementation

This constant was determined using benchmarking. More information: https://github.com/apple/swift-algorithms/pull/152#issuecomment-887130149

* Remove check for collections fewer than 8 elements

* Make `_partitioned` `internal`

* Prefer `Array` over `ContiguousArray`

* Document `partitioned(_:)` on `Collection`

* Remove `partitioned(upTo:)`

* Remove `_tupleMap`

* Remove `_partitioned` and use it inline (since it’s no longer used by the `Collection` implementation

* Remove unnecessary conversation of `Array` to `Array`

* Correct indentation

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Consistent syntax

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Add an external `by:` label to `partitioned`

* Add labels to returned tuple `falseElements`, `trueElements`

* Correct function signature

* Rename `belongsInSecondCollection` parameter name to simply `predicate`

The parameter name was potentially confusing. Unlike the other `partition` functions, this function can rely on its named tuple to clarify its behavior.

* Update copyright information

Co-authored-by: Nate Cook <natecook@apple.com>

* Update documentation

Co-authored-by: Nate Cook <natecook@apple.com>

* Update comment

* Add a precondition to ensure that the count matches up with the number of actual elements found while iterating

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
Co-authored-by: Nate Cook <natecook@apple.com>
2021-10-20 22:21:39 +02:00
Tim Vermeulen c5ea9e3e36 Prepare for Swift Algorithms 1.0 (#167)
* Rename types to include a Sequence or Collection suffix, and clean up some guides

* Various refactors

* Replace `validateIndexTraversals` test method with `IndexValidator` type

* Add LazySequenceProtocol to StridingSequence, StridingCollection

* Update the Numerics dependency to 1.0

* Set `IndexedCollection.Indices` to `Base.Indices`

* Remove deprecations

* Update the changelog/README for the 1.0.0 release

* Fix compiler error on Linux

* Rename `WindowsCollection` to `WindowsOfCountCollection`
2021-09-09 01:01:36 +02:00
Tim Vermeulen 9f628005fe Ensure chunked(by:) always compares consecutive elements (#162) 2021-08-18 00:22:36 +02:00
Tim Vermeulen 4b779b8b3b Add joined(by:) (#138) 2021-06-01 10:14:34 -05:00
Federico Ciardi 0e2941ef50 Add trimmingSuffix, trimmingPrefix and mutating variants (#104) 2021-05-25 12:50:03 -05:00
Nate Cook 3484ac31f6 Make chunked(on:) include subject value in Element type (#142) 2021-05-16 15:02:03 -05:00
Nate Cook f9f3a5ccc4 Minor revisions to minAndMax() (#143)
* Settle on 2-way approach (s/o to @NevinBR)
* Add tests for predicate usage and stability
* Update the return value semantics
2021-05-13 12:31:13 -05:00
Daryle Walker 4038b5da40 Simultaneous Minimum and Maximum Evaluation (#90)
* Add function to find MIN and MAX in one pass

Add a method extending sequences that finds both the minimal and maximal elements of the receiver in a single pass, based off a given ordering predicate.  Add an overload that defaults the comparison predicate to the standard less-than operator.
2021-05-12 16:11:33 -05:00
Nate CookandTim Vermeulen 65ef6cb966 Update AdjacentPairs implementations and tests (#139)
* Update AdjacentPairs implementations and tests

* Add conditional lazy conformances

* Apply suggestions from code review

Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>

Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>
2021-05-05 19:15:47 +02:00
Michael Pangburn 0f27d99c89 Introduce adjacentPairs (#119) 2021-05-04 13:08:33 -05:00
Tim Vermeulen abc5559ef6 Remove collection conformances to Equatable and Hashable (#124) 2021-04-09 14:44:16 -05:00
Pranav Kasetti bcdbac800a Add new FiniteCycle type as the return type of Collection.cycled(times:) (#106) 2021-04-07 14:57:26 -05:00
Tim Vermeulen 718220d0f8 Make uniqued() lazy by default (#71)
* Make `uniqued()` lazy by default
2021-04-07 14:51:11 -05:00
Luciano Almeida 1639c1e2c3 [tests] Ensure correct implementation of hash for ChunkedByCount (#123) 2021-04-07 09:42:28 -05:00
Luciano Almeida 198eee004e [Compacted] Adding compacted() method to remove all nils in a sequence or collection (#112) 2021-04-05 09:52:53 -05:00
Kyle Macomber 0065c60a22 Remove the inout on reductions(into:_:) to be consistent with the stdlib reduce(into:_) (#118) 2021-03-27 07:17:34 -07:00
Daniel Tull b171b81577 Add reductions(_:_:) (#46)
* 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
2021-03-24 17:10:09 +01:00
Nate CookandTim Vermeulen e652f01949 Add uniquePermutations as wrapper for nextPermutation (#91)
* Add uniquePermutations as wrapper for nextPermutation

* Implement uniquePermutations(ofCount:) variants

* Simplify UniquePermutations.Iterator.next()

* Add tests for uniquePermutations

* Move to a predicate-based uniquePermutations

* Add method documentation

* Convert to Hashable-based uniquing instead of Comparable

* Streamline tests, cover uniqueness

* Use optional count to collapse no-count method

* Update API documentation

* Remove leftover version of nextPermutation()

* Remove Hashable requirement from the UniquePermutations type

* Update documentation

* Apply Tim's great revisions

Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>

* Add lazy conformance to UniquePermutations

Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>
2021-03-24 15:17:27 +01:00
Charles Maria TorandXiaodi Wu e64cff7e1d Add Sequence.firstNonNil(_:) (#31)
* Add Sequence.firstNonNil(of:) and associated tests

* Rename First.swift to FirstTests.swift

* Address PR comments by adding guide and changing naming

* Address PR comments by changing name

* Update Guides/FirstNonNil.md

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Update Sources/Algorithms/FirstNonNil.swift

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
2021-03-23 19:55:33 +01:00
Luciano Almeida ca8af77420 [test] Adding missing BidirectionalCollection::partition(subrange:) and MutableCollection::partition(subrange:) unit tests (#95) 2021-03-23 13:20:21 -05:00
Ahmad Alhashemi e077f67639 Improve XCTAssertEqualSequences to log difference (#105) 2021-03-23 07:52:56 -07:00
Oliver AtkinsonandTim Vermeulen 18ec8500b7 Improve the performance of Stride when Base isa Collection (#88)
* Implement StrideSequence so that the default implementation for collection uses IndexingIterator

* Update Sources/Algorithms/Stride.swift

Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>

* reinstate documentation for Sequence and remove explicit Sequence conformance on Stride

* documentation

* rename Stride to StrideCollection

Co-authored-by: Tim Vermeulen <tvermeulen@apple.com>
2021-03-10 18:20:00 +01:00
Tim Vermeulen ab0ec2adf4 Internally add Collection.endOfPrefix(while:) and BidirectionalCollection.startOfSuffix(while:) (#92)
* Add endOfPrefix(while:) and startOfSuffix(while:) internally

* Add tests

* Use these methods throughout the package
2021-03-09 20:03:55 +01:00
Todd Thomas e7c6716499 Add lazy split implementation. (#78)
`split(maxSplits:omittingEmptySubsequences:whereSeparator:)`

and

`split(separator:maxSplits:omittingEmptySubsequences:)`

now operate lazily when called on `LazySequenceProtocol`- or
`LazyCollectionProtocol`-conforming values. In the latter case,
no additional storage is allocated on the heap.
2021-03-09 01:48:58 -06:00
Nate Cook bd64bef3d2 Rename sortedPrefix methods (#77)
This renames sortedPrefix to min(count:), adds max(count:),
and moves both methods down to Sequence.

In order to still provide the optimization when `count` is close to
a collection's length, we include a Collection-specific overload of
all methods. These won't be accessed when in a Sequence-generic context,
but it's the best we can do without adding these methods as protocol
requirements.
2021-02-26 13:57:55 -06:00
Nate Cook cc20b55cc6 Rename slidingWindows back to windows(ofCount:) 2021-02-18 12:31:58 -06:00
Matt Zanchelli bf1de3240d Add unit tests for reverse functions 2021-02-15 13:31:34 -08:00
Matt Zanchelli dd564ca517 Add unit tests for documented examples of rotate functions 2021-02-15 13:31:22 -08:00
Iceman 1a3c7061de Fix RandomSample crash when 0 was out from random. (#70) 2021-02-12 06:41:26 -08:00
Matt Zanchelli d8a11e0d51 All permutations (#56)
Adds the ability to easily get permutations of a range of sizes, like #51 did for Combinations.
2021-02-12 06:32:32 -08:00
michiboo 3b511072a6 Fix suffix tests 2021-02-09 12:56:08 -06:00
michiboo 813ea7905e add more test for suffix Tests 2021-01-22 16:17:28 +02:00
michiboo 1acc2136d9 add test for suffix 2021-01-20 15:23:38 +02:00
Nate Cook 9836a4f80b [Chunked] Add chunks(of:) variant that divides a collection in chunks of a given size (#54) 2021-01-15 22:43:13 -06:00
Luciano Almeida 622703b97f [Chunked] Improve tests for ChunksOfCount traversal 2021-01-14 22:56:28 -03:00
Matt Zanchelli 5b7993ca8d All combinations (#51)
Add an overload of combinations(ofCount:) that accepts a range.
2021-01-14 13:32:47 -06:00
Luciano Almeida 05df988369 [Chunked] Minor adjustments on tests 2021-01-10 17:18:49 -03:00
Luciano Almeida 79aeb24e9d Fixing review comments 2021-01-08 08:21:01 -03:00
Luciano Almeida e0fed41419 [Chunked] Adding tests for chunks of count collection implementation 2021-01-08 00:08:21 -03:00