185 Commits
Author SHA1 Message Date
Swastik TripathiandClaude Opus 5.5 8da3cafe2e Fix doc comment examples in OrderedDictionary, BitArray and SortedDictionary
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 09:04:13 +05:30
Karoy Lorentey 93b0a28178 [OrderedCollections] Resolve filename ambiguity with InternalCollectionsUtilities/UnsafeBitSet/_UnsafeBitSet.swift
This only affects some build setups using the single-module configuration on
case-insensitive filesystems, but let’s get rid of it anyway.
2026-10-26 11:37:41 -07:00
Karoy Lorentey 24339ad6a3 CMake: Remove obsolete references to source variables 2026-10-12 12:24:03 -07:00
Karoy Lorentey 1b936eda0b Disable strict memory safety on older toolchains
It would take too much effort to try to mitigate stdlib differences.

Fix low-hanging warnings, but turn off `.strictMemorySafety` on older toolchains by splitting the package manifest.
2026-10-01 13:40:27 -07:00
Karoy Lorentey d07dc32dfa Sources: Add unsafe keywords where needed 2026-10-01 11:14:29 -07:00
Karoy Lorentey 0ec72a9532 Stop using @_spi attribute and @testable import 2026-09-29 18:32:55 -07:00
Karoy Lorentey e611f62e67 Disable _UniqueCollection fast paths in embedded Swift
As of 2026-09-01, conformance checks are now diagnosed as errors:

```
 70 |     // Fast paths for when we know elements are all unique
 71 |     if elements is _UniqueCollection {
    |                 `- error: cannot perform a dynamic cast to a type involving protocol '_UniqueCollection' in Embedded Swift
```
2026-09-01 10:31:54 -07:00
Karoy Lorentey fcfa3b00cb Merge pull request #703 from inju2403/cleanup/replaceelement-exchange-same-key
[OrderedCollections] Use exchange(_:with:) in replaceElement's equal-key path
2026-08-25 14:02:39 -07:00
inju2403 7a67033ee2 [OrderedCollections] Use exchange(_:with:) in replaceElement's equal-key path
#688 factored `replaceElement`'s general case onto `OrderedSet._replaceNew(at:with:in:)` and, per review feedback, switched the value overwrite there to `exchange(_:with:)`. The equal-key path above it was outside that diff, so it still reads the old value into a local and then assigns over it.

Switch it to `exchange(_:with:)` as well, so both paths move the old value out instead of copying it. No behavior change: the returned element, the preconditions, and the expected amortized O(1) complexity are all unchanged.
2026-08-14 09:19:43 +09:00
Anton Karpov c2b2092f94 Name the documented arguments the way the declarations name them
Twenty-five doc comments in the shipping modules name an argument that the
declaration below them does not have, so DocC has nothing to bind the
description to and drops it. Nothing warns about this.

Most of them are renames that the comment did not follow, and the file itself
usually holds the proof: RigidDeque.prepend says - maxCount while its own
Complexity line already says newItemCount, and append(copying:) has three
overloads where the oldest still takes newElements and the two newer ones take
items with the older one's wording.

Three entries name an argument that no longer exists at all and are removed
rather than renamed.
2026-08-13 16:29:06 +03:00
Karoy Lorentey 2875152465 Merge pull request #688 from inju2403/cleanup/replaceelement-reuse-replace
[OrderedCollections] Share OrderedSet's replace primitive with OrderedDictionary.replaceElement
2026-08-11 14:11:16 -07:00
Karoy Lorentey 8380ca41da Merge pull request #682 from lorentey/container-updates
[ContainersPreview] Update Container protocols
2026-07-22 13:08:25 -07:00
Seungju LeeandKaroy Lorentey 771d6984e4 [OrderedCollections] Use exchange(_:with:) to avoid copying the old value
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2026-07-21 19:37:07 +09:00
inju2403 f2d090be11 [OrderedCollections] Share OrderedSet's replace primitive with OrderedDictionary.replaceElement
#669 left `OrderedDictionary.replaceElement` untouched to keep that PR purely additive, noting it could later be reimplemented on top of `replace(at:with:)`. Both it and `OrderedSet.replace(at:with:)` performed the same "append the new member, swap it into position, then remove the old one from the end" step inline.

Factor that step into an internal `OrderedSet._replaceNew(at:with:in:)` primitive and call it from both, matching the existing `_appendNew` / `_removeExistingMember` primitives that `OrderedDictionary` already delegates its key-side mutations to. In `replaceElement` only the keys go through the primitive; the value at `index` is overwritten in place.

Sharing the mechanical step this way — rather than having `replaceElement` call `replace` directly — keeps each type's diagnostics intact: `OrderedDictionary` still traps with "Duplicate key: '\(key)'" and "Index out of range", not `OrderedSet`'s "Duplicate element" / "Index out of bounds". The shared step is already verified by the caller, so there is no behavior change.
2026-07-18 18:20:02 +09:00
Karoy Lorentey 899809d3f8 Merge pull request #669 from inju2403/feature/ordered-set-replace-at-index
[OrderedCollections] Add OrderedSet.replace(_:at:)
2026-07-13 13:04:37 -07:00
inju2403 1f0184e99d [OrderedCollections] Rename OrderedSet.replace(_:at:) to replace(at:with:)
Swap the parameter order and add the `with` label so the API matches the shape of `replaceSubrange(_:with:)` on RangeReplaceableCollection, per review feedback.
2026-07-08 17:57:15 +09:00
Karoy Lorentey 638c62cd7b Merge pull request #674 from wjdtjq6/fix/superset-doc-comments
Fix superset set algebra doc comments
2026-07-07 15:08:52 -07:00
Karoy Lorentey 00ffe8caec [HashTreeCollections] Avoid parsing issue with func read
When the BorrowAndMutateAccessors feature is enabled, this code appears to be parsed as if it declared a `read` accessor, rather than a getter passing a closure to the `read` function:

```swift
@inlinable
  internal var collisionHash: _Hash {
    read { $0.collisionHash }
  }
