diff --git a/Sources/ElementaryCycles/Private/ElementaryCyclesSearch+toArray.swift b/Sources/ElementaryCycles/Private/ElementaryCyclesSearch+toArray.swift index 7e11591..44a351e 100644 --- a/Sources/ElementaryCycles/Private/ElementaryCyclesSearch+toArray.swift +++ b/Sources/ElementaryCycles/Private/ElementaryCyclesSearch+toArray.swift @@ -15,7 +15,7 @@ import Swift import ElementaryCyclesSearch extension ElementaryCyclesSearch { - static func toArray(elementaryCycles: Vector>) -> [[Node]] { + static func toArray(elementaryCycles: Array>) -> [[Node]] { var cycles = [[Node]]() for vector in elementaryCycles { var cycle = [Node]() diff --git a/Sources/ElementaryCycles/Public/ElementaryCycles.swift b/Sources/ElementaryCycles/Public/ElementaryCycles.swift index c23a479..8e133a0 100644 --- a/Sources/ElementaryCycles/Public/ElementaryCycles.swift +++ b/Sources/ElementaryCycles/Public/ElementaryCycles.swift @@ -18,7 +18,7 @@ public struct ElementaryCycles { public static func find(graph: [Node: [Node]], sort: ((Node, Node) -> Bool)? = nil) -> [[Node]] { let nodes = AdjacencyMatrix.getNodes(graph: graph, sort: sort) let adjacencyMatrix = try! AdjacencyMatrix.getAdjacencyMatrix(nodes: nodes, adjacencyDictionary: graph) - let elementaryCycles = ElementaryCyclesSearch.getElementaryCycles(adjacencyMatrix: adjacencyMatrix, graphNodes: Vector(array: nodes)) + let elementaryCycles = ElementaryCyclesSearch.getElementaryCycles(adjacencyMatrix: adjacencyMatrix, graphNodes: nodes) return ElementaryCyclesSearch.toArray(elementaryCycles: elementaryCycles) } } diff --git a/Sources/ElementaryCyclesSearch/Private/AdjacencyList+getAdjacencyList.swift b/Sources/ElementaryCyclesSearch/Private/AdjacencyList+getAdjacencyList.swift index 46e8c31..cfc7c7e 100644 --- a/Sources/ElementaryCyclesSearch/Private/AdjacencyList+getAdjacencyList.swift +++ b/Sources/ElementaryCyclesSearch/Private/AdjacencyList+getAdjacencyList.swift @@ -39,16 +39,16 @@ extension Matrix where Element == Int { let list = Matrix(adjacencyMatrix.reservedLength) for i in 0 ..< adjacencyMatrix.reservedLength { - let v = Vector() + var v = Array() for j in 0 ..< adjacencyMatrix[i].reservedLength { if let isAdjacent = adjacencyMatrix[i]?[j], isAdjacent { - v.add(j) + v.append(j) } } - - list[i] = Vector(v.size) - for j in 0 ..< v.size { - let integer = v.get(j) + + list[i] = Vector(v.count) + for j in 0 ..< v.count { + let integer = v[j] list[i][j] = integer } } diff --git a/Sources/ElementaryCyclesSearch/Private/StrongConnectedComponents.swift b/Sources/ElementaryCyclesSearch/Private/StrongConnectedComponents.swift index 4c48592..9c60e69 100644 --- a/Sources/ElementaryCyclesSearch/Private/StrongConnectedComponents.swift +++ b/Sources/ElementaryCyclesSearch/Private/StrongConnectedComponents.swift @@ -77,7 +77,7 @@ class StrongConnectedComponents { private var visited: Vector! /** Helpattribute for finding scc's */ - private var stack: Vector! + private var stack: Array! /** Helpattribute for finding scc's */ private var lowlink: Vector! @@ -89,7 +89,7 @@ class StrongConnectedComponents { private var strongConnectedComponentsCounter = 0; /** Helpattribute for finding scc's */ - private var currentStrongConnectedComponents: Vector>! + private var currentStrongConnectedComponents: Array>! /** * Constructor. @@ -116,9 +116,9 @@ class StrongConnectedComponents { lowlink = Vector(self.adjacencyListOriginal.reservedLength) number = Vector(self.adjacencyListOriginal.reservedLength) visited = Vector(self.adjacencyListOriginal.reservedLength) - stack = Vector() - currentStrongConnectedComponents = Vector>() - + stack = Array() + currentStrongConnectedComponents = Array>() + makeAdjacencyListSubgraph(node: node); for i in node ..< self.adjacencyListOriginal.reservedLength { @@ -153,17 +153,17 @@ class StrongConnectedComponents { adjacencyList = AdjacencyList(adjacencyListOriginal.reservedLength, 0) for i in node ..< adjacencyList.reservedLength { - let successors = Vector() + var successors = Array() for j in 0 ..< self.adjacencyListOriginal[i].reservedLength { guard let original = adjacencyListOriginal[i]?[j] else { continue } if original >= node { - successors.add(original) + successors.append(original) } } - if successors.size > 0 { - adjacencyList[i] = Vector(successors.size) - for j in 0 ..< successors.size { - let succ = successors.get(j) + if successors.count > 0 { + adjacencyList[i] = Vector(successors.count) + for j in 0 ..< successors.count { + let succ = successors[j] adjacencyList[i][j] = succ } } @@ -176,14 +176,14 @@ class StrongConnectedComponents { * * @return Vector::Integer of the strongConnectedComponents containing the lowest nodenumber */ - private func getLowestIdComponent() -> Vector? { + private func getLowestIdComponent() -> Array? { var min = adjacencyList.reservedLength; - var currScc: Vector? + var currScc: Array? - for i in 0 ..< currentStrongConnectedComponents.size { - let strongConnectedComponents = currentStrongConnectedComponents.get(i) - for j in 0 ..< strongConnectedComponents.size { - let node = strongConnectedComponents.get(j) + for i in 0 ..< currentStrongConnectedComponents.count { + let strongConnectedComponents = currentStrongConnectedComponents[i] + for j in 0 ..< strongConnectedComponents.count { + let node = strongConnectedComponents[j] if node < min { currScc = strongConnectedComponents min = node @@ -199,14 +199,14 @@ class StrongConnectedComponents { * strong connected component with least vertex in the currently viewed * subgraph */ - private func getAdjList(nodes: Vector?) -> AdjacencyList? { + private func getAdjList(nodes: Array?) -> AdjacencyList? { guard let nodes = nodes else { return nil } let lowestIdAdjacencyList = AdjacencyList(adjacencyList.reservedLength) for i in 0 ..< lowestIdAdjacencyList.reservedLength { lowestIdAdjacencyList[i] = Vector() } - for i in 0 ..< nodes.size { - let node = nodes.get(i) + for i in 0 ..< nodes.count { + let node = nodes[i] guard let adjListNode = adjacencyList[node] else { continue } for j in 0 ..< adjListNode.reservedLength { guard let succ = adjacencyList[node]?[j] else { continue } @@ -228,8 +228,8 @@ class StrongConnectedComponents { strongConnectedComponentsCounter += 1 lowlink[root] = strongConnectedComponentsCounter number[root] = strongConnectedComponentsCounter - visited[root] = true; - stack.add(root); + visited[root] = true + stack.append(root) for i in 0 ..< adjacencyList[root].reservedLength { guard let w = adjacencyList[root]?[i] else { continue } @@ -244,19 +244,19 @@ class StrongConnectedComponents { } // found strongConnectedComponents - if (lowlink[root] == number[root]) && (stack.size > 0) { + if (lowlink[root] == number[root]) && (stack.count > 0) { var next = -1; - let strongConnectedComponents = Vector() + var strongConnectedComponents = Array() repeat { - next = stack.get(stack.size - 1) - stack.remove(at: stack.size - 1) - strongConnectedComponents.add(next) + guard let popped = stack.popLast() else { break } + next = popped + strongConnectedComponents.append(next) } while number[next]! > number[root]! // simple scc's with just one node will not be added - if strongConnectedComponents.size > 1 { - currentStrongConnectedComponents.add(strongConnectedComponents); + if strongConnectedComponents.count > 1 { + currentStrongConnectedComponents.append(strongConnectedComponents); } } } diff --git a/Sources/ElementaryCyclesSearch/Public/ElementaryCyclesSearch.swift b/Sources/ElementaryCyclesSearch/Public/ElementaryCyclesSearch.swift index 8df4bdd..19b2aa5 100644 --- a/Sources/ElementaryCyclesSearch/Public/ElementaryCyclesSearch.swift +++ b/Sources/ElementaryCyclesSearch/Public/ElementaryCyclesSearch.swift @@ -42,13 +42,13 @@ import Swift */ public class ElementaryCyclesSearch { /** List of cycles */ - private var cycles: Vector> + private var cycles: Array> /** Adjacency-list of graph */ private var adjacencyList: AdjacencyList /** Graphnodes */ - private var graphNodes: Vector + private var graphNodes: Array /** Blocked nodes, used by the algorithm of Johnson */ private var blocked: Vector @@ -57,7 +57,7 @@ public class ElementaryCyclesSearch { private var B: Matrix /** Stack for nodes, used by the algorithm of Johnson */ - private var stack: Vector + private var stack: Array /** * Returns List::List::Object with the Lists of nodes of all elementary @@ -70,7 +70,7 @@ public class ElementaryCyclesSearch { * * @return List::List::Object with the Lists of the elementary cycles. */ - public static func getElementaryCycles(adjacencyMatrix: AdjacencyMatrix, graphNodes: Vector) -> Vector> { + public static func getElementaryCycles(adjacencyMatrix: AdjacencyMatrix, graphNodes: Array) -> Array> { let ecs = ElementaryCyclesSearch(adjacencyMatrix: adjacencyMatrix, graphNodes: graphNodes) return ecs.getElementaryCycles() } @@ -83,13 +83,13 @@ public class ElementaryCyclesSearch { * build sets of the elementary cycles containing the objects of the original * graph-representation */ - private init(adjacencyMatrix: AdjacencyMatrix, graphNodes: Vector) { + private init(adjacencyMatrix: AdjacencyMatrix, graphNodes: Array) { self.graphNodes = graphNodes; self.adjacencyList = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjacencyMatrix) - cycles = Vector>() + cycles = Array>() blocked = Vector(adjacencyList.reservedLength) B = Matrix(adjacencyList.reservedLength) - stack = Vector() + stack = Array() } /** @@ -98,7 +98,7 @@ public class ElementaryCyclesSearch { * * @return List::List::Object with the Lists of the elementary cycles. */ - private func getElementaryCycles() -> Vector> { + private func getElementaryCycles() -> Array> { let sccs = StrongConnectedComponents(adjacencyList: adjacencyList) var s = 0 @@ -135,20 +135,20 @@ public class ElementaryCyclesSearch { */ private func findCycles(v: Int, s: Int, adjacencyList: AdjacencyList) -> Bool { var f = false - stack.add(v) + stack.append(v) blocked[v] = true for i in 0 ..< adjacencyList[v].size { let w = adjacencyList[v].get(i) // found cycle if w == s { - let cycle = Vector() - for j in 0 ..< stack.size { - let index = stack.get(j) - guard let node = graphNodes[index] else { continue } - cycle.add(node) + var cycle = Array() + for j in 0 ..< stack.count { + let index = stack[j] + let node = graphNodes[index] // WARNING guard + cycle.append(node) } - cycles.add(cycle) + cycles.append(cycle) f = true } else if !(blocked[w] ?? false) { if findCycles(v: w, s: s, adjacencyList: adjacencyList) { @@ -167,8 +167,10 @@ public class ElementaryCyclesSearch { } } } - - stack.remove(element: v) + + if let index = stack.index(of: v) { + stack.remove(at: index) + } return f } diff --git a/Sources/ElementaryCyclesSearchExample/main.swift b/Sources/ElementaryCyclesSearchExample/main.swift index 7be9a83..8a415db 100644 --- a/Sources/ElementaryCyclesSearchExample/main.swift +++ b/Sources/ElementaryCyclesSearchExample/main.swift @@ -26,12 +26,12 @@ import ElementaryCyclesSearch typealias Node = String -private func printCycles(_ cycles: Vector>) { - for i in 0 ..< cycles.size { - let cycle = cycles.get(i) - for j in 0 ..< cycle.size { - let node = cycle.get(j) - if j < (cycle.size - 1) { +private func printCycles(_ cycles: Array>) { + for i in 0 ..< cycles.count { + let cycle = cycles[i] + for j in 0 ..< cycle.count { + let node = cycle[j] + if j < (cycle.count - 1) { print(node + " -> ", terminator: "") } else { print(node, terminator: "") @@ -41,10 +41,10 @@ private func printCycles(_ cycles: Vector>) { } } -let nodes: Vector = { - let vector = Vector(10) +let nodes: Array = { + var vector = Array() for i in 0 ..< 10 { - vector[i] = "Node \(i)" + vector.append("Node \(i)") } return vector }() diff --git a/Tests/ElementaryCyclesSearchTests/ElementaryCyclesSearchTests.swift b/Tests/ElementaryCyclesSearchTests/ElementaryCyclesSearchTests.swift index 508200d..4ef3191 100644 --- a/Tests/ElementaryCyclesSearchTests/ElementaryCyclesSearchTests.swift +++ b/Tests/ElementaryCyclesSearchTests/ElementaryCyclesSearchTests.swift @@ -6,10 +6,10 @@ final class ElementaryCyclesSearchTests: XCTestCase { private typealias Node = String - private var nodes: Vector { - let nodes = Vector(10) + private var nodes: Array { + var nodes = Array() for i in 0 ..< 10 { - nodes[i] = "Node \(i)" + nodes.append("Node \(i)") } return nodes } @@ -29,15 +29,15 @@ final class ElementaryCyclesSearchTests: XCTestCase { matrix[6][1] = true } - private var sut: ((AdjacencyMatrix, Vector) -> Vector>)! + private var sut: ((AdjacencyMatrix, Array) -> Array>)! - private func prettify(cycles: Vector>) -> String { + private func prettify(cycles: Array>) -> String { var description = "" - for i in 0 ..< cycles.size { - let cycle = cycles.get(i) - for j in 0 ..< cycle.size { - let node = cycle.get(j) - if j < (cycle.size - 1) { + for i in 0 ..< cycles.count { + let cycle = cycles[i] + for j in 0 ..< cycle.count { + let node = cycle[j] + if j < (cycle.count - 1) { description.append(node + " -> ") } else { description.append(node) diff --git a/Tests/ElementaryCyclesTests/ElementaryCyclesSearchShould.swift b/Tests/ElementaryCyclesTests/ElementaryCyclesSearchShould.swift index 5d6671e..1578903 100644 --- a/Tests/ElementaryCyclesTests/ElementaryCyclesSearchShould.swift +++ b/Tests/ElementaryCyclesTests/ElementaryCyclesSearchShould.swift @@ -7,10 +7,10 @@ final class ElementaryCyclesSearchShould: XCTestCase { private typealias Node = String - private var nodes: Vector { - let nodes = Vector(10) + private var nodes: Array { + var nodes = Array() for i in 0 ..< 10 { - nodes[i] = "Node \(i)" + nodes.append("Node \(i)") } return nodes }