Skip to content

Instantly share code, notes, and snippets.

@danielt1263
Last active September 13, 2018 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielt1263/df9e22c0ee89203d7bbf to your computer and use it in GitHub Desktop.
Save danielt1263/df9e22c0ee89203d7bbf to your computer and use it in GitHub Desktop.
//
// ArrayCompare.swift
//
// Created by Daniel Tartaglia on Sep 10, 2015.
// Inspired by http://www.swift-studies.com/blog/2015/5/15/swift-coding-challenge-incremental-updates-to-views
//
public func findDifferences<T : Hashable>(old: [T], new:[T], moveBlock: (_ from: Int, _ to: Int)->()) -> (insertions: [Int], removals: [Int])
{
var insertionIndexs = [Int]()
var removalIndexs = [Int]()
var newPositions = [T : Int]()
for index in 0 ..< new.count {
newPositions[new[index]]=index
}
for oldPosition in 0 ..< old.count {
let item = old[oldPosition]
if let newPosition = newPositions[item] {
if oldPosition != newPosition{
moveBlock(oldPosition, newPosition)
}
newPositions.removeValue(forKey: item)
} else {
removalIndexs.append(oldPosition)
}
}
for (_, position) in newPositions {
insertionIndexs.append(position)
}
return (insertions: insertionIndexs, removals: removalIndexs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment