NCL 411: Nested Arrays
In the last lesson, we built multidimensional arrays by flattening several coordinates into one index.
That worked because our arrays were regular.
Every row had the same width. Every layer had the same height. Once we knew the dimensions, arithmetic could tell us where any element lived.
coordinates → index → address
But data doesn't always come in such convenient shapes.
An Irregular Grid
Suppose we want to represent a small hexagonal board.
Its rows contain:
| Row | Cells |
|---|---|
| 0 | 3 |
| 1 | 4 |
| 2 | 5 |
| 3 | 4 |
| 4 | 3 |
On the Display, we might arrange those cells something like this:
[][][]
[][][][]
[][][][][]
[][][][]
[][][]
There are 19 cells altogether.
Now try to apply the calculation from the previous lesson:
index = y × width + x
We immediately run into a question:
width = ?
There isn't one width.
The middle row contains five cells. The rows above and below it contain four. The first and last contain only three.
Our data is two-dimensional, but it isn't rectangular.
We Could Make It Rectangular
One perfectly valid solution is to allocate a 5×5 array anyway.
That gives us 25 elements:
5 × 5 = 25
Only 19 of them represent actual cells. The other six are padding.
This preserves the simple calculation from the previous lesson:
index = y × 5 + x
There's nothing inherently wrong with that.
Sometimes a little wasted memory is a good trade for a simpler representation.
But we're also pretending that every row has five cells when our actual structure says otherwise.
There is another way.
Arrays Inside Arrays
An array element is just a value.
And an address is a value.
That means an array can contain the addresses of other arrays.
Suppose an outer array contains five addresses, one for each row of our board.
| Row | Value |
|---|---|
| 0 | Address of row 0 |
| 1 | Address of row 1 |
| 2 | Address of row 2 |
| 3 | Address of row 3 |
| 4 | Address of row 4 |
Each of those addresses leads to another array.
Those inner arrays can have their own lengths:
| Row | Length |
|---|---|
| 0 | 3 |
| 1 | 4 |
| 2 | 5 |
| 3 | 4 |
| 4 | 3 |
Now the structure in memory matches the structure we're trying to represent.
Instead of calculating:
row × width + column
we do two array accesses:
row → row address → column
First we use the row coordinate to select an element from the outer array.
That element gives us the address of an inner array.
Then we use the column coordinate to select an element from that array.
This is a nested array: an array whose elements refer to other arrays.
Building One
Let's use headed arrays for the rows.
Our outer array begins at $100:
| Address | Value |
|---|---|
$100 |
5 |
$101 |
0x200 |
$102 |
0x240 |
$103 |
0x180 |
$104 |
0x300 |
$105 |
0x280 |
The first value is its length. The remaining five values are addresses.
Notice that the rows themselves aren't stored in order.
They don't have to be.
Suppose row 2 lives at $180:
| Address | Value |
|---|---|
$180 |
5 |
$181 |
cell 0 |
$182 |
cell 1 |
$183 |
cell 2 |
$184 |
cell 3 |
$185 |
cell 4 |
To access column 3 of row 2, we first find row 2's address.
The outer array has a one-value header, so:
row entry = outer base + 1 + row
For row 2:
0x100 + 1 + 2 = 0x103
$103 contains:
0x180
That's the address of our inner array.
That array also has a one-value header, so column 3 lives at:
0x180 + 1 + 3 = 0x184
We've followed one array into another.
Following It in NCL
Suppose #row and #column contain our zero-based coordinates.
#row $000
#column $001
#address $002
#rowBase $003
#cell [$002]
ADD #address 0x101 #row
MOVE #rowBase [#address]
ADD #address #rowBase 1
ADD #address #address #column
After this, #cell refers to the selected cell.
There is no special nested-array instruction.
We used an array to find an address, then used that address as the base of another array.
That's all nesting requires.
Jagged Arrays
When the inner arrays can have different lengths, the resulting structure is often called a jagged array.
Our hexagonal board is jagged:
3
4
5
4
3
Each row is an independently sized array.
This is different from the multidimensional array we built in the previous lesson.
| Flat multidimensional array | Nested array |
|---|---|
| One contiguous region | Several arrays |
| Dimensions have regular sizes | Inner arrays may have different sizes |
| Position found with arithmetic | Position found by following addresses |
| One regular layout | Inner arrays may live anywhere |
Both can represent data we think about in multiple dimensions.
They simply make different agreements about how that data is stored.
Nested doesn't necessarily mean jagged
The inner arrays of a nested array can all have the same length.
For example, five separate arrays of five elements each could still represent a 5×5 grid.
Nested describes how the arrays are stored and connected.
Jagged describes the fact that their lengths differ.
The Rows Can Move
Our row arrays were scattered around memory:
row 0 → $200
row 1 → $240
row 2 → $180
row 3 → $300
row 4 → $280
That doesn't matter to the program using them.
The outer array tells us where each row is.
If we move row 2 from $180 to $380, we don't need to move the other rows or reorganize the entire board.
We change its entry in the outer array:
row 2 → $380
Code following that address now finds the row in its new location.
We've seen this idea before.
In NCL 407: Linked Lists, we stored addresses so one record could tell us where another record lived.
Here, we're doing the same thing with arrays.
Addresses let the structure of our data differ from its physical arrangement in memory.
The Inner Arrays Can Have Structure Too
So far, each inner array has contained individual cells.
But there's no reason it has to stop there.
A row could be a strided array of records.
Each row could have its own header.
A cell could contain an address to another array.
That array could contain addresses to still more arrays.
There is nothing special about the outer array. Its elements happen to be addresses.
There is nothing special about the inner arrays either.
They are arrays, so their elements can also be addresses.
The same idea can be repeated as deeply as the data requires.
This gets big quickly
Once structures can contain addresses to other structures, we can represent far more than grids.
Collections can contain collections. Records can refer to collections. Collections can refer to records which refer to other collections.
Many much larger data structures are built from combinations of the same ideas we've already used: consecutive values, records, metadata, and addresses.
The individual pieces remain simple.
It's their relationships that create the larger structure.
Shape, Addressing, and Storage
Our hex board gives us three different questions.
What shape does the data have?
Five rows of different lengths:
3
4
5
4
3
How do we identify a cell?
With a row and a column.
How is it stored?
As an outer array containing addresses to five inner arrays.
Those answers don't have to resemble each other.
We could represent exactly the same board as a padded 5×5 array. We could store each row separately. We could choose some entirely different representation if it made the operations our program performs easier.
How the data is shaped, how we address it, and how we store it are separate decisions.
What We've Built
We started in NCL 404: Arrays with consecutive values:
[value][value][value][value]
Then we gave elements strides so one element could contain several fields.
We added headers so arrays could describe themselves.
We stored addresses as data and used them to connect structures.
We searched arrays instead of requiring the program to know an element's position.
We associated keys with values.
We interpreted linear arrays in two, three, and even more dimensions.
And now we've put arrays inside arrays.
None of that required a different kind of memory.
The processor still has the same locations from $000 through $FFF.
Everything we've built came from deciding what those locations mean and how the values inside them relate to one another.
A data structure is an agreement about what some memory means.
Try It
A headed outer array begins at $100.
Its three elements contain the addresses of three headed inner arrays:
| Row | Address |
|---|---|
| 0 | $200 |
| 1 | $240 |
| 2 | $180 |
#row and #index contain zero-based positions.
Write code that leaves the selected value in #value.
Your code should work regardless of which of the three rows is selected or where that row happens to live.
Find the array first
Don't try to calculate the final address all at once.
First use #row to find the corresponding entry in the outer array.
That entry contains the base address of the inner array.
Still stuck?
Both arrays are headed.
That means their first elements begin one location after their base addresses.
First calculate:
outer base + 1 + row
Read the address stored there.
Then calculate:
inner base + 1 + index
Show me one solution
#row $000
#index $001
#address $002
#inner $003
#value $004
ADD #address 0x101 #row
MOVE #inner [#address]
ADD #address #inner 1
ADD #address #address #index
MOVE #value [#address]
In the 400-level, we've learned how to organize ordinary memory into stacks, arrays, records, linked lists, associative arrays, multidimensional arrays, and structures containing other structures.
In this lesson, we used those pieces together to build arrays whose elements lead to other arrays, letting us represent structures that don't need to fit one regular shape in memory.
Ready to continue? In the next lesson, NCL 501: The Call Stack, we'll turn our attention from organizing data to organizing the subroutines that work with it.
Want to try one yourself first? The 400-Level Challenge asks you to use ordinary memory and indirect addressing to build a stack of your own.