Use Array shorthand syntactic sugar

This commit is contained in:
hectr 2018-12-10 22:56:02 +01:00
parent 347691d331
commit c4f0749f1c
7 changed files with 29 additions and 29 deletions

View File

@ -15,7 +15,7 @@ import Swift
import ElementaryCyclesSearch import ElementaryCyclesSearch
extension ElementaryCyclesSearch { extension ElementaryCyclesSearch {
static func toArray(elementaryCycles: Array<Array<Node>>) -> [[Node]] { static func toArray(elementaryCycles: [[Node]]) -> [[Node]] {
var cycles = [[Node]]() var cycles = [[Node]]()
for vector in elementaryCycles { for vector in elementaryCycles {
var cycle = [Node]() var cycle = [Node]()

View File

@ -39,7 +39,7 @@ extension Matrix where Element == Int {
let list = Matrix<Int>(adjacencyMatrix.reservedLength) let list = Matrix<Int>(adjacencyMatrix.reservedLength)
for i in 0 ..< adjacencyMatrix.reservedLength { for i in 0 ..< adjacencyMatrix.reservedLength {
var v = Array<Int>() var v = [Int]()
for j in 0 ..< adjacencyMatrix[i].reservedLength { for j in 0 ..< adjacencyMatrix[i].reservedLength {
if let isAdjacent = adjacencyMatrix[i]?[j], isAdjacent { if let isAdjacent = adjacencyMatrix[i]?[j], isAdjacent {
v.append(j) v.append(j)

View File

@ -77,7 +77,7 @@ class StrongConnectedComponents {
private var visited: Vector<Bool>! private var visited: Vector<Bool>!
/** Helpattribute for finding scc's */ /** Helpattribute for finding scc's */
private var stack: Array<Int>! private var stack: [Int]!
/** Helpattribute for finding scc's */ /** Helpattribute for finding scc's */
private var lowlink: Vector<Int>! private var lowlink: Vector<Int>!
@ -89,7 +89,7 @@ class StrongConnectedComponents {
private var strongConnectedComponentsCounter = 0; private var strongConnectedComponentsCounter = 0;
/** Helpattribute for finding scc's */ /** Helpattribute for finding scc's */
private var currentStrongConnectedComponents: Array<Array<Int>>! private var currentStrongConnectedComponents: [[Int]]!
/** /**
* Constructor. * Constructor.
@ -116,8 +116,8 @@ class StrongConnectedComponents {
lowlink = Vector<Int>(self.adjacencyListOriginal.reservedLength) lowlink = Vector<Int>(self.adjacencyListOriginal.reservedLength)
number = Vector<Int>(self.adjacencyListOriginal.reservedLength) number = Vector<Int>(self.adjacencyListOriginal.reservedLength)
visited = Vector<Bool>(self.adjacencyListOriginal.reservedLength) visited = Vector<Bool>(self.adjacencyListOriginal.reservedLength)
stack = Array<Int>() stack = [Int]()
currentStrongConnectedComponents = Array<Array<Int>>() currentStrongConnectedComponents = [[Int]]()
makeAdjacencyListSubgraph(node: node); makeAdjacencyListSubgraph(node: node);
@ -153,7 +153,7 @@ class StrongConnectedComponents {
adjacencyList = AdjacencyList(adjacencyListOriginal.reservedLength, 0) adjacencyList = AdjacencyList(adjacencyListOriginal.reservedLength, 0)
for i in node ..< adjacencyList.reservedLength { for i in node ..< adjacencyList.reservedLength {
var successors = Array<Int>() var successors = [Int]()
for j in 0 ..< self.adjacencyListOriginal[i].reservedLength { for j in 0 ..< self.adjacencyListOriginal[i].reservedLength {
guard let original = adjacencyListOriginal[i]?[j] else { continue } guard let original = adjacencyListOriginal[i]?[j] else { continue }
if original >= node { if original >= node {
@ -176,9 +176,9 @@ class StrongConnectedComponents {
* *
* @return Vector::Integer of the strongConnectedComponents containing the lowest nodenumber * @return Vector::Integer of the strongConnectedComponents containing the lowest nodenumber
*/ */
private func getLowestIdComponent() -> Array<Int>? { private func getLowestIdComponent() -> [Int]? {
var min = adjacencyList.reservedLength; var min = adjacencyList.reservedLength;
var currScc: Array<Int>? var currScc: [Int]?
for i in 0 ..< currentStrongConnectedComponents.count { for i in 0 ..< currentStrongConnectedComponents.count {
let strongConnectedComponents = currentStrongConnectedComponents[i] let strongConnectedComponents = currentStrongConnectedComponents[i]
@ -199,7 +199,7 @@ class StrongConnectedComponents {
* strong connected component with least vertex in the currently viewed * strong connected component with least vertex in the currently viewed
* subgraph * subgraph
*/ */
private func getAdjList(nodes: Array<Int>?) -> AdjacencyList? { private func getAdjList(nodes: [Int]?) -> AdjacencyList? {
guard let nodes = nodes else { return nil } guard let nodes = nodes else { return nil }
let lowestIdAdjacencyList = AdjacencyList(adjacencyList.reservedLength) let lowestIdAdjacencyList = AdjacencyList(adjacencyList.reservedLength)
for i in 0 ..< lowestIdAdjacencyList.reservedLength { for i in 0 ..< lowestIdAdjacencyList.reservedLength {
@ -246,7 +246,7 @@ class StrongConnectedComponents {
// found strongConnectedComponents // found strongConnectedComponents
if (lowlink[root] == number[root]) && (stack.count > 0) { if (lowlink[root] == number[root]) && (stack.count > 0) {
var next = -1; var next = -1;
var strongConnectedComponents = Array<Int>() var strongConnectedComponents = [Int]()
repeat { repeat {
guard let popped = stack.popLast() else { break } guard let popped = stack.popLast() else { break }

View File

@ -42,13 +42,13 @@ import Swift
*/ */
public class ElementaryCyclesSearch<Node> { public class ElementaryCyclesSearch<Node> {
/** List of cycles */ /** List of cycles */
private var cycles: Array<Array<Node>> private var cycles: [[Node]]
/** Adjacency-list of graph */ /** Adjacency-list of graph */
private var adjacencyList: AdjacencyList private var adjacencyList: AdjacencyList
/** Graphnodes */ /** Graphnodes */
private var graphNodes: Array<Node> private var graphNodes: [Node]
/** Blocked nodes, used by the algorithm of Johnson */ /** Blocked nodes, used by the algorithm of Johnson */
private var blocked: Vector<Bool> private var blocked: Vector<Bool>
@ -57,7 +57,7 @@ public class ElementaryCyclesSearch<Node> {
private var B: Matrix<Int> private var B: Matrix<Int>
/** Stack for nodes, used by the algorithm of Johnson */ /** Stack for nodes, used by the algorithm of Johnson */
private var stack: Array<Int> private var stack: [Int]
/** /**
* Returns List::List::Object with the Lists of nodes of all elementary * Returns List::List::Object with the Lists of nodes of all elementary
@ -70,7 +70,7 @@ public class ElementaryCyclesSearch<Node> {
* *
* @return List::List::Object with the Lists of the elementary cycles. * @return List::List::Object with the Lists of the elementary cycles.
*/ */
public static func getElementaryCycles(adjacencyMatrix: AdjacencyMatrix, graphNodes: Array<Node>) -> Array<Array<Node>> { public static func getElementaryCycles(adjacencyMatrix: AdjacencyMatrix, graphNodes: [Node]) -> [[Node]] {
let ecs = ElementaryCyclesSearch(adjacencyMatrix: adjacencyMatrix, graphNodes: graphNodes) let ecs = ElementaryCyclesSearch(adjacencyMatrix: adjacencyMatrix, graphNodes: graphNodes)
return ecs.getElementaryCycles() return ecs.getElementaryCycles()
} }
@ -83,13 +83,13 @@ public class ElementaryCyclesSearch<Node> {
* build sets of the elementary cycles containing the objects of the original * build sets of the elementary cycles containing the objects of the original
* graph-representation * graph-representation
*/ */
private init(adjacencyMatrix: AdjacencyMatrix, graphNodes: Array<Node>) { private init(adjacencyMatrix: AdjacencyMatrix, graphNodes: [Node]) {
self.graphNodes = graphNodes; self.graphNodes = graphNodes;
self.adjacencyList = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjacencyMatrix) self.adjacencyList = AdjacencyList.getAdjacencyList(adjacencyMatrix: adjacencyMatrix)
cycles = Array<Array<Node>>() cycles = [[Node]]()
blocked = Vector<Bool>(adjacencyList.reservedLength) blocked = Vector<Bool>(adjacencyList.reservedLength)
B = Matrix<Int>(adjacencyList.reservedLength) B = Matrix<Int>(adjacencyList.reservedLength)
stack = Array<Int>() stack = [Int]()
} }
/** /**
@ -98,7 +98,7 @@ public class ElementaryCyclesSearch<Node> {
* *
* @return List::List::Object with the Lists of the elementary cycles. * @return List::List::Object with the Lists of the elementary cycles.
*/ */
private func getElementaryCycles() -> Array<Array<Node>> { private func getElementaryCycles() -> [[Node]] {
let sccs = StrongConnectedComponents(adjacencyList: adjacencyList) let sccs = StrongConnectedComponents(adjacencyList: adjacencyList)
var s = 0 var s = 0
@ -142,7 +142,7 @@ public class ElementaryCyclesSearch<Node> {
let w = adjacencyList[v].get(i) let w = adjacencyList[v].get(i)
// found cycle // found cycle
if w == s { if w == s {
var cycle = Array<Node>() var cycle = [Node]()
for j in 0 ..< stack.count { for j in 0 ..< stack.count {
let index = stack[j] let index = stack[j]
let node = graphNodes[index] // WARNING guard let node = graphNodes[index] // WARNING guard

View File

@ -26,7 +26,7 @@ import ElementaryCyclesSearch
typealias Node = String typealias Node = String
private func printCycles(_ cycles: Array<Array<Node>>) { private func printCycles(_ cycles: [[Node]]) {
for i in 0 ..< cycles.count { for i in 0 ..< cycles.count {
let cycle = cycles[i] let cycle = cycles[i]
for j in 0 ..< cycle.count { for j in 0 ..< cycle.count {
@ -41,8 +41,8 @@ private func printCycles(_ cycles: Array<Array<Node>>) {
} }
} }
let nodes: Array<Node> = { let nodes: [Node] = {
var vector = Array<Node>() var vector = [Node]()
for i in 0 ..< 10 { for i in 0 ..< 10 {
vector.append("Node \(i)") vector.append("Node \(i)")
} }

View File

@ -6,8 +6,8 @@ final class ElementaryCyclesSearchTests: XCTestCase {
private typealias Node = String private typealias Node = String
private var nodes: Array<Node> { private var nodes: [Node] {
var nodes = Array<Node>() var nodes = [Node]()
for i in 0 ..< 10 { for i in 0 ..< 10 {
nodes.append("Node \(i)") nodes.append("Node \(i)")
} }
@ -29,9 +29,9 @@ final class ElementaryCyclesSearchTests: XCTestCase {
matrix[6][1] = true matrix[6][1] = true
} }
private var sut: ((AdjacencyMatrix, Array<Node>) -> Array<Array<Node>>)! private var sut: ((AdjacencyMatrix, [Node]) -> [[Node]])!
private func prettify(cycles: Array<Array<Node>>) -> String { private func prettify(cycles: [[Node]]) -> String {
var description = "" var description = ""
for i in 0 ..< cycles.count { for i in 0 ..< cycles.count {
let cycle = cycles[i] let cycle = cycles[i]

View File

@ -7,8 +7,8 @@ final class ElementaryCyclesSearchShould: XCTestCase {
private typealias Node = String private typealias Node = String
private var nodes: Array<Node> { private var nodes: [Node] {
var nodes = Array<Node>() var nodes = [Node]()
for i in 0 ..< 10 { for i in 0 ..< 10 {
nodes.append("Node \(i)") nodes.append("Node \(i)")
} }