HeckelDiff is a powerful library that provides a straightforward and efficient algorithm for computing differences between two sorted lists. It is widely used in various scenarios such as revision control systems, data synchronization, and text comparison utilities. This documentation will guide you through the installation process, API usage, and provide examples to help you get started with HeckelDiff.
Installation
To use HeckelDiff in your project, follow these steps:
- Make sure you have a compatible version of Swift (Xcode 10 or later) installed.
- Add HeckelDiff to your project using one of the following methods:
- Using CocoaPods:
- Open your project’s Podfile and add:
- Run
pod install
in your project’s root directory. - Import the HeckelDiff module in your code files.
- You are now ready to use HeckelDiff in your project!
- Manually:
- Download the latest release from the HeckelDiff GitHub repository.
- Add the HeckelDiff source files to your Xcode project.
- Make sure the files are included in your target’s “Compile Sources” build phase.
- Import the HeckelDiff module in your code files.
- You are now ready to use HeckelDiff in your project!
pod 'HeckelDiff', '~> 1.0'
API Reference
Diff Class
The Diff class is the main interface for computing differences between two lists.
Properties:
oldList: [T]
– The previous list to compare.newList: [T]
– The current list to compare.diffs: [DiffElement
– The computed differences between the lists.]
Methods:
init(oldList: [T], newList: [T])
– Initializes a new Diff instance with the given lists.compute()
– Computes the differences between the lists.patch()
– Applies the computed differences to the old list, generating the new list.
DiffElement Structure
The DiffElement structure represents an atomic change between two elements in the list, such as an insertion, deletion, or no change.
Properties:
oldIndex: Int
– The index of the element in the old list.newIndex: Int
– The index of the element in the new list.element: T?
– The element itself, if it exists (nil for deletions).type: DiffType
– The type of change (insertion, deletion, or no change).
Enum: DiffType
insertion: DiffType
– An insertion of an element.deletion: DiffType
– A deletion of an element.noChange: DiffType
– No change between the elements.
Example Usage
Here is a basic example that demonstrates how to use HeckelDiff:
// Create two lists
let oldList = ["apple", "orange", "banana", "kiwi"]
let newList = ["orange", "strawberry", "banana", "kiwi", "mango"]
// Instantiate a Diff object
let diff = Diff(oldList: oldList, newList: newList)
// Compute the differences
diff.compute()
// Apply the changes to the old list
diff.patch()
// Print the computed differences
for diffElement in diff.diffs {
print(diffElement)
}