```

Avoid this by adding a `self` prefix, as in `self.read { $0.collisionHash }`.
2026-07-07 14:18:09 -07:00
wjdtjq6 646274ed7c docs: fix superset set algebra comments 2026-06-23 13:21:36 +09:00
inju2403 95d8afe88f [OrderedCollections] Add OrderedSet.replace(_:at:)
`OrderedSet` already offers index-based mutations -- `update(_:at:)`, `insert(_:at:)`, `swapAt`, `remove(at:)` -- but no way to replace the member at an index with a *different* element. `update(_:at:)` looks like it should, but it requires the replacement to compare equal to the existing member.

Add `replace(_:at:)`, which replaces the member at a given index with a new element in expected amortized O(1) time. If the new element compares equal to the member at `index`, it is replaced in place via `update(_:at:)`; otherwise it is appended, swapped into position, and the old member is removed from the end -- each step O(1). Replacing a member with an element already present at a different index traps, as that would introduce a duplicate.

This is the OrderedSet counterpart of `OrderedDictionary.replaceElement(at:withKey:value:)` (#616), which already implements this dance on its keys' OrderedSet internally.
2026-06-14 19:38:01 +09:00
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 e31c04143e Update CMake & Xcode build configurations 2026-06-05 18:45:56 -07: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 215a9ac0a7 Merge pull request #632 from lorentey/assorted-fixes
[BasicContainers, DequeModule]: Assorted fixes
2026-04-30 15:53:21 -07:00
Karoy Lorentey 0fa494d3a8 [OrderedCollections] Minor fixes in _HashTable.UnsafeHandle 2026-04-30 15:29:25 -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 80878519f2 [doc] Fix most issues reported by docc
DocC keeps changing its link syntax. The latest iteration appears to have trouble dealing with `Self`-taking or `Self`-returning methods, so change our API to replace `Self` with a direct mention of the concrete type. ¯\\\_(ツ)\_/¯

That said, the new compiler also found a large number of mismatching parameter names, and that was really helpful.
2026-03-09 14:14:51 -07:00
Karoy Lorentey ee9808c72f [Dequemodule, OrderedCollections] Avoid using floating point arithmetic 2026-03-06 18:19:16 -08: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
Nate Cook 54ca53612f Include notes about index mutation in span(after/before:)
The indexes passed to span(after:) and friends on UniqueArray
and RigidArray are modified before return. This adds notes
about the updated values to the method documentation, along
with a few other minor formatting changes (dropping smart
quotes, single-sentence abstracts).
2026-03-02 16:38:48 -08:00
Karoy Lorentey ecafd9e947 Merge pull request #578 from ordo-one/isempty 2026-02-24 19:15:00 -08:00
Diana Ma b52dcf61e8 give constant folding an opportunity to select a much faster code path 2026-02-23 04:15:07 +00: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 ebaef60379 [OrderedCollections] Fix doc typo 2026-02-03 13:22:00 -08:00
Karoy Lorentey 5f03d1bc24 Adopt availability macros 2025-08-17 13:23:01 -07:00
Tony Parker c883a1729a Add support for embedded Swift mode
Remove calls to `debugPrint` (which does not exist), and ifdef out support for `Codable` and `Reflection`.
2025-07-17 16:36:56 -07:00
Alex Azarov 9f68408a70 Update OrderedSet.swift
Make values immutable if they are never mutated in example usage docs.
2025-06-30 11:35:45 +02:00
Alex Azarov d7301b7278 Fix OrderedSet example usage 2025-06-29 16:48:57 +02:00
Karoy Lorentey 4216d2ad1b Merge pull request #478 from ozumin/patch-1
fix comment for OrderedSet.appending(contentsOf:)
2025-05-21 09:24:42 -07:00
Karoy Lorentey 0f73f0ace7 Merge branch 'release/1.2'
# Conflicts:
#	.github/workflows/pull_request.yml
#	.swiftci/5_10_ubuntu2204
#	.swiftci/5_7_ubuntu2204
#	.swiftci/5_8_ubuntu2204
#	.swiftci/5_9_ubuntu2204
#	.swiftci/nightly_6_0_macos
#	.swiftci/nightly_6_0_ubuntu2204
#	.swiftci/nightly_main_macos
#	.swiftci/nightly_main_ubuntu2204
2025-05-20 11:13:32 -07:00
Mizuo Nagayama 1812091001 fix comment for OrderedSet.appending(contentsOf:) 2025-05-20 18:38:06 +09:00
Peter Meyers 3fb4855e0e [OrderedSet] Add OrderedSet.appending(contentsOf:) 2025-05-16 00:16:00 -07:00