All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.epam.deltix.util.collections.generated.DecimalHashSet Maven / Gradle / Ivy

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.*;
import com.epam.deltix.dfp.Decimal64;

@SuppressWarnings ("unchecked")
public class DecimalHashSet extends DecimalHashMapBase
{
    public DecimalHashSet () {
        super ();
    }

    public DecimalHashSet (int cap) {
        super (cap);
    }

    public DecimalHashSet (HashCodeComputer hashCodeComputer) {
        super (0, hashCodeComputer);
    }

    public DecimalHashSet (int cap, HashCodeComputer hashCodeComputer) {
        super (cap, hashCodeComputer);
    }

    public DecimalHashSet (Decimal64[] 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 Decimal64 []            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 (Decimal64 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 (Decimal64 key) {
    	return (containsKey (key));
    }

    public boolean              add (Decimal64 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 Decimal64 []       toArray (Decimal64 [] 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.writeLong (Decimal64.toUnderlying(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++) {
            Decimal64       key = Decimal64.fromUnderlying(in.readLong ());

            add (key);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy