Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement changes for Swift 4.2 and 5.0 with Xcode 10.2 #55

Merged
merged 5 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
alanzeino marked this conversation as resolved.
Show resolved Hide resolved
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)
alanzeino marked this conversation as resolved.
Show resolved Hide resolved
}
}

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