swift-collections icon indicating copy to clipboard operation
swift-collections copied to clipboard

[Treap] Fast random insertions collection

Open alexdremov opened this issue 2 years ago • 3 comments

At this moment, no collection can address random insertions. I have a data structure implemented through implicit Treap that behaves like a general array but can do random insertions/deletions/access with O(log n).

If such a container is needed, I can open a PR. What do you think?

alexdremov avatar Nov 12 '21 21:11 alexdremov

At the meantime, here is the comparison. The better performance is noticeable at n ~ 10^4

Screenshot-2021-11-13-at-13-01-57 Screenshot-2021-11-13-at-12-54-34 Screenshot-2021-11-13-at-13-01-25 Screenshot-2021-11-13-at-13-04-55

alexdremov avatar Nov 13 '21 10:11 alexdremov

Opened a topic on the forum: https://forums.swift.org/t/treap-fast-random-insertions-deletions-collection/53468

alexdremov avatar Nov 13 '21 19:11 alexdremov

As it seems like it is not really needed in swift-collections, I've created a package that has more efficient implementation. Also, I benchmarked the solution and created comparison graphs.

AlexRoar/TreeArray

Preview:

Random insertions

Random removals

Prepend

Remove first

alexdremov avatar Jan 17 '23 21:01 alexdremov