Skip to content

Commit

Permalink
Added support for == operator and initialization from pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskubanek committed Sep 10, 2015
1 parent b00cb64 commit c318c55
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Sources/OrderedDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/// The `OrderedDictionary` is a collection which combines the features of `Dictionary` and `Array`.
/// It maps keys to values and additionally sorts the key-value pairs by zero-based integer index.
public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLiteralConvertible, CustomStringConvertible {
public struct OrderedDictionary<Key: Hashable, Value>: CollectionType, ArrayLiteralConvertible, CustomStringConvertible {

// MARK: - Initialization

Expand All @@ -17,14 +17,18 @@ public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLit
self.keysToValues = [:]
}

public init(arrayLiteral elements: Element...) {
public init(elements: [Element]) {
self.init()

for element in elements {
self[element.0] = element.1
}
}

public init(arrayLiteral elements: Element...) {
self.init(elements: elements)
}

// MARK: - Type Aliases

public typealias Element = (Key, Value)
Expand Down Expand Up @@ -168,3 +172,7 @@ public struct OrderedDictionary<Key : Hashable, Value>: CollectionType, ArrayLit
public var endIndex: Index { return orderedKeys.endIndex }

}

public func == <Key: Equatable, Value: Equatable>(lhs: OrderedDictionary<Key, Value>, rhs: OrderedDictionary<Key, Value>) -> Bool {
return lhs.orderedKeys == rhs.orderedKeys && lhs.keysToValues == rhs.keysToValues
}
10 changes: 10 additions & 0 deletions Tests/OrderedDictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ class OrderedDictionaryTests: XCTestCase {
expect(self.orderedDictionary.containsKey("C")) == true
}

func testInitializationUsingPairs() {
let elements = [
("A", 1),
("B", 2),
("C", 3)
]

expect(OrderedDictionary(elements: elements) == self.orderedDictionary).to(beTrue())
}

func testElementsGenerator() {
for entry in self.orderedDictionary.enumerate() {
expect(self.orderedDictionary[entry.index].0) == entry.element.0
Expand Down

0 comments on commit c318c55

Please sign in to comment.