GithubHelp home page GithubHelp logo

Comments (4)

SeaDve avatar SeaDve commented on June 29, 2024 1

If you're still interested, could you give #294 a try?

I tested it and it works well, thanks!

from indexmap.

cuviper avatar cuviper commented on June 29, 2024

there could also be some method that could insert an item to a particular index, so the behavior of splice could be replicated.

That's #173 -- the core pieces are there with move_index, at least. But it would be inefficient if you are inserting many items this way.

Currently, I do the following to replicate the behavior:

let end_part = inner_index.split_off(position);
inner_index.extend(new_items.chain(end_part.into_iter().skip(n_removed)));

This is not bad, although we could avoid some extra hashing if we did this internally.

However, if new_items and end_part have any overlapping keys, the latter will replace its value. I would expect you actually want the keep the new_items value -- or maybe you're just sure there's no overlap in your scenario.

let end_part = inner_index.split_off(position);
// this may overlap the beginning keys -- should they move?
inner_index.extend(new_items); 
for (key, value) in end_part.into_iter().skip(n_removed) {
    // This will avoid clobbering any new_items, but should they be
    // in their old (shifted/relative) position or the new spliced position?
    inner_index.entry(key).or_insert(value);
}

from indexmap.

SeaDve avatar SeaDve commented on June 29, 2024

or maybe you're just sure there's no overlap in your scenario.

Yep, in my use case, it is ensured that there are no duplicate keys.

In general, though, I would expect existing keys to stay where they are; only unique keys are put on the gap, and the non-unique ones are simply used to update existing values at both ends. But, the behavior may be unexpected and might not overlap with other use cases.

A better alternative, I think, is to insert only unique keys (i.e., keys that are not on the map) on the gap, and return the non-unique keys as an iterator, so the caller could choose to use them to update values on the map explicitly, simply ignore them, or use them to assert that all items are inserted on the gap.

from indexmap.

cuviper avatar cuviper commented on June 29, 2024

If you're still interested, could you give #294 a try?

from indexmap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.