Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1028 committed May 7, 2019
1 parent 7a98512 commit 617edde
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions Sources/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
return
}

// Returns the changesets that all deletions if source is not empty and target is empty
// Returns the changesets that all deletions if source is not empty and target is empty.
if !sourceElements.isEmpty && targetElements.isEmpty {
self.init([Changeset(data: target, elementDeleted: sourceElements.indices.map { ElementPath(element: $0, section: section) })])
return
}

// Returns the changesets that all insertions if source is empty and target is not empty
// Returns the changesets that all insertions if source is empty and target is not empty.
if sourceElements.isEmpty && !targetElements.isEmpty {
self.init([Changeset(data: target, elementInserted: targetElements.indices.map { ElementPath(element: $0, section: section) })])
return
Expand Down Expand Up @@ -187,7 +187,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
flattenSourceIdentifiers.reserveCapacity(flattenSourceCount)
flattenSourceElementPaths.reserveCapacity(flattenSourceCount)

// Calculate the section differences.
// Calculate section differences.

let sectionResult = differentiate(
source: sourceSections,
Expand All @@ -196,7 +196,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
mapIndex: { $0 }
)

// Calculate the element differences.
// Calculate element differences.

var elementDeleted = [ElementPath]()
var elementInserted = [ElementPath]()
Expand All @@ -214,9 +214,9 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C

flattenSourceIdentifiers.withUnsafeBufferPointer { bufferPointer in
// The pointer and the table key are for optimization.
var sourceOccurrencesTable = [TableKey<ElementIdentifier>: Occurrence](minimumCapacity: flattenSourceCount * 2)
var sourceOccurrencesTable = [TableKey<ElementIdentifier>: Occurrence](minimumCapacity: flattenSourceCount)

// Record the index where the element was found in flatten source collection into occurrences table.
// Track indices of elements found in flatten source collection into occurrences table.
for flattenSourceIndex in flattenSourceIdentifiers.indices {
let pointer = bufferPointer.baseAddress!.advanced(by: flattenSourceIndex)
let key = TableKey(pointer: pointer)
Expand All @@ -234,7 +234,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
}
}

// Record the target index and the source index that the element having the same identifier.
// Track an target and source indices of the elements having same identifier.
for targetSectionIndex in contiguousTargetSections.indices {
let targetElements = contiguousTargetSections[targetSectionIndex]

Expand Down Expand Up @@ -267,14 +267,14 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
}
}

