Skip to content

Allow converting values, preserving keys #80

Open

Description

@NickNick

I have a slotmap like <FooID, FooV1> and now I'd like to make <FooID, FooV2>, without changing the keys. Is there some way I can do this? I couldn't figure it out. Thanks.

Activity

  1. orlp commented on Nov 17, 2021

    @orlp
    Owner

    Right now there isn't a direct way to do this on a slotmap. You could have the keys allocated by one slotmap and then store the values in a SecondaryMap. You can have multiple SecondaryMaps with the same key, so creating a v2 shouldn't be an issue.

  2. NeverGivinUp commented on Feb 11, 2022

    @NeverGivinUp

    Same requirement here. Though it took a lot of debugging to even notice the source of my problem...

    I layout a graph. The layout happens in sequential process steps. In each step I need some shared and some different data.
    When I convert the nodes they get new keys. But the edges and other data structure still refer to the old indices. And since the keys are of the same type, this went unnoticed for a while and was really hard to debug, when I eventually noticed it.

    Update the references to nodes (the keys) would take a lot of computing time. There usually are much more references to nodes than there are nodes.

    I havn't looked into using a SecondaryMap yet. What is the impact of using SecondaryMap on cache locality?

  3. NeverGivinUp commented on Feb 28, 2022

    @NeverGivinUp

    @orlp Using SecondaryMap works well, generally. And the conversion quite easy, too. I have some duplicate data now, but that isn't so bad, and could be remedied, if it proved problematic.

    One problem I noted is that when iterating over the secondary map after removing an element from the primary, but not from the secondary, the removed element still is iterated.

  4. orlp commented on Feb 28, 2022

    @orlp
    Owner

    @NeverGivinUp

    One problem I noted is that when iterating over the secondary map after removing an element from the primary, but not from the secondary, the removed element still is iterated.

    Yes, a SecondaryMap is completely independent from its primary map. It can re-use slots so old things get deleted automatically eventually if their space is needed, but nothing is guaranteed. If you want them to stay synced you have to perform a delete on a primary map and all secondary maps.

    Note that I do think I will implement the following function in slotmap 2.0, when I have the time and energy to make it:

    pub fn map<U, F>(self, f: F) -> SlotMap<K, U>
    where F: FnMut((K, V) -> U)
  5. lmondada commented on Aug 14, 2025

    @lmondada

    I have a use case for a map function that is not covered by SecondaryMap.

    I need to serialize/deserialize a SlotMap<T>, however in my case T is not serializable. Using a map function, I would proceed as follows

    • serialization: SlotMap<T> -> SlotMap<U> -> Serial where U is serializable/deserializable
    • deserialization: Serial -> SlotMap<U> -> SlotMap<T>

    Using a SecondaryMapfor this use case is not possible, as I cannot implement a Deserialize that would recover the original SlotMap<T> from a SecondaryMap<U>.

    @orlp Would you consider a PR that provides the map implementation? Happy to write it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions