//===----------------------------------------------------------------------===// // // This source file is part of the Swift Collections open source project // // Copyright (c) 2023 - 2026 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // // SPDX-License-Identifier: Apache-2.0 WITH Swift-exception // //===----------------------------------------------------------------------===// extension Rope { @inlinable public func forEachWhile( _ body: (Element) -> Bool ) -> Bool { guard _root != nil else { return true } return root.forEachWhile(body) } @inlinable public func forEachWhile( from position: Int, in metric: some RopeMetric, _ body: (Element, Element.Index?) -> Bool ) -> Bool { guard _root != nil else { precondition(position == 0, "Position out of bounds") return true } return root.forEachWhile(from: position, in: metric, body) } } extension Rope._Node { @inlinable internal func forEachWhile( _ body: (Element) -> Bool ) -> Bool { if isLeaf { return unsafe readLeaf { let c = unsafe $0.children for i in 0 ..< c.count { guard unsafe body(c[i].value) else { return false } } return true } } return unsafe readInner { let c = unsafe $0.children for i in 0 ..< c.count { guard unsafe c[i].forEachWhile(body) else { return false } } return true } } @inlinable internal func forEachWhile( from position: Int, in metric: some RopeMetric, _ body: (Element, Element.Index?) -> Bool ) -> Bool { if isLeaf { return unsafe readLeaf { let c = unsafe $0.children var (slot, rem) = unsafe $0.findSlot( at: position, in: metric, preferEnd: false) let i = metric.index(at: rem, in: unsafe c[slot].value) if !body(unsafe c[slot].value, i) { return false } slot += 1 while slot < c.count { if !body(unsafe c[slot].value, nil) { return false } slot += 1 } return true } } return unsafe readInner { let c = unsafe $0.children var (slot, rem) = unsafe $0.findSlot(at: position, in: metric, preferEnd: false) if unsafe !c[slot].forEachWhile(from: rem, in: metric, body) { return false } slot += 1 while slot < c.count { if unsafe !c[slot].forEachWhile({ body($0, nil) }) { return false } slot += 1 } return true } } }