Grid data structure

Typically, “extensible grids” are presented as a list of lists (a list of rows, each row has a list of cells), and these lists are some related lists.

Manipulating (deleting, inserting) rows in this data structure is simple and inexpensive, it's just a matter of reinstalling previous nodes, but when it comes to columns, like deleting a column, this is a very long operation, I need to 'cycle all rows to delete these index cells. Obviously, this is not good behavior, at least for my case.

Here I am not talking about databases; a good example i found for this is some text file in a text editor (as i know) text editors, basically dividing lines into lines and easily deleted lines. I want a column deletion to be as inexpensive and efficient as deleting a row.

Finally, I need some kind of multidimensional mesh, but I think that any 2d simple mesh is applicable for MD, am I right?

+4
source share
3 answers

You may have a two-dimensional “coupled matrix” (I forgot the correct terminology):

... Col 3 ... Col 4 ... | | ... --X-- ... --Y-- ... | | ... ... ... ... ... 

Each cell has four neighborhoods as shown. In addition, you need row and column headings that can indicate the position of a row / column, and also point to the first cell in each row or column. They are most easily represented as special cells without an adjacent one (for column headings).

Inserting a new column between 3 and 4 means iterating over the X cells in column 3 and inserting a new right neighbor Z. This new Z cell joins left and right X and right to Y. You also need to add a new header column and link the new cells vertically. Then the positions of all columns after 4 can be renumbered (col 4 becomes col 5).

 ... Col 3 Col 4 Col 5 ... | | | ... --X-----Z-----Y-- ... | | | ... ... ... ... ... 

The cost of inserting a column is O (n) to insert and link new cells, and again O (m) to update the column headers. This is a similar process to delete.

Since each cell consists of only four links, the same algorithms are used to insert / delete rows.

+1
source

Keep your existing data structure as is. Also, give each column a unique identifier when it will be created. When you delete a column, simply add its identifier to the hash table of all deleted column identifiers. Every time you go through a row, check each element column identifier (which should be saved along with all the other data for the element) against the hash table and splic it from the row if it was deleted.

A hash table and identifiers are not needed if you have a data structure for columns that each grid element can point to. Then you just need the remote bit in this data structure.

By the way, Edmund's scheme will be good for you. Although it takes O (n) time to delete a row or column of length n, you can probably amortize this cost compared to the cost of creating these n elements by making O (1) delete time.

+1
source

I know that Linked-Lists are usually evaluated theoretically, but in practice they are usually ineffective.

I would suggest switching to random access containers to get some speed. The simplest would be an array, but depending on the size of the data we are talking about, a two-line queue or an indexed skip list / B * might be better.

It is clear that it does not change much (yet), however you can go to the given index in O (1) (array, deque) / O (log N) (skip list / B * tree) and not O (N) with a simple associated a list.

And then it's time for magic.

Keith has already revealed the basic idea: instead of actually deleting the column, you just need to mark it as deleted, and then “jump” over it when you go through its structure. However, the hash table requires a linear jump to get to the Nth column. Using the Fenwick tree will provide an efficient way to calculate the real index, and you can jump there right away.

Note that the key benefit of marking a deleted line is the obvious possibility of an undo operation.

Also note that you may need to build a compaction function so that you delete deleted columns from time to time and prevent them from accumulating.

0
source

All Articles