ladvoc / BijectiveDictionary

A specialized dictionary structure offering bijective mapping and bidirectional O(1) access.
https://jacobgelman.com/posts/swift-bijective-dictionary
MIT License
2 stars 1 forks source link

Consider useful features #5

Open ladvoc opened 2 weeks ago

ladvoc commented 2 weeks ago

Given the unique characteristics of BijectiveDictionary, think about features that would be useful in general-purpose applications and consider which should be added. For example, the bimap crate in Rust (which implements a bijective dictionary) includes an insert method that informs the caller if any existing entry was overwritten during the insertion.

DandyLyons commented 2 weeks ago

Agreed. That functionality would be very useful. IdentifiedArray has something similar where it will inform you if the item was successfully inserted or not. It might be worth it to draw inspiration from this API, so that we don't have to reinvent the wheel. (Or there might be an example in the standard library or Swift Collections, that would be better to follow).

https://swiftpackageindex.com/pointfreeco/swift-identified-collections/main/documentation/identifiedcollections/identifiedarray/insert(_:at:)

Also, part of me would like to be able to declare at the call site, if it is okay to override. If I don't want it to override, and if the element already exists, then it will not insert, and it will let me know the result.

Something like bijectiveDictionary.insert(left: element, override: true)