Skip to content

Commit

Permalink
Include element updates in changet stage 4
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1028 committed Aug 22, 2018
1 parent 6afba13 commit 2a4d0e9
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions Sources/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
// If the element target section is recorded as insertion, record its element path as deletion.
if let targetElementPath = sourceElementTraces[sourceElementPath].reference,
case .some = sectionResult.metadata.targetReferences[targetElementPath.section] {
let targetElement = contiguousTargetSections[targetElementPath]
firstStageElements.append(targetElement)
let sourceElement = contiguousSourceSections[sourceElementPath]
firstStageElements.append(sourceElement)
continue
}

Expand All @@ -263,8 +263,8 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
offsetByDelete += 1
}

let section = Section(source: sourceSections[sourceSectionIndex], elements: firstStageElements)
firstStageSections.append(section)
let firstStageSection = Section(source: sourceSections[sourceSectionIndex], elements: firstStageElements)
firstStageSections.append(firstStageSection)
}

// Record the element updates/moves/insertions.
Expand All @@ -284,30 +284,32 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
let secondStageSection = firstStageSections[sourceSectionIndex - sectionDeleteOffset]
secondStageSections.append(secondStageSection)

let thirdStageSection = Section(source: secondStageSection, elements: targetElements)
thirdStageSections.append(thirdStageSection)
var thirdStageElements = ContiguousArray<Element>()
thirdStageElements.reserveCapacity(targetElements.count)

for targetElementIndex in targetElements.indices {
untrackedSourceIndex = untrackedSourceIndex.flatMap { index in
sourceElementTraces[sourceSectionIndex].suffix(from: index).index { !$0.isTracked }
}

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.
guard let sourceElementPath = targetElementReferences[targetElementPath],
let movedSourceSectionIndex = sectionResult.metadata.sourceTraces[sourceElementPath.section].reference else {
thirdStageElements.append(targetElement)
elementInserted.append(targetElementPath)
continue
}

sourceElementTraces[sourceElementPath].isTracked = true

let sourceElement = contiguousSourceSections[sourceElementPath]
let targetElement = contiguousTargetSections[targetElementPath]
thirdStageElements.append(sourceElement)

if !targetElement.isContentEqual(to: sourceElement) {
elementUpdated.append(sourceElementPath)
elementUpdated.append(targetElementPath)
}

if sourceElementPath.section != sourceSectionIndex || sourceElementPath.element != untrackedSourceIndex {
Expand All @@ -316,19 +318,21 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
elementMoved.append((source: moveSourceElementPath, target: targetElementPath))
}
}

let thirdStageSection = Section(source: secondStageSection, elements: thirdStageElements)
thirdStageSections.append(thirdStageSection)
}

var changesets = ContiguousArray<Changeset<Collection>>()

// The 1st stage changeset includes only the section deletions and element deletions, element updates
// and the collection that its changes applied to source collection.
if !sectionResult.deleted.isEmpty || !elementDeleted.isEmpty || !elementUpdated.isEmpty {
// The 1st stage changeset includes only the section deletions and element deletions and the collection
// that its changes applied to source collection.
if !sectionResult.deleted.isEmpty || !elementDeleted.isEmpty {
changesets.append(
Changeset(
data: Collection(firstStageSections),
sectionDeleted: sectionResult.deleted,
elementDeleted: elementDeleted,
elementUpdated: elementUpdated
elementDeleted: elementDeleted
)
)
}
Expand Down Expand Up @@ -358,11 +362,12 @@ public extension StagedChangeset where Collection: RangeReplaceableCollection, C
}

// The 4th stage changeset includes only the section updates and the collection that its changes applied to 3rd stage collection.
if !sectionResult.updated.isEmpty {
if !sectionResult.updated.isEmpty || !elementUpdated.isEmpty {
changesets.append(
Changeset(
data: target,
sectionUpdated: sectionResult.updated
sectionUpdated: sectionResult.updated,
elementUpdated: elementUpdated
)
)
}
Expand Down

0 comments on commit 2a4d0e9

Please sign in to comment.