Click to See Complete Forum and Search --> : Please help, I don't know what to do now!


April 7th, 1999, 08:39 AM
Hi,

I have the following situation:
I have a program that allows manipulation of user details, ie: you can add/delete/modify/view a user. Each user when added has all of it's data stored
in a file in a dir. The filename is the user's ID. The program stores the users in an array in memory. I do NOT use linklist, because of other constraints.
The program, when started reads all the users from the dir, and then new users
can be added, existing ones can be manipulated. My problem is that, I get a random list of user ID's that was saved the last time. And the new ID's I have to allocate must be unique and not any of the existing ones. However they are not sorted. In some instances I could have,
1, 10, 112, 17, 4 ... etc.
These are not sorted, well actually, they are sorted on character ascending, since they are also filenames (thus strings).
Now normal adding happened, by having a counter that was incremented from 1, and each new user got his "unique" ID. If such an ID existed before, there would be two users with the same ID. I want some sort of algorithm or code sample that would go through the list of already "taken" ID's, and then like in the example above assign 2, to be the next available unique ID! I want a funtion, say int GetNextID() to do this. I cannot take the highest, in this case 112, and just add 1 to it for the new ID. Soon I'll run out of ID's!
16-bit unsigned max is 65535, and that's not enough. I will have to re-use deleted ID's.

Please help, and give some ideas.

PS: NO MFC, or classes, plain C code if you have samples.

Thanks in advance.

Dave Lorde
April 7th, 1999, 09:46 AM
Why no classes? This is the Visual C++ Programming discussion group.

My solution would be to read the user IDs into an STL set, then, starting at zero, increment an ID counter until the counter value is not found in the set. This value is the new ID.

Don't forget to add the new value to the set!

Dave

chafey
April 7th, 1999, 11:08 AM
This sounds like a perfect application for linked lists. What is the constraint that prevents you from using a linked list? Anyway, if you can't use a linked list, your best bet is to create a sorted list of user IDs. If you can't afford the memory (~128k) you will probably have to iterate through the users looking for the lowest user id. Problems like these are best solved by redesigning your data structures rather than using some fancy algorithm.

Chris Hafey