Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Generate Unique Ids

Generate Unique Ids

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 10533450
    wrote on last edited by
    #1

    Generate Unique Ids

    FormStrings(unsigned int id, string bigText)
    {
    //create small strings from bigText say string1, string2...string k
    for(int x = 0; x< k; ++x)
    unsigned int id uid = generateUniqueId(..);
    ForwardString(uid, stringx)
    }

    FormStrings() method receives id which starts from 1 and increments ahead as FormStrings() method is called.

    FormStrings() method further split the bigText received into smaller strings and passes to other methods.

    Requirement is to generate a unique 32 bit id for the new strings created. I should also take care of how many uids are getting created from id, so that if later anyone calls delete id, all the sub-uids string should get deleted.

    For example:

    FormStrings(1, "Hi, I want to learn cpp.")
    string1 = Hi,
    String2 = I want to
    String3 = learn cpp.
    Here id 1 has 3 substrings. I need to create 3 uids.

    Next time when FormStrings() method is called, it will come with an id 2, then 3 and so on.

    I thought of creating id using 32 bits as
    16 bits for id - 65535 ids
    8 bits for storing number of strings formed.

    but when I ran the actual program, I found out that 65535 is a small number to store id, it could be a very big number(20 bits will also not work), even number of strings formed from bigText is not fixed. I am open to create new variables, structs etc

    Any suggestions are welcome.

    L L 2 Replies Last reply
    0
    • M Member 10533450

      Generate Unique Ids

      FormStrings(unsigned int id, string bigText)
      {
      //create small strings from bigText say string1, string2...string k
      for(int x = 0; x< k; ++x)
      unsigned int id uid = generateUniqueId(..);
      ForwardString(uid, stringx)
      }

      FormStrings() method receives id which starts from 1 and increments ahead as FormStrings() method is called.

      FormStrings() method further split the bigText received into smaller strings and passes to other methods.

      Requirement is to generate a unique 32 bit id for the new strings created. I should also take care of how many uids are getting created from id, so that if later anyone calls delete id, all the sub-uids string should get deleted.

      For example:

      FormStrings(1, "Hi, I want to learn cpp.")
      string1 = Hi,
      String2 = I want to
      String3 = learn cpp.
      Here id 1 has 3 substrings. I need to create 3 uids.

      Next time when FormStrings() method is called, it will come with an id 2, then 3 and so on.

      I thought of creating id using 32 bits as
      16 bits for id - 65535 ids
      8 bits for storing number of strings formed.

      but when I ran the actual program, I found out that 65535 is a small number to store id, it could be a very big number(20 bits will also not work), even number of strings formed from bigText is not fixed. I am open to create new variables, structs etc

      Any suggestions are welcome.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You could use each half of the 32 bit value such that the most significant half is the first digit (1, 2 etc.), and the second half is the string sequence number. So your id is generated by something like:

      FormStrings(unsigned int id, string bigText)
      {
      unsigned int uid = id << 16;
      //create small strings from bigText say string1, string2...string k
      for(int x = 0; x< k; ++x)
      {
      uid += 1;
      ForwardString(uid, stringx)
      }
      }

      This should provide enough unique values for most purposes.

      1 Reply Last reply
      0
      • M Member 10533450

        Generate Unique Ids

        FormStrings(unsigned int id, string bigText)
        {
        //create small strings from bigText say string1, string2...string k
        for(int x = 0; x< k; ++x)
        unsigned int id uid = generateUniqueId(..);
        ForwardString(uid, stringx)
        }

        FormStrings() method receives id which starts from 1 and increments ahead as FormStrings() method is called.

        FormStrings() method further split the bigText received into smaller strings and passes to other methods.

        Requirement is to generate a unique 32 bit id for the new strings created. I should also take care of how many uids are getting created from id, so that if later anyone calls delete id, all the sub-uids string should get deleted.

        For example:

        FormStrings(1, "Hi, I want to learn cpp.")
        string1 = Hi,
        String2 = I want to
        String3 = learn cpp.
        Here id 1 has 3 substrings. I need to create 3 uids.

        Next time when FormStrings() method is called, it will come with an id 2, then 3 and so on.

        I thought of creating id using 32 bits as
        16 bits for id - 65535 ids
        8 bits for storing number of strings formed.

        but when I ran the actual program, I found out that 65535 is a small number to store id, it could be a very big number(20 bits will also not work), even number of strings formed from bigText is not fixed. I am open to create new variables, structs etc

        Any suggestions are welcome.

        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        What you are making is called a hash table but you have a linking requirement. This will need a linked list due to this part of the requirement => if later anyone calls delete id, all the sub-uids string should get deleted. In its simplest form your substring structure using ID's will be

        struct uiString {
        unsigned long uid; // here assumed your ids' are 32 bits
        struct uiString* next; // Pointer to next substring
        };

        The humour is your uid is the same length as just holding a pointer to the string which is guaranteed unique :-) You bascially keep deleting a each "next" sub string until your reach NULL for your above requirement. Unless the text is in or going into a database I can't see a point to the uid you might as well use the pointer to the text as the uid. So I guess the question I ask is what is the purpose of the hash table .. security?

        In vino veritas

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups