Commit Graph
65 Commits
Author SHA1 Message Date
inju2403 86ddbd9705 [OrderedCollections] OrderedSet.reverse(): update the hash table in place
Reversing an ordered set is a deterministic permutation: the element at offset `i` moves to offset `count - 1 - i`. Hash values are unaffected, so every element stays in its current bucket and only the stored offset needs to be flipped. `reverse()` was instead regenerating the whole hash table from scratch, rehashing every element -- exactly what the existing `// FIXME: Update hash table contents in place.` called out.

Replace `_regenerateHashTable()` with an in-place offset flip (`v -> count - 1 - v`) over the table's buckets, reusing the same bucket-walk idiom as `adjustContents` and `swapBucketValues`. This drops all hash recomputation and the table reallocation.
2026-06-12 08:57:42 +09:00
Karoy Lorentey 5c8d97dccb Merge pull request #660 from apple/dn/ordered-set-move
[OrderedCollections] Add move operations
2026-06-05 17:45:16 -07:00
David Nadoba 72f074a23a [OrderedCollections] Use pre-removal destination index for move APIs 2026-06-05 17:05:03 -07:00
David Nadoba 5a6af29d55 [OrderedCollections] Refine move APIs and simplify no-op check 2026-06-05 14:44:22 -07:00
David Nadoba a83773321e [OrderedCollections] Rework move APIs and extend to OrderedDictionary
Reworks the relocation API and extends it to `OrderedDictionary`:

* Renames the index-range move to `moveSubrange(_:to:)` (with a
  `RangeExpression` overload) and the by-value move to `move(contentsOf:to:)`,
  matching the conventions of `removeSubrange`/`insert(_:at:)`. Adds
  `move(fromIndices:to:)`, which takes the source indices directly.
* Factors the element rearrangement into generic
  `UnsafeMutableBufferPointer` operations (rotation / rotation+permutation /
  scatter) so `OrderedDictionary` relocates its keys and values in lockstep
  through the same code, adding only the parallel value move; the hash-table
  maintenance stays entirely in `OrderedSet`.
* The hot path runs in O(*d* + *k*) and falls back to O(`count`) for large
  moves; bucket maintenance chooses targeted updates or a full rebuild by the
  same threshold.
* `move(contentsOf:to:)` ignores elements/keys that aren't present; duplicate
  members are detected and trap.

Adds tests (exhaustive hash-table integrity, value alignment under lifetime
tracking) and move benchmarks for both types.
2026-06-04 22:33:54 -07:00
David Nadoba 508f36f48d [OrderedSet] Add move(_:toOffset:)
Adds two overloads for relocating elements within an `OrderedSet`: one
taking a contiguous `Range<Int>`, and one taking a `Sequence<Element>`.
The moved elements occupy consecutive positions starting at the
destination, in the order they appear in the input.

