Minor cleanup.

This commit is contained in:
Simon Forman 2023-02-16 13:26:11 -08:00
parent 6cf168c280
commit e056831577
1 changed files with 32 additions and 30 deletions

View File

@ -3,13 +3,15 @@ An exploration of Phil Bagwell's VList.
A VList is modeled as a 5-tuple: A VList is modeled as a 5-tuple:
base: VList base: VList
offset: int (indexing base VList data) offset: int, indexing (from 0) the base VList data list.
size: int size: int, length of the data list.
last_used: to make a mutable int it's an int in a list last_used: to make a mutable int it's an int in a list
It's a count here rather than an offset! It's a count here rather than an offset!
data: a list of length length That is, it starts from 1, rather than 0.
data: a list of length size.
a Pointer to a VList is a pair of (VList and offset). A pointer to a VList is a two-tuple of (VList, offset) where
the offset is an index into the VList's data list.
''' '''
@ -18,34 +20,36 @@ def cons(thing, vlist_ptr):
if not vlist_ptr: if not vlist_ptr:
return ((), 0, 1, [1], [thing]), 0 return ((), 0, 1, [1], [thing]), 0
(base, offset, size, last_used_list, data), pointer_offset = vlist_ptr (base, _offset, size, last_used_list, data), pointer_offset = vlist_ptr
[last_used] = last_used_list [last_used] = last_used_list
''' '''
During the consing of (9) the pointer offset is compared with the
During the consing of (9) the pointer offset last used offset, LastUsed. If it is the same and less than the block
is compared with the last used offset, LastUsed. If it is the same and less than size then it is simply incremented, the new entry made and LastUsed
the block size then it is simply incremented, the new entry made and LastUsed
updated. updated.
''' '''
if pointer_offset == last_used - 1 and last_used < size: if pointer_offset == last_used - 1 and last_used < size:
pointer_offset += 1 pointer_offset += 1
data[pointer_offset] = thing data[pointer_offset] = thing
last_used_list[0] = last_used + 1 last_used_list[0] = last_used + 1
return vlist_ptr[0], pointer_offset return vlist_ptr[0], pointer_offset
'''
If on the other-hand the pointer offset is less than the LastUsed a cons is being applied
to the tail of a longer list, as is the case with the (9). In this case a new list block
must be allocated and its Base-Offset pointer set to the tail contained in the original
list. The offset part being set to the point in tail that must be extended. The new
entry can now be made and additional elements added.
''' '''
If on the other-hand the pointer offset is less than the LastUsed a
cons is being applied to the tail of a longer list, as is the case
with the (9). In this case a new list block must be allocated and its
Base-Offset pointer set to the tail contained in the original list.
The offset part being set to the point in tail that must be extended.
The new entry can now be made and additional elements added.
'''
# Is this where we increase the size x 2? # Is this where we increase the size x 2?
size <<= 1 ; l = [None] * size ; l[0] = thing size <<= 1
return (vlist_ptr[0], pointer_offset, size, [1], l), 0 data = [None] * size
data[0] = thing
return (vlist_ptr[0], pointer_offset, size, [1], data), 0
def head(vlist_ptr): def head(vlist_ptr):
@ -78,11 +82,10 @@ def pick(n, vlist_ptr):
raise ValueError("Empty list!") raise ValueError("Empty list!")
''' '''
Consider starting with a list pointer in Fig 1 then to find the nth
Consider starting with a list pointer in Fig 1 then to find the nth element subtract element subtract n from the pointer offset. If the result is positive
n from the pointer offset. If the result is positive then the element is in the first then the element is in the first block of the list at the calculated
block of the list at the calculated offset from the base. If the result is negative then... offset from the base. If the result is negative then...
''' '''
vlist, pointer_offset = vlist_ptr vlist, pointer_offset = vlist_ptr
@ -91,13 +94,12 @@ def pick(n, vlist_ptr):
return vlist[-1][q] return vlist[-1][q]
''' '''
...move to the next block using the Base-Offset pointer. Add the
...move to the next block using the Base-Offset pointer. Add the Previous pointer Previous pointer offset to the negative offset. While this remains
offset to the negative offset. While this remains negative keep moving onto the next negative keep moving onto the next block. When it finally becomes
block. When it finally becomes positive the position of the required element has positive the position of the required element has been found
been found
''' '''
while True: while True:
assert q < 0 assert q < 0
if not vlist: if not vlist: