hash - Generating a Unique ID in c++ -
what best way generate unique id 2 (or more) short ints in c++? trying uniquely identify vertices in graph. vertices contain 2 4 short ints data, , ideally id kind of hash of them. prefer portability , uniqueness on speed or ease.
there lot of great answers here, trying them tonight see fits problem best. few more words on i'm doing.
the graph collection of samples audio file. use graph markov chain generate new audio file old file. since each vertex stores few samples , points sample, , samples short ints, seemed natural generate id data. combining them long long sounds good, maybe simple 0 1 2 3 generateid
need. not sure how space necessary guarantee uniqueness, if each vertex stores 2 16 bit samples, there 2^32 possible combinations correct? , if each vertex stores 4 samples, there 2^64 possible combinations?
library , platform specific solutions not relevant question. don't want else might compile program have download additional libraries or change code suit os.
a simple solution use 64 bit integer lower 16 bits first vertex coordinate, next 16 bits second, , on. unique vertices, though not compact.
so here's half-assed code this. got casts right.
uint64_t generateid( uint16_t v1, uint16_t v2, uint16_t v3, uint16_t v4) { uint64_t id; id = v1 | (((uint64_t)v2) << 16) | (((uint64_t)v3) << 32) | (((uint64_t)v4) << 48); return id; }
optionally done union (great idea leon timmermans, see comment). clean way:
struct vertex { uint16_t v1; uint16_t v2; uint16_t v3; uint16_t v4; }; union vertexwithid { vertex v; uint64_t id; }; int main() { vertexwithid vwithid; // setup vertices vwithid.v.v1 = 2; vwithid.v.v2 = 5; // id automatically setup you! std::cout << "id " << vwithid.id << std::endl; return 0; }
Comments
Post a Comment