Code patternsDataStore and fetch by index

Store and fetch by index

Reads or writes item number i of an array, when i is a value on the tape.

Watch it run

The form “Fetch, carrying the value home” works on 21 cells; the named ones are “zero”, “index”, “out”, “count”, “item 0”, “carry”, “item 1”, “item 2”, “item 3”, “item 4” and “item 5”. Press Play, drag the step slider forwards and back, or change index i with the slider and edit the array:

The figure needs JavaScript. The program below is the result for the example values.

There are no addresses. The program cannot say "go to cell i". It can only walk, and count the walk down: the index travels along with the pointer and loses 1 at every item.

In our form each item is three cells: a counter slot, the item, and a carry slot. Walking out, the counter is moved one item on and a 1 is left behind as a trail. At the item, the value is copied into the carry slot.

Walking back, the trail says "not home yet": each step clears one trail mark and moves the carry one item to the left. A zero cell before the array ends the walk.

The wiki form uses two cells per item and lays the whole trail first. Its fetch walks the trail once for every unit of the value, so its cost grows with index × value. Ours grows with the index only.

The code

This is the program the figure ran. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:

>[->>+<<]>>[-[->>>+<<<]+>>>]>[->+<<+>]<[->+<]<<<[->>>>>[-<<<+>>>]<<<<<<<<]>>>>>[-<<<+>>>]<<<<<
Open in the playground94 characters

What it needs

Common mistakes
  • An index past the end: the walk goes on into whatever follows the array.
  • Forgetting that the walk costs time. Item 1000 is 1000 hops away, every time. The project cuts this to a few dozen with long hops: see Build toward a chatbot, lesson 7, "No addresses".

What it costs

A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:

The figure needs JavaScript. The table below has the measured steps.

Cost as a formula, and measured
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
Fetch, carrying the value homegrows with the index, and with the value times the index7 + 7 per hop
Storegrows with the index, and with the value times the index6 + 7 per hop
Fetch, the form from the wikigrows with index × valuegrows with index × value
Store, the form from the wikigrows with index × valuegrows with index × value
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Fetch, carrying the value homethe array = 7, 3, 9, 4, 6, 2; index i = 08941787
Fetch, carrying the value homethe array = 7, 3, 9, 4, 6, 2; index i = 589439842
Fetch, carrying the value homethe array = 7, 3, 99, 4, 6, 2; index i = 28944,05321
Storethe array = 7, 3, 9, 4, 6, 2; index i = 2; value = 508741,69120
Fetch, the form from the wikithe array = 7, 3, 9, 4, 6, 2; index i = 08114298121
Fetch, the form from the wikithe array = 7, 3, 9, 4, 6, 2; index i = 58114563171
Fetch, the form from the wikithe array = 7, 3, 99, 4, 6, 2; index i = 281146,1922,321
Store, the form from the wikithe array = 7, 3, 9, 4, 6, 2; index i = 2; value = 5081072,265596

Other forms

Store

Item i becomes the value. The layout of the main form (Fetch, carrying the value home, the first figure), with the value in the third cell.

>[->>+<<]>[->>>+<<<]>[-[->>>+<<<]>>[->>>+<<<]<<+>>>]>[-]>[-<+>]<<<<<[-<<<]
Open in the playground74 characters
Run this form step by step

The form “Store” works on 21 cells; the named ones are “zero”, “index”, “value”, “count”, “item 0”, “carry”, “item 1”, “item 2”, “item 3”, “item 4” and “item 5”. Press Play, drag the step slider forwards and back, or change index i and value with the sliders and edit the array:

The figure needs JavaScript. The program below is the result for the example values.

Fetch, the form from the wiki

out gets item i; the index is kept. Two cells per item. The walk is repeated for every unit of the value.

[->>>>>+<+<<<<]>>>>[-<<<<+>>>>]<>>[[>>]+[<<]>>-]+[>>]<[<[<<]>+<<<+>>>>[>>]<-]<[<<]>[>[>>]<+<[<<]>-]>[>>]<<[-<<]<<<
Open in the playground114 characters
Run this form step by step

The form “Fetch, the form from the wiki” works on 18 cells; the named ones are “index”, “out”, “head”, “temp”, “trail”, “item 0”, “item 1”, “item 2”, “item 3”, “item 4” and “item 5”. Press Play, drag the step slider forwards and back, or change index i with the slider and edit the array:

The figure needs JavaScript. The program below is the result for the example values.

Store, the form from the wiki

Item i becomes the value; index and value are kept.

[->>>>>+<<<+<<]>>[-<<+>>]<[->>>+<<+<]>[-<+>]>>>[[>>]+[<<]>>-]+[>>]<[-]<[<<]>[>[>>]<+<[<<]>-]>[>>]<<[-<<]<<<
Open in the playground107 characters
Run this form step by step

The form “Store, the form from the wiki” works on 18 cells; the named ones are “index”, “value”, “head”, “temp”, “trail”, “item 0”, “item 1”, “item 2”, “item 3”, “item 4” and “item 5”. Press Play, drag the step slider forwards and back, or change index i and value with the sliders and edit the array:

The figure needs JavaScript. The program below is the result for the example values.

Where it is used

Sources

Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.