Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author i17017
- */
- public class Main {
- public static void main(String[] args) {
- FoldingHashInteger<String> h = new FoldingHashInteger(11, 1, 1);
- //h.insert(5, "John Smith");
- //h.insert(16, "Jane Smith");
- h.insert(456987098, "aaa");
- //System.out.println(h.search(5));
- //System.out.println(h.search(7));
- // ModularHashString<Integer> a = new ModularHashString(11, 1, 1);
- }
- }
- abstract class HashTable<K, V> {
- protected Data[] table;
- protected int cap;
- protected double c1, c2;
- private class Data {
- K key;
- V value;
- Data (K key, V value) {
- this.key = key;
- this.value = value;
- }
- }
- public HashTable (int cap, double c1, double c2) {
- this.cap = cap;
- this.table = (Data[]) new HashTable.Data[cap];
- this.c1 = c1;
- this.c2 = c2;
- }
- abstract protected int hashFunction (K key);
- public V search (K key) {
- int idx = this.hashFunction(key);
- if (this.table[idx] != null && this.table[idx].key.equals(key))
- return this.table[idx].value;
- else
- return null;
- }
- public boolean insert (K key, V value) {
- Data obj = new Data(key, value);
- int k0 = this.hashFunction(key);
- int idx;
- return true;
- }
- public V delete (K key) {
- int idx = this.hashFunction(key);
- if (this.table[idx] == null || !this.table[idx].key.equals(key))
- return null;
- else {
- V temp = this.table[idx].value;
- this.table[idx] = null;
- return temp;
- }
- }
- protected int quadraticProbic (int k0, int i) {
- return (int)(k0 + (this.c1 * i) + (this.c2 * i * i)) % this.cap;
- }
- }
- class ModularHashInteger<V> extends HashTable<Integer, V> {
- public ModularHashInteger (int cap, double c1, double c2) {
- super(cap, c1, c2);
- }
- @Override
- protected int hashFunction (Integer key) {
- return key % this.cap;
- }
- }
- class MultiplicativeHashInteger<V> extends HashTable<Integer, V> {
- public MultiplicativeHashInteger (int cap, double c1, double c2) {
- super(cap, c1, c2);
- }
- @Override
- protected int hashFunction (Integer key) {
- double golden = (Math.sqrt(5) - 1) / 2;
- return (int)Math.floor((key * golden) % 1.0) * cap;
- }
- }
- class FoldingHashInteger<V> extends HashTable<Integer, V> {
- public FoldingHashInteger (int cap, double c1, double c2) {
- super(cap, c1, c2);
- }
- @Override
- protected int hashFunction (Integer key) {
- // fold 3
- ArrayList<Integer> list = new ArrayList();
- int idx = 0;
- int it = 0;
- while (key > 0) {
- int aaa = key % 1000;
- if ((it & 1) != 0)
- aaa = reverse(aaa);
- list.add(aaa);
- key /= 1000;
- it++;
- }
- while (true) {
- int sum = 0;
- int count = 0;
- for (int i = 0; i < list.size(); i++) {
- if (list.get(i) == 0)
- count++;
- else {
- sum += list.get(i) % 10;
- System.out.println(sum);
- list.set(i, list.get(i) / 10);
- }
- }
- System.out.println(sum);
- if (count == list.size())
- break;
- else {
- idx = idx * 10 + sum % 10;
- }
- }
- System.out.println(idx);
- return idx;
- }
- private int reverse (int num) {
- int res = 0;
- while (num > 0) {
- res = res * 10 + num % 10;
- num /= 10;
- }
- return res;
- }
- }
- class ModularHashString<V> extends HashTable<String, V> {
- public ModularHashString (int cap, double c1, double c2) {
- super(cap, c1, c2);
- }
- @Override
- protected int hashFunction (String key) {
- int idx = 0;
- for (int i = 0; i < key.length(); i++) {
- int char_val = (int)key.charAt(i);
- int pos = key.length() - i - 1;
- int modRes = this.modPow(256, pos);
- idx += modMultiply(char_val, modRes);
- }
- return idx % this.cap;
- }
- private int modPow (int num, int power) {
- int res = 1;
- for (int i = 0; i < power; i++) {
- res *= num % cap;
- }
- return res % this.cap;
- }
- private int modMultiply (int a, int b) {
- return ((a % this.cap) * (b % this.cap)) % this.cap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement