Skip to content

Conversation

danieleggert
Copy link

The insert method

public mutating func insert<C>(_ index: Bound, within collection: C) -> Bool

now returns a Bool indicating whether the range set was modified.

The other insert method, insert(contentsOf:), and the modified one now call into an internal version:

internal mutating func _insert(contentsOf range: Range<Bound>) -> Bool

The public version of insert(contentsOf:) ignores the return value, and insert(:within:) returns the result.

Motivation

Some use cases need to know if the range set was modified, and would previously have to call contains() and then insert() basically doubling the work to be done — since both have to do a search in the range set.

One example is building a type that conforms to SetAlgebra and basing it on RangeSet. The insert() method needed for SetAlgebra needs to know if its value has been mutated.

The insert method
```
public mutating func insert<C>(_ index: Bound, within collection: C) -> Bool
```
now returns a `Bool` indicating whether the range set was modified.

The other insert method, `insert(contentsOf:)`, and the modified one now call into an `internal` version:
```
internal mutating func _insert(contentsOf range: Range<Bound>) -> Bool
```

The `public` version of `insert(contentsOf:)` ignores the return value, and `insert(:within:)` returns the result.

## Motivation

Some use cases need to know if the range set was modified, and would previously have to call `contains()` and then `insert()` basically doubling the work to be done — since both have to do a search in the range set.

One example is building a type that conforms to `SetAlgebra` and basing it on `RangeSet`. The `insert()` method needed for `SetAlgebra` needs to know if its value has been mutated.
@jmschonfeld
Copy link

This change has been incorporated into the latest API design of the RangeSet type being added to the stdlib itself, and is now included in the implementation at swiftlang/swift#69766

@glessard
Copy link
Contributor

swiftlang/swift#69766, which includes this suggestion, has been merged.

@glessard glessard closed this Jan 24, 2024
@glessard glessard deleted the insert-modified branch January 24, 2024 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants