-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,60 @@ extension CxxDictionary { | |
} | ||
} | ||
|
||
@inlinable | ||
public func mapValues<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 | ||
try result.__insertUnsafe(R.Element(first: pair.first, second: transform(pair.second))) | ||
iterator = iterator.successor() | ||
} | ||
|
||
return result | ||
} | ||
|
||
@inlinable | ||
@_disfavoredOverload | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just trying to understand this better, why do we need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without |
||
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() | ||
|
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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?