com.epam.deltix.util.collections.generated.ObjectHashMapBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of timebase-collections Show documentation
Show all versions of timebase-collections Show documentation
Timebase Common utilities and collections
The newest version!
/*
* Copyright 2021 EPAM Systems, Inc
*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership. Licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.epam.deltix.util.collections.generated;
import com.epam.deltix.util.collections.*;
import com.epam.deltix.util.collections.hash.*;
import java.util.*;
/**
* Base class for Object to anything hash maps.
*/
@SuppressWarnings ("unchecked")
public abstract class ObjectHashMapBase extends VLinkHashMapBase {
protected Object [] keys;
public ObjectHashMapBase () {
super ();
}
public ObjectHashMapBase (int cap) {
super (cap);
}
public ObjectHashMapBase (int cap, HashCodeComputer hashCodeComputer) {
super (cap, hashCodeComputer);
}
protected void putKey (int idx, K key) {
keys [idx] = key;
}
@Override
public long getSizeInMemory () {
return (
super.getSizeInMemory () + (SIZE_OF_POINTER + ARRAY_OVERHEAD) +
keys.length * SIZE_OF_POINTER
);
}
protected boolean keyEquals (K a, K b) {
return (Objects.equals (a, b));
}
@Override
public void clear () {
super.clear ();
Arrays.fill (keys, null);
}
@Override
protected void free (int idx) {
super.free (idx);
keys [idx] = null;
}
protected final class KeyIterator implements Iterator {
private int pos = -1;
public KeyIterator () {
move ();
}
private void move () {
do {
pos++;
} while (pos < keys.length && isEmpty (pos));
}
@Override
public boolean hasNext () {
return (pos < keys.length);
}
@Override
public K next () {
K ret = (K) keys [pos];
move ();
return (ret);
}
@Override
public void remove () {
throw new UnsupportedOperationException ();
}
}
public Iterator keyIterator () {
return (new KeyIterator ());
}
@Override
protected void allocTable (int cap) {
super.allocTable (cap);
keys = new Object [cap];
}
protected final int hashIndex (K key) {
return (hashCodeComputer.modHashCode (key, hashIndex.length));
}
public boolean remove (K key) {
int idx = find (key);
if (idx == NULL)
return (false);
free (idx);
return (true);
}
protected int find (K key) {
return (find (hashIndex (key), key));
}
protected int find (int hidx, K key) {
for (int chain = hashIndex [hidx]; chain != NULL; chain = next [chain]) {
assert hashIndex ((K)keys [chain]) == hidx;
if (keyEquals (key, (K)keys [chain]))
return (chain);
}
return (NULL);
}
/**
* Quick test for the presence of the specified key.
*
* @param key The key to search.
*/
public boolean containsKey (K key) {
return (find (key) != NULL);
}
public final boolean isEmpty () {
return (count == 0);
}
protected final class KeyEnumeration implements ElementsEnumeration {
private int pos = -1;
public KeyEnumeration () {
move ();
}
private void move () {
do {
pos++;
} while (pos < keys.length && isEmpty (pos));
}
@Override
public boolean hasMoreElements () {
return (pos < keys.length);
}
@Override
public void reset() {
pos = -1;
move();
}
@Override
public Object nextElement () {
K ret = (K) keys [pos];
move ();
return (ret);
}
}
public ElementsEnumeration keys () {
return (new KeyEnumeration ());
}
public final K [] keysToArray (K [] ret) {
if (ret.length < count)
ret = (K [])
java.lang.reflect.Array.newInstance (
ret.getClass().getComponentType(),
count
);
int retIdx = 0;
for (int ii = 0; ii < keys.length; ii++)
if (isFilled (ii))
ret [retIdx++] = (K) keys [ii];
assert retIdx == count;
if (ret.length > count)
ret [count] = null;
return (ret);
}
static final long serialVersionUID = 1L;
private void writeObject (java.io.ObjectOutputStream out)
throws java.io.IOException
{
out.writeShort (1);
}
private void readObject (java.io.ObjectInputStream in)
throws java.io.IOException, ClassNotFoundException
{
@SuppressWarnings("unused")
short readSerialVersion = in.readShort();
}
}