Fastest container or algorithm for unique reusable ids in C++

前端 未结 8 871
暗喜
暗喜 2021-02-04 10:33

I have a need for unique reusable ids. The user can choose his own ids or he can ask for a free one. The API is basically

class IdManager {
public:
  int Allocat         


        
8条回答
  •  再見小時候
    2021-02-04 11:09

    I'm assuming that you want to be able to use all available values for the Id type and that you want to reuse freed Ids? I'm also assuming that you'll lock the collection if you're using it from more than one thread...

    I'd create a class with a set to store the allocated ids, a list to store the free ids and a max allocated value to prevent me having to preload the free id list with every available id.

    So you start off with an empty set of allocated ids and empty list of free ids and the max allocated as 0. You allocate, take the head of the free list if there is one, else take max, check it's not in your set of allocated ids as it might be if someone reserved it, if it is, increment max and try again, if not add it to the set and return it.

    When you free an id you simply check it's in your set and if so push it on your free list.

    To reserve an id you simply check the set and if not present add it.

    This recycles ids quickly, which may or may not be good for you, that is, allocate(), free(), allocate() will give you the same id back if no other threads are accessing the collection.

提交回复
热议问题