A list with no duplicates or an ordered set

后端 未结 2 1941
梦毁少年i
梦毁少年i 2021-02-20 02:27

Is there a library which provides a data structure, which preserves the order of items and does not contain any duplicates? And does there exist a proper name for such a data st

2条回答
  •  没有蜡笔的小新
    2021-02-20 03:07

    Here's one solution:

    Use a fingertree with the Set monoid as your measure. Then on inserts, check membership first, using the measure of your full fingertree. This gives you O(log(n)) cons and snoc, O(1) deletes.

    Here's another solution:

    Pair a normal list with a normal Set and get basically the same effect. You get better constant factors, but O(log(n)) deletes.

    Here's a question: What do you want to happen on insert of a duplicate? Should the existing position be preserved? The new position? Priority Queues may be close to what you want, depending.

提交回复
热议问题