question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feature request] insert with hint for TreeContainers

See original GitHub issue

Is your feature request related to a problem? Please describe.

I want to update the element in the TreeContainer (e.g. OrderedSet), but it is prohibited. It is reasonable because updated element could insert the different position.

However, users sometimes know the updated element is still the same position. For example,

Before update:
    V
[1, 3, 5]

After update:
    V
[1, 4, 5]

When I want to update the element, I need to do the following steps:

  1. container.find(key) the element’s iterator. Then got it.
  2. container.eraseElementByIterator(it)
  3. Create the new element based on the gotten element and insert it.

Step1 and 3 are O(logN) time complexity. I want to eliminate Step3 cost.

Describe the solution you’d like js-sdsl’s design refers to C++ STL. C++ std::set::insert has insert(hint, value) overload. https://en.cppreference.com/w/cpp/container/set/insert See (3) (4) overloads.

Since C++ 11 3-4) Amortized constant if the insertion happens in the position just before the hint, logarithmic in the size of the container otherwise.

container.eraseElementByIterator(it) at the step2 returns the iterator just after the erased element. So if I use the returned iterator as the hint, then the hint iterator is the insertion happens in the position just before the hint.

So the steps become as follows:

  1. container.find(key) the element’s iterator. Then got it.
  2. it = container.eraseElementByIterator(it)
  3. Create the new element based on the gotten element and insert it with hint. container.insertWithHint(it, 4)

Step3 becomes O(1) time complexity thanks to the hint it.

I guess that js-sdsl can implement similar way to the C++ one.

Describe alternatives you’ve considered

Directly modify the element value by user’s risk. However, insert() with hint achieves the same goal (eliminate extra O(logN) time complexity) more elegant way.

Additional context

libc++ implementation of std::set::insert(hint, value) https://github.com/llvm-mirror/libcxx/blob/2f961a057f1ecb29d40892fe9198fe4e4b5c5619/include/set#L704-L711

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ZLY201commented, Aug 31, 2022

The test version 4.1.4-beta.0 has been released, if there are no bug reports within a week, we will released the official version.

0reactions
redboltzcommented, Sep 1, 2022

Thank you for implementing the feature !! I used it on my number-allocator and it works fine. https://github.com/redboltz/number-allocator/pull/31

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature Request Template: How to Manage Suggestions at ...
Streamline and organize user feedback with this free feature request template. Available in Google Docs and Sheets (no email required).
Read more >
10 Tips for Responding Graciously to Customer Feature ...
Picture this scenario: A customer requests a feature. Support politely tells them that it can't be done while still providing top quality service....
Read more >
How To Manage Feature Requests [Template included]
This guide will teach you everything about feature requests – how to process them, manage them, respond to them, prioritize them – so...
Read more >
7 Useful Tips to Manage Feature Requests - Craft.io
Feature requests can provide product managers with great ideas for product improvement, but they must be managed correctly. Here are our top tips....
Read more >
Product Feature Requests - How to Write and Submit Them
Describes the Feature Request process and provides tips on how to write a Feature Request for best results.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found