Skip to content

Commit

Permalink
Run swift-format
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis authored and actions-user committed Feb 14, 2022
1 parent 5b23c62 commit e9205f1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 75 deletions.
98 changes: 48 additions & 50 deletions Sources/CustomDump/Internal/CollectionDifference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,27 @@ struct CollectionDifference<ChangeElement> {

// Internal common field accessors
internal var _offset: Int {
get {
switch self {
case .insert(offset: let o, element: _, associatedWith: _):
return o
case .remove(offset: let o, element: _, associatedWith: _):
return o
}
switch self {
case .insert(offset: let o, element: _, associatedWith: _):
return o
case .remove(offset: let o, element: _, associatedWith: _):
return o
}
}
internal var _element: ChangeElement {
get {
switch self {
case .insert(offset: _, element: let e, associatedWith: _):
return e
case .remove(offset: _, element: let e, associatedWith: _):
return e
}
switch self {
case .insert(offset: _, element: let e, associatedWith: _):
return e
case .remove(offset: _, element: let e, associatedWith: _):
return e
}
}
internal var _associatedOffset: Int? {
get {
switch self {
case .insert(offset: _, element: _, associatedWith: let o):
return o
case .remove(offset: _, element: _, associatedWith: let o):
return o
}
switch self {
case .insert(offset: _, element: _, associatedWith: let o):
return o
case .remove(offset: _, element: _, associatedWith: let o):
return o
}
}
}
Expand Down Expand Up @@ -162,12 +156,12 @@ struct CollectionDifference<ChangeElement> {
///
/// Complexity: O(`changes.count`)
private static func _validateChanges<Changes: Collection>(
_ changes : Changes
_ changes: Changes
) -> Bool where Changes.Element == Change {
if changes.isEmpty { return true }

var insertAssocToOffset = Dictionary<Int,Int>()
var removeOffsetToAssoc = Dictionary<Int,Int>()
var insertAssocToOffset = [Int: Int]()
var removeOffsetToAssoc = [Int: Int]()
var insertOffset = Set<Int>()
var removeOffset = Set<Int>()

Expand Down Expand Up @@ -201,14 +195,15 @@ struct CollectionDifference<ChangeElement> {
}

func inverse() -> Self {
return CollectionDifference(_validatedChanges: self.map { c in
switch c {
return CollectionDifference(
_validatedChanges: self.map { c in
switch c {
case .remove(let o, let e, let a):
return .insert(offset: o, element: e, associatedWith: a)
case .insert(let o, let e, let a):
return .remove(offset: o, element: e, associatedWith: a)
}
})
}
})
}
}

Expand Down Expand Up @@ -321,8 +316,9 @@ extension CollectionDifference where ChangeElement: Hashable {
///
/// - Complexity: O(*n*) where *n* is the number of collection differences.
func inferringMoves() -> CollectionDifference<ChangeElement> {
let uniqueRemovals: [ChangeElement:Int?] = {
var result = [ChangeElement:Int?](minimumCapacity: Swift.min(removals.count, insertions.count))
let uniqueRemovals: [ChangeElement: Int?] = {
var result = [ChangeElement: Int?](
minimumCapacity: Swift.min(removals.count, insertions.count))
for removal in removals {
let element = removal._element
if result[element] != .none {
Expand All @@ -334,8 +330,9 @@ extension CollectionDifference where ChangeElement: Hashable {
return result.filter { (_, v) -> Bool in v != .none }
}()

let uniqueInsertions: [ChangeElement:Int?] = {
var result = [ChangeElement:Int?](minimumCapacity: Swift.min(removals.count, insertions.count))
let uniqueInsertions: [ChangeElement: Int?] = {
var result = [ChangeElement: Int?](
minimumCapacity: Swift.min(removals.count, insertions.count))
for insertion in insertions {
let element = insertion._element
if result[element] != .none {
Expand All @@ -347,25 +344,26 @@ extension CollectionDifference where ChangeElement: Hashable {
return result.filter { (_, v) -> Bool in v != .none }
}()

return CollectionDifference(_validatedChanges: map({ (change: Change) -> Change in
switch change {
case .remove(offset: let offset, element: let element, associatedWith: _):
if uniqueRemovals[element] == nil {
return change
}
if let assoc = uniqueInsertions[element] {
return .remove(offset: offset, element: element, associatedWith: assoc)
}
case .insert(offset: let offset, element: let element, associatedWith: _):
if uniqueInsertions[element] == nil {
return change
}
if let assoc = uniqueRemovals[element] {
return .insert(offset: offset, element: element, associatedWith: assoc)
return CollectionDifference(
_validatedChanges: map({ (change: Change) -> Change in
switch change {
case .remove(let offset, let element, associatedWith: _):
if uniqueRemovals[element] == nil {
return change
}
if let assoc = uniqueInsertions[element] {
return .remove(offset: offset, element: element, associatedWith: assoc)
}
case .insert(let offset, let element, associatedWith: _):
if uniqueInsertions[element] == nil {
return change
}
if let assoc = uniqueRemovals[element] {
return .insert(offset: offset, element: element, associatedWith: assoc)
}
}
}
return change
}))
return change
}))
}
}

Expand Down
52 changes: 27 additions & 25 deletions Sources/CustomDump/Internal/Diffing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extension CollectionDifference {
}

// Error type allows the use of throw to unroll state on application failure
private enum _ApplicationError : Error { case failed }
private enum _ApplicationError: Error { case failed }

extension RangeReplaceableCollection {
/// Applies the given difference to this collection.
Expand Down Expand Up @@ -88,17 +88,17 @@ extension RangeReplaceableCollection {
var currentIndex = self.startIndex
try difference._fastEnumeratedApply { change in
switch change {
case .remove(offset: let offset, element: _, associatedWith: _):
case .remove(let offset, element: _, associatedWith: _):
let origCount = offset - enumeratedOriginals
try append(into: &result, contentsOf: self, from: &currentIndex, count: origCount)
if currentIndex == self.endIndex {
// Removing nonexistent element off the end of the collection
throw _ApplicationError.failed
}
enumeratedOriginals += origCount + 1 // Removal consumes an original element
enumeratedOriginals += origCount + 1 // Removal consumes an original element
currentIndex = self.index(after: currentIndex)
enumeratedRemoves += 1
case .insert(offset: let offset, element: let element, associatedWith: _):
case .insert(let offset, let element, associatedWith: _):
let origCount = (offset + enumeratedRemoves - enumeratedInserts) - enumeratedOriginals
try append(into: &result, contentsOf: self, from: &currentIndex, count: origCount)
result.append(element)
Expand Down Expand Up @@ -185,15 +185,15 @@ extension BidirectionalCollection where Element: Equatable {
private struct _V {

private var a: [Int]
#if INTERNAL_CHECKS_ENABLED
private let isOdd: Bool
#endif
#if INTERNAL_CHECKS_ENABLED
private let isOdd: Bool
#endif

init(maxIndex largest: Int) {
#if INTERNAL_CHECKS_ENABLED
_internalInvariant(largest >= 0)
isOdd = largest % 2 == 1
#endif
#if INTERNAL_CHECKS_ENABLED
_internalInvariant(largest >= 0)
isOdd = largest % 2 == 1
#endif
a = [Int](repeating: 0, count: largest + 1)
}

Expand All @@ -206,33 +206,35 @@ private struct _V {

subscript(index: Int) -> Int {
get {
#if INTERNAL_CHECKS_ENABLED
_internalInvariant(isOdd == (index % 2 != 0))
#endif
#if INTERNAL_CHECKS_ENABLED
_internalInvariant(isOdd == (index % 2 != 0))
#endif
return a[_V.transform(index)]
}
set(newValue) {
#if INTERNAL_CHECKS_ENABLED
_internalInvariant(isOdd == (index % 2 != 0))
#endif
#if INTERNAL_CHECKS_ENABLED
_internalInvariant(isOdd == (index % 2 != 0))
#endif
a[_V.transform(index)] = newValue
}
}
}

private func _myers<C,D>(
private func _myers<C, D>(
from old: C, to new: D,
using cmp: (C.Element, D.Element) -> Bool
) -> CollectionDifference<C.Element>
where
C: BidirectionalCollection,
D: BidirectionalCollection,
C.Element == D.Element
where
C: BidirectionalCollection,
D: BidirectionalCollection,
C.Element == D.Element
{

// Core implementation of the algorithm described at http://www.xmailserver.org/diff2.pdf
// Variable names match those used in the paper as closely as possible
func _descent(from a: UnsafeBufferPointer<C.Element>, to b: UnsafeBufferPointer<D.Element>) -> [_V] {
func _descent(from a: UnsafeBufferPointer<C.Element>, to b: UnsafeBufferPointer<D.Element>)
-> [_V]
{
let n = a.count
let m = b.count
let max = n + m
Expand Down Expand Up @@ -271,7 +273,7 @@ private func _myers<C,D>(

while x < n && y < m {
if !cmp(a[x], b[y]) {
break;
break
}
x &+= 1
y &+= 1
Expand Down Expand Up @@ -355,7 +357,7 @@ private func _myers<C,D>(

return _withContiguousStorage(for: old) { a in
return _withContiguousStorage(for: new) { b in
return CollectionDifference(_formChanges(from: a, to: b, using:_descent(from: a, to: b)))!
return CollectionDifference(_formChanges(from: a, to: b, using: _descent(from: a, to: b)))!
}
}
}

0 comments on commit e9205f1

Please sign in to comment.