Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8.  
  9. /**
  10. *
  11. * @author i17017
  12. */
  13. public class Main {
  14. public static void main(String[] args) {
  15. FoldingHashInteger<String> h = new FoldingHashInteger(11, 1, 1);
  16. //h.insert(5, "John Smith");
  17. //h.insert(16, "Jane Smith");
  18. h.insert(456987098, "aaa");
  19. //System.out.println(h.search(5));
  20. //System.out.println(h.search(7));
  21. // ModularHashString<Integer> a = new ModularHashString(11, 1, 1);
  22. }
  23. }
  24.  
  25. abstract class HashTable<K, V> {
  26. protected Data[] table;
  27. protected int cap;
  28. protected double c1, c2;
  29.  
  30. private class Data {
  31. K key;
  32. V value;
  33.  
  34. Data (K key, V value) {
  35. this.key = key;
  36. this.value = value;
  37. }
  38. }
  39.  
  40. public HashTable (int cap, double c1, double c2) {
  41. this.cap = cap;
  42. this.table = (Data[]) new HashTable.Data[cap];
  43. this.c1 = c1;
  44. this.c2 = c2;
  45. }
  46.  
  47. abstract protected int hashFunction (K key);
  48.  
  49. public V search (K key) {
  50. int idx = this.hashFunction(key);
  51. if (this.table[idx] != null && this.table[idx].key.equals(key))
  52. return this.table[idx].value;
  53. else
  54. return null;
  55. }
  56.  
  57. public boolean insert (K key, V value) {
  58. Data obj = new Data(key, value);
  59. int k0 = this.hashFunction(key);
  60. int idx;
  61. return true;
  62. }
  63.  
  64. public V delete (K key) {
  65. int idx = this.hashFunction(key);
  66. if (this.table[idx] == null || !this.table[idx].key.equals(key))
  67. return null;
  68. else {
  69. V temp = this.table[idx].value;
  70. this.table[idx] = null;
  71. return temp;
  72. }
  73. }
  74.  
  75. protected int quadraticProbic (int k0, int i) {
  76. return (int)(k0 + (this.c1 * i) + (this.c2 * i * i)) % this.cap;
  77. }
  78. }
  79.  
  80. class ModularHashInteger<V> extends HashTable<Integer, V> {
  81. public ModularHashInteger (int cap, double c1, double c2) {
  82. super(cap, c1, c2);
  83. }
  84.  
  85. @Override
  86. protected int hashFunction (Integer key) {
  87. return key % this.cap;
  88. }
  89. }
  90.  
  91. class MultiplicativeHashInteger<V> extends HashTable<Integer, V> {
  92. public MultiplicativeHashInteger (int cap, double c1, double c2) {
  93. super(cap, c1, c2);
  94. }
  95.  
  96. @Override
  97. protected int hashFunction (Integer key) {
  98. double golden = (Math.sqrt(5) - 1) / 2;
  99. return (int)Math.floor((key * golden) % 1.0) * cap;
  100. }
  101. }
  102.  
  103. class FoldingHashInteger<V> extends HashTable<Integer, V> {
  104. public FoldingHashInteger (int cap, double c1, double c2) {
  105. super(cap, c1, c2);
  106. }
  107.  
  108. @Override
  109. protected int hashFunction (Integer key) {
  110. // fold 3
  111. ArrayList<Integer> list = new ArrayList();
  112. int idx = 0;
  113. int it = 0;
  114. while (key > 0) {
  115. int aaa = key % 1000;
  116. if ((it & 1) != 0)
  117. aaa = reverse(aaa);
  118.  
  119. list.add(aaa);
  120. key /= 1000;
  121. it++;
  122. }
  123. while (true) {
  124. int sum = 0;
  125. int count = 0;
  126. for (int i = 0; i < list.size(); i++) {
  127. if (list.get(i) == 0)
  128. count++;
  129. else {
  130. sum += list.get(i) % 10;
  131. System.out.println(sum);
  132. list.set(i, list.get(i) / 10);
  133. }
  134. }
  135. System.out.println(sum);
  136. if (count == list.size())
  137. break;
  138. else {
  139. idx = idx * 10 + sum % 10;
  140. }
  141. }
  142. System.out.println(idx);
  143. return idx;
  144. }
  145.  
  146. private int reverse (int num) {
  147. int res = 0;
  148. while (num > 0) {
  149. res = res * 10 + num % 10;
  150. num /= 10;
  151. }
  152. return res;
  153. }
  154. }
  155.  
  156. class ModularHashString<V> extends HashTable<String, V> {
  157. public ModularHashString (int cap, double c1, double c2) {
  158. super(cap, c1, c2);
  159. }
  160.  
  161. @Override
  162. protected int hashFunction (String key) {
  163. int idx = 0;
  164. for (int i = 0; i < key.length(); i++) {
  165. int char_val = (int)key.charAt(i);
  166. int pos = key.length() - i - 1;
  167. int modRes = this.modPow(256, pos);
  168. idx += modMultiply(char_val, modRes);
  169. }
  170. return idx % this.cap;
  171. }
  172.  
  173. private int modPow (int num, int power) {
  174. int res = 1;
  175. for (int i = 0; i < power; i++) {
  176. res *= num % cap;
  177. }
  178. return res % this.cap;
  179. }
  180.  
  181. private int modMultiply (int a, int b) {
  182. return ((a % this.cap) * (b % this.cap)) % this.cap;
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement