Delete/Change Dictionary Keys

That is exactly what I am already doing… the issue is with changing the key to “insert a row or column” or "delete a row/column)
Where inserting would require adding 1 to the row or column component of some of the keys, and deleting would require removing any keys on that row (easy), and then decrementing the row or column component of other entries

Well, you’d have a row array of 200,000 keys and a column array of 200,000 keys (neither are in the dictionary)… so in total 1.6MB of memory used (200k integers +200k integers). The dictionary would have 2 entries consisting of “aaaaaa” and “bbbbbb”. The arrays are not in the dictionary, only the cell values that actually exist. You can insert/remove rows using array.insert and array.remove the same as columns. With no looping required.

Have you considered the possibility that a Dictionary is the wrong tool for this problem?

I think it is… perhaps an in-memory database would be easier to manipulate the data, but harder to implement

Its exactly the right tool for representing a grid of data that may have lots of empty spaces in it

Thats a sparse array (by definition) which is what most “spreadsheets” are

The problem is using OBJECTS as keys in the dictionaries - basically DON’T

3 dictionaries should be all thats required

  1. one for “rows” which holds a reference to all the “cells” in the row (so you can quickly find them all)
    it has keys that are simple strings - literally str(rownumber)
    so this one will have keys 1,2,3,4, 98 etc (but ONLY 1 entry IF there is one cell in the row)

  2. one for “columns”
    basically the same criteria as rows but for columns

  3. and one that is all the “cells”
    and these have keys that are strings that are unique cell address like ROW | COLUMN

I’ll modify my sparse array example on my web site to do exactly this when I get a spare moment

Most of my original problem was not from using PAIR as the key (it turns out, internally the code uses pairs, but when data goes into or out of the dictionary they are converted to “well crafted strings” :slight_smile: )… But I had two places where it DID put pairs in, and the mix of strings and pairs screwed things up big time…

I have ONE dictionary with 5 types of “keys”
“X” refers to the grid itself (holds all the “default” colors, fonts etc)
“R|#” (where # is a number), this is the default values for a specific row (“R|-1” indicates attributes for the entire row)
“C|#” just like “R|#” but for a specific column
“H|#” and “L|#” similar to row/column but for HEADERS and LABELS (think excel)
“#|#” which is a specific cell

When attempting to draw a cell it first looks for the “#|#” key,
if not found, it looks for “C|#” to use the column defaults to draw the cell
if thats not found, it looks for “R|#”, and finally if all those fail it goes for “X”

This part works very well, and is very fast…
and I think I have come up with a process to insert rows or columns with the dictionary in place (no external arrays etc)… Its recursive, but I think it will work

[quote]When attempting to draw a cell it first looks for the “#|#” key,
if not found, it looks for “C|#” to use the column defaults to draw the cell
if thats not found, it looks for “R|#”, and finally if all those fail it goes for “X”[/quote]

Interesting.
I would have expected
X
replaced by C or R
replaced by specifics in a local paint event

…assuming you could paint ‘the whole thing in blue’, followed by ‘this column in pink’, followed by ‘this cell in green if it exists’

Although without knowing why you are building that may not make sense.

Overnight, it did seem that a database was the way to go after all.

You can easily add and delete rows and columns using update and delete commands.
Assuming row is a field, and column is a field

Insert a row:
Change the row number of all records of your row number or higher, by +1

Delete a row:
Delete all records with your row number, then
Change the row number of all records of your row number or higher by -1

Should be pretty swift in SQL.
The question is how fast can you retrieve #|# on demand for specific cells?

I start at the most specific spot specified, usually a cell
If the cell has specific attributes (color,font etc) then those are used
if the cell doesn’t have an entry in the dictionary, or the specified attribute is “default” then it goes up to the next level

So assuming the Grid was BLUE, the Column was Pink and the CELL was Green
then the cell is drawn in GREEN, while any non-existing cells in the same column are PINK, and non-existing cells in any other column are BLUE

So you don’t want any other attributes of the cell to “cascade” a la CSS?

Not sure what you mean… they DO cascade…
A cell attribute (forecolor, backcolor, font, border etc) comes from the nearest level that has a non-default value

If a cell has a specific value for an attribute, that is what is used… if not it climbs the tree looking for one

You might find it’s more efficient to go the other way. But ymmv.

Why? if the cell has the attribute, I stop
this method takes the shortest path to find a viable attribute
If I went from GRID to ROW to COL to CELL the cell has the lowest priorty, not the highest as it should

  1. Does the cell exist - if yes then
  2. Does the Cell have the requested attribute - if yes then goto 6
  3. Does a default exist for the column the cell is in - if yes then goto 6
  4. Does a default exist for the row the cell is in? - if yes then then goto 6
  5. use the default value for the grid
  6. I now have the attribute to use when drawing the cell

But then you have to run the list for every attribute. Wouldn’t it be more efficient to collect all the attributes in one pass?

I do, but Daves code needs must differ.
For my needs, i do all ‘cells’ in the default ( which, if it is a background colour, means one big rectangle, job done)
Then get all cells with an override and handle those.
There may only be 10 with an override.

Compare that with
For every cell,
seek an override

if nothing , seek the column
if nothing, seek the row
if nothing, seek the default.

For an array of 10 x 10, where 50% of the cells have an override and nothing at row or column level, but there is a ‘default’, the different in seek operations is this:

Daves way:

100 seek operations, 50 finds plus 50 fallbacks to the default condition

Other way:

One default condition, get overrides as a set, iterate through 50 overrides

Only if all the attributes were default values but thats not the case

example

RowFont(3)=“Arial”
ColBack(3)=“Blue”
CellFore(3,3)=“Yellow”
CellFont(3,3)=“System”

Assume 3 attribute Font,BackColor, ForeColor

Cell(3,3) has a DEFAULT value for BackColor, but specific values for Forecolor and Font

so Cell(3,3) finds the COLUMN back color to use
where every other cell in Column 3 is also blue background, and every cell in ROW 3 has Arial as the font