Minor cleanup.

This commit is contained in:
Simon Forman 2023-02-16 13:36:31 -08:00
parent e056831577
commit bf30bbe69b
1 changed files with 11 additions and 20 deletions

View File

@ -78,36 +78,27 @@ def repr_vlist(vlist_ptr):
def pick(n, vlist_ptr):
if not vlist_ptr:
raise ValueError("Empty list!")
'''
Consider starting with a list pointer in Fig 1 then to find the nth
element subtract n from the pointer offset. If the result is positive
then the element is in the first 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 move to the next
block using the Base-Offset pointer. Add the Previous pointer offset
to the negative offset. While this remains negative keep moving onto
the next block. When it finally becomes positive the position of the
required element has been found
'''
assert n >= 0
if not vlist_ptr:
raise ValueError("Empty list!")
vlist, pointer_offset = vlist_ptr
q = pointer_offset - n
if q >= 0:
return vlist[-1][q]
'''
...move to the next block using the Base-Offset pointer. Add the
Previous pointer offset to the negative offset. While this remains
negative keep moving onto the next block. When it finally becomes
positive the position of the required element has been found
'''
while True:
assert q < 0
while q < 0:
if not vlist:
raise ValueError(f'Pick index {n} greater than length of list.')
vlist, offset = vlist[:2]
q += offset + 1 # Offsets in the paper count from one, not zero?
if q >= 0:
return vlist[-1][q]
return vlist[-1][q]
def length(vlist_ptr):
@ -134,7 +125,7 @@ print(repr_vlist(p))
for n in range(length(p)):
print(pick(n, p), end=' ')
print()