I am trying a resource insensitive functionality in C++. I am implementing an array which has 10000 records but any record will have only possible 3 values i.e. 0,1,2. So i was wondering instead of storing memory for 10000 instance all 3 together if some how I can just save one instance of each and manage logically. Not sure how exactly to implemenet.
For example my array would be something like this.
{1, 0, 0, 1, 2, 1, 1, 1, 0, 2, 2, 0, 0, 0, 2,………….}
we might go for more than 10000 records too
It sounds like you could just create an array of 2500 bytes, with 4 values per byte (each value takes 2 bits). Access any single value using bit-shifting/masking. I suspect that’s going to be simpler than a scheme which groups the values, and will be more “array-like” for access. Of course, it’s hard to say for sure as we don’t know what you need to do with the values.
You could actually fit 5 values into each byte (as 35 is 243) so you’d only need a byte array of size 2000… but the access code would be somewhat trickier. I would resist this extra complexity unless you really need it.
Additionally, if the values are relatively sparse – e.g. almost everything is 0, with just a few 1s and 2s – then you could obviously store that more efficiently.
EDIT: Okay, so I haven’t done any C++ for a long time, but it would be something like:
EDIT: Thinking about it further, you might even want to create an array of
uint32_toruint64_tinstead of bytes, and put 16 or 32 “real” values into each array element. I suspect that on most processors that may make for more efficient memory access.