com.epam.deltix.util.collections.generated.IntegerHashSet 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.hash.*;
import com.epam.deltix.util.collections.*;
import java.io.*;
import java.util.*;
@SuppressWarnings ("unchecked")
public class IntegerHashSet extends IntegerHashMapBase
{
public IntegerHashSet () {
super ();
}
public IntegerHashSet (int cap) {
super (cap);
}
public IntegerHashSet (HashCodeComputer hashCodeComputer) {
super (0, hashCodeComputer);
}
public IntegerHashSet (int cap, HashCodeComputer hashCodeComputer) {
super (cap, hashCodeComputer);
}
public IntegerHashSet (int[] values) {
super ();
for (int i = 0; values != null && i < values.length; i++)
add(values[i]);
}
protected void resizeTable (int newSize) {
final int curLength = keys.length;
final int [] saveKeys = keys;
final int [] savePrev = prev;
allocTable (newSize);
for (int ii = 0; ii < curLength; ii++)
if (savePrev [ii] != NULL)
putNewNoSpaceCheck (saveKeys [ii]);
}
private void putNewNoSpaceCheck (int key) {
int hidx = hashIndex (key);
int idx = find (hidx, key);
if (idx != NULL)
throw new IllegalArgumentException (
"Key " + key + " already exists"
);
idx = allocEntry (hidx);
keys [idx] = key;
}
public final boolean contains (int key) {
return (containsKey (key));
}
public boolean add (int key) {
int hidx = hashIndex (key);
int idx = find (hidx, key);
if (idx != NULL)
return (false);
if (freeHead == NULL) {
resizeTable (keys.length * 2);
hidx = hashIndex (key); // recompute!
}
idx = allocEntry (hidx);
putKey (idx, key);
return (true);
}
@Override
public final boolean equals (Object o) {
throw new UnsupportedOperationException ();
}
@Override
public int hashCode () {
throw new UnsupportedOperationException ();
}
public final int [] toArray (int [] reuse) {
return (keysToArray (reuse));
}
static final long serialVersionUID = 1L;
private void writeObject (ObjectOutputStream out)
throws IOException
{
out.writeShort (1);
out.writeInt (size ());
final int tabSize = keys.length;
int numWritten = 0;
for (int ii = 0; ii < tabSize; ii++) {
if (isFilled (ii)) {
numWritten++;
out.writeInt (keys [ii]);
}
}
if (numWritten != size ())
throw new RuntimeException (
"Size mismatch: " + numWritten + " instead of " + size ()
);
}
private void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException
{
@SuppressWarnings("unused")
short readSerialVersion = in.readShort();
int inCount = in.readInt ();
allocTable (inCount);
for (int ii = 0; ii < inCount; ii++) {
int key = in.readInt ();
add (key);
}
}
}