cesarz
cesarz
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
Well, anyways, thanks for the help!
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
and idk, maybe an additional check in the loop if there are any correct characters left? There's a possibility that someone will for some reason name a file "Abc######" so the loop would continue for 6 more times for no reason, but idk if it makes too much of a difference
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
It skips the incorrect characters (-), adds the first 6 to the ID, if 6 are already added then it skips through all the other correct chars until there are only 7 left
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
int length = chars.Length;
int correctChars = 0;
for (int i = 0; i < length; ++i)
{
switch (chars[i])
{
case >= '0' and <= '9':
++correctChars;
chars[i] -= '0';
break;
case >= 'A' and <= 'Z':
++correctChars;
chars[i] -= 'A';
break;
case >= 'a' and <= 'z':
++correctChars;
chars[i] -= 'a';
break;
default:
chars[i] = '-';
break;
}
}
ulong ID = 0;
for (int i = 0, x = 0; i < length; ++i)
{
if (chars[i] == '-')
continue;
if (x != 6)
{
ID += chars[i] * (ulong)Mathf.Pow(26, x++);
--correctChars;
}
else
{
if (correctChars > 7)
{
--correctChars;
continue;
}
++x;
}
}
return ID;
}
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
int length = chars.Length;
int correctChars = 0;
for (int i = 0; i < length; ++i)
{
switch (chars[i])
{
case >= '0' and <= '9':
++correctChars;
chars[i] -= '0';
break;
case >= 'A' and <= 'Z':
++correctChars;
chars[i] -= 'A';
break;
case >= 'a' and <= 'z':
++correctChars;
chars[i] -= 'a';
break;
default:
chars[i] = '-';
break;
}
}
ulong ID = 0;
for (int i = 0, x = 0; i < length; ++i)
{
if (chars[i] == '-')
continue;
if (x != 6)
{
ID += chars[i] * (ulong)Mathf.Pow(26, x++);
--correctChars;
}
else
{
if (correctChars > 7)
{
--correctChars;
continue;
}
++x;
}
}
return ID;
}
How about this one? Now there are no queues, only the initial char array
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
@#$_&-+()/ etc
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
and some are ignored
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
first 6 and last 7
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
-1 would be placed during the first loop with the switch in places of characters which aren't A-Z/a-z/0-9 alongside with an incrementing length variable for the ones which are
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
What would you recommend as an alternative? Would an array, where -1 is to be skipped, be a better alternative?
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
Queue<int> inputQueue = new Queue<int>();
Queue<int> idGenQueue = new Queue<int>();
foreach (char c in chars)
{
switch (c)
{
case >= '0' and <= '9':
inputQueue.Enqueue(c - '0');
break;
case >= 'A' and <= 'Z':
inputQueue.Enqueue(c - 'A');
break;
case >= 'a' and <= 'z':
inputQueue.Enqueue(c - 'a');
break;
}
}
int length = inputQueue.Count;
if (length < 13)
{
while (inputQueue.Count > 0)
idGenQueue.Enqueue(inputQueue.Dequeue());
}
else
{
for (int i = 0; i < 6; ++i)
idGenQueue.Enqueue(inputQueue.Dequeue());
while (inputQueue.Count > 7)
inputQueue.Dequeue();
while (inputQueue.Count > 0)
idGenQueue.Enqueue(inputQueue.Dequeue());
}
ulong ID = 0;
length = idGenQueue.Count;
for (int i = 0; i < length; ++i)
ID += (ulong)idGenQueue.Dequeue() * (ulong)Mathf.Pow(26, i);
return ID;
}
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
Queue<int> inputQueue = new Queue<int>();
Queue<int> idGenQueue = new Queue<int>();
foreach (char c in chars)
{
switch (c)
{
case >= '0' and <= '9':
inputQueue.Enqueue(c - '0');
break;
case >= 'A' and <= 'Z':
inputQueue.Enqueue(c - 'A');
break;
case >= 'a' and <= 'z':
inputQueue.Enqueue(c - 'a');
break;
}
}
int length = inputQueue.Count;
if (length < 13)
{
while (inputQueue.Count > 0)
idGenQueue.Enqueue(inputQueue.Dequeue());
}
else
{
for (int i = 0; i < 6; ++i)
idGenQueue.Enqueue(inputQueue.Dequeue());
while (inputQueue.Count > 7)
inputQueue.Dequeue();
while (inputQueue.Count > 0)
idGenQueue.Enqueue(inputQueue.Dequeue());
}
ulong ID = 0;
length = idGenQueue.Count;
for (int i = 0; i < length; ++i)
ID += (ulong)idGenQueue.Dequeue() * (ulong)Mathf.Pow(26, i);
return ID;
}
Here's the final implementation. If anyone has any ideas on how to improve this further then feel free to ping me
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
Should probably work fine as long as nobody uses names such as "hhhhhhhhhh" and "hhhhhhhhhhhhhhh" which will then probably generate the same ID
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
13 characters is quite a lot to work with already. I can divide it in 'half' => 6 and 7 characters Take 6 characters from the beginning of the input And 7 characters from the end of the input Unless the input length is < 13, then I just take every character. That would probably work out fine enough?
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
Now there's a 13 character limit... Any progress is good.
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
I could possibly try and reduce the possible characters by also ignoring numbers / or not adding 10 to the letters resulting in a range of 0 to 25 instead of 0 to 35. Numbers are rarely used in my file names anyways so there is little to no risk in there being any collisions with this I suppose.
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
Queue<int> queue = new Queue<int>();
foreach (char c in chars)
{
switch (c)
{
case >= '0' and <= '9':
queue.Enqueue(c - '0');
break;
case >= 'A' and <= 'Z':
queue.Enqueue(c - 'A' + 10);
break;
case >= 'a' and <= 'z':
queue.Enqueue(c - 'a' + 10);
break;
}
}
int length = queue.Count;
ulong ID = 0;
for (int i = 0; i < length; ++i)
ID += (ulong)queue.Dequeue() * (ulong)Mathf.Pow(36, i);
return ID;
}
public static ulong UniqueIdGen(string str)
{
char[] chars = str.ToCharArray();
Queue<int> queue = new Queue<int>();
foreach (char c in chars)
{
switch (c)
{
case >= '0' and <= '9':
queue.Enqueue(c - '0');
break;
case >= 'A' and <= 'Z':
queue.Enqueue(c - 'A' + 10);
break;
case >= 'a' and <= 'z':
queue.Enqueue(c - 'a' + 10);
break;
}
}
int length = queue.Count;
ulong ID = 0;
for (int i = 0; i < length; ++i)
ID += (ulong)queue.Dequeue() * (ulong)Mathf.Pow(36, i);
return ID;
}
Here's the current implementation
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
For ulong limit 18_446_744_073_709_551_615 Testing this method using highest-value character z (int 35) we get a limit of 12 characters, any higher that that and there's an overflow... It's better than my previous 9 character limit but still far away from 32 character limit...
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
Thanks, I'll take a look
26 replies
CC#
Created by cesarz on 7/12/2023 in #help
❔ Unique Integer ID GEN from string input
The string input will be a name given to a file in my game so I guess there is no limit to how big these can get, depends on the situation, but most likely not bigger than 32 characters I suppose (even if they are bigger I can just reduce them to 32 chars, with this length there shouldn't be any problems)
26 replies