Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 723067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:03:52+00:00 2026-05-14T06:03:52+00:00

I have a need for unique reusable ids. The user can choose his own

  • 0

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 AllocateId();          // Allocates an id
  void FreeId(int id);       // Frees an id so it can be used again
  bool MarkAsUsed(int id);   // Let's the user register an id. 
                             // returns false if the id was already used.
  bool IsUsed(int id);       // Returns true if id is used.
};

Assume ids happen to start at 1 and progress, 2, 3, etc. This is not a requirement, just to help illustrate.

IdManager mgr;
mgr.MarkAsUsed(3);
printf ("%d\n", mgr.AllocateId());
printf ("%d\n", mgr.AllocateId());
printf ("%d\n", mgr.AllocateId());

Would print

1
2
4

Because id 3 has already been declared used.

What’s the best container / algorithm to both remember which ids are used AND find a free id?

If you want to know the a specific use case, OpenGL’s glGenTextures, glBindTexture and glDeleteTextures are equivalent to AllocateId, MarkAsUsed and FreeId

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:03:52+00:00Added an answer on May 14, 2026 at 6:03 am

    My idea is to use std::set and Boost.interval so IdManager will hold a set of non-overlapping intervals of free IDs.
    AllocateId() is very simple and very quick and just returns the left boundary of the first free interval. Other two methods are slightly more difficult because it might be necessary to split an existing interval or to merge two adjacent intervals. However they are also quite fast.

    So this is an illustration of the idea of using intervals:

    IdManager mgr;    // Now there is one interval of free IDs:  [1..MAX_INT]
    mgr.MarkAsUsed(3);// Now there are two interval of free IDs: [1..2], [4..MAX_INT]
    mgr.AllocateId(); // two intervals:                          [2..2], [4..MAX_INT]
    mgr.AllocateId(); // Now there is one interval:              [4..MAX_INT]
    mgr.AllocateId(); // Now there is one interval:              [5..MAX_INT]
    

    This is code itself:

    #include <boost/numeric/interval.hpp>
    #include <limits>
    #include <set>
    #include <iostream>
    
    
    class id_interval 
    {
    public:
        id_interval(int ll, int uu) : value_(ll,uu)  {}
        bool operator < (const id_interval& ) const;
        int left() const { return value_.lower(); }
        int right() const {  return value_.upper(); }
    private:
        boost::numeric::interval<int> value_;
    };
    
    class IdManager {
    public:
        IdManager();
        int AllocateId();          // Allocates an id
        void FreeId(int id);       // Frees an id so it can be used again
        bool MarkAsUsed(int id);   // Let's the user register an id. 
    private: 
        typedef std::set<id_interval> id_intervals_t;
        id_intervals_t free_;
    };
    
    IdManager::IdManager()
    {
        free_.insert(id_interval(1, std::numeric_limits<int>::max()));
    }
    
    int IdManager::AllocateId()
    {
        id_interval first = *(free_.begin());
        int free_id = first.left();
        free_.erase(free_.begin());
        if (first.left() + 1 <= first.right()) {
            free_.insert(id_interval(first.left() + 1 , first.right()));
        }
        return free_id;
    }
    
    bool IdManager::MarkAsUsed(int id)
    {
        id_intervals_t::iterator it = free_.find(id_interval(id,id));
        if (it == free_.end()) {
            return false;
        } else {
            id_interval free_interval = *(it);
            free_.erase (it);
            if (free_interval.left() < id) {
                free_.insert(id_interval(free_interval.left(), id-1));
            }
            if (id +1 <= free_interval.right() ) {
                free_.insert(id_interval(id+1, free_interval.right()));
            }
            return true;
        }
    }
    
    void IdManager::FreeId(int id)
    {
        id_intervals_t::iterator it = free_.find(id_interval(id,id));
        if (it != free_.end()  && it->left() <= id && it->right() > id) {
            return ;
        }
        it = free_.upper_bound(id_interval(id,id));
        if (it == free_.end()) {
            return ;
        } else {
            id_interval free_interval = *(it);
    
            if (id + 1 != free_interval.left()) {
                free_.insert(id_interval(id, id));
            } else {
                if (it != free_.begin()) {
                    id_intervals_t::iterator it_2 = it;
                    --it_2;
                    if (it_2->right() + 1 == id ) {
                        id_interval free_interval_2 = *(it_2);
                        free_.erase(it);
                        free_.erase(it_2);
                        free_.insert(
                            id_interval(free_interval_2.left(), 
                                        free_interval.right()));
                    } else {
                        free_.erase(it);
                        free_.insert(id_interval(id, free_interval.right()));
                    }
                } else {
                        free_.erase(it);
                        free_.insert(id_interval(id, free_interval.right()));
                }
            }
        }
    }
    
    bool id_interval::operator < (const id_interval& s) const
    {
        return 
          (value_.lower() < s.value_.lower()) && 
          (value_.upper() < s.value_.lower());
    }
    
    
    int main()
    {
        IdManager mgr;
    
        mgr.MarkAsUsed(3);
        printf ("%d\n", mgr.AllocateId());
        printf ("%d\n", mgr.AllocateId());
        printf ("%d\n", mgr.AllocateId());
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unique requirement which I need to meet to with sitecore, and
I have a table and I need to count all unique users on it.
I rarely use SQL and have a unique issue I need to solve. I
I have 20 different variable-length arrays, each holding a unique value, and I need
I have tracking table tbl_track with id, session_id, created_date fields I need count unique
I understand we do need an unique APP ID, but seems I have tested
I need to have an ability to have unique items in a collection. I
I am working on a webby, which would need unique user identification like IP/MAC
I am developing a mysql database. I need a unique id for each user
My need is to have items in kind of Collections.Generic.Dictionary where I can get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.