Quote:
Original post by westond
Im thinking of trying to compress my classes a bit by hashing some of my ID variables.
What do you hope to achieve by doing this?
Quote:
Original post by westond
I have a set of variables that give an individual game cell a location in the game world...
int zone_id
int zone_y
int zone_x
each zone id can be broken down into a description of the area of the world its in as well as being the declaration of the array the cells are held in.
Why not just use a structure or a class? What compells you to try and fit this all into a integer?
Quote:
Original post by westond
zone_id=1001 World zone 1 sub zone 1
zone_id=1101 World zone 1 town zone 1 subzone 1
etc...
the position is array1001<zone_y,zone_x>
what I would like to do is create a cell id that combines all this info
ex.
zone_id=1001
zone_y=2
zone_x=1
long cell_id=100121
but i need a way of breaking that up when I need zone_id, zone_y, and zone_x.
I've looked, but cant find anything that will just read the first 4 digits of cell_id and assign it to zone_id and the 5th digit to zone_y and so on. Im sure there is an easy way of doing this that doesnt involve character arrays and casting. Or am I mistaken?
If you were using bit flags, it would be possible to just pull off the bits, but without writing a little code, there's no way to pull off the characters. What you're doing is essentially trying to treat all this info as a concatenated string. (see long cell_id) Now, if you dont care about how your cell_id appears, you can also use Unions. With a union, you create a byte aligned set of values which all share the same space in memory...for example:
union CellInfo{ long cell_id; // 32 bit long integer struct { short zone_id; // 16 bit short integer char zone_x; // 8 bit byte char zone_y; // 8 bit byte // 32 bits total } cell_info;};
By creating a union from the cell_id and the struct, you can now refer to the same space in memory in multiple ways. For example:
CellInfo myCell;// This line, although assigning a value to cell_id, actually stores stuff// in the same memory which contains zone_id, x, and y...with the first 16// bits being the zone_id, the next 8 being the x, and the last 8 being the y.myCell.cell_id = (id << 16) | (x << 8) | y;// You can then access the fields as follows...long cell_id = myCell.cell_id;short zone_id = myCell.cell_info.zone_id; // this contains the value of "id" abovechar zone_x = myCell.cell_info.zone_x; // this contains the value of "x" above.
I'm still not sure WHY you'd want to do this, but here's a possible way to do it. I dont believe unions are in the C++ book, as they are C features, and not strictly C++. In general, you should just store your x, y, and id's in a class. There's no reason that I can think of to hash them all into a single variable. But in either event, I hope this helps.
Cheers!