 
 //MARK: BTree definition
 
+/// An in-memory b-tree data structure, efficiently mapping `Comparable` keys to arbitrary payloads.
+/// Iterating over the elements in a b-tree returns them in ascending order of their keys.
 public struct BTree<Key: Comparable, Payload> {
 
     /// A sorted array of keys.

    internal var isTooSmall: Bool { return keys.count < minKeys }
    internal var isTooLarge: Bool { return keys.count > maxKeys }
    internal var isBalanced: Bool { return keys.count >= minKeys && keys.count <= maxKeys }
}

//MARK: Lookup

        guard slot.match else { return nil }
        return node.payloads[slot.index]
    }

    public func indexOf(key: Key) -> Int? {
        var node = self
        var index = 0
        while !node.isLeaf {
            let slot = node.slotOf(key)
            index += node.children[0 ..< slot.index].reduce(0, combine: { $0 + $1.count })
            if slot.match {
                return index
            }
            node = node.children[slot.index]
        }
        let slot = node.slotOf(key)
        guard slot.match else { return nil }
        return index + slot.index
    }
}

//MARK: Insertion

extension BTree {
    public mutating func set(key: Key, to payload: Payload) -> Payload? {
        return self.insert(payload, at: key, replacingExisting: true)
    }
    public mutating func insert(payload: Payload, at key: Key) {
        self.insert(payload, at: key, replacingExisting: false)
    }

    private mutating func insert(payload: Payload, at key: Key, replacingExisting replace: Bool) -> Payload? {
        let (old, splinter) = self.insertAndSplit(key, payload, replace: replace)
        guard let (separator, right) = splinter else { return old }
        let left = self
        keys.removeAll()
        payloads.removeAll()
