Skip to content

Commit

Permalink
Merge pull request #276 from JaapWijnen/hashsetswift3
Browse files Browse the repository at this point in the history
Hashset migrate to swift3
  • Loading branch information
kelvinlauKL committed Nov 26, 2016
2 parents c783351 + 8328491 commit 85b05cf
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 122 deletions.
72 changes: 0 additions & 72 deletions Hash Set/HashSet.playground/Contents.swift
@@ -1,33 +1,5 @@
//: Playground - noun: a place where people can play

public struct HashSet<T: Hashable> {
private var dictionary = Dictionary<T, Bool>()

public mutating func insert(element: T) {
dictionary[element] = true
}

public mutating func remove(element: T) {
dictionary[element] = nil
}

public func contains(element: T) -> Bool {
return dictionary[element] != nil
}

public func allElements() -> [T] {
return Array(dictionary.keys)
}

public var count: Int {
return dictionary.count
}

public var isEmpty: Bool {
return dictionary.isEmpty
}
}

var set = HashSet<String>()

set.insert("one")
Expand All @@ -43,24 +15,8 @@ set.remove("one")
set.allElements()
set.contains("one")



/* Union */

extension HashSet {
public func union(otherSet: HashSet<T>) -> HashSet<T> {
var combined = HashSet<T>()
for obj in dictionary.keys {
combined.insert(obj)
}
for obj in otherSet.dictionary.keys {
combined.insert(obj)
}
return combined
}
}


var setA = HashSet<Int>()
setA.insert(1)
setA.insert(2)
Expand All @@ -76,41 +32,13 @@ setB.insert(6)
let union = setA.union(setB)
union.allElements() // [5, 6, 2, 3, 1, 4]



/* Intersection */

extension HashSet {
public func intersect(otherSet: HashSet<T>) -> HashSet<T> {
var common = HashSet<T>()
for obj in dictionary.keys {
if otherSet.contains(obj) {
common.insert(obj)
}
}
return common
}
}

let intersection = setA.intersect(setB)
intersection.allElements() // [3, 4]



/* Difference */

extension HashSet {
public func difference(otherSet: HashSet<T>) -> HashSet<T> {
var diff = HashSet<T>()
for obj in dictionary.keys {
if !otherSet.contains(obj) {
diff.insert(obj)
}
}
return diff
}
}

let difference1 = setA.difference(setB)
difference1.allElements() // [2, 1]

Expand Down
76 changes: 76 additions & 0 deletions Hash Set/HashSet.playground/Sources/HashSet.swift
@@ -0,0 +1,76 @@
//: Playground - noun: a place where people can play

public struct HashSet<T: Hashable> {
fileprivate var dictionary = Dictionary<T, Bool>()

public init() {

}

public mutating func insert(_ element: T) {
dictionary[element] = true
}

public mutating func remove(_ element: T) {
dictionary[element] = nil
}

public func contains(_ element: T) -> Bool {
return dictionary[element] != nil
}

public func allElements() -> [T] {
return Array(dictionary.keys)
}

public var count: Int {
return dictionary.count
}

public var isEmpty: Bool {
return dictionary.isEmpty
}
}

/* Union */

extension HashSet {
public func union(_ otherSet: HashSet<T>) -> HashSet<T> {
var combined = HashSet<T>()
for obj in self.dictionary.keys {
combined.insert(obj)
}
for obj in otherSet.dictionary.keys {
combined.insert(obj)
}
return combined
}
}

/* Intersection */

extension HashSet {
public func intersect(_ otherSet: HashSet<T>) -> HashSet<T> {
var common = HashSet<T>()
for obj in dictionary.keys {
if otherSet.contains(obj) {
common.insert(obj)
}
}
return common
}
}

/* Difference */

extension HashSet {
public func difference(_ otherSet: HashSet<T>) -> HashSet<T> {
var diff = HashSet<T>()
for obj in dictionary.keys {
if !otherSet.contains(obj) {
diff.insert(obj)
}
}
return diff
}
}
104 changes: 54 additions & 50 deletions Hash Set/README.markdown
Expand Up @@ -38,31 +38,35 @@ Here are the beginnings of `HashSet` in Swift:

```swift
public struct HashSet<T: Hashable> {
private var dictionary = Dictionary<T, Bool>()

public mutating func insert(element: T) {
dictionary[element] = true
}

public mutating func remove(element: T) {
dictionary[element] = nil
}

public func contains(element: T) -> Bool {
return dictionary[element] != nil
}

public func allElements() -> [T] {
return Array(dictionary.keys)
}

public var count: Int {
return dictionary.count
}

public var isEmpty: Bool {
return dictionary.isEmpty
}
fileprivate var dictionary = Dictionary<T, Bool>()

public init() {

}

public mutating func insert(_ element: T) {
dictionary[element] = true
}

public mutating func remove(_ element: T) {
dictionary[element] = nil
}

public func contains(_ element: T) -> Bool {
return dictionary[element] != nil
}

public func allElements() -> [T] {
return Array(dictionary.keys)
}

public var count: Int {
return dictionary.count
}

public var isEmpty: Bool {
return dictionary.isEmpty
}
}
```

Expand Down Expand Up @@ -101,16 +105,16 @@ Here is the code for the union operation:

```swift
extension HashSet {
public func union(otherSet: HashSet<T>) -> HashSet<T> {
var combined = HashSet<T>()
for obj in dictionary.keys {
combined.insert(obj)
}
for obj in otherSet.dictionary.keys {
combined.insert(obj)
public func union(_ otherSet: HashSet<T>) -> HashSet<T> {
var combined = HashSet<T>()
for obj in self.dictionary.keys {
combined.insert(obj)
}
for obj in otherSet.dictionary.keys {
combined.insert(obj)
}
return combined
}
return combined
}
}
```

Expand Down Expand Up @@ -141,15 +145,15 @@ The *intersection* of two sets contains only the elements that they have in comm

```swift
extension HashSet {
public func intersect(otherSet: HashSet<T>) -> HashSet<T> {
var common = HashSet<T>()
for obj in dictionary.keys {
if otherSet.contains(obj) {
common.insert(obj)
}
public func intersect(_ otherSet: HashSet<T>) -> HashSet<T> {
var common = HashSet<T>()
for obj in dictionary.keys {
if otherSet.contains(obj) {
common.insert(obj)
}
}
return common
}
return common
}
}
```

Expand All @@ -166,15 +170,15 @@ Finally, the *difference* between two sets removes the elements they have in com

```swift
extension HashSet {
public func difference(otherSet: HashSet<T>) -> HashSet<T> {
var diff = HashSet<T>()
for obj in dictionary.keys {
if !otherSet.contains(obj) {
diff.insert(obj)
}
public func difference(_ otherSet: HashSet<T>) -> HashSet<T> {
var diff = HashSet<T>()
for obj in dictionary.keys {
if !otherSet.contains(obj) {
diff.insert(obj)
}
}
return diff
}
return diff
}
}
```

Expand Down

0 comments on commit 85b05cf

Please sign in to comment.