-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparseBoundedGrid3.java
79 lines (69 loc) · 1.89 KB
/
SparseBoundedGrid3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package info.gridworld.grid;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
/**
* An <code>UnboundedGrid</code> is a rectangular grid with an unbounded number of rows and
* columns. <br />
* The implementation of this class is testable on the AP CS AB exam.
*/
public class SparseBoundedGrid3<E> extends AbstractGrid<E>{
private Map<Location, E> occupantMap;
private int rows;
private int cols;
/**
* Constructs an empty unbounded grid.
*/
public SparseBoundedGrid3(int rowsNum,int colsNum)
{
if(rowsNum <= 0){
throw new IllegalArgumentException("rowsNum <= 0");
}
if(colsNum <= 0) {
throw new IllegalArgumentException("colsNum <= 0");
}
rows = rowsNum;
cols = colsNum;
occupantMap = new HashMap<Location, E>();
}
public int getNumRows()
{
return rows;
}
public int getNumCols()
{
return cols;
}
public boolean isValid(Location loc)
{
return 0 <= loc.getRow() && loc.getRow() < getNumRows()
&& 0 <= loc.getCol() && loc.getCol() < getNumCols();
}
public ArrayList<Location> getOccupiedLocations()
{
ArrayList<Location> a = new ArrayList<Location>();
for (Location loc : occupantMap.keySet())
a.add(loc);
return a;
}
public E get(Location loc)
{
if (loc == null)
throw new NullPointerException("loc == null");
return occupantMap.get(loc);
}
public E put(Location loc, E obj)
{
if (loc == null)
throw new NullPointerException("loc == null");
if (obj == null)
throw new NullPointerException("obj == null");
return occupantMap.put(loc, obj);
}
public E remove(Location loc)
{
if (loc == null)
throw new NullPointerException("loc == null");
return occupantMap.remove(loc);
}
}