Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection conformance for LeftValues and RightValues #7

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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