Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. /**
  8. *
  9. * @author moham
  10. */
  11. public class DelegateHash implements IDelegateDB{
  12. private Delegate[] table;
  13. private static int tableSize = 10;
  14. private int numEntries;
  15. private double loadFactor;
  16.  
  17. public DelegateHash(){
  18. table = new Delegate[tableSize];
  19. }
  20.  
  21. /**
  22. * Empties the database.
  23. * @pre true
  24. */
  25.  
  26. @Override
  27. public void clearDB() {
  28. table = new Delegate[tableSize];
  29. numEntries = 0;
  30. }
  31. /**
  32. * Determines whether a delegates name exists as a key inside the database
  33. * @pre name not null or empty string
  34. * @param name the Delegate name (key) to locate
  35. * @return true iff the name exists as a key in the database
  36. */
  37. @Override
  38. public boolean containsName(String name){
  39. assert !name.equals("");
  40. for(Delegate d: table){
  41. if((d != null) && d.getName().equals(name)){
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. /**
  48. * Returns a Delegate object mapped to the supplied name.
  49. * @pre name not null or empty string
  50. * @param name The Delegate name (key) to locate
  51. * @return the Delegate object mapped to the key name if the name
  52. * exists as key in the database, otherwise null
  53. */
  54. @Override
  55. public Delegate get(String name){
  56. int pos = hashFunction(name); // get position hashing
  57. int i = 1;
  58. pos = quadraticProbing(pos, i);
  59.  
  60. while (pos < tableSize) {
  61. if (table[pos] != null) {
  62. if(table[pos].getName().equals(name)){
  63. return table[pos];
  64. }
  65. }
  66.  
  67. else {
  68. pos = quadraticProbing(pos, i);
  69. i++;
  70. }
  71. }
  72. System.out.println(name + " is not in the table");
  73. return null;
  74. }
  75. /**
  76. * Returns the number of delegates in the database
  77. * @pre true
  78. * @return number of delegates in the database. 0 if empty
  79. */
  80. @Override
  81. public int size(){
  82. return numEntries;
  83. }
  84.  
  85. /**
  86. * Determines if the database is empty or not.
  87. * @pre true
  88. * @return true iff the database is empty
  89. */
  90. @Override
  91. public boolean isEmpty() {
  92. return size() == 0;
  93. }
  94.  
  95. /**
  96. * Inserts a delegate object into the database, with the key of the supplied
  97. * delegates name.
  98. * Note: If the name already exists as a key, then then the original entry
  99. * is overwritten.
  100. * This method must return the previous associated value
  101. * if one exists, otherwise null
  102. *
  103. * @pre delegate not null
  104. */
  105. @Override
  106. public Delegate put(Delegate delegate) {
  107. assert delegate != null;
  108. assert delegate.getName() != null && !delegate.getName().equals("");
  109.  
  110. int pos = hashFunction(delegate.getName()); // get position hashing
  111. Delegate previous = null;
  112. int i = 1;
  113. pos = quadraticProbing(pos, i);
  114.  
  115. while (pos < tableSize && table[pos] != delegate) {
  116. System.out.println(delegate.getName() + " inital array location (buckets visited) is " + pos);
  117. if (table[pos] == null) {
  118. table[pos] = delegate;
  119. numEntries++;
  120. }
  121. else if (table[pos].getName().equals(delegate.getName())) {
  122. previous = table[pos]; // overwrite it and put prev as one there originally
  123. table[pos] = delegate;
  124. }
  125. else {
  126. pos = quadraticProbing(pos, i);
  127. i++;
  128. }
  129.  
  130. }
  131. System.out.println(delegate.getName() + " newer array location (buckets visited) is " + pos);
  132. System.out.println("The load factor after putting " + delegate.getName() + " is " + updateLoadFactor());
  133. return null;
  134. }
  135.  
  136. /**
  137. * Prints the names and IDs of all the delegates in the database in
  138. * alphabetic order.
  139. * @pre true
  140. */
  141. @Override
  142. public void displayDB(){
  143. String[] tArray = new String[tableSize];
  144. int counter = 0;
  145. String temptwo;
  146. for (Delegate d:table){
  147. if(d != null && d.getName() != null){
  148. tArray[counter] = d.getName();
  149. System.out.println(d.getName());
  150. System.out.println(d.getAffiliation());
  151. counter++;
  152. }
  153.  
  154. for (int i = 0; i < tArray.length; i++){
  155.  
  156. for (int j = i + 1; j < tArray.length; j++){
  157. assert tArray[i] != null;
  158. assert tArray[j] != null;
  159. if (tArray[j] != null && tArray[i] != null && tArray[i].compareTo(tArray[j])>0){
  160. temptwo = tArray[i];
  161. tArray[i] = tArray[j];
  162. tArray[j] = temptwo;
  163. }
  164. }
  165. }
  166.  
  167. for (String names : tArray){
  168. if (names != null){
  169. Delegate temp = get(names);
  170. System.out.println("Name: " + temp.getName() + " Affiliation: " + temp.getAffiliation());
  171. }
  172. }
  173. }
  174. if(counter == 0){
  175. System.out.println("The Table is empty.");
  176. }
  177. }
  178. /**
  179. * Removes and returns a delegate from the database, with the key
  180. * the supplied name.
  181. * @param name The name (key) to remove.
  182. * @pre name not null or empty string
  183. * @return the removed delegate object mapped to the name, or null if
  184. * the name does not exist.
  185. */
  186. @Override
  187. public Delegate remove(String name){
  188. Delegate removeIt = new Delegate();
  189. int pos = hashFunction(name);
  190. int i = 1;
  191. boolean done = false;
  192. pos = quadraticProbing(pos, i);
  193.  
  194. while(!done){
  195. if(table[pos] == null){
  196. done = true;
  197. }
  198. else if (table[pos].getName().equals(name)){
  199. Delegate removedDel = new Delegate();
  200. removedDel = table[pos];
  201. table[pos] = removeIt;
  202. numEntries--;
  203. System.out.println(removedDel + " is removed, the load factor now is: " + updateLoadFactor());
  204. done = true;
  205. return removedDel;
  206. }
  207. else{
  208. pos = quadraticProbing(pos, i);
  209. i++;
  210. }
  211. }
  212. return null;
  213. }
  214.  
  215. public int hashFunction(String name){
  216. int index = 0;
  217. int newIndex = 0;
  218. while(index != name.length()){
  219. //System.out.println((int)name.charAt(index)); // UNCOMMENT THIS
  220. newIndex += name.charAt(index);
  221. index++;
  222. }
  223. return newIndex;
  224. }
  225.  
  226. public int quadraticProbing(int newIndex, int i) {
  227. return (newIndex + (i*i)) % tableSize; // SAY THIS IS OVERFLOW USING MOD
  228. }
  229.  
  230. public double updateLoadFactor(){
  231. double entry = numEntries;
  232. double theSize = tableSize;
  233. loadFactor = entry / theSize;
  234. if (loadFactor >= 0.5){
  235. resizeArray();
  236. }
  237. loadFactor = entry / theSize;
  238. return loadFactor;
  239. }
  240.  
  241. public void resizeArray(){
  242. Delegate[] temp = new Delegate[tableSize];
  243. int count = 0;
  244. int previous;
  245. previous = tableSize;
  246. System.out.println("The table before resizing is " + tableSize);
  247.  
  248. for (Delegate d:table){
  249. if(d != null && d.getName() != null){
  250. temp[count] = d;
  251. count++;
  252. }
  253. }
  254. tableSize = tableSize *2;
  255. clearDB();
  256. System.out.println("The table size after resizing is " + tableSize);
  257. for(Delegate d: temp){
  258. if(d != null){
  259. put(d);
  260. }
  261. }
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement