-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMovieTable.java
103 lines (78 loc) · 2.45 KB
/
MovieTable.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
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
import java.util.ArrayList;
import java.util.HashSet;
/**
* Created by jkeys on 12/3/2015.
*/
public class MovieTable {
//private Object[] keys;
//private Object[] dataX;
private boolean[] used;
private int num;
Movie data[];
public MovieTable() {
//keys = new Object[501];
//dataX = new Object[501];
used = new boolean[501];
data = new Movie[501];
num = 0;
for(int i = 0; i < used.length; i++) {
data[i] = null;
used[i] = false;
}
}
private int hash(Movie m) {
return m.getMovieID() % used.length;
}
public void put(Movie m) {
int mHash = hash(m);
while(data[mHash] != null)
mHash++;
data[mHash] = m;
num++;
}
public boolean remove(int movieid) {
if(search(movieid) == -1) return false;
int idx = search(movieid);
used[idx] = true;
data[idx] = null;
num--;
return true;
}
public int search(int movieid) {
int result = -1;
int mHash = movieid % used.length;
if(data[mHash] != null)
return mHash;
while(true) {
if(data[mHash] == null)
return -1;
if(data[mHash].getMovieID() == movieid)
return mHash;
mHash++;
}
}
public static void main(String[] args) {
MovieTable mt = new MovieTable();
Movie m1 = new Movie("hello world");
Movie m2 = new Movie("star trek");
Movie m3 = new Movie("tarantino");
m1.setMovieID(5555);
m2.setMovieID(1111);
m3.setMovieID(3333);
System.out.println(mt.search(m1.getMovieID())); //should print "-1"
System.out.println(mt.search(m2.getMovieID())); //should print "-1"
System.out.println(mt.search(m3.getMovieID())); //should print "-1"
mt.put(m1);
mt.put(m2);
mt.put(m3);
System.out.println(mt.search(m1.getMovieID()));
System.out.println(mt.search(m2.getMovieID()));
System.out.println(mt.search(m3.getMovieID()));
mt.remove(m1.getMovieID());
mt.remove(m2.getMovieID());
mt.remove(m3.getMovieID());
System.out.println(mt.search(m1.getMovieID())); //should print "-1"
System.out.println(mt.search(m2.getMovieID())); //should print "-1"
System.out.println(mt.search(m3.getMovieID())); //should print "-1"
}
}