The hot path runs in O(*d* + *k*) time, where *d* is the distance moved
and *k* is the number of elements moved; the implementation falls back
to O(`count`) for large moves.
2026-05-25 23:01:28 -07:00
Karoy Lorentey 6017342a4d Merge pull request #616 from inju2403/feature/ordered-dictionary-replace-element-at-index
[OrderedCollections] Add OrderedDictionary.replaceElement(at:withKey:…
2026-04-27 18:05:52 -07:00
inju2403 8c5830ee46 [OrderedCollections] OrderedDictionary.replaceElement: allow the new key to equal the key at index
Per review feedback, relax the precondition so that the new key may compare equal to the key currently stored at index. This enables replacing an existing key with an equal-but-not-identical instance — for example, swapping a decomposed Unicode string with its precomposed equivalent — without an inefficient remove/insert workaround.

When the new key matches the one being replaced, the pair is updated in place via OrderedSet.update(_:at:) and the hash table is left untouched, preserving the expected amortized O(1) complexity.

Add a test_replaceElement_sameKey test on both OrderedDictionary and its Elements view that exercises the new path with LifetimeTracked keys carrying equal payloads but distinct instances.
2026-04-23 21:38:11 +09:00
Manan 396577ca27 OrderedSet: Don't crash on negative capacity values
Fixes https://github.com/apple/swift-collections/issues/608, and starts behaving similar to Set()
2026-04-16 10:10:02 +05:30
inju2403 9426246879 [OrderedCollections] Add OrderedDictionary.replaceElement(at:withKey:value:) 2026-04-03 16:55:14 +09:00
Karoy Lorentey 1c69635852 Add SPDX-License-Identifier tags 2026-03-05 19:39:24 -08:00
Karoy Lorentey d9b09ecde8 Update copyright dates 2026-03-05 19:39:20 -08:00
Karoy Lorentey 9503f8fe57 Improve consistency of precondition failure messages
More uniform message strings should feel better, and they should ever so slightly reduce code size.
2026-02-04 10:27:17 -08:00
Karoy Lorentey 65d174cc77 OrderedSetTests: Add missing import 2025-10-10 16:13:11 -07:00
Karoy Lorentey 006ab60c42 Xcode/Collections.xcodeproj: Fix ability to build & test BigString 2025-10-10 15:53:16 -07:00
Karoy Lorentey 2ca8bca67d Resolve Swift 6 concurrency issues
This is just slapping `nonisolated(unsafe)` on global variables in the test suite.
Our tests do not support concurrent execution.
2025-08-17 13:33:28 -07:00
Karoy Lorentey 5f03d1bc24 Adopt availability macros 2025-08-17 13:23:01 -07:00
Karoy Lorentey 54c65310f4 Adopt #filePath in test assertions 2025-08-17 13:23:00 -07:00
Peter Meyers c5ac12e2bc Add test 2025-02-20 15:34:26 -05:00
Karoy Lorentey 35d5746117 Rename _CollectionsUtilities to InternalCollectionsUtilities
We are forced to do this due to an Xcode issue (#364) with underscored module names.

This module remains unstable, despite the removal of the underscore. Directly importing it from outside the package remains unsupported. This module and/or its entire contents may go away or change in incompatible ways in any future swift-collections release.
2024-05-29 14:00:13 -07:00
Karoy Lorentey ffa4a58bc1 Update remaining copyright notices 2024-02-02 16:20:22 -08:00
Karoy Lorentey 1a0ac8c06c Merge branch 'release/1.0' into release/1.1
# Conflicts:
#	Sources/DequeModule/CMakeLists.txt
#	Sources/DequeModule/Deque+Collection.swift
#	Sources/DequeModule/Deque+Sendable.swift
#	Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Elements.SubSequence.swift
#	Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Elements.swift
#	Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Sendable.swift
#	Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Sequence.swift
#	Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Values.swift
#	Sources/OrderedCollections/OrderedSet/OrderedSet+Sendable.swift
#	Sources/OrderedCollections/OrderedSet/OrderedSet+SubSequence.swift
#	Sources/OrderedCollections/OrderedSet/OrderedSet+UnorderedView.swift
#	Sources/OrderedCollections/OrderedSet/OrderedSet+UnstableInternals.swift
2023-12-21 15:17:15 -08:00
Karoy Lorentey 8b8a14f0e1 Merge pull request #340 from vanvoorden/vanvoorden/ordered-set-equality
[OrderedSet] forward ordered set equality to elements property
2023-12-12 21:54:30 -05:00
Rick van Voorden 525bb545c4 clean up ordered set equality test 2023-12-12 14:00:00 -08:00
Rick van Voorden 00ccf9a1c0 remove ordered set print statements 2023-12-12 12:31:14 -08:00
Rick van Voorden 0031fc7e36 update ordered set subsequence tests 2023-12-12 12:30:14 -08:00
Rick van VoordenandKaroy Lorentey 88404c3ae8 test through ordered set subsequences
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2023-12-12 11:38:24 -08:00
Rick van VoordenandKaroy Lorentey d2662892cd remove extra ordered dictionary elements test
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2023-12-12 11:18:50 -08:00
Rick van VoordenandKaroy Lorentey baa3880e6e add ordered dictionary test for equal identity
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2023-12-12 11:17:06 -08:00
Rick van Voorden ba88bcad62 updated set equality tests 2023-12-11 17:40:12 -08:00
Rick van Voorden edb431ee36 add set equality tests 2023-12-11 15:46:59 -08:00
Rick van Voorden e42fb868b7 forward ordered dictionary values equality to values property 2023-12-08 17:39:25 -08:00
Karoy Lorentey 09c09f0f73 Fix CustomStringConvertible/CustomDebugStringConvertible conformances
- For CustomStringConvertible, `description` should print elements using `String(reflecting:)` rather than `String(describing:)`, to avoid element output from interfering with syntactically relevant punctuation within structured list displays.
- To make this work without making displays overly verbose, make `debugDescription` mostly the same as `description`, except for escaping raw string displays when and if needed.
- Avoid printing type names in `debugDescription` — it is generally not helpful, as the type is already easily accessible elsewhere. Including it makes for overly verbose displays when the type serves as an Element of a collection.
- Avoid having `debugDescription` return the empty string — it can be confusing when such a description appears in list displays.

Resolves #301.
2023-07-06 17:25:11 -07:00
Karoy Lorentey a0666cdcce Resolve some leftover build issues 2023-03-23 14:23:45 -07:00
Karoy Lorentey f17cf642c1 Add support building with COLLECTIONS_SINGLE_MODULE
- Sources/_CollectionsTestSupport → Tests/_CollectionsTestSupport
- Refactor Package.swift to allow switching between the default
   polymodule and the new monomodule product configurations
   (by editing the manifest file).
- Fix build errors in the monomodule variant.
2023-03-22 21:44:23 -07:00
Karoy Lorentey ea60d293b4 Fix test issues with single-module variant 2023-03-21 18:43:46 -07:00
Karoy Lorentey cbab9e3737 Introduce COLLECTIONS_SINGLE_MODULE condition 2023-03-21 18:42:33 -07:00
Karoy Lorentey a2f4b18ae0 [BitSet, OrderedCollections] Unify UnsafeBitSet implementations 2022-11-28 16:30:55 -08:00
Karoy Lorentey 8f7d5fd505 [SetAlgebra types] isEqual(to:) → isEqualSet(to:)
The `isEqual(to:)` methods are a bit too confusing — they implement
mathematical set comparisons (ignoring element ordering and, in the
case of the Sequence variant, even element multiplicities), so it
seems better to highlight this by putting a `Set` in the name.
2022-10-31 15:05:12 -07:00
Karoy Lorentey 2dae70dba7 [OrderedSet, BitSet] Fix display style of custom mirror 2022-10-27 18:29:32 -07:00
Karoy Lorentey 0914e6f546 [OrderedSet] More test coverage for set operations; clarify docs 2022-10-21 18:24:08 -07:00
Karoy Lorentey 7143421800 [OrderedCollections] Add missing Custom[Debug]StringConvertable conformances, review existing ones
Add `Custom[Debug]StringConvertible` conformance on:

- `OrderedDictionary.Values`
- `OrderedDictionary.Elements.Subsequence`
- `OrderedSet.SubSequence`
2022-10-20 17:14:52 -07:00
Karoy Lorentey 5dfd51238c [OrderedSet] Review, test, fix, and document set relation predicates 2022-10-19 15:20:30 -07:00
Karoy Lorentey 4604c1bc38 [test] Add SetAPIChecker protocol
This is useful for validating the API surface of set-like types,
ensuring that we haven’t forgotten to implement members that
are part of users’ baseline expectations.

To ensure maximum utility, this protocol refines neither Collection
nor SetAlgebra.
2022-10-09 13:33:23 -07:00
Karoy Lorentey 6437aea75a [PersistentCollections] Improve test coverage & fixtures 2022-09-27 14:18:40 -07:00
Karoy Lorentey b7bfc52664 [test] Add Dictionary API checker protocol
This can be used to verify that a new dictionary implementation
provides all expected members.
2022-09-19 21:25:07 -07:00
Karoy Lorentey 9a9d7d87d8 Merge branch 'release/1.0'
# Conflicts:
#	.swiftpm/xcode/xcshareddata/xcschemes/swift-collections-Package.xcscheme
#	Utils/swift-collections.xcworkspace/xcshareddata/xcschemes/Collections.xcscheme
#	Utils/swift-collections.xcworkspace/xcshareddata/xcschemes/OrderedCollections.xcscheme
#	Utils/swift-collections.xcworkspace/xcshareddata/xcschemes/swift-collections-Package.xcscheme
2022-09-02 15:30:31 -07:00
Karoy Lorentey 71aa4a39ae [OrderedDictionary] Resolve spurious(?) call-site ambiguities 2022-08-31 17:57:24 -07:00
Konrad `ktoso` Malawski 876586f924 expect type is correct 2022-08-17 11:06:07 +09:00
Konrad `ktoso` Malawski 4138ccd68e improve test to cover more cases 2022-08-17 11:01:07 +09:00