Array 9 January 2015 OSU CSE 1
Array The Array component family allows you to manipulate arrays in a way that overcomes surprising limitations of built-in Java arrays, but retains the time/space performance of built-in arrays Another generic type like Sequence and Set A best practice alternative from the OSU CSE components to the built-in Java array (more like built-in arrays than Sequence is) 9 January 2015 OSU CSE 2
Problems With Built-in Arrays Suppose you want to declare an array of type T, where T is a generic parameter, e.g.: T[] table = new T[100]; Oops; this won t compile! The reason is complicated, having to do with backward compatibility when Java generics were introduced and the inability to call a constructor for type T, among other oddities 9 January 2015 OSU CSE 3
Problems With Built-in Arrays Or, suppose you want to declare an array of type Queue<T>, e.g.: Queue<T>[] table = new Queue<T>[100]; Oops; this won t compile either! Yet this is exactly the kind of thing you want to do when creating a hashtable: the buckets are collections of a parameterized type 9 January 2015 OSU CSE 4
Workarounds There are various workarounds that can be used, but they are ugly and generate warnings that you should generally heed rather than ignore These problems do not arise with the Array component family, so it can (and should) be used in many cases where you might be inclined to try built-in Java arrays 9 January 2015 OSU CSE 5
The Intuition The rather simple idea is to provide a generic interface Array<T> that provides the same functionality as built-in arrays, but where the entries may be of any type T The data representation used in Array1L<T> is a built-in array But this data representation is hidden from the client, along with one of the workarounds mentioned earlier 9 January 2015 OSU CSE 6
Interfaces and Classes Standard extends extends Iterable ArrayKernel extends Array implements Array1L 9 January 2015 OSU CSE 7
Interfaces and Classes Standard extends extends Iterable ArrayKernel ArrayKernel has contracts for four methods: setentry entry maybeexamined length extends Array implements Array1L 9 January 2015 OSU CSE 8
Interfaces and Classes Standard Array has contracts for Iterable two methods: extends replaceentry extends exchangeentries ArrayKernel Array Array1L extends implements 9 January 2015 OSU CSE 9
Interfaces and Classes There is really Standard abstract Iterable class as usual in the chain here, but it is not extends shown extends because these slides describe the client view, ArrayKernel and a client needs only interface Array and class Array1L. extends Array Array1L implements 9 January 2015 OSU CSE 10
Mathematical Model ARRAY_MODEL is ( entries: string of T, examinableindices: finite set of integer) exemplar a constraint for all i: integer where (i is in a.examinableindices) (0 <= i and i < a.entries ) type ArrayKernel is modeled by ARRAY_MODEL 9 January 2015 OSU CSE 11
Mathematical Model ARRAY_MODEL is ( entries: string of T, examinableindices: finite set of integer) exemplar a constraint for all i: integer The set examinableindices where (i is keeps in a.examinableindices) track of the indices in the Array that have been initialized (0 <= i and by i a < call a.entries ) to setentry, because only these indices may be type ArrayKernel is examined modeled by a by call ARRAY_MODEL to entry. 9 January 2015 OSU CSE 12
Constructor from int There is no no-argument constructor, but there is a constructor with one parameter int n Requires: n >= 0 Ensures: this.entries = n and this.examinableindices = {} 9 January 2015 OSU CSE 13
Example Code State Array<Integer> ai = new Array1L<>(3); 9 January 2015 OSU CSE 14
Example Code State Array<Integer> ai = new Array1L<>(3); ai = (<?,?,?>, {}) 9 January 2015 OSU CSE 15
Example All we know from the constructor Code contract is that the length of this string of integer is 3; we do not know its actual value. Array<Integer> ai = new Array1L<>(3); State ai = (<?,?,?>, {}) 9 January 2015 OSU CSE 16
setentry void setentry(int i, T x) Sets the entry at index i of this to x. Aliases: reference x Updates: this Requires: 0 <= i and i < this.entries Ensures: this.entries = #this.entries[0, i) * <x> * #this.entries[i+1, #this.entries ) and this.examinableindices = #this.examinableindices union {i} 9 January 2015 OSU CSE 17
Example Code State ai = (<?,?,?>, {}) z = 70 ai.setentry(1, z); 9 January 2015 OSU CSE 18
Example Code State ai = (<?,?,?>, {}) z = 70 ai.setentry(1, z); ai = (<?, 70,?>, {1}) z = 70 9 January 2015 OSU CSE 19
Example Note the alias created here, which Code you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it. State ai = (<?,?,?>, {}) z = 70 ai.setentry(1, z); ai = (<?, 70,?>, {1}) z = 70 9 January 2015 OSU CSE 20
Another Example Code State ai = (<?, -8,?>, {1}) z = 70 ai.setentry(1, z); 9 January 2015 OSU CSE 21
Another Example Code State ai = (<?, -8,?>, {1}) z = 70 ai.setentry(1, z); ai = (<?, 70,?>, {1}) z = 70 9 January 2015 OSU CSE 22
entry T entry (int i) Reports the entry at index i of this. Aliases: reference returned by entry Requires: i is in this.examinableindices Ensures: <entry> = this.entries[i, i+1) 9 January 2015 OSU CSE 23
entry T entry(int i) i is within bounds, i.e.: Reports the entry at position 0 <= i iof this. and i < this.entries Aliases: reference returned by entryat Note that this implies the index Requires: i is in this.examinableindices Ensures: <entry> = this.entries[i, i+1) 9 January 2015 OSU CSE 24
Example Code State ai = (<?, 70, -3>, {1, 2}) z = 584 z = ai.entry(1); 9 January 2015 OSU CSE 25
Example Code State ai = (<?, 70, -3>, {1, 2}) z = 584 z = ai.entry(1); ai = (<?, 70, -3>, {1, 2}) z = 70 9 January 2015 OSU CSE 26
Example Note the alias Code created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it. z = ai.entry(1); State ai = (<?, 70, -3>, {1, 2}) z = 584 ai = (<?, 70, -3>, {1, 2}) z = 70 9 January 2015 OSU CSE 27
maybeexamined boolean maybeexamined(int i) Reports whether the entry at index i of this may be examined using entry. Ensures: maybeexamined = i is in this.examinableindices 9 January 2015 OSU CSE 28
Example Code State ai = (<?, 70, -3>, {1, 2}) boolean ok = ai.maybeexamined(1); 9 January 2015 OSU CSE 29
Example Code State ai = (<?, 70, -3>, {1, 2}) boolean ok = ai.maybeexamined(1); ai = (<?, 70, -3>, {1, 2}) ok = true 9 January 2015 OSU CSE 30
length int length() Reports the length of this. Ensures: length = this.entries 9 January 2015 OSU CSE 31
replaceentry T replaceentry(int i, T x) Replaces the entry at index i of this with x, and returns the old entry at that index. Aliases: reference x Updates: this.entries Requires: i is in this.examinableindices Ensures: this.entries = #this.entries[0, i) * <x> * #this.entries[i+1, #this ) and <replaceentry> = #this.entries[i, i+1) 9 January 2015 OSU CSE 32
Example Code State ai = (<?, 70, -3>, {1, 2}) z = 58 w = 94 w = ai.replaceentry(2, z); 9 January 2015 OSU CSE 33
Example Code State ai = (<?, 70, -3>, {1, 2}) z = 58 w = 94 w = ai.replaceentry(2, z); ai = (<?, 70, 58>, {1, 2}) z = 58 w = -3 9 January 2015 OSU CSE 34
Example Note the alias Code created here, which you cannot see in the tracing table; you should be able to draw the appropriate diagram showing it. State ai = (<?, 70, -3>, {1, 2}) z = 58 w = 94 w = ai.replaceentry(2, z); ai = (<?, 70, 58>, {1, 2}) z = 58 w = -3 9 January 2015 OSU CSE 35
Another Example Code State ai = (<?, 70, -3>, {1, 2}) z = 58 z = ai.replaceentry(2, z); 9 January 2015 OSU CSE 36
Another Example Code State ai = (<?, 70, -3>, {1, 2}) z = 58 z = ai.replaceentry(2, z); ai = (<?, 70, 58>, {1, 2}) z = -3 9 January 2015 OSU CSE 37
Another Example This use of Code the method avoids creating an alias: it swaps z with the entry previously at index 2. State ai = (<?, 70, -3>, {1, 2}) z = 58 z = ai.replaceentry(2, z); ai = (<?, 70, 58>, {1, 2}) z = -3 9 January 2015 OSU CSE 38
exchangeentries void exchangeentries(int i, int j) Exchanges entries at indices i and j of this. Updates: this.entries Requires: {i, j} is subset of this.examinableindices Ensures: this.entries = [#this.entries with entries at indices i and j exchanged] 9 January 2015 OSU CSE 39
Example Code State ai = (<?, 70, -3>, {1, 2}) ai.exchangeentries(1, 2); 9 January 2015 OSU CSE 40
Example Code State ai = (<?, 70, -3>, {1, 2}) ai.exchangeentries(1, 2); ai = (<?, -3, 70>, {1, 2}) 9 January 2015 OSU CSE 41
iterator Iterator<T> iterator() Returns an iterator over a set of elements of type T. Ensures: ~this.seen * ~this.unseen = [string of entries of this.entries, in order by increasing index, where index is in this.examinableindices] 9 January 2015 OSU CSE 42
Resources OSU CSE Components API: Array http://cse.osu.edu/software/common/doc/ 9 January 2015 OSU CSE 43