Generate Unique Ids
-
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.
-
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.
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.
-
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.
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