Array<T> Module
A resizable array of T with bounds checking.
Imports: Fatal
This module defines a resizable array of T that also performs bounds checking.
A real version would use exceptions to handle errors. The legal index range
of an Array of size x is 0 to x-1. Out-of-bounds accesses cause a fatal
error. The minimum size is 0.
Implicit State
- int size
- Size of the array
- T f[]
- The array
Constructors and Destructors
- Array(int s)
-
Preconditions: s >=0
Create an array of size s, init elements via the no-arg constructor for T
Postcondition: size=s
- Array()
-
Syntactic sugar for Array(100)
- Array(Array<T> &source)
-
Copy constructor
- ~Array()
-
Destructor
Operations
- T Get(int x) const
-
Preconditions: x >=0 and x < size
Modifies: none
Returns f[x]
- void Set(int x, T value)
-
Preconditions: x >=0 and x < size
Modifies: f[x]
Sets f[x] to value
- void Resize(int newSize)
-
Preconditions: s >= 0
Modifies: If array is resized to be smaller, this will delete the elements
at the end (above the resulting size).
Sets size=newSize. Elements that are defined before and after keep their
values; any new elements are initialized with the no-arg constructor for T.
- T operator[](int x) const
-
Syntactic sugar for Get(x)
Invariants
- size >=0
- f[x] defined for all 0 <= x < size
Author: Eric A. Brewer