s LearnSqueak.com

SMALLTALK LAB

Sparse tables in Squeak: answer static range minima

Precompute windows of power-of-two lengths, then answer a range minimum with two overlapping windows.

7 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Use native Squeak 6.1. The sample readings are fixed; queries use one-based, inclusive endpoints. Start with the eight readings shown, and keep the original array separate from the table.

These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.

Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.

01 - Store the length-one row

The minimum of a one-element window is that element. A copy keeps the first row independent from the original list.

Independent Workspace expression - select all, then Print it
| data |
data := #(9 4 7 3 8 2 6 5).
data copy

Expected: #(9 4 7 3 8 2 6 5)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Store the length-one row
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Which reading is the smallest? It is 2.

Download this checkpoint

02 - Join adjacent halves

Combine adjacent singletons to compute each length-two window. Its left coordinate still identifies the result.

Independent Workspace expression - select all, then Print it
| data |
data := #(9 4 7 3 8 2 6 5).
((1 to: data size - 1) collect: [:i |
 (data at: i) min: (data at: i + 1)]) asArray

Expected: #(4 4 3 3 2 2 5)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Join adjacent halves
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Why is the row one slot shorter? The last item has no following item.

Download this checkpoint

03 - Build every power-of-two row

Build each width 2h from two width-h windows starting h positions apart. Here the widths are 1, 2, 4 and 8.

Independent Workspace expression - select all, then Print it
| build |
build := [:data | | rows half prev |
 data isEmpty ifTrue: [Error signal: 'Need at least one value'].
 rows := OrderedCollection with: data asArray copy.
 half := 1.
 [half * 2 <= data size] whileTrue: [
  prev := rows last.
  rows add: ((1 to: data size - (2 * half) + 1)
   collect: [:i | (prev at: i) min: (prev at: i + half)]) asArray.
  half := half * 2].
 rows asArray].
build value: #(9 4 7 3 8 2 6 5)

Expected: #((9 4 7 3 8 2 6 5) (4 4 3 3 2 2 5) (3 3 2 2 2) (2))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Build every power-of-two row
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The length-four row at position 2 represents positions 2 through 5.

Download this checkpoint

04 - Precompute the width and level lookup

For lengths 1–8, cache the largest power of two that fits and its row number. This gives constant-time lookup without floating logarithms.

Independent Workspace expression - select all, then Print it
| powers levels |
powers := Array new: 8 withAll: 1.
levels := Array new: 8 withAll: 1.
2 to: 8 do: [:i | | half |
 half := i // 2.
 powers at: i put: (powers at: half) * 2.
 levels at: i put: (levels at: half) + 1].
{powers. levels}

Expected: #((1 2 2 4 4 4 4 8) (1 2 2 3 3 3 3 4))

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Precompute the width and level lookup
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Length 6 uses width 4 in table row 3. For inputs of other lengths, build lookups through that length.

Download this checkpoint

05 - Cover the range with overlapping windows

For positions 2–6, take the width-four windows 2–5 and 3–6. The overlap is harmless because repeating a value does not change a minimum.

Independent Workspace expression - select all, then Print it
| build powers levels query |
build := [:data | | rows half prev |
 data isEmpty ifTrue: [Error signal: 'Need at least one value'].
 rows := OrderedCollection with: data asArray copy.
 half := 1.
 [half * 2 <= data size] whileTrue: [
  prev := rows last.
  rows add: ((1 to: data size - (2 * half) + 1)
   collect: [:i | (prev at: i) min: (prev at: i + half)]) asArray.
  half := half * 2].
 rows asArray].
powers := Array new: 8 withAll: 1.
levels := Array new: 8 withAll: 1.
2 to: 8 do: [:i | | half |
 half := i // 2.
 powers at: i put: (powers at: half) * 2.
 levels at: i put: (levels at: half) + 1].
query := [:table :left :right | | len width row |
 ((left < 1 or: [right > table first size]) or: [left > right])
  ifTrue: [Error signal: 'Invalid inclusive range'].
 len := right - left + 1.
 width := powers at: len.
 row := table at: (levels at: len).
 (row at: left) min: (row at: right - width + 1)].
