Skip to content

Commit

Permalink
Merge pull request #55 from alanzeino/xcode102
Browse files Browse the repository at this point in the history
Implement changes for Swift 4.2 and 5.0 with Xcode 10.2
  • Loading branch information
ra1028 authored Mar 27, 2019
2 parents 302f518 + 36203d4 commit 69f8cf4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
27 changes: 18 additions & 9 deletions Sources/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
///
/// - Complexity: O(n)
@inlinable
public init(source: Collection, target: Collection) {
init(source: Collection, target: Collection) {
self.init(source: source, target: target, section: 0)
}

Expand All @@ -44,7 +44,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
///
/// - Complexity: O(n)
@inlinable
public init(source: Collection, target: Collection, section: Int) {
init(source: Collection, target: Collection, section: Int) {
let sourceElements = ContiguousArray(source)
let targetElements = ContiguousArray(target)

Expand Down Expand Up @@ -151,7 +151,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
///
/// - Complexity: O(n)
@inlinable
public init(source: Collection, target: Collection) {
init(source: Collection, target: Collection) {
typealias Section = Collection.Element
typealias SectionIdentifier = Collection.Element.DifferenceIdentifier
typealias Element = Collection.Element.Collection.Element
Expand Down Expand Up @@ -327,7 +327,11 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C

for targetElementIndex in targetElements.indices {
untrackedSourceIndex = untrackedSourceIndex.flatMap { index in
sourceElementTraces[sourceSectionIndex].suffix(from: index).index { !$0.isTracked }
#if swift(>=5.0)
return sourceElementTraces[sourceSectionIndex].suffix(from: index).firstIndex { !$0.isTracked }
#else
return sourceElementTraces[sourceSectionIndex].suffix(from: index).index { !$0.isTracked }
#endif
}

let targetElementPath = ElementPath(element: targetElementIndex, section: targetSectionIndex)
Expand Down Expand Up @@ -537,7 +541,11 @@ internal func differentiate<E: Differentiable, I>(
// Record the updates/moves/insertions.
for targetIndex in target.indices {
untrackedSourceIndex = untrackedSourceIndex.flatMap { index in
sourceTraces.suffix(from: index).index { !$0.isTracked }
#if swift(>=5.0)
return sourceTraces.suffix(from: index).firstIndex { !$0.isTracked }
#else
return sourceTraces.suffix(from: index).index { !$0.isTracked }
#endif
}

if let sourceIndex = targetReferences[targetIndex] {
Expand Down Expand Up @@ -653,14 +661,11 @@ internal final class IndicesReference {
/// Dictionary key using UnsafePointer for performance optimization.
@usableFromInline
internal struct TableKey<T: Hashable>: Hashable {
@usableFromInline
internal let hashValue: Int
@usableFromInline
internal let pointer: UnsafePointer<T>

@inlinable
internal init(pointer: UnsafePointer<T>) {
self.hashValue = pointer.pointee.hashValue
self.pointer = pointer
}

Expand All @@ -669,11 +674,15 @@ internal struct TableKey<T: Hashable>: Hashable {
return lhs.hashValue == rhs.hashValue
&& (lhs.pointer.distance(to: rhs.pointer) == 0 || lhs.pointer.pointee == rhs.pointer.pointee)
}

public func hash(into hasher: inout Hasher) {
hasher.combine(pointer.pointee)
}
}

internal extension MutableCollection where Element: MutableCollection, Index == Int, Element.Index == Int {
@inlinable
internal subscript(path: ElementPath) -> Element.Element {
subscript(path: ElementPath) -> Element.Element {
get { return self[path.section][path.element] }
set { self[path.section][path.element] = newValue }
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Changeset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public struct Changeset<Collection: Swift.Collection> {
public extension Changeset {
/// The number of section changes.
@inlinable
public var sectionChangeCount: Int {
var sectionChangeCount: Int {
return sectionDeleted.count
+ sectionInserted.count
+ sectionUpdated.count
Expand All @@ -74,7 +74,7 @@ public extension Changeset {

/// The number of element changes.
@inlinable
public var elementChangeCount: Int {
var elementChangeCount: Int {
return elementDeleted.count
+ elementInserted.count
+ elementUpdated.count
Expand All @@ -83,25 +83,25 @@ public extension Changeset {

/// The number of all changes.
@inlinable
public var changeCount: Int {
var changeCount: Int {
return sectionChangeCount + elementChangeCount
}

/// A Boolean value indicating whether has section changes.
@inlinable
public var hasSectionChanges: Bool {
var hasSectionChanges: Bool {
return sectionChangeCount > 0
}

/// A Boolean value indicating whether has element changes.
@inlinable
public var hasElementChanges: Bool {
var hasElementChanges: Bool {
return elementChangeCount > 0
}

/// A Boolean value indicating whether has changes.
@inlinable
public var hasChanges: Bool {
var hasChanges: Bool {
return changeCount > 0
}
}
Expand Down
12 changes: 12 additions & 0 deletions Sources/Extensions/AppKitExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@ public extension NSTableView {
/// updates should be stopped and performed reloadData. Default is nil.
/// - setData: A closure that takes the collection as a parameter.
/// The collection should be set to data-source of NSTableView.

func reload<C>(
using stagedChangeset: StagedChangeset<C>,
with animation: @autoclosure () -> NSTableView.AnimationOptions,
interrupt: ((Changeset<C>) -> Bool)? = nil,
setData: (C) -> Void
) {
#if swift(>=5.0)
reload(
using: stagedChangeset,
deleteRowsAnimation: animation(),
insertRowsAnimation: animation(),
reloadRowsAnimation: animation(),
interrupt: interrupt,
setData: setData
)
#else
reload(
using: stagedChangeset,
deleteRowsAnimation: animation,
Expand All @@ -29,6 +40,7 @@ public extension NSTableView {
interrupt: interrupt,
setData: setData
)
#endif
}

/// Applies multiple animated updates in stages using `StagedChangeset`.
Expand Down
14 changes: 14 additions & 0 deletions Sources/Extensions/UIKitExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ public extension UITableView {
interrupt: ((Changeset<C>) -> Bool)? = nil,
setData: (C) -> Void
) {
#if swift(>=5.0)
reload(
using: stagedChangeset,
deleteSectionsAnimation: animation(),
insertSectionsAnimation: animation(),
reloadSectionsAnimation: animation(),
deleteRowsAnimation: animation(),
insertRowsAnimation: animation(),
reloadRowsAnimation: animation(),
interrupt: interrupt,
setData: setData
)
#else
reload(
using: stagedChangeset,
deleteSectionsAnimation: animation,
Expand All @@ -32,6 +45,7 @@ public extension UITableView {
interrupt: interrupt,
setData: setData
)
#endif
}

/// Applies multiple animated updates in stages using `StagedChangeset`.
Expand Down
6 changes: 5 additions & 1 deletion Sources/StagedChangeset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public struct StagedChangeset<Collection: Swift.Collection> {
}
}

extension StagedChangeset: RandomAccessCollection, RangeReplaceableCollection, MutableCollection {
extension StagedChangeset: RandomAccessCollection, RangeReplaceableCollection {
@inlinable
public init() {
self.init([])
Expand Down Expand Up @@ -76,6 +76,10 @@ extension StagedChangeset: RandomAccessCollection, RangeReplaceableCollection, M
}
}

#if !compiler(>=5.0) && compiler(>=4.0)
extension StagedChangeset: MutableCollection {}
#endif

extension StagedChangeset: Equatable where Collection: Equatable {
@inlinable
public static func == (lhs: StagedChangeset, rhs: StagedChangeset) -> Bool {
Expand Down

0 comments on commit 69f8cf4

Please sign in to comment.