From 0cd60e4d83872501ed6d56a4edcfd03ddbd5d3d0 Mon Sep 17 00:00:00 2001 From: Sergey Ignatov Date: Fri, 26 Jul 2019 23:40:49 -0700 Subject: [PATCH] deprecate our own hash set GitOrigin-RevId: db8da97daf3049767d8ac0fadc58ef4fc55e4a2a --- .../util/containers/hash/HashSet.java | 231 ++---------------- 1 file changed, 19 insertions(+), 212 deletions(-) diff --git a/platform/util/src/com/intellij/util/containers/hash/HashSet.java b/platform/util/src/com/intellij/util/containers/hash/HashSet.java index 16000f08da3a..44fc135faa0b 100644 --- a/platform/util/src/com/intellij/util/containers/hash/HashSet.java +++ b/platform/util/src/com/intellij/util/containers/hash/HashSet.java @@ -15,394 +15,201 @@ */ package com.intellij.util.containers.hash; - - +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import java.util.AbstractSet; - import java.util.Iterator; - import java.util.Set; - - +/** + * @deprecated Use {@link java.util.HashSet} + */ +@Deprecated +@ApiStatus.ScheduledForRemoval(inVersion = "2020.1") public class HashSet extends AbstractSet implements Set { - - - private Entry[] table; - private int capacity; - private int size; - private final float loadFactor; - - public HashSet() { - this(0); - } - - public HashSet(int capacity) { - this(capacity, HashUtil.DEFAULT_LOAD_FACTOR); - } - - public HashSet(int capacity, float loadFactor) { - this.loadFactor = loadFactor; - clear(capacity); - } - - @Override public boolean contains(Object key) { - final Entry[] table = this.table; - final int hash = HashUtil.hash(key); - final int index = hash % table.length; - - - for (Entry e = table[index]; e != null; e = e.hashNext) { - final E entryKey; - if (e.keyHash == hash && ((entryKey = e.key) == key || entryKey.equals(key))) { - return true; - } - } - - - return false; - } - - @Override public boolean add(E key) { - final Entry[] table = this.table; - final int hash = HashUtil.hash(key); - final int index = hash % table.length; - - - for (Entry e = table[index]; e != null; e = e.hashNext) { - final E entryKey; - if (e.keyHash == hash && ((entryKey = e.key) == key || entryKey.equals(key))) { - return false; - } - } - - - final Entry e = new Entry<>(key); - e.hashNext = table[index]; - table[index] = e; - size++; - - - if (size > capacity) { - - rehash((int) (capacity * HashUtil.CAPACITY_MULTIPLE)); - + rehash((int)(capacity * HashUtil.CAPACITY_MULTIPLE)); } - return true; - } - - @Override public boolean remove(Object key) { - final Entry[] table = this.table; - final int hash = HashUtil.hash(key); - final int index = hash % table.length; - Entry e = table[index]; - - - if (e == null) return false; - - - E entryKey; - if (e.keyHash == hash && ((entryKey = e.key) == key || entryKey.equals(key))) { - table[index] = e.hashNext; - - } else { - - for (; ;) { - - final Entry last = e; - - e = e.hashNext; - - if (e == null) return false; - - if (e.keyHash == hash && ((entryKey = e.key) == key || entryKey.equals(key))) { - - last.hashNext = e.hashNext; - - break; - - } - - } - } - + else { + for (; ; ) { + final Entry last = e; + e = e.hashNext; + if (e == null) return false; + if (e.keyHash == hash && ((entryKey = e.key) == key || entryKey.equals(key))) { + last.hashNext = e.hashNext; + break; + } + } + } size--; - return true; - } - - @NotNull @Override public Iterator iterator() { - return new HashSetIterator() { - @Override public E next() { - return nextEntry().key; - } - }; - } - - @Override public int size() { - return size; - } - - @Override public boolean isEmpty() { - return size() == 0; - } - - private void init(int capacity) { - - table = new Entry[HashUtil.adjustTableSize((int) (capacity / loadFactor))]; - + table = new Entry[HashUtil.adjustTableSize((int)(capacity / loadFactor))]; this.capacity = capacity; - } - - private void clear(int capacity) { - if (capacity < HashUtil.MIN_CAPACITY) { - capacity = HashUtil.MIN_CAPACITY; - } - init(capacity); - size = 0; - } - - private void rehash(int capacity) { - final Iterator> entries = new HashSetIterator>() { - @Override public Entry next() { - return nextEntry(); - } - }; - init(capacity); - final Entry[] table = this.table; - final int tableLen = table.length; - while (entries.hasNext()) { - final Entry e = entries.next(); - final int hash = e.keyHash % tableLen; - e.hashNext = table[hash]; - table[hash] = e; - } - } - - private static class Entry { - - - private final E key; - private final int keyHash; - private Entry hashNext; - - Entry(final E key) { - this.key = key; - keyHash = HashUtil.hash(key); - } - } - - private abstract class HashSetIterator implements Iterator { - - - private final Entry[] table = HashSet.this.table; - private int index = 0; - private Entry e = null; - private Entry last; - - HashSetIterator() { - initNextEntry(); - } - - @Override public boolean hasNext() { - return e != null; - } - - @Override public void remove() { - if (last == null) { - throw new IllegalStateException(); - } - HashSet.this.remove(last.key); - last = null; - } - - protected Entry nextEntry() { - final Entry result = last = e; - initNextEntry(); - return result; - } - - private void initNextEntry() { - Entry result = e; - if (result != null) { - result = result.hashNext; - } - final Entry[] table = this.table; - while (result == null && index < table.length) { - result = table[index++]; - } - e = result; - } - } - } -