-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
46 changed files
with
422 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ use_frameworks! | |
|
||
target 'SampleApp' do | ||
|
||
pod 'TableViewKit', :path => '..' | ||
pod 'TableViewKit', :path => '../..' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// | ||
// Diff.swift | ||
// TableViewKit | ||
// | ||
// Created by Alfredo Delli Bovi on 10/09/2016. | ||
// Copyright © 2016 odigeo. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Diff { | ||
public var inserts: [Int] | ||
public var deletes: [Int] | ||
} | ||
|
||
|
||
class DiffIterator : GeneratorType { | ||
struct Coordinates { | ||
var x: Int | ||
var y: Int | ||
} | ||
var last: Coordinates | ||
private let matrix: [[Int]] | ||
|
||
init(matrix: [[Int]]){ | ||
self.matrix = matrix | ||
self.last = Coordinates(x: matrix.count-1, y: matrix.first!.count-1) | ||
} | ||
|
||
func next() -> ArrayChanges? { | ||
while(last.x > 0 || last.y > 0) { | ||
if last.x == 0 { | ||
last.y -= 1 | ||
return .inserts([last.y]) | ||
} else if last.y == 0 { | ||
last.x -= 1 | ||
return .deletes([last.x]) | ||
} else if matrix[last.x][last.y] == matrix[last.x][last.y - 1] { | ||
last.y -= 1 | ||
return .inserts([last.y]) | ||
} else if matrix[last.x][last.y] == matrix[last.x - 1][last.y] { | ||
last.x -= 1 | ||
return .deletes([last.x]) | ||
} else { | ||
last.x -= 1 | ||
last.y -= 1 | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
class DiffSequence : SequenceType { | ||
private let matrix: [[Int]] | ||
|
||
init(matrix: [[Int]]){ | ||
self.matrix = matrix | ||
} | ||
|
||
func generate() -> DiffIterator { | ||
return DiffIterator(matrix: matrix) | ||
} | ||
} | ||
|
||
|
||
|
||
extension Array { | ||
|
||
static func diff(between x: [Element], and y: [Element], where predicate: Predicate) -> Diff { | ||
var matrix = [[Int]](count: x.count+1, repeatedValue: [Int](count: y.count+1, repeatedValue: 0)) | ||
for (i, xElem) in x.enumerate() { | ||
for (j, yElem) in y.enumerate() { | ||
if predicate(xElem, yElem) { | ||
matrix[i+1][j+1] = matrix[i][j] + 1 | ||
} else { | ||
matrix[i+1][j+1] = Swift.max(matrix[i][j+1], matrix[i+1][j]) | ||
} | ||
} | ||
} | ||
|
||
let changes = [ArrayChanges](DiffSequence(matrix: matrix)) | ||
let inserts: [Int] = changes.flatMap { change -> [Int] in | ||
guard case .inserts(let array) = change else { return [] } | ||
return array | ||
}.sort { $0 > $1 } | ||
|
||
|
||
let deletes: [Int] = changes.flatMap { change -> [Int] in | ||
guard case .deletes(let array) = change else { return [] } | ||
return array | ||
}.sort { $0 < $1 } | ||
|
||
|
||
return Diff(inserts: inserts, deletes: deletes) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// | ||
// ObservableArray.swift | ||
// TableViewKit | ||
// | ||
// Created by Alfredo Delli Bovi on 09/09/2016. | ||
// | ||
// | ||
|
||
import Foundation | ||
|
||
typealias Predicate = (Any, Any) -> Bool | ||
|
||
enum ArrayChanges { | ||
case inserts([Int]) | ||
case deletes([Int]) | ||
case updates([Int]) | ||
case moves([(Int, Int)]) | ||
case beginUpdates | ||
case endUpdates | ||
} | ||
|
||
public struct ObservableArray<T>: ArrayLiteralConvertible, CollectionType, MutableCollectionType, RangeReplaceableCollectionType { | ||
|
||
public typealias Element = T | ||
|
||
private var _array: [T] { | ||
willSet { | ||
callback?(.beginUpdates) | ||
} | ||
didSet { | ||
let newArray = _array | ||
var oldArray = oldValue | ||
_arraySet(oldArray, newArray: newArray) | ||
callback?(.endUpdates) | ||
} | ||
} | ||
|
||
private func _arraySet(oldArray: [T], newArray: [T]) { | ||
var oldArray = oldArray | ||
let moves = newArray.enumerate().flatMap { (toIndex, element) -> (Int, Int)? in | ||
let anyElement = element as! AnyObject | ||
guard let fromIndex = oldArray.indexOf({ $0 as! AnyObject === anyElement }) where | ||
fromIndex != toIndex else { return nil } | ||
|
||
oldArray.removeAtIndex(fromIndex) | ||
if (toIndex >= oldArray.count) { | ||
oldArray.append(element) | ||
} else { | ||
oldArray.insert(element, atIndex: toIndex) | ||
} | ||
return (fromIndex, toIndex) | ||
} | ||
|
||
let equals: Predicate = { lhs, rhs in | ||
let lhs = lhs as! AnyObject | ||
let rhs = rhs as! AnyObject | ||
return lhs === rhs | ||
} | ||
let diff = Array.diff(between: oldArray, and: newArray, where: equals) | ||
let deletes = diff.deletes | ||
let inserts = diff.inserts | ||
|
||
callback?(.moves(moves)) | ||
callback?(.deletes(deletes)) | ||
callback?(.inserts(inserts)) | ||
} | ||
|
||
var callback: ((ArrayChanges) -> ())? | ||
|
||
public init() { | ||
self._array = [] | ||
} | ||
|
||
public init(array: [T]) { | ||
self._array = array | ||
} | ||
|
||
public init(arrayLiteral elements: Element...) { | ||
self._array = elements | ||
} | ||
|
||
public func generate() -> Array<T>.Generator { | ||
return _array.generate() | ||
} | ||
|
||
public var startIndex: Int { | ||
return _array.startIndex | ||
} | ||
|
||
public var endIndex: Int { | ||
return _array.endIndex | ||
} | ||
|
||
public var isEmpty: Bool { | ||
return _array.isEmpty | ||
} | ||
|
||
public var count: Int { | ||
return _array.count | ||
} | ||
|
||
public subscript(index: Int) -> T { | ||
get { | ||
return _array[index] | ||
} | ||
set { | ||
_array[index] = newValue | ||
} | ||
} | ||
|
||
public mutating func replaceRange<C where C : CollectionType, C.Generator.Element == T>(_ subrange: Range<Int>, with newElements: C) { | ||
_array.replaceRange(subrange, with: newElements) | ||
} | ||
|
||
public mutating func replace(with array: [T]) { | ||
_array = array | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters