Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. public class LqHashed
  2. { int N;
  3. int n = 0; // the number of nodes in the structure
  4. int defaultQuotient = 9967; // the default 4k+3 prime
  5. double loadingFactor = 0.75;
  6. Listing deleted; // the dummy node, v2
  7. private Listing[] data; // the primary storage array
  8. public LqHashed(int length)
  9. { int pct = (int)((1.0/loadingFactor-1) *100.0);
  10. N = fourKPlus3(length, pct);
  11. data = new Listing[N];
  12. deleted = new Listing("","","");
  13. for(int i = 0; i<N; i++)
  14. data[i] = null;
  15. }// end of constructor
  16. public boolean insert(Listing newListing)
  17. { boolean noError;
  18. boolean hit = false;
  19. int pass, q, offset, ip;
  20. int pk = stringToInt(newListing.getKey()); // preprocess the key
  21. if( (((double) n)/N) < loadingFactor)// insert the node
  22. { pass = 0;
  23. q = pk / N;
  24. offset = q;
  25. ip = pk % N;
  26. if(q%N == 0)
  27. offset = defaultQuotient;
  28. while(pass < N)
  29. { if(data[ip] == null || data[ip] == deleted)
  30. { hit = true;
  31. break;
  32. }
  33. ip = (ip + offset)%N;
  34. pass = pass +1;
  35. }
  36. if(hit == true) // insert the node
  37. { data[ip]=newListing.deepCopy( );
  38. n++;
  39. return noError = true;
  40. }
  41. else
  42. return noError = false;
  43. }
  44. else //structure full to loading factor
  45. return noError = false;
  46. }// end for the Insert method
  47. public Listing fetch(String targetKey)
  48. { boolean noError;
  49. boolean hit = false;
  50. int pass, q, offset, ip;
  51. int pk = stringToInt(targetKey); // preprocess the key
  52. pass = 0;
  53. q = pk / N;
  54. offset = q ;
  55. ip = pk % N;
  56. if(q%N == 0)
  57. offset = defaultQuotient;
  58. while(pass < N)
  59. { if(data[ip] == null) //node not in structure
  60. break;
  61. if(data[ip].compareTo(targetKey) == 0) //node found
  62. { hit = true;
  63. break;
  64. }
  65. ip = (ip + offset)%N; //collision occurred
  66. pass = pass +1;
  67. }
  68. if(hit == true) //return a deep copy of the node
  69. return data[ip].deepCopy( );
  70. else
  71. return null;
  72. }//end of the Fetch method
  73. public boolean delete(String targetKey)
  74. { boolean noError;
  75. boolean hit = false;
  76. int pass, q, offset, ip;
  77. int pk = stringToInt(targetKey); // preprocess the key
  78. pass = 0;
  79. q = pk / N;
  80. offset = q;
  81. ip = pk % N;
  82. if(q%N == 0)
  83. offset = defaultQuotient;
  84. while(pass < N)
  85. { if(data[ip] == null) //node not in structure
  86. break;
  87. if(data[ip].compareTo(targetKey) == 0) // node found
  88. { hit = true;
  89. break;
  90. }
  91. ip = (ip + offset)%N; //collision occurred
  92. pass = pass +1;
  93. }
  94. if(hit == true) //delete the node
  95. { data[ip] = deleted;
  96. n--;
  97. return noError = true;
  98. }
  99. else
  100. return noError = false;
  101. }//end of the Delete method
  102. public boolean update(String targetKey, Listing newListing)
  103. { if(delete(targetKey) == false)
  104. return false;
  105. else if(insert(newListing) == false)
  106. return false;
  107. return true;
  108. }//end of the Update method
  109. public void showAll()
  110. { for(int i= 0; i<N; i++)
  111. if(data[i] != null && data[i] != deleted)
  112. data[i].toString();
  113. }
  114. public static int fourKPlus3(int n, int pct) // from Figure 5.16
  115. { boolean fkp3 = false;
  116. boolean aPrime = false;
  117. int prime, highDivisor, d;
  118. double pctd = pct;
  119. prime = (int)(n * (1.0 + (pctd/100.0))); // guess the prime pct
  120. percent larger than n
  121. if(prime%2 == 0) //if even make the prime guess odd
  122. prime = prime +1;
  123. while(fkp3 == false) // not a 4k+3 prime
  124. { while(aPrime == false) // not a prime
  125. { highDivisor = (int)(Math.sqrt(prime) + 0.5);
  126. for(d = highDivisor; d > 1; d--)
  127. { if(prime%d == 0)
  128. break;
  129. }
  130. if(d != 1) // prime not found
  131. prime = prime +2;
  132. else
  133. aPrime = true;
  134. }// end of the prime search loop
  135. if((prime-3)%4 == 0)
  136. fkp3 = true;
  137. else
  138. { prime = prime +2;
  139. aPrime = false;
  140. }
  141. }// end of 4k+3 prime search loop
  142. return prime;
  143. }
  144. public static int stringToInt(String aKey) // from Figure 5.18
  145. { int pseudoKey = 0;
  146. int n = 1;
  147. int cn= 0;
  148. char c[] = aKey.toCharArray();
  149. int grouping =0;
  150. while (cn < aKey.length()) // there is still more character in the
  151. key
  152. { grouping = grouping << 8; // pack next four characters in i
  153. grouping = grouping + c[cn];
  154. cn = cn + 1;
  155. if (n==4 || cn == aKey.length()) // 4 characters are processed
  156. or no more characters
  157. { pseudoKey = pseudoKey + grouping;
  158. n = 0;
  159. grouping = 0;
  160. }
  161. n = n + 1;
  162. }
  163. return Math.abs(pseudoKey);
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement