Skip to content

Commit

Permalink
Merge pull request #7 from DandyLyons/main
Browse files Browse the repository at this point in the history
`Collection` conformance for `LeftValues` and `RightValues`
  • Loading branch information
ladvoc authored Sep 8, 2024
2 parents 9b84532 + 545481b commit 175cec4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
19 changes: 17 additions & 2 deletions Sources/BijectiveDictionary/BijectiveDictionary+LeftValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,23 @@ extension BijectiveDictionary.LeftValues: Sequence {
}

// MARK: - Collection

// TODO: implement collection
extension BijectiveDictionary.LeftValues: Collection {

public typealias Index = BijectiveDictionary<Left, Right>.Index

@inlinable public var startIndex: Index { Index(_ltr.startIndex) }
@inlinable public var endIndex: Index { Index(_ltr.endIndex) }

@inlinable
public func index(after i: Index) -> Index {
Index(_ltr.index(after: i._ltrIndex))
}

@inlinable
public subscript(position: Index) -> Left {
_ltr[position._ltrIndex].key
}
}

// MARK: - Other Conformances

Expand Down
20 changes: 17 additions & 3 deletions Sources/BijectiveDictionary/BijectiveDictionary+RightValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,23 @@ extension BijectiveDictionary.RightValues: Sequence {
}

// MARK: - Collection

// TODO: implement collection

extension BijectiveDictionary.RightValues: Collection {

public typealias Index = BijectiveDictionary<Left, Right>.Index

@inlinable public var startIndex: Index { Index(_ltr.startIndex) }
@inlinable public var endIndex: Index { Index(_ltr.endIndex) }

@inlinable
public func index(after i: Index) -> Index {
Index(_ltr.index(after: i._ltrIndex))
}

@inlinable
public subscript(position: Index) -> Right {
_ltr[position._ltrIndex].value
}
}
// MARK: - Other Conformances

extension BijectiveDictionary.RightValues: CustomStringConvertible {
Expand Down
12 changes: 12 additions & 0 deletions Tests/BijectiveDictionaryTests/BijectiveDictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,23 @@ func collection(dict: BijectiveDictionary<String, Int>) {
@Test func leftValues() {
let dict: BijectiveDictionary = ["A": 1, "B": 2, "C": 3]
#expect(Set(dict.leftValues) == ["A", "B", "C"])

let leftValues = dict.leftValues
let assertions = ["A", "B", "C"]
for assertion in assertions {
#expect(leftValues.contains(assertion))
}
}

@Test func rightValues() {
let dict: BijectiveDictionary = ["A": 1, "B": 2, "C": 3]
#expect(Set(dict.rightValues) == [1, 2, 3])

let rightValues = dict.rightValues
let assertions = [1, 2, 3]
for assertion in assertions {
#expect(rightValues.contains(assertion))
}
}

@Test("leftValues and rightValues should have the same order")
Expand Down

0 comments on commit 175cec4

Please sign in to comment.