Proofs: Sequential search symbol table
These are the human proofs of Sequential search symbol table, at full length.
The sequential search symbol table returns the stored values and keeps its invariant full
This proof shows: correctness, termination.
Statement. Let satisfy (I1) to (I3) for a map with keys. Then GET, PUT and DELETE stop, and:
- GET returns if and nil otherwise, and changes nothing;
- after PUT, (I1) to (I3) hold for the map ;
- after DELETE, (I1) to (I3) hold for the map without .
Setup. By (I1), the nodes from are in order, with . Write for nil.
Lemma (the scan). In GET, each time line L2 tests its condition, for some with , and no node among has key . The loop runs at most rounds.
Proof. Line L1 sets , and the range of earlier nodes is empty. Assume the claim with and that the test succeeds, so . If line L3 finds , the procedure returns. Otherwise , and line L4 sets , so the claim holds for . The number drops by exactly 1 per round, so there are at most rounds.
GET. If line L3 returns, it returns with . By (I2) is the only node with key , and by (I3) its value is . If the loop ends, , so , and by the lemma no node has key ; by (I3), , and line L5 returns nil. GET writes no field.
PUT. Lines L6 to L9 are the scan of the lemma, with line L8 in place of line L3. If line L8 finds the node of , it sets its value to and returns. The keys and the list do not change, so (I1) and (I2) hold, and the pairs are those of with the pair of replaced by , which is (I3) for . Otherwise the scan ends with no node of key , so . Line L10 makes a node with key , value and (or nil when ), sets and . The list from is , which has nodes and ends in nil (I1). The keys are distinct, because differs from every key of (I2). The pairs are those of plus , and this is since (I3).
DELETE. Each time line D2 tests its condition, with , (nil for ), and no node before has key ; this holds after line D1 and is kept by each round, which moves both pointers one node forward, and the rounds end after at most steps as in the lemma. If the loop ends with , then , line D3 returns, and the map without is . Otherwise is the only node with key . If , line D4 sets ; else it sets . In both cases the list from is , and line D5 sets , which is its length (I1). Its keys are a subset of distinct keys (I2), and its pairs are those of without the pair of (I3).
Compare counts. In the scan, each round makes one compare of with a key. A hit at ends in round , after compares; a miss makes rounds and compares. This gives the counts of the Complexity section.