Skip to content

Commit

Permalink
Merge source code into a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
Frugghi committed Feb 16, 2018
1 parent a42c5cc commit d1b5ca6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ import PackageDescription

let package = Package(
dependencies: [
.Package(url: "https://github.com/Frugghi/SwiftLCS.git", majorVersion: 1, minor: 1)
.Package(url: "https://github.com/Frugghi/SwiftLCS.git", majorVersion: 1, minor: 3)
]
)
```

### Manual
Include `SwiftLCS.swift` and *optionally* `SwiftLCS+Foundation.swift` to your project.
Include `SwiftLCS.swift` into your project.

## :book: Documentation
The API documentation is available [here](http://cocoadocs.org/docsets/SwiftLCS/).
Expand Down
10 changes: 0 additions & 10 deletions SwiftLCS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@
/* Begin PBXBuildFile section */
182E30D21BD4F44A008EC94E /* SwiftLCS.h in Headers */ = {isa = PBXBuildFile; fileRef = 182E30D11BD4F44A008EC94E /* SwiftLCS.h */; settings = {ATTRIBUTES = (Public, ); }; };
182E30DD1BD4F46A008EC94E /* SwiftLCS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DB1BD4F46A008EC94E /* SwiftLCS.swift */; };
182E30DE1BD4F46A008EC94E /* SwiftLCS+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DC1BD4F46A008EC94E /* SwiftLCS+Foundation.swift */; };
182E30E11BD4F502008EC94E /* SwiftLCS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 182E30CF1BD4F44A008EC94E /* SwiftLCS.framework */; };
1886DD141BCEC60900E67CD7 /* StringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1886DD131BCEC60900E67CD7 /* StringTests.swift */; };
1886DD161BCEC7FB00E67CD7 /* ArrayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1886DD151BCEC7FB00E67CD7 /* ArrayTests.swift */; };
1886DD181BCECA3000E67CD7 /* IndexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1886DD171BCECA3000E67CD7 /* IndexTests.swift */; };
18BCD0F61D8B71BA00B6E4B5 /* SwiftLCS.h in Headers */ = {isa = PBXBuildFile; fileRef = 182E30D11BD4F44A008EC94E /* SwiftLCS.h */; settings = {ATTRIBUTES = (Public, ); }; };
18BCD0F71D8B71C600B6E4B5 /* SwiftLCS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DB1BD4F46A008EC94E /* SwiftLCS.swift */; };
18BCD0F81D8B71C600B6E4B5 /* SwiftLCS+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DC1BD4F46A008EC94E /* SwiftLCS+Foundation.swift */; };
18BCD1081D8B73F800B6E4B5 /* SwiftLCS.h in Headers */ = {isa = PBXBuildFile; fileRef = 182E30D11BD4F44A008EC94E /* SwiftLCS.h */; settings = {ATTRIBUTES = (Public, ); }; };
18BCD1091D8B740200B6E4B5 /* SwiftLCS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DB1BD4F46A008EC94E /* SwiftLCS.swift */; };
18BCD10A1D8B740200B6E4B5 /* SwiftLCS+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DC1BD4F46A008EC94E /* SwiftLCS+Foundation.swift */; };
18BCD1191D8B757B00B6E4B5 /* SwiftLCS.h in Headers */ = {isa = PBXBuildFile; fileRef = 182E30D11BD4F44A008EC94E /* SwiftLCS.h */; settings = {ATTRIBUTES = (Public, ); }; };
18BCD11A1D8B758200B6E4B5 /* SwiftLCS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DB1BD4F46A008EC94E /* SwiftLCS.swift */; };
18BCD11B1D8B758400B6E4B5 /* SwiftLCS+Foundation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DC1BD4F46A008EC94E /* SwiftLCS+Foundation.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -42,7 +38,6 @@
182E30D11BD4F44A008EC94E /* SwiftLCS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftLCS.h; sourceTree = "<group>"; };
182E30D31BD4F44A008EC94E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
182E30DB1BD4F46A008EC94E /* SwiftLCS.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftLCS.swift; sourceTree = "<group>"; };
182E30DC1BD4F46A008EC94E /* SwiftLCS+Foundation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SwiftLCS+Foundation.swift"; sourceTree = "<group>"; };
1886DD131BCEC60900E67CD7 /* StringTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringTests.swift; sourceTree = "<group>"; };
1886DD151BCEC7FB00E67CD7 /* ArrayTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArrayTests.swift; sourceTree = "<group>"; };
1886DD171BCECA3000E67CD7 /* IndexTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IndexTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -128,7 +123,6 @@
isa = PBXGroup;
children = (
182E30DB1BD4F46A008EC94E /* SwiftLCS.swift */,
182E30DC1BD4F46A008EC94E /* SwiftLCS+Foundation.swift */,
18BCD10B1D8B740C00B6E4B5 /* Supporting Files */,
);
path = SwiftLCS;
Expand Down Expand Up @@ -381,7 +375,6 @@
buildActionMask = 2147483647;
files = (
182E30DD1BD4F46A008EC94E /* SwiftLCS.swift in Sources */,
182E30DE1BD4F46A008EC94E /* SwiftLCS+Foundation.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -390,7 +383,6 @@
buildActionMask = 2147483647;
files = (
18BCD0F71D8B71C600B6E4B5 /* SwiftLCS.swift in Sources */,
18BCD0F81D8B71C600B6E4B5 /* SwiftLCS+Foundation.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -399,7 +391,6 @@
buildActionMask = 2147483647;
files = (
18BCD1091D8B740200B6E4B5 /* SwiftLCS.swift in Sources */,
18BCD10A1D8B740200B6E4B5 /* SwiftLCS+Foundation.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -408,7 +399,6 @@
buildActionMask = 2147483647;
files = (
18BCD11A1D8B758200B6E4B5 /* SwiftLCS.swift in Sources */,
18BCD11B1D8B758400B6E4B5 /* SwiftLCS+Foundation.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
55 changes: 0 additions & 55 deletions SwiftLCS/SwiftLCS+Foundation.swift

This file was deleted.

74 changes: 61 additions & 13 deletions SwiftLCS/SwiftLCS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,85 @@
// SOFTWARE.
//

import Foundation

/**
A generic struct that represents a diff between two collections.
*/
public struct Diff<Index: Comparable> {
public struct Diff<Index> {

/// The indexes whose corresponding values in the old collection are in the LCS.
public var commonIndexes: [Index] {
return self.common.indexes
return self._commonIndexSet.indexes
}

/// The indexes whose corresponding values in the new collection are not in the LCS.
public var addedIndexes: [Index] {
return self.added.indexes
return self._addedIndexSet.indexes
}

/// The indexes whose corresponding values in the old collection are not in the LCS.
public var removedIndexes: [Index] {
return self.removed.indexes
return self._removedIndexSet.indexes
}

internal let common: (indexes: [Index], startIndex: Index)
internal let added: (indexes: [Index], startIndex: Index)
internal let removed: (indexes: [Index], startIndex: Index)

/// Construct the `Diff` between two given collections.
public init<C: Collection>(_ old: C, _ new: C) where C.Index == Index, C.Iterator.Element: Equatable {
self = old.diff(new)
}

fileprivate init<C: Collection>(common: ([Index], C), added: ([Index], C), removed: ([Index], C)) where C.Index == Index {
self.common = (indexes: common.0, startIndex: common.1.startIndex)
self.added = (indexes: added.0, startIndex: added.1.startIndex)
self.removed = (indexes: removed.0, startIndex: removed.1.startIndex)
fileprivate let _commonIndexSet: DiffIndexSet<Index>
fileprivate let _addedIndexSet: DiffIndexSet<Index>
fileprivate let _removedIndexSet: DiffIndexSet<Index>

fileprivate init(commonIndexes: DiffIndexSet<Index>, addedIndexes: DiffIndexSet<Index>, removedIndexes: DiffIndexSet<Index>) {
self._commonIndexSet = commonIndexes
self._addedIndexSet = addedIndexes
self._removedIndexSet = removedIndexes
}

}

/**
An extension of `Diff`, which adds support for `IndexSet`.
*/
public extension Diff where Index: Strideable, Index.Stride: SignedInteger {

/// The indexes whose corresponding values in the old collection are in the LCS.
public var commonIndexSet: IndexSet {
return self._commonIndexSet.indexSet
}

/// The indexes whose corresponding values in the new collection are not in the LCS.
public var addedIndexSet: IndexSet {
return self._addedIndexSet.indexSet
}

/// The indexes whose corresponding values in the old collection are not in the LCS.
public var removedIndexSet: IndexSet {
return self._removedIndexSet.indexSet
}

}

private struct DiffIndexSet<Index> {

let startIndex: Index
let indexes: [Index]

init(_ indexes: [Index], startIndex: Index) {
self.indexes = indexes
self.startIndex = startIndex
}

}

private extension DiffIndexSet where Index: Strideable, Index.Stride: SignedInteger {

var indexSet: IndexSet {
let indexes = self.indexes.map { Int((self.startIndex..<$0).count) }

return IndexSet(indexes)
}

}
Expand Down Expand Up @@ -110,7 +156,9 @@ public extension Collection where Iterator.Element: Equatable {
otherIndex = otherCollection.index(after: otherIndex)
}

return Diff(common: (commonIndexes, self), added: (addedIndexes, otherCollection), removed: (removedIndexes, self))
return Diff(commonIndexes: DiffIndexSet(commonIndexes, startIndex: self.startIndex),
addedIndexes: DiffIndexSet(addedIndexes, startIndex: otherCollection.startIndex),
removedIndexes: DiffIndexSet(removedIndexes, startIndex: self.startIndex))
}

// MARK: Private functions
Expand Down

0 comments on commit d1b5ca6

Please sign in to comment.