-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparseMatrix.java
More file actions
169 lines (163 loc) · 4.23 KB
/
SparseMatrix.java
File metadata and controls
169 lines (163 loc) · 4.23 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.ArrayList;
public class SparseMatrix<anyType> implements Matrixable<anyType>, Cloneable
{
private int rows = 0;
private int cols = 0;
private ArrayList<matrixentry> al = new ArrayList<matrixentry>();
private char bl = '-';
public SparseMatrix(int r, int c)
{
rows = r;
cols = c;
}
private SparseMatrix(ArrayList<matrixentry> mx,int r, int c)
{
al = mx;
rows = r;
cols = c;
}
public Matrixable<anyType> clone()
{
ArrayList<matrixentry> a = new ArrayList<matrixentry>();//(ArrayList<matrixentry>)al.clone();
for(int i = 0; i < al.size(); i++)
{
// a.set(i,a.get(i).clone());
a.add(al.get(i));
}
return (Matrixable<anyType>)(new SparseMatrix<anyType>(a,rows,cols));
}
private int getkey(int r,int c) {
return (r*cols)+c; }
public anyType get(int r, int c) //returns the element at row r, col c
{
int k = getkey(r,c);
for(int i = 0; i < al.size(); i++)
{
matrixentry m = al.get(i);
if(getkey(m.getrow(),m.getcol()) == k)
return (anyType)al.get(i).getobj();
}
return null;
}
public anyType set(int r, int c, anyType x) //changes element at (r,c), returns old value
{
int k = getkey(r,c);
for (int i = 0; i < al.size(); i++)
{
matrixentry m = al.get(i);
if(getkey(m.getrow(),m.getcol()) == k)
{
anyType o = (anyType)m.getobj();
m.setobj(x);
return o;
}
}
add(r,c,x);
return null;
}
public void add(int r, int c, anyType x) //adds obj at row r, col c
{
matrixentry m = new matrixentry(r,c,x);
for (int i = 0; i < al.size(); i++)
{
matrixentry old = al.get(i);
if (getkey(old.getrow(),old.getcol()) > getkey(m.getrow(),m.getcol()))
{
al.add(i,m);
return;
}
}
al.add(m);
}
public anyType remove(int r, int c)
{
int k = getkey(r,c);
anyType o = null;
for (int i = 0; i < al.size(); i++)
{
matrixentry m = al.get(i);
if (getkey(m.getrow(),m.getcol()) == k)
{
o = (anyType)m.getobj();
al.remove(i);
}
}
return o;
}
public int size() //returns # actual elements stored
{
return al.size();
}
public int numRows() //returns # rows set in constructor
{
return rows;
}
public int numColumns() //returns # cols set in constructor
{
return cols;
}
public String toString()
{
String s = "";
for (int r = 0; r < rows; r++)
{
//s+=r;
for (int c = 0; c < cols; c++)
{
anyType t = get(r,c);
if (t != null) s+= t+" ";
else s+=bl+" ";
}
s+="\n";
}
return s;
}
public Object[][] toArray() //returns equivalent structure in 2-D array form
{
Object[][] o = new Object[numRows()][numColumns()];
for (int i = 0; i < al.size(); i++)
{
matrixentry m = al.get(i);
o[m.getrow()][m.getcol()] = m.getobj();
}
return o;
}
public boolean contains(anyType x) //true if x exists in list
{
if (getLocation(x) != null)
return true;
else
return false;
}
public int[] getLocation(anyType x) //returns location [r,c] of where x exists in list, null otherwise
{
int[] coord = new int[2];
for (int i = 0; i < al.size(); i++)
{
matrixentry m = al.get(i);
if (m.getobj().equals(x))
{
coord[0] = m.getrow();
coord[1] = m.getcol();
return coord;
}
}
return null;
}
public boolean isEmpty() //returns true if there are no actual elements stored
{
if (al.size() == 0)
return true;
else
return false;
}
public void clear() //clears all elements out of the list
{
al.clear();
}
public void setBlank(char blank) //allows the client to set the character that a blank spot in the array is
//represented by in String toString()
{
bl = blank;
}
}