Move Vector and Matrix2D to Idioms package
This commit is contained in:
parent
e7ea5a92a8
commit
e9ddc48972
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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"]),
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
*/
|
||||
|
||||
import Swift
|
||||
import Idioms
|
||||
import ElementaryCyclesSearch
|
||||
|
||||
extension Matrix2D where Element == Bool {
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
*/
|
||||
|
||||
import Swift
|
||||
import Idioms
|
||||
import ElementaryCyclesSearch
|
||||
|
||||
extension Matrix2D where Element == Bool {
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
*/
|
||||
|
||||
import Swift
|
||||
import Idioms
|
||||
import ElementaryCyclesSearch
|
||||
|
||||
public struct ElementaryCycles {
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
|
||||
import Swift
|
||||
import Idioms
|
||||
|
||||
/**
|
||||
* Calculates the adjacency-list for a given adjacency-matrix.
|
||||
|
|
|
@ -15,5 +15,6 @@
|
|||
*/
|
||||
|
||||
import Swift
|
||||
import Idioms
|
||||
|
||||
typealias AdjacencyList = Matrix2D<Int>
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
|
||||
import Swift
|
||||
import Idioms
|
||||
|
||||
/**
|
||||
* This is a helpclass for the search of all elementary cycles in a graph
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
|
||||
import Swift
|
||||
import Idioms
|
||||
|
||||
public typealias AdjacencyMatrix = Matrix2D<Bool>
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
|
||||
import Swift
|
||||
import Idioms
|
||||
|
||||
/**
|
||||
* Searchs all elementary cycles in a given directed graph. The implementation
|
||||
|
|
|
@ -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<Element>: IteratorProtocol {
|
||||
private let matrix: Matrix2D<Element>
|
||||
private var currentIndex = -1
|
||||
private var currentIterator: VectorIterator<Element>?
|
||||
|
||||
fileprivate init(matrix: Matrix2D<Element>) {
|
||||
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<Element> {
|
||||
return Matrix2DIterator(matrix: self)
|
||||
}
|
||||
}
|
|
@ -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<Element> {
|
||||
private var vector: Vector<Vector<Element>>
|
||||
|
||||
public subscript(_ row: Int) -> Vector<Element>! {
|
||||
get {
|
||||
return vector[row]
|
||||
}
|
||||
set(newValue) {
|
||||
vector[row] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
public init(_ reservedRows: Int, _ reservedColumns: Int = 0) {
|
||||
self.vector = Vector<Vector<Element>>(reservedRows)
|
||||
for i in 0 ..< reservedLength {
|
||||
vector[i] = Vector<Element>(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<Element>, rhs: Matrix2D<Element>) -> 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
|
||||
}
|
||||
}
|
|
@ -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<Element>: IteratorProtocol {
|
||||
private let vector: Vector<Element>
|
||||
private var currentIndex = -1
|
||||
|
||||
fileprivate init(vector: Vector<Element>) {
|
||||
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<Element> {
|
||||
return VectorIterator(vector: self)
|
||||
}
|
||||
}
|
|
@ -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<Element> {
|
||||
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<Element>, rhs: Vector<Element>) -> 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
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
import XCTest
|
||||
import Idioms
|
||||
@testable import ElementaryCyclesSearch
|
||||
|
||||
final class AdjacencyListTests: XCTestCase {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import XCTest
|
||||
import Idioms
|
||||
@testable import ElementaryCyclesSearch
|
||||
|
||||
final class AdjacencyMatrixTests: XCTestCase {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import XCTest
|
||||
import Idioms
|
||||
@testable import ElementaryCyclesSearch
|
||||
|
||||
final class StrongConnectedComponentsTests: XCTestCase {
|
||||
|
|
Loading…
Reference in New Issue