I was thinking about how a spreadsheet knows which cells to update and what order to do them in when the value in a cell changes. I've though of a possible method - would this work and is it how things are normally done? I've assumed that there are nocircular references, for completeness I would need to check for this.
I'll say that if a cell B depends on the value of a cell A, then B is a "child" of A and A a "parent" of B. Each cell keeps track of its parents, its children and also has a flag to say whether it needs to be updated.
When a cell is altered, you first look at what its parents now are, make a note of this and also inform the old and new parents so they can update their list of children. Then do the following, starting with the updated cell:
If the cell is already marked as needing updating, do nothing;
otherwise, mark the cell as needing updating and apply the same procedure to its children.
Then do the following, again starting with the updated cell:
If one of more of the parents requires updating, do nothing;
otherwise, update the value, clear the flag, and apply the same procedure to its children.
The idea is that no cell is updated too early, but each cell which is "passed over" will eventually be done, after its last parent gets done.
So will this work, and is it normal?
On 11/02/2022 4:09 pm, Paul N wrote:no circular references, for completeness I would need to check for this.
I was thinking about how a spreadsheet knows which cells to update and what order to do them in when the value in a cell changes. I've though of a possible method - would this work and is it how things are normally done? I've assumed that there are
I'll say that if a cell B depends on the value of a cell A, then B is a "child" of A and A a "parent" of B. Each cell keeps track of its parents, its children and also has a flag to say whether it needs to be updated.
When a cell is altered, you first look at what its parents now are, make a note of this and also inform the old and new parents so they can update their list of children. Then do the following, starting with the updated cell:
If the cell is already marked as needing updating, do nothing;
otherwise, mark the cell as needing updating and apply the same procedure to its children.
Then do the following, again starting with the updated cell:
If one of more of the parents requires updating, do nothing;
otherwise, update the value, clear the flag, and apply the same procedure to its children.
The idea is that no cell is updated too early, but each cell which is "passed over" will eventually be done, after its last parent gets done.
So will this work, and is it normal?If I've read it right, it should work fine. To check for circular
reference, seek the root of the tree you've built. You should find it
before you find yourself, so to speak.
t = here;
do
{
t = t->parent;
} while(t!= NULL && t != here);
if(t == here)
{
circular reference
}
If you do that for ALL active (occupied) cells, you will detect any
circular refs.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 547 |
Nodes: | 16 (2 / 14) |
Uptime: | 60:29:41 |
Calls: | 10,398 |
Calls today: | 6 |
Files: | 14,067 |
Messages: | 6,417,476 |
Posted today: | 1 |