General purpose, fast diff algorithm supporting [m] level nested diffs.
- Most diffing algorithm are O(nlogn) or O(n.m)
- Most algorithm solve Least Common Subsequence problem which is hard to grasp at first
- Supports nested diffing as per requirement.
pod 'FastDiff'
And then in the terminal pod update
. If you are new to cocoapods please check out Cocoapods Installation
Declare the dependency in the swift Package.swift
file like such:
dependencies: [
///.... other deps
.package(url: "https://www.github.com/kandelvijaya/FastDiff", from: "1.0.0"),
]
Execute the update command swift package update
and then swift package generate-xcodeproj
.
Go to the source directory, and run:
$ swift test
let oldModels = ["apple", "microsoft"]
let newModels = ["apple", "microsoft", "tesla"]
/// Algorithm
let changeSet = diff(oldModels, newModels)
// [.addition("tesla", at: 2)]
/// Verification
oldModels.merged(with: changeSet) == newModels
Note that diff
produces changeset that can't be merged into old collections as is, most of the times.
The changeset has to be ordered
in-order for successful merge. This is also useful if you want to
apply changeset to UITableView
or UICollectionView
.
let chnageSet = diff(["A","B"], [“C”,"D"])
// [.delete("A",0), .delete("B",1), .add("C",0), .add(“D",1)]
let orderedChangeSet = orderedOperation(from: changeSet)
// [.delete("b",1), .delete("a",0), .add("c",0), .add("d",1)]
Please check out this presentation slides that I gave at @mobiconf 2018
Feel free to contribute with Pull Requests. There are many tasks (trivial) left to do.
- Cocoapods support
- Documentation grammar correction
- Adding CI and build status
At the moment, solely me:
- @kandelvijaya (https://twitter.com/kandelvijaya)
This project is licensed under the MIT License - see the LICENSE.md file for details
- Inspired by Paul Heckel's paper & algorithm