Rename Matrix to Matrix2D
This commit is contained in:
parent
7a87ec23a7
commit
e7ea5a92a8
|
@ -14,7 +14,7 @@
|
|||
import Swift
|
||||
import ElementaryCyclesSearch
|
||||
|
||||
extension Matrix where Element == Bool {
|
||||
extension Matrix2D where Element == Bool {
|
||||
enum Error: Swift.Error {
|
||||
case indexNotFound(node: AnyHashable, nodes: [AnyHashable])
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
import Swift
|
||||
import ElementaryCyclesSearch
|
||||
|
||||
extension Matrix where Element == Bool {
|
||||
extension Matrix2D where Element == Bool {
|
||||
static func getNodes<Node: Hashable>(graph: [Node: [Node]], sort: ((Node, Node) -> Bool)?) -> [Node] {
|
||||
var nodes = [Node]()
|
||||
for (node, adjacentNodes) in graph {
|
||||
|
|
|
@ -24,7 +24,7 @@ import Swift
|
|||
* @version 1.0, 26.08.2006
|
||||
*
|
||||
*/
|
||||
extension Matrix where Element == Int {
|
||||
extension Matrix2D where Element == Int {
|
||||
/**
|
||||
* Calculates a adjacency-list for a given array of an adjacency-matrix.
|
||||
*
|
||||
|
@ -35,8 +35,8 @@ extension Matrix where Element == Int {
|
|||
* adjacency, the second dimension represents the indicies of those nodes,
|
||||
* that are direct successornodes of the node.
|
||||
*/
|
||||
static func getAdjacencyList(adjacencyMatrix: AdjacencyMatrix) -> Matrix<Int> {
|
||||
let list = Matrix<Int>(adjacencyMatrix.reservedLength)
|
||||
static func getAdjacencyList(adjacencyMatrix: AdjacencyMatrix) -> Matrix2D<Int> {
|
||||
let list = Matrix2D<Int>(adjacencyMatrix.reservedLength)
|
||||
|
||||
for i in 0 ..< adjacencyMatrix.reservedLength {
|
||||
var v = [Int]()
|
||||
|
|
|
@ -16,4 +16,4 @@
|
|||
|
||||
import Swift
|
||||
|
||||
typealias AdjacencyList = Matrix<Int>
|
||||
typealias AdjacencyList = Matrix2D<Int>
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
|
||||
import Swift
|
||||
|
||||
public typealias AdjacencyMatrix = Matrix<Bool>
|
||||
public typealias AdjacencyMatrix = Matrix2D<Bool>
|
||||
|
||||
extension Matrix where Element == Bool {
|
||||
extension Matrix2D where Element == Bool {
|
||||
public convenience init(_ reservedRows: Int, _ reservedColumns: Int = 0, builder: (AdjacencyMatrix) -> Void) {
|
||||
self.init(reservedRows, reservedColumns)
|
||||
builder(self)
|
||||
|
|
|
@ -54,7 +54,7 @@ public class ElementaryCyclesSearch<Node> {
|
|||
private var blocked: Vector<Bool>
|
||||
|
||||
/** B-Lists, used by the algorithm of Johnson */
|
||||
private var B: Matrix<Int>
|
||||
private var B: Matrix2D<Int>
|
||||
|
||||
/** Stack for nodes, used by the algorithm of Johnson */
|
||||
private var stack: [Int]
|
||||
|
@ -88,7 +88,7 @@ public class ElementaryCyclesSearch<Node> {
|
|||
self.adjacencyList = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjacencyMatrix)
|
||||
cycles = [[Node]]()
|
||||
blocked = Vector<Bool>(adjacencyList.reservedLength)
|
||||
B = Matrix<Int>(adjacencyList.reservedLength)
|
||||
B = Matrix2D<Int>(adjacencyList.reservedLength)
|
||||
stack = [Int]()
|
||||
}
|
||||
|
||||
|
|
|
@ -13,15 +13,15 @@
|
|||
|
||||
import Swift
|
||||
|
||||
public struct MatrixIterator<Element>: IteratorProtocol {
|
||||
private let matrix: Matrix<Element>
|
||||
public struct Matrix2DIterator<Element>: IteratorProtocol {
|
||||
private let matrix: Matrix2D<Element>
|
||||
private var currentIndex = -1
|
||||
private var currentIterator: VectorIterator<Element>?
|
||||
|
||||
fileprivate init(matrix: Matrix<Element>) {
|
||||
|
||||
fileprivate init(matrix: Matrix2D<Element>) {
|
||||
self.matrix = matrix
|
||||
}
|
||||
|
||||
|
||||
mutating public func next() -> Element? {
|
||||
if let element = currentIterator?.next() {
|
||||
return element
|
||||
|
@ -34,8 +34,8 @@ public struct MatrixIterator<Element>: IteratorProtocol {
|
|||
}
|
||||
}
|
||||
|
||||
extension Matrix: Sequence {
|
||||
public func makeIterator() -> MatrixIterator<Element> {
|
||||
return MatrixIterator(matrix: self)
|
||||
extension Matrix2D: Sequence {
|
||||
public func makeIterator() -> Matrix2DIterator<Element> {
|
||||
return Matrix2DIterator(matrix: self)
|
||||
}
|
||||
}
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
import Swift
|
||||
|
||||
public final class Matrix<Element> {
|
||||
public final class Matrix2D<Element> {
|
||||
private var vector: Vector<Vector<Element>>
|
||||
|
||||
public subscript(_ row: Int) -> Vector<Element>! {
|
||||
|
@ -24,14 +24,14 @@ public final class Matrix<Element> {
|
|||
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 {
|
||||
|
@ -42,26 +42,26 @@ public final class Matrix<Element> {
|
|||
self[offset] = Vector(array: row, reservedLength: reservedColumns)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public var reservedLength: Int {
|
||||
return vector.reservedLength
|
||||
}
|
||||
}
|
||||
|
||||
extension Matrix: CustomDebugStringConvertible {
|
||||
extension Matrix2D: CustomDebugStringConvertible {
|
||||
public var debugDescription: String {
|
||||
return vector.debugDescription
|
||||
}
|
||||
}
|
||||
|
||||
extension Matrix: CustomStringConvertible {
|
||||
extension Matrix2D: CustomStringConvertible {
|
||||
public var description: String {
|
||||
return vector.description
|
||||
}
|
||||
}
|
||||
|
||||
extension Matrix: Equatable where Element: Equatable {
|
||||
public static func == (lhs: Matrix<Element>, rhs: Matrix<Element>) -> Bool {
|
||||
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 }
|
|
@ -77,12 +77,12 @@ public final class Vector<Element> {
|
|||
}
|
||||
|
||||
extension Vector where Element: Equatable {
|
||||
func contains(_ element: Element) -> Bool{
|
||||
public func contains(_ element: Element) -> Bool {
|
||||
return array.contains(element)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func remove(element: Element) -> Bool {
|
||||
public func remove(element: Element) -> Bool {
|
||||
if let index = array.firstIndex(of: element) {
|
||||
array.remove(at: -index.distance(to: 0))
|
||||
return true
|
||||
|
|
|
@ -16,7 +16,7 @@ final class AdjacencyListTests: XCTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
var sut: ((AdjacencyMatrix) -> Matrix<Int>)!
|
||||
var sut: ((AdjacencyMatrix) -> Matrix2D<Int>)!
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
|
|
@ -22,9 +22,9 @@ final class AdjacencyMatrixTests: XCTestCase {
|
|||
|
||||
func testConvenienceInit() {
|
||||
let rows = [["a"],[],["b", "c"]]
|
||||
let matrix = Matrix<String>(rows: rows)
|
||||
let expectedMatrix: Matrix<String> = {
|
||||
let matrix = Matrix<String>(3, 2)
|
||||
let matrix = Matrix2D<String>(rows: rows)
|
||||
let expectedMatrix: Matrix2D<String> = {
|
||||
let matrix = Matrix2D<String>(3, 2)
|
||||
matrix[0][0] = "a"
|
||||
matrix[2][0] = "b"
|
||||
matrix[2][1] = "c"
|
||||
|
@ -34,7 +34,7 @@ final class AdjacencyMatrixTests: XCTestCase {
|
|||
}
|
||||
|
||||
func testSequence() {
|
||||
let matrix = Matrix<Int>(4, 4)
|
||||
let matrix = Matrix2D<Int>(4, 4)
|
||||
matrix[0][1] = 1
|
||||
matrix[1][2] = 2
|
||||
matrix[3][0] = 3
|
||||
|
|
|
@ -7,7 +7,7 @@ final class StrongConnectedComponentsTests: XCTestCase {
|
|||
|
||||
|
||||
func test1() {
|
||||
let adjMatrix = Matrix(10, 10) { matrix in
|
||||
let adjMatrix = Matrix2D(10, 10) { matrix in
|
||||
matrix[0][1] = true
|
||||
matrix[1][2] = true
|
||||
matrix[2][0] = true; matrix[2][6] = true
|
||||
|
@ -20,7 +20,7 @@ final class StrongConnectedComponentsTests: XCTestCase {
|
|||
matrix[6][1] = true
|
||||
}
|
||||
|
||||
let adjacencyList: Matrix<Int> = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjMatrix)
|
||||
let adjacencyList: Matrix2D<Int> = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjMatrix)
|
||||
let sccs = StrongConnectedComponents(adjacencyList: adjacencyList)
|
||||
|
||||
var description = ""
|
||||
|
@ -91,7 +91,7 @@ i: 9
|
|||
}
|
||||
|
||||
func test2() {
|
||||
let adjMatrix: AdjacencyMatrix = Matrix(10, 10) { matrix in
|
||||
let adjMatrix: AdjacencyMatrix = Matrix2D(10, 10) { matrix in
|
||||
matrix[0][1] = true
|
||||
matrix[1][2] = true
|
||||
matrix[2][0] = true
|
||||
|
@ -106,7 +106,7 @@ i: 9
|
|||
matrix[9][6] = true
|
||||
}
|
||||
|
||||
let adjacencyList: Matrix<Int> = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjMatrix)
|
||||
let adjacencyList: Matrix2D<Int> = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjMatrix)
|
||||
let sccs = StrongConnectedComponents(adjacencyList: adjacencyList)
|
||||
|
||||
var description = ""
|
||||
|
|
Loading…
Reference in New Issue