FastDiff icon indicating copy to clipboard operation
FastDiff copied to clipboard

Add a function that allows subsequent add/delete to be grouped as updates.

Open kandelvijaya opened this issue 6 years ago • 0 comments

Something like this.

-- Make sure to add unit tests. -- On OrderedDiff, Delete and Inserts are not nearby besides the last one.

fileprivate func packingConsequetiveDeleteAddWithUpdate<T>(from diffResult:  [DiffOperation<T>.Simple]) -> [DiffOperation<T>.Simple] {
    if diffResult.isEmpty { return [] }

    var currentSeekIndex = 0 // This is the index that is not processed.
    
    var accumulator: [DiffOperation<T>.Simple] = []
    while currentSeekIndex < diffResult.count {
        let thisItem = diffResult[currentSeekIndex]
        let nextIndex = currentSeekIndex.advanced(by: 1)

        if nextIndex < diffResult.count {
            let nextItem = diffResult[nextIndex]
            switch (thisItem, nextItem) {
            case let (.delete(di, dIndex), .add(ai, aIndex)) where dIndex == aIndex:
                let update = DiffOperation<T>.Simple.update(di, ai, dIndex)
                accumulator.append(update)
            default:
                accumulator.append(thisItem)
                accumulator.append(nextItem)

kandelvijaya avatar Sep 12 '19 23:09 kandelvijaya