// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.common.util;

import com.hypixel.hytale.sneakythrow.SneakyThrow;
import com.hypixel.hytale.unsafe.UnsafeUtil;
import javax.annotation.Nonnull;
import java.util.BitSet;

public class BitSetUtil
{
    public static final long WORDS_OFFSET;
    public static final long WORDS_IN_USE_OFFSET;
    
    public static void copyValues(@Nonnull final BitSet from, @Nonnull final BitSet to) {
        if (UnsafeUtil.UNSAFE == null) {
            copyValuesSlow(from, to);
            return;
        }
        final int wordsInUse = UnsafeUtil.UNSAFE.getInt(from, BitSetUtil.WORDS_IN_USE_OFFSET);
        UnsafeUtil.UNSAFE.putInt(to, BitSetUtil.WORDS_IN_USE_OFFSET, wordsInUse);
        final long[] fromWords = (long[])UnsafeUtil.UNSAFE.getObject(from, BitSetUtil.WORDS_OFFSET);
        long[] toWords = (long[])UnsafeUtil.UNSAFE.getObject(to, BitSetUtil.WORDS_OFFSET);
        if (wordsInUse > toWords.length) {
            toWords = new long[wordsInUse];
            UnsafeUtil.UNSAFE.putObject(to, BitSetUtil.WORDS_OFFSET, toWords);
        }
        System.arraycopy(fromWords, 0, toWords, 0, wordsInUse);
    }
    
    public static void copyValuesSlow(@Nonnull final BitSet from, @Nonnull final BitSet to) {
        to.clear();
        to.or(from);
    }
    
    static {
        try {
            if (UnsafeUtil.UNSAFE != null) {
                WORDS_OFFSET = UnsafeUtil.UNSAFE.objectFieldOffset(BitSet.class.getDeclaredField("words"));
                WORDS_IN_USE_OFFSET = UnsafeUtil.UNSAFE.objectFieldOffset(BitSet.class.getDeclaredField("wordsInUse"));
            }
            else {
                WORDS_OFFSET = 0L;
                WORDS_IN_USE_OFFSET = 0L;
            }
        }
        catch (final NoSuchFieldException e) {
            throw SneakyThrow.sneakyThrow(e);
        }
    }
}
