|
| 1 | +package javasabr.rlib.collections.dictionary.impl.gc.optimized; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Deque; |
| 5 | +import javasabr.rlib.collections.deque.DequeFactory; |
| 6 | +import javasabr.rlib.collections.dictionary.IntToRefDictionary; |
| 7 | +import javasabr.rlib.collections.dictionary.impl.AbstractMutableHashBasedIntToRefDictionary; |
| 8 | +import javasabr.rlib.collections.dictionary.impl.ImmutableHashBasedIntToRefDictionary; |
| 9 | +import lombok.AccessLevel; |
| 10 | +import lombok.Getter; |
| 11 | +import lombok.experimental.Accessors; |
| 12 | +import lombok.experimental.FieldDefaults; |
| 13 | +import org.jspecify.annotations.Nullable; |
| 14 | + |
| 15 | +@Getter |
| 16 | +@Accessors(fluent = true) |
| 17 | +@FieldDefaults(level = AccessLevel.PROTECTED) |
| 18 | +public class GcOptimizedMutableHashBasedIntToRefDictionary<V> extends |
| 19 | + AbstractMutableHashBasedIntToRefDictionary<V, ReusableLinkedHashIntToRefEntry<V>> { |
| 20 | + |
| 21 | + private static final int MAX_POOL_SIZE = 40; |
| 22 | + |
| 23 | + final Deque<ReusableLinkedHashIntToRefEntry<V>> entryPool; |
| 24 | + @Nullable ReusableLinkedHashIntToRefEntry<V>[] entries; |
| 25 | + int size; |
| 26 | + int threshold; |
| 27 | + |
| 28 | + public GcOptimizedMutableHashBasedIntToRefDictionary() { |
| 29 | + this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); |
| 30 | + } |
| 31 | + |
| 32 | + public GcOptimizedMutableHashBasedIntToRefDictionary(int initCapacity, float loadFactor) { |
| 33 | + super(loadFactor); |
| 34 | + //noinspection unchecked |
| 35 | + this.entries = new ReusableLinkedHashIntToRefEntry[initCapacity]; |
| 36 | + this.threshold = (int) (initCapacity * loadFactor); |
| 37 | + this.entryPool = DequeFactory.arrayBasedBased(ReusableLinkedHashIntToRefEntry.class); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean isEmpty() { |
| 42 | + return size < 1; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected int incrementSize() { |
| 47 | + return size++; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected int decrementSize() { |
| 52 | + return size--; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void clear() { |
| 57 | + for (ReusableLinkedHashIntToRefEntry<V> entry : entries()) { |
| 58 | + while (entry != null) { |
| 59 | + ReusableLinkedHashIntToRefEntry<V> next = entry.next(); |
| 60 | + deallocate(entry); |
| 61 | + entry = next; |
| 62 | + } |
| 63 | + } |
| 64 | + Arrays.fill(entries, null); |
| 65 | + size = 0; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void threshold(int threshold) { |
| 70 | + this.threshold = threshold; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void entries(@Nullable ReusableLinkedHashIntToRefEntry<V>[] entries) { |
| 75 | + this.entries = entries; |
| 76 | + } |
| 77 | + |
| 78 | + @Nullable |
| 79 | + @Override |
| 80 | + public V remove(int key) { |
| 81 | + ReusableLinkedHashIntToRefEntry<V> entry = removeEntryForKey(key); |
| 82 | + if (entry == null) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + V value = entry.value(); |
| 86 | + deallocate(entry); |
| 87 | + return value; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected ReusableLinkedHashIntToRefEntry<V> allocate( |
| 92 | + int hash, |
| 93 | + int key, |
| 94 | + @Nullable V value, |
| 95 | + @Nullable ReusableLinkedHashIntToRefEntry<V> next) { |
| 96 | + ReusableLinkedHashIntToRefEntry<V> reused = entryPool.pollLast(); |
| 97 | + if (reused != null) { |
| 98 | + return reused.reinit(next, key, value, hash); |
| 99 | + } |
| 100 | + return new ReusableLinkedHashIntToRefEntry<>(next, key, value, hash); |
| 101 | + } |
| 102 | + |
| 103 | + private void deallocate(ReusableLinkedHashIntToRefEntry<V> entry) { |
| 104 | + if (entryPool.size() < MAX_POOL_SIZE) { |
| 105 | + entryPool.addLast(entry.clear()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Nullable |
| 110 | + @Override |
| 111 | + protected ReusableLinkedHashIntToRefEntry<V>[] allocate(int length) { |
| 112 | + //noinspection unchecked |
| 113 | + return new ReusableLinkedHashIntToRefEntry[length]; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public IntToRefDictionary<V> toReadOnly() { |
| 118 | + @Nullable ReusableLinkedHashIntToRefEntry<V>[] copied = Arrays.copyOf(entries, entries.length); |
| 119 | + return new ImmutableHashBasedIntToRefDictionary<>(copied, size); |
| 120 | + } |
| 121 | +} |
0 commit comments