Skip to content

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

Open
@kandelvijaya

Description

@kandelvijaya

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)

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions