tags:

views:

467

answers:

2

I need a simple UUID generator. The ID is required to be unique for this single instance. Another requirement is, that it has n hashes coexisting at a time, and being releasable. I don't know wether this fits the UUID concept or not. I allrdy thought about a Stack with n-values using pop and push, but this practice seems bad memory wise.

Using random based UUIDs (excluding cryptographic ones) isn't save enough, as by bad luck there could be 2 matching IDs, which can not be accapet (though neglegable chance), as this is supposed to be used in an productive environment.

+1  A: 

I highly recommend this library, it's a boost candidate - we're using it in one of our projects and it works just fine. The link is to v13. This version + other versions can be located at www.boostpro.com

Example :

#include <string>
#include <iostream>
#include <UUID/boost/uuid/uuid.hpp>
using namespace std;

/*! returns a filename which is a uuid + .dat*/
std::string generate_filename()
{
    boost::uuids::uuid_generator gen;
    boost::uuids::uuid u = gen();
    return u.to_string()+ ".dat";
}

void main()
{
    for(int i = 0; i < 10; i++)
        cout << generate_filename() << endl;
}

If that's what you seek, please mark this post as an answer :)

Maciek
edit - added an example
Maciek
This generates random-based v4 UUIDs, which is no better than `QUuid::createUuid()`.
Lukáš Lalinský
So this is no solution, random based Uuids are no option.The objects which I assign the UUIDs to are created and killed *very* frequently, but there are max N-objects which have a UUID set at a time.Thx for your reply.
penguinpower
If I understand you correctly, you're attempting to have an amount of UUIDs that can be recreated? as In uuid(1) will always result in aaa-bbb-ccc and uuid(5) will always result in ddd-eee-fff ?I'm not sure I understand what's the point of it, I'd probably need some more info to be of more help.
Maciek
Ok, here we go:Depending on userinput I render Nodes. Every Node needs a certain ID, so I can identify it.I know there are no more than N (~100.000) Nodes at once in ram/displayed.The whole viewport ca be altered, so new tiles get loaded, old get deleted (IDs get "free" again).What I would like to get is something like:int AquireNewID(); //get it from the poolvoid freeID(); //return it to the ID poolIf this is unrealistic I will mark your post as solving my issue and I finally have to go with QUuid.GreetsThank you for your time
penguinpower
I didn't solve it the way you wanted, so please don't mark it as resolved. I don't deserve the credit.
Maciek
Actually it does, though, the appproach is different, but I was not able to recognize that untill "Panic" posted his/her answer and I re-read the wiki article.Thank you for your time.I will use QUuid which basically works the same way as boost, but as I allready use the Qt GUI, I prefer it over boost.
penguinpower
+1  A: 

Universally Unique Identifiers (UUID) / Globally Unique Identifier (GUID)

The problem of generating unique IDs can be broken down as uniqueness over space and uniqueness over time which, when combined, aim to produce a globally unique sequence.

UUIDs are officially and specifically defined as part of the ISO-11578 standard other specifications also exist, like RFC 4122, ITU-T Rec. X.667.

OSSP uuid ( http://www.ossp.org/pkg/lib/uuid/ ) is an API for ISO C, ISO C++, Perl and PHP and a corresponding CLI for the generation of DCE 1.1, ISO/IEC 11578:1996, and RFC4122 compliant Universally Unique Identifiers (UUIDs). It supports DCE 1.1 variant UUIDs of version 1 (time and node based), version 3 (name based, MD5), version 4 (random number based), and version 5 (name based, SHA-1). UUIDs are 128-bit numbers that are intended to have a high likelihood of uniqueness over space and time and are computationally difficult to guess. They are globally unique identifiers that can be locally generated without contacting a global registration authority. It is Open Sourced under the MIT/X Consortium License.

I have included some further explanations in the http://en.wikibooks.org/wiki/The%5FWorld%5Fof%5FPeer-to-Peer%5F%28P2P%29/Building%5Fa%5FP2P%5FSystem#Unique%5FID

On windows check the RPC library (see #include "Rpcdce.h" ) it has functions to generate UUIDs.

Panic
I actually calculated the collision liklyhood and it is in the range ov 10^-33, so I guess I can ignore duplicate IDs. Thx for your answer, it clariefied a lot.
penguinpower