Skip to content

Commit

Permalink
[Rope] replaceSubrange: take a sequence, not a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
lorentey committed Apr 14, 2023
1 parent 8d7c630 commit b0fe7ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 14 additions & 2 deletions Sources/RopeModule/Rope/Basics/Rope+Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ extension Rope {
return
}
var item = item
if prefix.rebalance(nextNeighbor: &item) {
if (prefix.isUndersized || item.isUndersized), prefix.rebalance(nextNeighbor: &item) {
self._prefix = prefix
return
}
Expand All @@ -221,7 +221,19 @@ extension Rope {
guard rope._root != nil else { return }
_insertBeforeTip(rope.root)
}


@inlinable
public mutating func insertBeforeTip<S: Sequence<Element>>(_ items: __owned S) {
if S.self == Rope.self {
let items = _identityCast(items, to: Rope.self)
self.insertBeforeTip(items)
} else {
for item in items {
self.insertBeforeTip(item)
}
}
}

@inlinable
mutating func _insertBeforeTip(_ node: __owned Rope._Node) {
defer { _invariantCheck() }
Expand Down
6 changes: 2 additions & 4 deletions Sources/RopeModule/Rope/Operations/Rope+RemoveSubrange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ extension Rope {
public mutating func replaceSubrange(
_ bounds: Range<Int>,
in metric: some RopeMetric<Element>,
with newElements: __owned some Collection<Element>
with newElements: __owned some Sequence<Element>
) {
// FIXME: Implement insert(contentsOf:at:in:) and dispatch to it when bounds.isEmpty.
// FIXME: Add fast path for replacing tiny ranges with tiny data.
// FIXME: Add special cases if newElements is itself a _Rope etc.
_invalidateIndices()
var builder = builder(removing: bounds, in: metric)
for item in newElements {
builder.insertBeforeTip(item)
}
builder.insertBeforeTip(newElements)
self = builder.finalize()
}

Expand Down

0 comments on commit b0fe7ca

Please sign in to comment.