diff --git a/ElementaryCyclesSearch.podspec b/ElementaryCyclesSearch.podspec index 2ee1cce..15856e6 100644 --- a/ElementaryCyclesSearch.podspec +++ b/ElementaryCyclesSearch.podspec @@ -13,5 +13,6 @@ The implementation is pretty much generic, all it needs is a adjacency-matrix of s.osx.deployment_target = '10.13' s.source = { :git => 'https://github.com/hectr/swift-elementary-cycles.git', :tag => s.version.to_s } s.source_files = 'Sources/ElementaryCyclesSearch/**/*' + s.dependency 'Idioms' s.swift_version = '5.0' end diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..f61289f --- /dev/null +++ b/Package.resolved @@ -0,0 +1,16 @@ +{ + "object": { + "pins": [ + { + "package": "Idioms", + "repositoryURL": "https://github.com/hectr/swift-idioms.git", + "state": { + "branch": null, + "revision": "320c051b0f4775d2ccca6120ab673416300679f9", + "version": "1.4.0" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift index 938d93e..0aebc3d 100644 --- a/Package.swift +++ b/Package.swift @@ -16,6 +16,7 @@ let package = Package( targets: ["ElementaryCyclesSearchExample"]), ], dependencies: [ + .package(url: "https://github.com/hectr/swift-idioms.git", from: "1.4.0"), ], targets: [ .target( @@ -23,7 +24,7 @@ let package = Package( dependencies: ["ElementaryCyclesSearch"]), .target( name: "ElementaryCyclesSearch", - dependencies: []), + dependencies: ["Idioms"]), .target( name: "ElementaryCycles", dependencies: ["ElementaryCyclesSearch"]), diff --git a/Sources/ElementaryCycles/Private/AdjacencyMatrix+getAdjacencyMatrix.swift b/Sources/ElementaryCycles/Private/AdjacencyMatrix+getAdjacencyMatrix.swift index f367ae0..2a0e614 100644 --- a/Sources/ElementaryCycles/Private/AdjacencyMatrix+getAdjacencyMatrix.swift +++ b/Sources/ElementaryCycles/Private/AdjacencyMatrix+getAdjacencyMatrix.swift @@ -12,6 +12,7 @@ */ import Swift +import Idioms import ElementaryCyclesSearch extension Matrix2D where Element == Bool { diff --git a/Sources/ElementaryCycles/Private/AdjacencyMatrix+getNodes.swift b/Sources/ElementaryCycles/Private/AdjacencyMatrix+getNodes.swift index 836b597..95814e4 100644 --- a/Sources/ElementaryCycles/Private/AdjacencyMatrix+getNodes.swift +++ b/Sources/ElementaryCycles/Private/AdjacencyMatrix+getNodes.swift @@ -12,6 +12,7 @@ */ import Swift +import Idioms import ElementaryCyclesSearch extension Matrix2D where Element == Bool { diff --git a/Sources/ElementaryCycles/Public/ElementaryCycles.swift b/Sources/ElementaryCycles/Public/ElementaryCycles.swift index 8e133a0..0605279 100644 --- a/Sources/ElementaryCycles/Public/ElementaryCycles.swift +++ b/Sources/ElementaryCycles/Public/ElementaryCycles.swift @@ -12,6 +12,7 @@ */ import Swift +import Idioms import ElementaryCyclesSearch public struct ElementaryCycles { diff --git a/Sources/ElementaryCyclesSearch/Private/AdjacencyList+getAdjacencyList.swift b/Sources/ElementaryCyclesSearch/Private/AdjacencyList+getAdjacencyList.swift index 3c516ab..f46986f 100644 --- a/Sources/ElementaryCyclesSearch/Private/AdjacencyList+getAdjacencyList.swift +++ b/Sources/ElementaryCyclesSearch/Private/AdjacencyList+getAdjacencyList.swift @@ -15,6 +15,7 @@ */ import Swift +import Idioms /** * Calculates the adjacency-list for a given adjacency-matrix. diff --git a/Sources/ElementaryCyclesSearch/Private/AdjacencyList.swift b/Sources/ElementaryCyclesSearch/Private/AdjacencyList.swift index 6e52f7f..8df916d 100644 --- a/Sources/ElementaryCyclesSearch/Private/AdjacencyList.swift +++ b/Sources/ElementaryCyclesSearch/Private/AdjacencyList.swift @@ -15,5 +15,6 @@ */ import Swift +import Idioms typealias AdjacencyList = Matrix2D diff --git a/Sources/ElementaryCyclesSearch/Private/StrongConnectedComponents.swift b/Sources/ElementaryCyclesSearch/Private/StrongConnectedComponents.swift index add272f..a30ced0 100644 --- a/Sources/ElementaryCyclesSearch/Private/StrongConnectedComponents.swift +++ b/Sources/ElementaryCyclesSearch/Private/StrongConnectedComponents.swift @@ -15,6 +15,7 @@ */ import Swift +import Idioms /** * This is a helpclass for the search of all elementary cycles in a graph diff --git a/Sources/ElementaryCyclesSearch/Public/AdjacencyMatrix.swift b/Sources/ElementaryCyclesSearch/Public/AdjacencyMatrix.swift index a4b4776..5b2dbc9 100644 --- a/Sources/ElementaryCyclesSearch/Public/AdjacencyMatrix.swift +++ b/Sources/ElementaryCyclesSearch/Public/AdjacencyMatrix.swift @@ -15,6 +15,7 @@ */ import Swift +import Idioms public typealias AdjacencyMatrix = Matrix2D diff --git a/Sources/ElementaryCyclesSearch/Public/ElementaryCyclesSearch.swift b/Sources/ElementaryCyclesSearch/Public/ElementaryCyclesSearch.swift index 807d5f3..b4bae31 100644 --- a/Sources/ElementaryCyclesSearch/Public/ElementaryCyclesSearch.swift +++ b/Sources/ElementaryCyclesSearch/Public/ElementaryCyclesSearch.swift @@ -15,6 +15,7 @@ */ import Swift +import Idioms /** * Searchs all elementary cycles in a given directed graph. The implementation diff --git a/Sources/ElementaryCyclesSearch/Public/Vector/Matrix2D+Sequence.swift b/Sources/ElementaryCyclesSearch/Public/Vector/Matrix2D+Sequence.swift deleted file mode 100644 index 659ce7a..0000000 --- a/Sources/ElementaryCyclesSearch/Public/Vector/Matrix2D+Sequence.swift +++ /dev/null @@ -1,41 +0,0 @@ -/* - (BSD-2 license) - - Copyright (c) 2018, Hèctor Marquès - - Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -import Swift - -public struct Matrix2DIterator: IteratorProtocol { - private let matrix: Matrix2D - private var currentIndex = -1 - private var currentIterator: VectorIterator? - - fileprivate init(matrix: Matrix2D) { - self.matrix = matrix - } - - mutating public func next() -> Element? { - if let element = currentIterator?.next() { - return element - } - currentIndex += 1 - guard matrix.reservedLength > currentIndex else { return nil } - let vector = matrix[currentIndex] - currentIterator = vector?.makeIterator() - return next() - } -} - -extension Matrix2D: Sequence { - public func makeIterator() -> Matrix2DIterator { - return Matrix2DIterator(matrix: self) - } -} diff --git a/Sources/ElementaryCyclesSearch/Public/Vector/Matrix2D.swift b/Sources/ElementaryCyclesSearch/Public/Vector/Matrix2D.swift deleted file mode 100644 index 7da7d74..0000000 --- a/Sources/ElementaryCyclesSearch/Public/Vector/Matrix2D.swift +++ /dev/null @@ -1,71 +0,0 @@ -/* - (BSD-2 license) - - Copyright (c) 2018, Hèctor Marquès - - Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -import Swift - -public final class Matrix2D { - private var vector: Vector> - - public subscript(_ row: Int) -> Vector! { - get { - return vector[row] - } - set(newValue) { - vector[row] = newValue - } - } - - public init(_ reservedRows: Int, _ reservedColumns: Int = 0) { - self.vector = Vector>(reservedRows) - for i in 0 ..< reservedLength { - vector[i] = Vector(reservedColumns) - } - } - - public convenience init(rows: [[Element]]) { - var reservedColumns = 0 - for row in rows { - reservedColumns = Swift.max(reservedColumns, row.count) - } - self.init(rows.count, reservedColumns) - for (offset, row) in rows.enumerated() { - self[offset] = Vector(array: row, reservedLength: reservedColumns) - } - } - - public var reservedLength: Int { - return vector.reservedLength - } -} - -extension Matrix2D: CustomDebugStringConvertible { - public var debugDescription: String { - return vector.debugDescription - } -} - -extension Matrix2D: CustomStringConvertible { - public var description: String { - return vector.description - } -} - -extension Matrix2D: Equatable where Element: Equatable { - public static func == (lhs: Matrix2D, rhs: Matrix2D) -> Bool { - guard lhs.reservedLength == rhs.reservedLength else { return false } - for index in 0 ..< lhs.reservedLength { - guard lhs[index] == rhs[index] else { return false } - } - return true - } -} diff --git a/Sources/ElementaryCyclesSearch/Public/Vector/Vector+Sequence.swift b/Sources/ElementaryCyclesSearch/Public/Vector/Vector+Sequence.swift deleted file mode 100644 index f34642d..0000000 --- a/Sources/ElementaryCyclesSearch/Public/Vector/Vector+Sequence.swift +++ /dev/null @@ -1,36 +0,0 @@ -/* - (BSD-2 license) - - Copyright (c) 2018, Hèctor Marquès - - Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -import Swift - -public struct VectorIterator: IteratorProtocol { - private let vector: Vector - private var currentIndex = -1 - - fileprivate init(vector: Vector) { - self.vector = vector - } - - mutating public func next() -> Element? { - currentIndex += 1 - guard vector.size > currentIndex else { return nil } - guard let element = vector[currentIndex] else { return next() } - return element - } -} - -extension Vector: Sequence { - public func makeIterator() -> VectorIterator { - return VectorIterator(vector: self) - } -} diff --git a/Sources/ElementaryCyclesSearch/Public/Vector/Vector.swift b/Sources/ElementaryCyclesSearch/Public/Vector/Vector.swift deleted file mode 100644 index 01089b2..0000000 --- a/Sources/ElementaryCyclesSearch/Public/Vector/Vector.swift +++ /dev/null @@ -1,115 +0,0 @@ -/* - (BSD-2 license) - - Copyright (c) 2018, Hèctor Marquès - - Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -import Swift - -public final class Vector { - private var array: [Element?] { - didSet { - reservedLength = Swift.max(reservedLength, array.count) - } - } - - public private(set) var reservedLength: Int { - didSet { - assert(reservedLength >= size) - } - } - - public subscript(_ index: Int) -> Element? { - get { - guard array.count > index || index >= reservedLength else { return nil } - return array[index] - } - set(newValue) { - while index >= array.count && index < reservedLength { - array.append(nil) - } - array[index] = newValue - } - } - - public init(array: [Element], reservedLength: Int? = nil) { - self.array = array - self.reservedLength = Swift.max(reservedLength ?? 0, array.count) - } - - public init(_ reservedLength: Int = 0) { - self.array = [Element?]() - self.reservedLength = reservedLength - } - - public var size: Int { - return array.count - } - - public func get(_ index: Int) -> Element { - guard let element = self[index] else { fatalError("\(#function): index (\(index)) out of bounds (\(reservedLength))") } - return element - } - - public func add(_ newElement: Element) { - array.append(newElement) - } - - @discardableResult - public func remove(at index: Int) -> Element? { - var removedObject: Element? - if index < array.count { - removedObject = array[index] - array.remove(at: index) - } else if index >= reservedLength { - preconditionFailure("Index out of range: \(index) is beyond count (\(array.count) and reservedLength \(reservedLength)") - } - reservedLength -= 1 - return removedObject - } -} - -extension Vector where Element: Equatable { - public func contains(_ element: Element) -> Bool { - return array.contains(element) - } - - @discardableResult - public func remove(element: Element) -> Bool { - if let index = array.firstIndex(of: element) { - array.remove(at: -index.distance(to: 0)) - return true - } - return false - } -} - -extension Vector: CustomDebugStringConvertible { - public var debugDescription: String { - return array.debugDescription - } -} - -extension Vector: CustomStringConvertible { - public var description: String { - return array.description - } -} - -extension Vector: Equatable where Element: Equatable { - public static func == (lhs: Vector, rhs: Vector) -> Bool { - guard lhs.reservedLength == rhs.reservedLength else { return false } - guard lhs.size == rhs.size else { return false } - for index in 0 ..< lhs.size { - guard lhs[index] == rhs[index] else { return false } - } - return true - } -} diff --git a/Tests/ElementaryCyclesSearchTests/AdjacencyListTests.swift b/Tests/ElementaryCyclesSearchTests/AdjacencyListTests.swift index cb8cfb4..64f99e8 100644 --- a/Tests/ElementaryCyclesSearchTests/AdjacencyListTests.swift +++ b/Tests/ElementaryCyclesSearchTests/AdjacencyListTests.swift @@ -1,4 +1,5 @@ import XCTest +import Idioms @testable import ElementaryCyclesSearch final class AdjacencyListTests: XCTestCase { diff --git a/Tests/ElementaryCyclesSearchTests/AdjacencyMatrixTests.swift b/Tests/ElementaryCyclesSearchTests/AdjacencyMatrixTests.swift index 5ac8087..ab400fb 100644 --- a/Tests/ElementaryCyclesSearchTests/AdjacencyMatrixTests.swift +++ b/Tests/ElementaryCyclesSearchTests/AdjacencyMatrixTests.swift @@ -1,4 +1,5 @@ import XCTest +import Idioms @testable import ElementaryCyclesSearch final class AdjacencyMatrixTests: XCTestCase { diff --git a/Tests/ElementaryCyclesSearchTests/StrongConnectedComponentsTests.swift b/Tests/ElementaryCyclesSearchTests/StrongConnectedComponentsTests.swift index 4d6810c..2646185 100644 --- a/Tests/ElementaryCyclesSearchTests/StrongConnectedComponentsTests.swift +++ b/Tests/ElementaryCyclesSearchTests/StrongConnectedComponentsTests.swift @@ -1,4 +1,5 @@ import XCTest +import Idioms @testable import ElementaryCyclesSearch final class StrongConnectedComponentsTests: XCTestCase {