You are on page 1of 2

adt Array

uses Any, Integer


defines Array<T: Any>
operations
new: Integer x T -/-> Array<T>
get: Array<T> x Integer -/-> T
put: Array<T> x Integer x T -/-> Array<T>
length: Array<T> ---> Integer
preconditions
new(n, t): 0 < n
get(a, i): 0 <= i < length(a)
put(a, i, t): 0 <= i < length(a)
axioms
get(new(n, t), i) = t
get(put(a, i, t), j) = (if i = j then t else get(a, j))
length(new(n, t)) = n
length(put(a, i, t)) = length(a)

adt UglyArray
uses Any, Integer
defines Array<T: Any>
operations
new: Integer -/-> Array<NULL>
get: Array<T> x Integer -/-> T
put: Array<T> x Integer x T -/-> Array<T>
length: Array<T> ---> Integer
preconditions
new(n, t): 0 < n
get(a, i): 0 <= i < length(a)
put(a, i, t): 0 <= i < length(a)
axioms
get(new(n, t), i) = t
get(put(a, i, t), j) = (if i = j then t else get(a, j))
length(new(n, t)) = n
length(put(a, i, t)) = length(a)

adt FlexibleArray
uses Any, Integer
defines Array<T: Any>
operations
new: Integer x Integer x Integer x T -/-> Array<T>
get: Array<T> x Integer -/-> T
put: Array<T> x Integer x T -/-> Array<T>
length: Array<T> ---> Integer
preconditions
new(j, k, t): 0 <= j <= k
get(a, i): 0 <= i < length(a)
put(a, i, t): 0 <= i < length(a)
axioms
get(new(j, k, t), i) = (if j <= i <= k then t else NULL)
get(new(j, j, t), i) = (if j = i, then t else NULL)
get(new(j, k, t), j) = t
get(new(j, k, t), k) = t
get(put(a, i, t), j) = (if i = j then t else get(a, j))
length(new(n, t)) = n
length(put(a, i, t)) = length(a)

Changes made:
Operations:
The constructor now receives three inputs of starting index, ending index, and
element to insert

Preconditions:
The constructor can receive starting index that is equal to zero, and also less
than or equal to
ending index

Axioms:
first line: only the indices initialized contains input argument, otherwise NULL
second line: if starting index = ending index, then it contains the input argument
only at one location
of starting or ending index, otherwise NULL.
third and fourth line:input arguments are initialized from starting to ending index
INCLUSIVE

Which array initializer I would have:


I would rather use the standard Array implementation because since every element is
initalized,
the program would not crash if I try to access an unitialized index. with
FlexibleArray, the program
could crash if one tries to access an uninitialized index, which may happen quite
frequently.

You might also like