开放 Hash 表 Java 代码


开放 Hash 表 代码

public class HashEntry {
      private int key;
      private int value;
      HashEntry(int key, int value) {
            this.key = key;
            this.value = value;
      }
      public int getValue() {
            return value;
      }
      public void setValue(int value) {
            this.value = value;
      }
      public int getKey() {
            return key;
      }
}
public class DeletedEntry extends HashEntry {
      private static DeletedEntry entry = null;
      private DeletedEntry() {
            super(-1, -1);
      }
      public static DeletedEntry getUniqueDeletedEntry() {
            if (entry == null)
                  entry = new DeletedEntry();
            return entry;
      }
}
public class HashMap {
      private final static int TABLE_SIZE = 128;
      HashEntry[] table;
      HashMap() {
            table = new HashEntry[TABLE_SIZE];

                  table[i] = null;
      }
      public int get(int key) {
            int hash = (key % TABLE_SIZE);
            int initialHash = -1;
            while (hash != initialHash
                        && (table[hash] == DeletedEntry.getUniqueDeletedEntry() || table[hash] != null
                                   && table[hash].getKey() != key)) {
                  if (initialHash == -1)
                        initialHash = hash;
                  hash = (hash + 1) % TABLE_SIZE;
            }
            if (table[hash] == null || hash == initialHash)
                  return -1;
            else
                  return table[hash].getValue();
      }
      public void put(int key, int value) {
            int hash = (key % TABLE_SIZE);
            int initialHash = -1;
            int indexOfDeletedEntry = -1;
            while (hash != initialHash
                        && (table[hash] == DeletedEntry.getUniqueDeletedEntry() || table[hash] != null
                                   && table[hash].getKey() != key)) {
                  if (initialHash == -1)
                        initialHash = hash;
                  if (table[hash] == DeletedEntry.getUniqueDeletedEntry())
                        indexOfDeletedEntry = hash;
                  hash = (hash + 1) % TABLE_SIZE;
            }
            if ((table[hash] == null || hash == initialHash)
                        && indexOfDeletedEntry != -1)
                  table[indexOfDeletedEntry] = new HashEntry(key, value);
            else if (initialHash != hash)
                  if (table[hash] != DeletedEntry.getUniqueDeletedEntry()
                             && table[hash] != null && table[hash].getKey() == key)
                        table[hash].setValue(value);
                  else
                        table[hash] = new HashEntry(key, value);
      }
      public void remove(int key) {
            int hash = (key % TABLE_SIZE);
            int initialHash = -1;
            while (hash != initialHash
                        && (table[hash] == DeletedEntry.getUniqueDeletedEntry() || table[hash] != null
                                   && table[hash].getKey() != key)) {
                  if (initialHash == -1)
                        initialHash = hash;
                  hash = (hash + 1) % TABLE_SIZE;
            }
            if (hash != initialHash && table[hash] != null)
                  table[hash] = DeletedEntry.getUniqueDeletedEntry();
      }
}

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《开放 Hash 表 Java 代码
本文地址:https://www.zhiletu.com/archives-7817.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微