Skip to content

Commit

Permalink
Refactored Hash List implementation to generic model..
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewbishop committed Jul 22, 2016
1 parent a101b5c commit 1368201
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 43 deletions.
16 changes: 8 additions & 8 deletions Source/Factories/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ public class SwiftGraph {


//check condition for an undirected graph
if (isDirected == false) {
if isDirected == false {


//create a new reversed edge
let reverseEdge = Edge()
//create a new reversed edge
let reverseEdge = Edge()


//establish the reversed properties
reverseEdge.neighbor = source
reverseEdge.weight = weight
neighbor.neighbors.append(reverseEdge)
//establish the reversed properties
reverseEdge.neighbor = source
reverseEdge.weight = weight
neighbor.neighbors.append(reverseEdge)

print("The neighbor of vertex: \(neighbor.key as String!) is \(source.key as String!)..")
print("The neighbor of vertex: \(neighbor.key as String!) is \(source.key as String!)..")

}

Expand Down
80 changes: 70 additions & 10 deletions Source/Factories/HashList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,31 @@ class HashList<T> {
}



//add element to list
func append(_ element: T, key: String) -> HashResults {
func append(_ element: T) -> HashResults {


let results: HashResults
var hashIndex = 0


//determine hash
let hashIndex = self.createHash(key)

//test list types
if let elementKey = model(with: element) {
hashIndex = self.createHash(elementKey)
}
else {
return HashResults.NotSupported
}


//placeholder elements
let childToUse = HashElement<T>(withKey: key)
let childToUse = HashElement<T>()
var head: HashElement<T>?



childToUse.element = element


Expand Down Expand Up @@ -93,24 +102,75 @@ class HashList<T> {
//check chained list for key
while current != nil {

if current?.key == key {
print("element found..")
return (current, HashResults.Success)

//test model - compare element keys
if let elementKey = model(with: (current?.element)!) {

if elementKey == key {
print("element found..")
return (current, HashResults.Success)
}
}

print("searching through chained list..")
current = current?.next

}


return (nil, HashResults.Fail)

}




//MARK: Helper Functions


//MARK: Helper Function

//determine supported list types
func model(with element: T) -> String? {


/*
note: since various structures will manage key data differently,
this model can be extended to support different types.
*/

switch element {


//string type
case is String:
return String(element)



//int type
case is Int:
let stringElement = String(element)
return stringElement



//vertex type
case is Vertex:
if let eVertex = element as? Vertex {
return eVertex.key
}
else {
return nil
}


default:
return nil
}


}




//hash based on string
Expand All @@ -131,7 +191,7 @@ class HashList<T> {

remainder = divisor % buckets.count

return remainder - 1
return remainder
}


Expand Down
7 changes: 4 additions & 3 deletions Source/Factories/PathHeap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ public class PathHeap {
//obtain the minimum path
func peek() -> Path! {

if (heap.count > 0) {
return heap[0] //the shortest path
if heap.count > 0 {
return heap[0] //the shortest path
}
else {
return nil
}

}



//remove the minimum path
func deQueue() {

if (heap.count > 0) {
if heap.count > 0 {
heap.remove(at: 0)
}

Expand Down
7 changes: 0 additions & 7 deletions Source/Structures/HashElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ import Foundation

class HashElement<T> {

var key: String
var element: T?
var next: HashElement<T>?


init(withKey key: String) {
self.key = key
}

}
1 change: 1 addition & 0 deletions Source/Structures/enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum HashResults {
case Success
case Collision
case NotFound
case NotSupported
case Fail
}

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,37 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftTests/ListTest.swift"
timestampString = "490838503.068996"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "51"
endingLineNumber = "51"
landmarkName = "testStringList()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftTests/ListTest.swift"
timestampString = "490838496.854884"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "34"
endingLineNumber = "34"
landmarkName = "testStringList()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
31 changes: 16 additions & 15 deletions SwiftTests/ListTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,29 @@ class ListTest: XCTestCase {


/*
note: each element has its own corresponding "slot" in the
hash list with the exception of hash "collisions".
note: each element has its own potential 'slot' in the
hash list. In this scenario, the hash table algorithm will
implement 'separate chaining' to support 'hash collisions'.
*/


//new string list
let slist = HashList<String>(capacity: 25)
let slist = HashList<String>(capacity: 3)


_ = slist.append("Wayne Bishop", key: "Wayne")
_ = slist.append("Frank Smith", key: "Frank")
_ = slist.append("Jennifer Hobbs", key: "Jennifer")
_ = slist.append("Tim Cook", key: "Tim")
_ = slist.append("Steve Jobs", key: "Steve")
_ = slist.append("Wayne Bishop", key: "Wayne") //should produce collision
_ = slist.append("Larry Page", key: "Larry")
_ = slist.append("Albert Einstien", key: "Albert")
_ = slist.append("Wayne Bishop")
_ = slist.append("Frank Smith")
_ = slist.append("Jennifer Hobbs")
_ = slist.append("Tim Cook")
_ = slist.append("Steve Jobs")
_ = slist.append("Wayne Bishop") //should produce collision
_ = slist.append("Larry Page")
_ = slist.append("Albert Einstien")



//obtain element
let element = slist.getElement(with: "Larry")
let element = slist.getElement(with: "Frank Smith")

if let results = element.0 {
let rString: String? = results.element
Expand Down Expand Up @@ -74,7 +75,7 @@ class ListTest: XCTestCase {
testVertex.key = "A"

let vList: HashList = HashList<Vertex>(capacity: 10)
_ = vList.append(testVertex, key: testVertex.key!)
_ = vList.append(testVertex)



Expand All @@ -100,8 +101,8 @@ class ListTest: XCTestCase {
//new float list
let fList = HashList<Float>(capacity: 5)

_ = fList.append(10.2, key: String(10.2))
_ = fList.append(8.6, key: String(8.6))
_ = fList.append(10.2)
_ = fList.append(8.6)


//element doesn't exist..
Expand Down

0 comments on commit 1368201

Please sign in to comment.