// Record the element deletions.
// Track element deletes.
for sourceSectionIndex in contiguousSourceSections.indices {
let sourceSection = sourceSections[sourceSectionIndex]
let sourceElements = contiguousSourceSections[sourceSectionIndex]
var firstStageElements = sourceElements

// Should not calculate the element deletions in the deleted section.
if case .some = sectionResult.metadata.sourceTraces[sourceSectionIndex].reference {
// Should not track element deletes in the deleted section.
if case .some = sectionResult.sourceTraces[sourceSectionIndex].reference {
var offsetByDelete = 0

var secondStageElements = ContiguousArray<Element>()
Expand All @@ -284,9 +284,9 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C

sourceElementTraces[sourceElementPath].deleteOffset = offsetByDelete

// If the element target section is recorded as insertion, record its element path as deletion.
// Track element deletes if target section is tracked as inserts.
if let targetElementPath = sourceElementTraces[sourceElementPath].reference,
case .some = sectionResult.metadata.targetReferences[targetElementPath.section] {
case .some = sectionResult.targetReferences[targetElementPath.section] {
let targetElement = contiguousTargetSections[targetElementPath]
firstStageElements[sourceElementIndex] = targetElement
secondStageElements.append(targetElement)
Expand All @@ -307,10 +307,10 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
firstStageSections[sourceSectionIndex] = firstStageSection
}

// Record the element updates/moves/insertions.
// Track element updates / moves / inserts.
for targetSectionIndex in contiguousTargetSections.indices {
// Should not calculate the element updates/moves/insertions in the inserted section.
guard let sourceSectionIndex = sectionResult.metadata.targetReferences[targetSectionIndex] else {
// Should not track element updates / moves / inserts in the inserted section.
guard let sourceSectionIndex = sectionResult.targetReferences[targetSectionIndex] else {
thirdStageSections.append(targetSections[targetSectionIndex])
fourthStageSections.append(targetSections[targetSectionIndex])
continue
Expand All @@ -319,7 +319,7 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
var untrackedSourceIndex: Int? = 0
let targetElements = contiguousTargetSections[targetSectionIndex]

let sectionDeleteOffset = sectionResult.metadata.sourceTraces[sourceSectionIndex].deleteOffset
let sectionDeleteOffset = sectionResult.sourceTraces[sourceSectionIndex].deleteOffset

let thirdStageSection = secondStageSections[sourceSectionIndex - sectionDeleteOffset]
thirdStageSections.append(thirdStageSection)
Expand All @@ -335,9 +335,9 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
let targetElementPath = ElementPath(element: targetElementIndex, section: targetSectionIndex)
let targetElement = contiguousTargetSections[targetElementPath]

// If the element source section is recorded as deletion, record its element path as insertion.
// Track element inserts if source section is tracked as deletes.
guard let sourceElementPath = targetElementReferences[targetElementPath],
let movedSourceSectionIndex = sectionResult.metadata.sourceTraces[sourceElementPath.section].reference else {
let movedSourceSectionIndex = sectionResult.sourceTraces[sourceElementPath.section].reference else {
fourthStageElements.append(targetElement)
elementInserted.append(targetElementPath)
continue
Expand Down Expand Up @@ -471,9 +471,9 @@ internal func differentiate<E: Differentiable, I>(

sourceIdentifiers.withUnsafeBufferPointer { bufferPointer in
// The pointer and the table key are for optimization.
var sourceOccurrencesTable = [TableKey<E.DifferenceIdentifier>: Occurrence](minimumCapacity: source.count * 2)
var sourceOccurrencesTable = [TableKey<E.DifferenceIdentifier>: Occurrence](minimumCapacity: source.count)

// Record the index where the element was found in source collection into occurrences table.
// Track indices of elements found in source collection into occurrences table.
for sourceIndex in sourceIdentifiers.indices {
let pointer = bufferPointer.baseAddress!.advanced(by: sourceIndex)
let key = TableKey(pointer: pointer)
Expand All @@ -491,7 +491,7 @@ internal func differentiate<E: Differentiable, I>(
}
}

// Record the target index and the source index that the element having the same identifier.
// Track an target and source indices of the elements having same identifier.
for targetIndex in target.indices {
var targetIdentifier = target[targetIndex].differenceIdentifier
let key = TableKey(pointer: &targetIdentifier)
Expand All @@ -518,7 +518,7 @@ internal func differentiate<E: Differentiable, I>(
var offsetByDelete = 0
var untrackedSourceIndex: Int? = 0

// Record the deletions.
// Track deletes.
for sourceIndex in source.indices {
sourceTraces[sourceIndex].deleteOffset = offsetByDelete

Expand All @@ -536,7 +536,7 @@ internal func differentiate<E: Differentiable, I>(
}
}

// Record the updates/moves/insertions.
// Track updates / moves / inserts.
for targetIndex in target.indices {
untrackedSourceIndex = untrackedSourceIndex.flatMap { index in
sourceTraces.suffix(from: index).firstIndex { !$0.isTracked }
Expand Down Expand Up @@ -567,15 +567,14 @@ internal func differentiate<E: Differentiable, I>(
inserted: inserted,
updated: updated,
moved: moved,
metadata: (sourceTraces: sourceTraces, targetReferences: targetReferences)
sourceTraces: sourceTraces,
targetReferences: targetReferences
)
}

/// A set of changes and metadata as a result of calculating differences in linear collection.
@usableFromInline
internal struct DifferentiateResult<Index> {
@usableFromInline
internal typealias Metadata = (sourceTraces: ContiguousArray<Trace<Int>>, targetReferences: ContiguousArray<Int?>)
@usableFromInline
internal let deleted: [Index]
@usableFromInline
Expand All @@ -585,21 +584,25 @@ internal struct DifferentiateResult<Index> {
@usableFromInline
internal let moved: [(source: Index, target: Index)]
@usableFromInline
internal let metadata: Metadata
internal let sourceTraces: ContiguousArray<Trace<Int>>
@usableFromInline
internal let targetReferences: ContiguousArray<Int?>

@inlinable
internal init(
deleted: [Index] = [],
inserted: [Index] = [],
updated: [Index] = [],
moved: [(source: Index, target: Index)] = [],
metadata: Metadata
sourceTraces: ContiguousArray<Trace<Int>>,
targetReferences: ContiguousArray<Int?>
) {
self.deleted = deleted
self.inserted = inserted
self.updated = updated
self.moved = moved
self.metadata = metadata
self.sourceTraces = sourceTraces
self.targetReferences = targetReferences
}
}

Expand Down

0 comments on commit 617edde

Please sign in to comment.