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 367837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:45:59+00:00 2026-05-12T13:45:59+00:00

I’m trying to find an efficient deterministic way of allocating a 32-bit handle in

  • 0

I’m trying to find an efficient deterministic way of allocating a 32-bit handle in such a way that it will operate indefinitely. A simple incrementing counter will not work because it will eventually loop around. Extending to 64-bits isn’t possible because the values will be used in a network protocol which is expecting a 32-bit value.

It’s for a real-time system so it has to be deterministic and quick. Though it’ll end up in an SQLite database I can’t just brute force test each key after loop around for example…

I think what I need is some sort of range tree that knows about all previously allocated handles (populating this on start up is fine). This seems to be a common(ish) sort of problem but it’s not one that’s solved by boost or the STL.

Any pointers?

Edit: Some additional clarification. I’m looking to have something of the order of 200k active handles in the system at any one time. Handles are deleted on a random basis.

  • 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-12T13:46:00+00:00Added an answer on May 12, 2026 at 1:46 pm

    You can’t allocate more than 2^32. But you can reallocate used handles if they are released and the problem is to keep track of the free handles.

    A tree is a good way to store the free handles. Each node has a lowest and a highest handle, the left subtree contains the handles that are lesser than the lowest and the right subtree contains the handles that are greater than the highest.

    An example is:

                6-9
                / \
               2   15
              / \
             0   4
    

    If a handle is released, it is stored in the tree. For example, if 10 is released, the tree looks like:

                6-10
                / \
               2   15
              / \
             0   4
    

    If handle 5 is released, you can chose to optimize the tree because 4 can be added to the 5-10 node as wel:

                5-10
                / \
               2   15
              / \
             0   4
    

    To:

                4-10
                / \
               2   15
              / 
             0  
    

    The allocation of a handle, searches for a leaf node with 1 handle and removes it from the tree. If there are no leaves with 1 handle, just use a leaf and decrement the side that is not connected to the parent:

             4-10
            /
          1-2
    

    In the above example we allocate 1 and not 2 because if 3 is released, you can combine it with 4 and you want to keep the number of nodes as low as possible.

    Below is a pseudocode algorithm. Some parts are left for the reader:

    Node = ( int lowest, highest; [Node] left, right)
    
    
    Node.Allocate() 
      if TryFindLonelyLeaf(handle)    return handle;
      else
        return FindLeafHandle(NULL);
    
    Node.TryFindLonelyLeaf(handle)
      if (left == NULL & right == NULL) 
        if (lowest == highest)
          handle == lowest;
          return TRUE;
        else
          return FALSE;
      if (left != NULL & left.TryFindLonelyLeaf(handle))
        if (left.lowest == handle) 
          left == NULL; // Free node
        return TRUE;
      elsif (right != NULL & right.TryFindLonelyLeaf(handle))
        if (right.lowest == handle)
           right = NULL; // Free node
        return TRUE;
      else
        return FALSE;
    
    Node.FindLeafHhandle(parent)
      if (left == NULL & right == NULL) 
        if (parent == NULL | parent.right == this) 
          handle = lowest;
          lowest++;
        else
          handle = highest;
          highest--;
        return handle;
      else if (left != NULL) 
        return left.FindLeafHandle();
      else // right != NULL
        return right.FindLeafHandle();
    
    Node.DeAllocate(handle) 
      Assert(handle<lowest or handle>highest);
      if (handle == lowest-1)
        lowest = CompactLeft(handle);
      elsif (handle == lowest+1)
        highest = CompactRight(handle); 
      elsif (handle<lowest)          
        if (left == NULL)
          left = (handle, handle, NULL, NULL);
        else
          left.DeAllocate(handle);
      elsif (handle>highest)
        if (right == NULL)
          right = (handle, handle, NULL, NULL);
        else
          right.DeAllocate(handle);
      else ERROR           
    
    Node.CompactLeft(handle)
      if (highest == handle-1) 
        handle = lowest;
        // deallocate node and replace with left subtree
        return handle;
      elsif (right != NULL) 
        return right.CompactLeft(handle)
      else
        return handle;
    
    Node.CompactRight(handle)
      if (lowest == handle+1) 
        handle = highest;
        // deallocate node and replace with right subtree
        return handle;
      elsif (left != NULL) 
        return left.CompactRight(handle)
      else
        return handle;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.