Skip to content

Commit

Permalink
Added basic SwiftLint config file. Fixing all linting errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Pilcher committed Jun 8, 2016
1 parent 3df07d4 commit e164943
Show file tree
Hide file tree
Showing 154 changed files with 1,240 additions and 1,232 deletions.
25 changes: 25 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cyclomatic_complexity: 12
file_length: 550
function_body_length: 80
function_parameter_count: 8
line_length: 150
type_body_length: 300
variable_name:
min_length:
error: 1
warning: 1
excluded:
- N

disabled_rules:
- valid_docs

custom_rules:
smiley_face:
name: "Smiley Face"
regex: "(\:\))"
match_kinds:
- comment
- string
message: "A closing parenthesis smiley :) creates a half-hearted smile, and thus is not preferred. Use :]"
severity: warning
2 changes: 1 addition & 1 deletion AVL Tree/AVLTree.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ tree.delete(5)
tree.delete(2)
tree.delete(1)
tree.delete(4)
tree.delete(3)
tree.delete(3)
40 changes: 19 additions & 21 deletions AVL Tree/Tests/AVLTreeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import XCTest

class AVLTreeTests: XCTestCase {
var tree : AVLTree<Int, String>?

var tree: AVLTree<Int, String>?

override func setUp() {
super.setUp()

tree = AVLTree()
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
Expand Down Expand Up @@ -60,8 +60,8 @@ class AVLTreeTests: XCTestCase {
}

func testEmptyInitialization() {
let tree = AVLTree<Int,String>()
let tree = AVLTree<Int, String>()

XCTAssertEqual(tree.size, 0)
XCTAssertNil(tree.root)
}
Expand All @@ -71,45 +71,45 @@ class AVLTreeTests: XCTestCase {
self.tree?.insert(5, "E")
}
}

func testMultipleInsertionsPerformance() {
self.measureBlock {
self.tree?.autopopulateWithNodes(50)
}
}

func testSearchExistentOnSmallTreePerformance() {
self.measureBlock {
self.tree?.search(2)
}
}

func testSearchExistentElementOnLargeTreePerformance() {
self.measureBlock {
self.tree?.autopopulateWithNodes(500)
self.tree?.search(400)
}
}

func testMinimumOnPopulatedTree() {
self.tree?.autopopulateWithNodes(500)
let min = self.tree?.root?.minimum()
XCTAssertNotNil(min, "Minimum function not working")
}

func testMinimumOnSingleTreeNode() {
let treeNode = TreeNode(key: 1, payload: "A")
let min = treeNode.minimum()

XCTAssertNotNil(min, "Minimum on single node should be returned")
XCTAssertEqual(min?.payload,treeNode.payload)
XCTAssertEqual(min?.payload, treeNode.payload)
}

func testDeleteExistentKey() {
self.tree?.delete(1)
XCTAssertNil(self.tree?.search(1), "Key should not exist anymore")
}

func testDeleteNotExistentKey() {
self.tree?.delete(1056)
XCTAssertNil(self.tree?.search(1056), "Key should not exist")
Expand All @@ -122,15 +122,15 @@ class AVLTreeTests: XCTestCase {
XCTAssertEqual(tree.size, i + 1, "Insert didn't update size correctly!")
}
}

func testDelete() {
let permutations = [
[5, 1, 4, 2, 3],
[2, 3, 1, 5, 4],
[4, 5, 3, 2, 1],
[3, 2, 5, 4, 1],
]

for p in permutations {
let tree = AVLTree<Int, String>()

Expand All @@ -151,8 +151,8 @@ class AVLTreeTests: XCTestCase {
}

extension AVLTree where Key : SignedIntegerType {
func autopopulateWithNodes(count : Int) {
var k : Key = 1
func autopopulateWithNodes(count: Int) {
var k: Key = 1
for _ in 0...count {
self.insert(k)
k = k + 1
Expand Down Expand Up @@ -185,5 +185,3 @@ extension AVLTree where Key : SignedIntegerType {
}
}
}


46 changes: 23 additions & 23 deletions AVL Tree/Tests/TreeNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,50 @@
import XCTest

class TreeNodeTests: XCTestCase {
var root : TreeNode<String,String>?
var left : TreeNode<String,String>?
var right : TreeNode<String,String>?

var root: TreeNode<String, String>?
var left: TreeNode<String, String>?
var right: TreeNode<String, String>?

override func setUp() {
super.setUp()

left = TreeNode<String,String>(key: "Name", payload: "Left")
right = TreeNode<String,String>(key: "Name", payload: "Right")
root = TreeNode<String,String>(key: "Name", payload: "Root", leftChild: left, rightChild: right, parent: nil, height: 0)
left = TreeNode<String, String>(key: "Name", payload: "Left")
right = TreeNode<String, String>(key: "Name", payload: "Right")
root = TreeNode<String, String>(key: "Name", payload: "Root", leftChild: left, rightChild: right, parent: nil, height: 0)
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}


func testSingleNodeCreationNOPayload() {
let treeNode = TreeNode<String,String>(key: "Building")
let treeNode = TreeNode<String, String>(key: "Building")
XCTAssertNil(treeNode.payload, "Payload for this case should be nil")
}

func testSingleNodeCreationWithPayload() {
XCTAssertNotNil(self.root!, "Payload for this case should not be nil")
}

func testIsRoot() {
XCTAssertTrue(self.root!.isRoot)
}

func testNotIsLeaf() {
XCTAssertFalse(self.root!.isLeaf, "root node is not leaf")
}

func testNotIsLeftChild() {
XCTAssertFalse(self.root!.isLeftChild, "root node is not left child")
}

func testNotIsRightChild() {
XCTAssertFalse(self.root!.isRightChild, "root node is not right child")
}

func testIsLeftChild() {
XCTAssertTrue(self.left!.isLeftChild)
}
Expand All @@ -64,21 +64,21 @@ class TreeNodeTests: XCTestCase {
func isLeaf() {
XCTAssertTrue(self.left!.isLeaf)
}

func testHasAnyChild() {
XCTAssertTrue(self.root!.hasAnyChild)
}

func testNotHasAnyChild() {
XCTAssertFalse(self.left!.hasAnyChild)
}

func testHasBothChildren() {
XCTAssertTrue(self.root!.hasBothChildren)
}

func testNotHasBothChildren() {
XCTAssertFalse(self.left!.hasBothChildren)
}

}
7 changes: 5 additions & 2 deletions All-Pairs Shortest Paths/APSP/APSP/APSP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import Foundation
import Graph

/**
`APSPAlgorithm` is a protocol for encapsulating an All-Pairs Shortest Paths algorithm. It provides a single function `apply` that accepts a subclass of `AbstractGraph` and returns an object conforming to `APSPResult`.
`APSPAlgorithm` is a protocol for encapsulating an All-Pairs Shortest Paths algorithm.
It provides a single function `apply` that accepts a subclass of `AbstractGraph` and
returns an object conforming to `APSPResult`.
*/
public protocol APSPAlgorithm {

Expand All @@ -21,7 +23,8 @@ public protocol APSPAlgorithm {
}

/**
`APSPResult` is a protocol defining functions `distance` and `path`, allowing for opaque queries into the actual data structures that represent the APSP solution according to the algorithm used.
`APSPResult` is a protocol defining functions `distance` and `path`, allowing for opaque
queries into the actual data structures that represent the APSP solution according to the algorithm used.
*/
public protocol APSPResult {

Expand Down
41 changes: 26 additions & 15 deletions All-Pairs Shortest Paths/APSP/APSP/FloydWarshall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
}

/**
For each iteration of `intermediateIdx`, perform the comparison for the dynamic algorith, checking for each pair of start/end vertices, whether a path taken through another vertex produces a shorter path.
For each iteration of `intermediateIdx`, perform the comparison for the dynamic algorith,
checking for each pair of start/end vertices, whether a path taken through another vertex
produces a shorter path.
- complexity: `Θ(V^2)` time/space
- returns: a tuple containing the next distance matrix with weights of currently known shortest paths and the corresponding predecessor matrix
- returns: a tuple containing the next distance matrix with weights of currently known
shortest paths and the corresponding predecessor matrix
*/
static private func nextStep<T>(intermediateIdx: Int, previousDistances: Distances, previousPredecessors: Predecessors, graph: AbstractGraph<T>) -> StepResult {
static private func nextStep<T>(intermediateIdx: Int, previousDistances: Distances,
previousPredecessors: Predecessors, graph: AbstractGraph<T>) -> StepResult {

let vertexCount = graph.vertices.count
var nextDistances = Array(count: vertexCount, repeatedValue: Array(count: vertexCount, repeatedValue: Double.infinity))
Expand Down Expand Up @@ -85,9 +89,11 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {

}

/**
We need to map the graph's weight domain onto the one required by the algorithm: the graph stores either a weight as a `Double` or `nil` if no edge exists between two vertices, but the algorithm needs a lack of an edge represented as ∞ for the `min` comparison to work correctly.
/**
We need to map the graph's weight domain onto the one required by the algorithm: the graph
stores either a weight as a `Double` or `nil` if no edge exists between two vertices, but
the algorithm needs a lack of an edge represented as ∞ for the `min` comparison to work correctly.
- complexity: `Θ(V^2)` time/space
- returns: weighted adjacency matrix in form ready for processing with Floyd-Warshall
*/
Expand Down Expand Up @@ -116,7 +122,7 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {

/**
Make the initial predecessor index matrix. Initially each value is equal to it's row index, it's "from" index when querying into it.
- complexity: `Θ(V^2)` time/space
*/
static private func constructInitialPredecessorMatrix(distances: Distances) -> Predecessors {
Expand All @@ -139,17 +145,20 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
}

/**
`FloydWarshallResult` encapsulates the result of the computation, namely the minimized distance adjacency matrix, and the matrix of predecessor indices.
It conforms to the `APSPResult` procotol which provides methods to retrieve distances and paths between given pairs of start and end nodes.
`FloydWarshallResult` encapsulates the result of the computation, namely the
minimized distance adjacency matrix, and the matrix of predecessor indices.
It conforms to the `APSPResult` procotol which provides methods to retrieve
distances and paths between given pairs of start and end nodes.
*/
public struct FloydWarshallResult<T where T: Hashable>: APSPResult {

private var weights: Distances
private var predecessors: Predecessors

/**
- returns: the total weight of the path from a starting vertex to a destination. This value is the minimal connected weight between the two vertices, or `nil` if no path exists
- returns: the total weight of the path from a starting vertex to a destination.
This value is the minimal connected weight between the two vertices, or `nil` if no path exists
- complexity: `Θ(1)` time/space
*/
public func distance(fromVertex from: Vertex<T>, toVertex to: Vertex<T>) -> Double? {
Expand All @@ -159,7 +168,8 @@ public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
}

/**
- returns: the reconstructed path from a starting vertex to a destination, as an array containing the data property of each vertex, or `nil` if no path exists
- returns: the reconstructed path from a starting vertex to a destination,
as an array containing the data property of each vertex, or `nil` if no path exists
- complexity: `Θ(V)` time, `Θ(V^2)` space
*/
public func path(fromVertex from: Vertex<T>, toVertex to: Vertex<T>, inGraph graph: AbstractGraph<T>) -> [T]? {
Expand All @@ -175,11 +185,13 @@ public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
}

/**
The recursive component to rebuilding the shortest path between two vertices using the predecessor matrix.
The recursive component to rebuilding the shortest path between
two vertices using the predecessor matrix.
- returns: the list of predecessors discovered so far
*/
private func recursePathFrom(fromVertex from: Vertex<T>, toVertex to: Vertex<T>, path: [Vertex<T>], inGraph graph: AbstractGraph<T>) -> [Vertex<T>]? {
private func recursePathFrom(fromVertex from: Vertex<T>, toVertex to: Vertex<T>, path: [Vertex<T>],
inGraph graph: AbstractGraph<T>) -> [Vertex<T>]? {

if from.index == to.index {
return [ from, to ]
Expand All @@ -198,7 +210,6 @@ public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
}

return nil

}

}
Loading

0 comments on commit e164943

Please sign in to comment.