diff --git a/Sources/Document/CRDT/CRDTTree.swift b/Sources/Document/CRDT/CRDTTree.swift index 32ddec48..03005e4f 100644 --- a/Sources/Document/CRDT/CRDTTree.swift +++ b/Sources/Document/CRDT/CRDTTree.swift @@ -621,8 +621,7 @@ class CRDTTree: CRDTElement { let allChildren = realParent.innerChildren let index = isLeftMost ? 0 : (allChildren.firstIndex(where: { $0 === leftNode }) ?? -1) + 1 - for index in index ..< allChildren.count { - let next = allChildren[index] + for next in allChildren.suffix(from: index) { if !next.id.createdAt.after(editedAt) { break } @@ -1141,8 +1140,7 @@ class CRDTTree: CRDTElement { // Generate ranges by accumulating consecutive nodes. var start: TreeToken? var end: TreeToken? - for index in 0 ..< candidates.count { - let cur = candidates[index] + for (index, cur) in candidates.enumerated() { let next = candidates[safe: index + 1] if start == nil { start = cur diff --git a/Sources/Util/IndexTree.swift b/Sources/Util/IndexTree.swift index c13b6907..83421db3 100644 --- a/Sources/Util/IndexTree.swift +++ b/Sources/Util/IndexTree.swift @@ -93,9 +93,7 @@ func addSizeOfLeftSiblings(parent: T, offset: Int) -> Int { let siblings = parent.children - for index in 0 ..< offset { - let leftSibling = siblings[index] - + for leftSibling in siblings.prefix(upTo: offset) { if leftSibling.isRemoved { continue } @@ -750,9 +748,7 @@ func findTextPos(node: T, pathElement: Int) throws -> TreePos< throw YorkieError.unexpected(message: "unacceptable path") } - for index in 0 ..< node.children.count { - let child = node.children[index] - + for child in node.children { if child.size < pathElement { pathElement -= child.size } else { @@ -859,14 +855,11 @@ class IndexTree { } var node = self.root - for index in 0 ..< path.count - 1 { - let pathElement = path[index] - - if node.children[safe: pathElement] == nil { + for pathElement in path.dropLast() { + guard let child = node.children[safe: pathElement] else { throw YorkieError.unexpected(message: "unacceptable path") } - - node = node.children[pathElement] + node = child } if node.hasTextChild {