Reputation: 893
I need suggestions about storing a 2D array in a binary file. I need it to be extensive, to be able to add new rows and columns very fast.
So far i've come up with this approach: i allocate a fixed size of memory for each row and every time i add a new row i fill it with an overhead of memory (say 1000 bytes) so as i don't need to shift rows every time i add a new column. When the overhead memory is filled up, i add an overhead of another 1000 bytes and so on.
This method proved to be very inefficient because i have to jump over the rows when i add a new column.
Ideally, if i didn't need to add new rows or columns i would store each row contigously, but that's not possible for what i want.
Upvotes: 1
Views: 549
Reputation: 65324
Your approach is quite nice, in fact it is what many DBs (notabene oracle) use. To jump over the "where is my row" hurdle you could maintain an in-memory index of the rows, just a long[] storing file offset to the start of the row for each (or each 2nd, 4th, 8th, ...) row. This way you can jump to your row fast, then add your col as initially planned.
Upvotes: 1