-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashing.java
More file actions
142 lines (138 loc) · 2.72 KB
/
Copy pathHashing.java
File metadata and controls
142 lines (138 loc) · 2.72 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
/**
* @file Hashing.java
* A class providing a hash function
*
* @author Paul Gibbons
* @date Created: Spring 2014
*/
public class Hashing
{
/**
* Constructor
* Returns new hash table
*
* @param numBuckets the number of buckets in the table
*/
public Hashing(int numBuckets)
{
buckets = new List[numBuckets];
}
/**
* Returns the location of the bucket containing the value
*
* @param name The name of the item
* @return the bucket containing the names value
*/
private int getBucket(String name)
{
int bucket = 0;
for(int i=0; i<name.length(); i++)
{
bucket+= name.charAt(i);
}
return bucket%buckets.length;
}
/**
* Adds a name and value to the hash table
* if a name is already in storage, changes the value associated with that name
*
* @param name The name of the item
* @param value The value to be stored with that name
*/
public void set(String name, int value)
{
if (name==null)
{
return;
}
int bucket = getBucket(name);
List current = new List(value, name);
if (this.buckets[bucket]==null) this.buckets[bucket] = current;
else this.buckets[bucket].append(current);
}
/**
* Returns the value stored with a given index
*
* @param name The search vale
* @param buckets Array of lists containing data
* @return value The value associated with the name, -1 if value was not found
*/
public int getValue(String name)
{
if (name==null)
{
return -1;
}
int bucket = getBucket(name);
List temp = this.buckets[bucket];
int rvalue = temp.find(name);
return rvalue;
}
/**
* Deletes the value associated with name
*
* @param name The name associated with the value to be deleted
* @param buckets Array of lists containing data
*/
public void delete(String name)
{
if (name==null)
{
return;
}
int bucket = getBucket(name);
if (buckets[bucket]==null) return;
buckets[bucket].remove(name);
}
/**
* Class to make linked List of values in buckets
* This will deal with collision
*/
private class List
{
int value;
String name;
List next;
public List(int value, String name)
{
this.value = value;
this.name = name;
next = null;
}
public void append(List newList)
{
if(newList.name.equals(name))
{
value = newList.value;
}
if (next==null)
{
next = newList;
}
else
{
next.append(newList);
}
}
public int find(String name)
{
if (name.equals(this.name)) return value;
if (next==null) return -1;
return next.find(name);
}
public List remove(String name)
{
if (this.name.equals(name))
{
return next;
}
if (next==null) return this;
next = next.remove(name);
return this;
}
}
/**
* Instance variables
*/
private List[] buckets;
}