4 Random Access Lists
4.1 Binary Random Access List
(require "binaryrandomaccesslist.ss") |
Random Access Lists are list data structures that provide array-like lookup and update operations. They have been implemented as a framework of binary numerical representation using complete binary leaf trees. It has a worst case running time of O(log(n)) for the operations cons, first, rest, list-ref and list-set.
(list a ...) → (List A) |
a : A |
Example: |
> (list 1 2 3 4 5 6) |
- : (U Null-RaList (Root Positive-Fixnum)) |
#<Root> |
In the above example, (list 1 2 3 4 5 6) gives a Binary Random Access List which is similar to lists but comes with efficient list-ref and list-set operations.
empty : (List Nothing) |
(empty? ral) → Boolean |
ral : (List A) |
(cons a ral) → (List A) |
a : A |
ral : (List A) |
In the above example, (cons 10 (list 5 3 5 6)) returns (list 10 5 3 5 6).
(first ral) → A |
ral : (List A) |
(rest ral) → (List A) |
ral : (List A) |
Examples: |
> (rest (list 1 2 3 4 5 6)) |
- : (U Null-RaList (Root Positive-Fixnum)) |
#<Root> |
> (rest empty) |
tail: given list is empty |
In the above example, (rest ral) returns the rest of the given list, (list 2 3 4 5 6).
(list-ref ral index) → A |
ral : (List A) |
index : Integer |
Examples: |
> (list-ref (list 12 5 3 2 15 23) 4) |
- : Positive-Fixnum |
15 |
> (list-ref (list 12 5 3 2 15 23) 10) |
list-ref: given index out of bound |
(list-set ral index newval) → (List A) |
ral : (List A) |
index : Integer |
newval : A |
Examples: |
> (list-set (list 1 2 3 4 5 6) 2 10) |
- : (U Null-RaList (Root Positive-Fixnum)) |
#<Root> |
> (list-set (list 1 2 3 4 5 6) 10 15) |
list-set: given index out of bound |
In the above example, (list-set (list 1 2 3 4 5 6) 2 10) returns (list 1 2 10 4 5 6) and (list-set (list 1 2 3 4 5 6) 10 15) throws an error.
(->list ral) → (Listof A) |
ral : (List A) |
Examples: |
> (define ral (list 1 2 3 4 5 6)) |
> (->list ral) |
- : (Listof Positive-Fixnum) |
'(1 2 3 4 5 6) |
In the above example, (->list ral) returns (list 1 2 3 4 5 6).
(drop ral num) → (List A) |
ral : (List A) |
num : Integer |
Examples: |
> (drop 3 (list 1 2 3 4 5 6)) |
- : (U Null-RaList (Root Positive-Fixnum)) |
#<Root> |
> (drop 10 (list 1 2 3 4 5 6)) |
drop: not enough elements to drop |
In the above example, (drop 3 (list 1 2 3 4 5 6)) returns the list (list 4 5 6). (drop 10 (list 1 2 3 4 5 6)) throws an error since 10 is larger than the number of elements in the list.
(list-length ral) → Integer |
ral : (List A) |
Examples: |
> (list-length (list 1 2 3 4 5 6)) |
- : Integer |
6 |
> (list-length empty) |
- : Integer |
0 |
4.2 Skew Binary Random Access List
(require "skewbinaryrandomaccesslist.ss") |
Random Access Lists are list data structures that provide array-like lookup and update operations. Skew Binary Random Access Lists are implemented using skew binary numbers. It provides a worst case running time of O(1) for the operations cons, first and rest and O(log(n)) for the operations list-ref and list-set.
(list a ...) → (List A) |
a : A |
Example: |
> (list 1 2 3 4 5 6) |
- : (Listof (Root Positive-Fixnum)) |
'(#<Root> #<Root>) |
In the above example, (list 1 2 3 4 5 6) gives a Skew Binary Random Access List which is similar to lists and has efficient lookup and update operations.
empty : (List Nothing) |
(empty? ral) → Boolean |
ral : (List A) |
(cons a ral) → (List A) |
a : A |
ral : (List A) |
In the above example, (cons 10 (list 1 2 3 4 5 6)) returns a (list 10 1 2 3 4 5 6).
(first ral) → A |
ral : (List A) |
Examples: |
> (first (list 1 2 3 4 5 6)) |
- : Positive-Fixnum |
1 |
> (first empty) |
head: given list is empty |
(rest ral) → (List A) |
ral : (List A) |
Examples: |
> (rest (list 1 2 3 4 5 6)) |
- : (Listof (Root Positive-Fixnum)) |
'(#<Root> #<Root> #<Root>) |
> (rest empty) |
tail: given list is empty |
In the above example, (rest (list 1 2 3 4 5 6)) returns the (list 2 3 4 5 6).
(list-ref ral index) → A |
ral : (List A) |
index : Integer |
Examples: |
> (list-ref (list 1 2 3 4 5 6) 0) |
- : Positive-Fixnum |
1 |
> (list-ref (list 1 2 3 4 5 6) 3) |
- : Positive-Fixnum |
4 |
> (list-ref (list 1 2 3 4 5 6) 10) |
list-ref: given index out of bounds |
(list-set ral index newval) → (List A) |
ral : (List A) |
index : Integer |
newval : A |
Examples: |
> (->list (list-set (list 1 2 3 4 5 6) 3 10)) |
- : (Listof Positive-Fixnum) |
'(1 2 3 10 5 6) |
> (->list (list-set (list 1 2 3 4 5 6) 6 10)) |
list-set: given index out of bounds |
In the above example, (list-set (list 1 2 3 4 5 6) 3 10) returns (list 1 2 3 10 5 6), (list-set (list 1 2 3 4 5 6) 6 10) throws an error since there are only six elements in the list and it is trying to update seventh element.
(->list ral) → (Listof A) |
ral : (List A) |
Examples: |
> (->list (list 1 2 3 4 5 6)) |
- : (Listof Positive-Fixnum) |
'(1 2 3 4 5 6) |
> (->list empty) |
- : (Listof Nothing) |
'() |
(drop num ral) → (List A) |
num : Integer |
ral : (List A) |
Examples: |
> (drop 3 (list 1 2 3 4 5 6)) |
- : (Listof (Root Positive-Fixnum)) |
'(#<Root>) |
> (drop 0 (list 1 2 3 4 5 6)) |
- : (Listof (Root Positive-Fixnum)) |
'(#<Root> #<Root>) |
> (drop 10 (list 1 2 3 4 5 6)) |
drop: not enough elements to drop |
In the above example, (drop 3 (list 1 2 3 4 5 6) 3) returns (list 4 5 6). (drop 0 (list 1 2 3 4 5 6)) returns the (list 1 2 3 4 5 6). If the given n is larger than the number of elements in the list, then it throws an error.
(list-length ral) → Integer |
ral : (List A) |
Examples: |
> (list-length (list 1 2 3 4 5 6)) |
- : Integer |
6 |
> (list-length (list 1 2 3)) |
- : Integer |
3 |
> (list-length empty) |
- : Integer |
0 |