Skip to content

Adds mapValues and compactMapValues to CxxDictionary: #83429

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions stdlib/public/Cxx/CxxDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,60 @@ extension CxxDictionary {
}
}

@inlinable
public func mapValues<R, E>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this method also be @inlinable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! I've added the @inlinable attribute.

Could you please re-trigger the test job?

_ transform: (Value) throws(E) -> R.Value
) throws(E) -> R where R: CxxDictionary, R.Key == Key {
var result = R.init()

var iterator = __beginUnsafe()
let endIterator = __endUnsafe()

while iterator != endIterator {
let pair = iterator.pointee
try result.__insertUnsafe(R.Element(first: pair.first, second: transform(pair.second)))
iterator = iterator.successor()
}

return result
}

@inlinable
@_disfavoredOverload
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just trying to understand this better, why do we need @_disfavoredOverload on this overload?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without @_disfavoredOverload, this method would call itself recursively by default.

public func mapValues<E>(
_ transform: (Value) throws(E) -> Value
) throws(E) -> Self {
return try mapValues(transform) as Self
}

@inlinable
public func compactMapValues<R, E>(
_ transform: (Value) throws(E) -> R.Value?
) throws(E) -> R where R: CxxDictionary, R.Key == Key {
var result = R.init()

var iterator = __beginUnsafe()
let endIterator = __endUnsafe()

while iterator != endIterator {
let pair = iterator.pointee
if let value = try transform(pair.second) {
result.__insertUnsafe(R.Element(first: pair.first, second: value))
}
iterator = iterator.successor()
}

return result
}

@inlinable
@_disfavoredOverload
public func compactMapValues<E>(
_ transform: (Value) throws(E) -> Value?
) throws(E) -> Self {
return try compactMapValues(transform) as Self
}

public func filter(_ isIncluded: (_ key: Key, _ value: Value) throws -> Bool) rethrows -> Self {
var filteredDictionary = Self.init()
var iterator = __beginUnsafe()
Expand Down
2 changes: 2 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/std-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ using Map = std::map<int, int>;
using MapStrings = std::map<std::string, std::string>;
using NestedMap = std::map<int, Map>;
using MapGroup = std::map<int, std::vector<int>>;
using MapIntString = std::map<int, std::string>;
using UnorderedMap = std::unordered_map<int, int>;
using UnorderedMapGroup = std::unordered_map<int, std::vector<int>>;
using UnorderedIntString = std::unordered_map<int, std::string>;
inline Map initMap() { return {{1, 3}, {2, 2}, {3, 3}}; }
inline UnorderedMap initUnorderedMap() { return {{1, 3}, {3, 3}, {2, 2}}; }
inline Map initEmptyMap() { return {}; }
Expand Down
79 changes: 79 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,85 @@ StdMapTestSuite.test("UnorderedMap.subscript(:default:)") {
expectEqual(m[-5, default: 555], 555)
}

StdMapTestSuite.test("Map.mapValues") {
let m = initMap()
let n = m.mapValues { v in v * 2 }
expectEqual(n[1], 6)
expectEqual(n[2], 4)
expectEqual(n[3], 6)

let n2: MapIntString = m.mapValues { v in std.string("\(v * 3)") }
expectEqual(n2[1], std.string("9"))
expectEqual(n2[2], std.string("6"))
expectEqual(n2[3], std.string("9"))

let n3: UnorderedMap = m.mapValues { v in v * 4 }
expectEqual(n3[1], 12)
expectEqual(n3[2], 8)
expectEqual(n3[3], 12)
}

StdMapTestSuite.test("UnorderedMap.mapValues") {
let m = initUnorderedMap()
let n = m.mapValues { v in v * 2 }
expectEqual(n[1], 6)
expectEqual(n[2], 4)
expectEqual(n[3], 6)

let n2: UnorderedIntString = m.mapValues { v in std.string("\(v * 3)") }
expectEqual(n2[1], std.string("9"))
expectEqual(n2[2], std.string("6"))
expectEqual(n2[3], std.string("9"))

let n3: Map = m.mapValues { v in v * 4 }
expectEqual(n3[1], 12)
expectEqual(n3[2], 8)
expectEqual(n3[3], 12)
}

StdMapTestSuite.test("Map.compactMapValues") {
let m = Map([1: 1, 2: 2, 3: 3, 4: 4])
let n = m.compactMapValues { v in v % 2 == 0 ? nil : v * 2 }
expectEqual(n[1], 2)
expectNil(n[2])
expectEqual(n[3], 6)
expectNil(n[4])

let n2: MapIntString = m.compactMapValues { v in v % 2 == 0 ? nil : std.string("\(v * 3)") }
expectEqual(n2[1], std.string("3"))
expectNil(n2[2])
expectEqual(n2[3], std.string("9"))
expectNil(n2[4])

let n3: UnorderedMap = m.compactMapValues { v in v % 2 == 0 ? nil : v * 4 }
expectEqual(n3[1], 4)
expectNil(n3[2])
expectEqual(n3[3], 12)
expectNil(n3[4])
}

StdMapTestSuite.test("UnorderedMap.compactMapValues") {
let m = UnorderedMap([1: 1, 2: 2, 3: 3, 4: 4])

let n = m.compactMapValues { v in v % 2 == 0 ? nil : v * 2 }
expectEqual(n[1], 2)
expectNil(n[2])
expectEqual(n[3], 6)
expectNil(n[4])

let n2: UnorderedIntString = m.compactMapValues { v in v % 2 == 0 ? nil : std.string("\(v * 3)") }
expectEqual(n2[1], std.string("3"))
expectNil(n2[2])
expectEqual(n2[3], std.string("9"))
expectNil(n2[4])

let n3: Map = m.compactMapValues { v in v % 2 == 0 ? nil : v * 4 }
expectEqual(n3[1], 4)
expectNil(n3[2])
expectEqual(n3[3], 12)
expectNil(n3[4])
}

StdMapTestSuite.test("Map.filter") {
var m = initMap()
var n = initEmptyMap()
Expand Down