Академический Документы
Профессиональный Документы
Культура Документы
Organizing and manipulating data is pervasive ADT abstracts away from a specific representation to
Often crucial to start by designing data structures focus on the semantic meaning of the data
In what sense are the following two definitions different?
Potential problems with choosing a data structure class Point { class Point {
Decisions about data structures are made too early float x, y; float r, theta;
} }
Very hard to change key data structures later on
Although the representations differ, the client should
instead consider a Point as a set of operations to
create and manipulate 2D points on the plane
By restricting the client to only call operations to access
data, the potential for modifying the representation (and
CSE 331 Autumn 2011
supporting algorithms) remains
CSE 331 Autumn 2011
class Point {
// A 2-d point exists somewhere in the plane, ... The only operations on objects of the type are those
public float x(); provided by the abstraction
public float y();
public float r(); The implementation is hidden
public float theta();
Point
x
// ... can be created, ...
y
public Point(); // new point at (0,0) rest of r
program theta
// ... can be moved, ...
translate
public void translate(float delta_x,
scale_rot
float delta_y);
public void scaleAndRotate(float delta_r, abstraction
clients implementation
float delta_theta); barrier
} CSE 331 Autumn 2011 CSE 331 Autumn 2011
1
ADTs and specifications Implementing an ADT
Specification: only in terms of the abstraction To implement a data abstraction
Never mentions the representation Select the representation of instances, the “rep”
Abstraction function: Object abstract value Implement operations in terms of that rep
What the data structure means as an abstract value In Java, you do this in a class – in fact, you’ve done it
2
Now we can locate the error Listing the elements of a CharSet
class CharSet {
// Rep invariant: elts has no nulls and no duplicates
Consider adding the following method to CharSet
// returns: a List containing the members of this
public List<Character> getElts();
private List<Character> elts = new ArrayList<Character>();
public void insert(Character c) { Consider this implementation
elts.add(c); // Rep invariant: elts has no nulls and no duplicates
} public List<Character> getElts() { return elts; }
public void delete(Character c) {
Does the implementation of getElts preserve the
elts.remove(c);
}
CharSet s = new CharSet();
Character a = new Character(‘a’);
rep invariant?
…
}
s.insert(a);
s.insert(a);
Kind of, sort of, not really…
s.delete(a);
if (s.member(a))
// print “wrong”;
else
// print “right”;
The abstraction function maps the representation to the Applying the abstraction function to the result of the
abstract value it represents call to insert yields AF(elts) {encrypt(‘a’)}
AF(CharSet this) = So when member is checked, the implementation looks for ‘a’
{ c | c is contained in this.elts } rather than the encrypted value of ‘a’ – from the client’s view, an
Or the “set of Characters contained in this.elts” inserted element is no longer found, even though it has not been
deleted
The abstraction function lets us reason about behavior from
the client perspective What if we used this abstraction function?
AF(this) = { c | encrypt(c) is contained in this.elts }
The AF is typically not executable
AF(this) = { decrypt(c) | c is contained in this.elts }
Do we satisfy the specification of insert?
// modifies: this
// effects: thispost = thispre {c}
public void insert (Character c);
CSE 331 Autumn 2011
3
Recap: the CharSet representation
19
Next steps
21
Assignment 1
Due tonight 11:59PM
Assignment 2
out later today
due in two parts (M 11:59PM and F 11:59PM)
Lectures
Abstract data types (M)
Modular design (W)