query value: (build value: #(9 4 7 3 8 2 6 5)) value: 2 value: 6

Expected: 2

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Cover the range with overlapping windows
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The windows have minima 3 and 2. Their minimum is 2.

Download this checkpoint

06 - Check singleton and full span

The windows coincide on exact powers of two, including a singleton. The same code intentionally takes both minima.

Independent Workspace expression - select all, then Print it
| build powers levels query |
build := [:data | | rows half prev |
 data isEmpty ifTrue: [Error signal: 'Need at least one value'].
 rows := OrderedCollection with: data asArray copy.
 half := 1.
 [half * 2 <= data size] whileTrue: [
  prev := rows last.
  rows add: ((1 to: data size - (2 * half) + 1)
   collect: [:i | (prev at: i) min: (prev at: i + half)]) asArray.
  half := half * 2].
 rows asArray].
powers := Array new: 8 withAll: 1.
levels := Array new: 8 withAll: 1.
2 to: 8 do: [:i | | half |
 half := i // 2.
 powers at: i put: (powers at: half) * 2.
 levels at: i put: (levels at: half) + 1].
query := [:table :left :right | | len width row |
 ((left < 1 or: [right > table first size]) or: [left > right])
  ifTrue: [Error signal: 'Invalid inclusive range'].
 len := right - left + 1.
 width := powers at: len.
 row := table at: (levels at: len).
 (row at: left) min: (row at: right - width + 1)].
{query value: (build value: #(9 4 7 3 8 2 6 5)) value: 4 value: 4.
 query value: (build value: #(9 4 7 3 8 2 6 5)) value: 1 value: 8}

Expected: #(3 2)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Check singleton and full span
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: What would the singleton at position 8 return? 5.

Download this checkpoint

07 - Check every span against a direct scan

Scan each of the 36 nonempty spans independently and compare it with the table result.

Independent Workspace expression - select all, then Print it
| build powers levels query readings grid checks |
build := [:data | | rows half prev |
 data isEmpty ifTrue: [Error signal: 'Need at least one value'].
 rows := OrderedCollection with: data asArray copy.
 half := 1.
 [half * 2 <= data size] whileTrue: [
  prev := rows last.
  rows add: ((1 to: data size - (2 * half) + 1)
   collect: [:i | (prev at: i) min: (prev at: i + half)]) asArray.
  half := half * 2].
 rows asArray].
powers := Array new: 8 withAll: 1.
levels := Array new: 8 withAll: 1.
2 to: 8 do: [:i | | half |
 half := i // 2.
 powers at: i put: (powers at: half) * 2.
 levels at: i put: (levels at: half) + 1].
query := [:table :left :right | | len width row |
 ((left < 1 or: [right > table first size]) or: [left > right])
  ifTrue: [Error signal: 'Invalid inclusive range'].
 len := right - left + 1.
 width := powers at: len.
 row := table at: (levels at: len).
 (row at: left) min: (row at: right - width + 1)].
readings := #(9 4 7 3 8 2 6 5). grid := build value: readings. checks := 0.
1 to: 8 do: [:left | left to: 8 do: [:right |
 (query value: grid value: left value: right) = (readings copyFrom: left to: right) min
  ifFalse: [Error signal: 'Reference scan disagrees'].
 checks := checks + 1]].
checks

Expected: 36

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 7: Check every span against a direct scan
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: This tests every span of the sample, not every possible input.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Sparse tables in Squeak: answer static range minima native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

The table answers queries about a fixed array snapshot. If the readings change often, use a segment tree or rebuild the table.

Overlap works for minimum because min(x,x) = x. It would double-count items for sum; use prefix sums for static sums.

Table construction uses O(n log n) time and space. The auxiliary width and level arrays take O(n) space and time; each checked query then uses O(1) array lookups.

Three practice challenges

Trace a new range

Find the minimum on positions 3–5 and identify both windows.

Show solution

Length 3 uses windows 3–4 and 4–5. Both have minimum 3.

Predict an overlap pitfall

What would happen if the range operation were sum?

Show solution

Positions in both windows would be counted twice, so the method would be incorrect.

Reject an empty range

Pass left 4 and right 3.

Show solution

The function raises Invalid inclusive range before looking up a width.

Reference

Bender and Farach-Colton: The LCA Problem Revisited, §3 A Faster RMQ Algorithm

Continue learning

Segment Tree | Prefix Sums | First Min Max

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.