M
M
Mercury132020-04-10 00:28:25
Algorithms
Mercury13, 2020-04-10 00:28:25

Is there an "extensible 2D table" data structure?

I want such data structure: the expanded/contracted two-dimensional table.

That is: we have, for example, a 5×5 table, and we add a row to it from above, and it becomes 6×5. And then we add two columns to the right, and it became 6 × 7. And then we remove two lines from the bottom, and it became 4 × 7.

Expanding and contracting is possible only from the edges - that is, you cannot remove a row or column from the middle, and you cannot add anything to the middle.

Tell me in which direction to move?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Mercury13, 2020-04-14
@Mercury13

Until stopped on such a mechanism.
There are two one-dimensional arrays - the table of contents of the rows and the table of contents of the columns, two one-dimensional arrays - the involvement of rows / columns, and a two-dimensional array - the buffer.
a[i,j] := buffer[rowIndex[i−i0], colIndex[j−j0]]
If rowIndex[i−i0] has an unused marker, find the unused row and add it to the table of contents.
To less re-allocate memory, re-allocation occurs in impulses and with a margin. For example, we want 20 lines, and we allocate 30 at once.
Such an array can only expand, but this is enough for me.

S
Sergey Sokolov, 2020-04-10
@sergiks

you can not be limited to 2D
Alternatively, look at the implementation of ndarray in Numpy.
Partially described here (in English)
The data is stored in a buffer - an array type in C. And there is metadata describing the "view" of this buffer.
When the form changes, a new metadata object is created, without changing the data itself.
But the topic of insertion is not disclosed here, how it changes the size of the buffer - create a new one or expand the old one. At the end, or stuffed into lines.

M
mayton2019, 2020-04-10
@mayton2019

The boxed solution is a hash table. Where the key is the coordinates of the cell and the value is what you put in the cell.

W
WinPooh32, 2020-04-10
@WinPooh32

No, there is no such thing in standard libraries, at least I don't know about such ones.
You can implement yourself:
1) Using a linked list;
2) On an array, with pre-allocated memory, with the addition of elements to the center. Further expand in the direction that is required.

A
Alexander Skusnov, 2020-04-10
@AlexSku

Matlab does this automatically for a matrix (you can even rearrange rows or columns in any order). It is based on Java.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question