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

tfw.immutable.ila.bitila.BitIlaXor Maven / Gradle / Ivy

Go to download

The FrameWork for building highly scalable and maintainable applications

The newest version!
package tfw.immutable.ila.bitila;

import java.io.IOException;
import tfw.check.Argument;
import tfw.immutable.ila.bitila.BitIlaUtil.BitFunction;

public final class BitIlaXor {
    private static final BitFunction XOR_BIT_FUNCTION = new BitFunction() {
        @Override
        public void partialLong(
                long[] left, long leftOffsetInBits, long[] right, long rightOffsetInBits, long lengthInBits) {
            final long rightPartial = BitIlaUtil.getPartialLong(right, rightOffsetInBits, lengthInBits);
            final long leftPartial = BitIlaUtil.getPartialLong(left, leftOffsetInBits, lengthInBits);

            BitIlaUtil.setPartialLong(left, leftOffsetInBits, rightPartial ^ leftPartial, lengthInBits);
        }

        @Override
        public void forwardAlignedFullLongs(
                long[] left, int leftOffsetInLongs, long[] right, int rightOffsetInLongs, int lengthInLongs) {
            for (int i = rightOffsetInLongs, j = leftOffsetInLongs; i < rightOffsetInLongs + lengthInLongs; i++, j++) {
                left[j] ^= right[i];
            }
        }

        @Override
        public void forwardMisalignedFullLongs(
                long[] left,
                int leftOffsetInLongs,
                long[] right,
                int rightOffsetInLongs,
                int lengthInLongs,
                int rightOffsetMod64) {
            for (int i = rightOffsetInLongs, j = leftOffsetInLongs; i < rightOffsetInLongs + lengthInLongs; i++, j++) {
                left[j] ^= right[i] << rightOffsetMod64 | right[i + 1] >>> (Long.SIZE - rightOffsetMod64);
            }
        }

        @Override
        public void reverseAlignedFullLongs(
                long[] left, int leftOffsetInLongs, long[] right, int rightOffsetInLongs, int lengthInLongs) {
            for (int i = rightOffsetInLongs, j = leftOffsetInLongs; i > rightOffsetInLongs - lengthInLongs; i--, j--) {
                left[j] ^= right[i];
            }
        }

        @Override
        public void reverseMisalignedFullLongs(
                long[] left,
                int leftOffsetInLongs,
                long[] right,
                int rightOffsetInLongs,
                int lengthInLongs,
                int rightOffsetMod64) {
            for (int i = rightOffsetInLongs, j = leftOffsetInLongs; i > rightOffsetInLongs - lengthInLongs; i--, j--) {
                left[j] ^= right[i] << rightOffsetMod64 | right[i + 1] >>> (Long.SIZE - rightOffsetMod64);
            }
        }
    };

    private BitIlaXor() {}

    public static void xor(
            final long[] left,
            final long leftOffsetInBits,
            final long[] right,
            final long rightOffsetInBits,
            final long lengthInBits) {
        BitIlaUtil.performBitFunction(XOR_BIT_FUNCTION, left, leftOffsetInBits, right, rightOffsetInBits, lengthInBits);
    }

    public static BitIla create(final BitIla leftIla, final BitIla rightIla) {
        return new BitIlaImpl(leftIla, rightIla);
    }

    private static class BitIlaImpl extends AbstractBitIla {
        private final BitIla leftIla;
        private final BitIla rightIla;

        public BitIlaImpl(final BitIla leftIla, final BitIla rightIla) {
            Argument.assertNotNull(leftIla, "leftIla");
            Argument.assertNotNull(rightIla, "rightIla");

            this.leftIla = leftIla;
            this.rightIla = rightIla;
        }

        @Override
        protected long lengthInBitsImpl() throws IOException {
            return leftIla.lengthInBits();
        }

        @Override
        protected void getImpl(long[] array, long arrayOffsetInBits, long ilaStartInBits, long lengthInBits)
                throws IOException {
            final long rightOffsetInBits = arrayOffsetInBits % Long.SIZE;
            final long[] rightArray = new long[(int) ((rightOffsetInBits + lengthInBits) / Long.SIZE + 1)];

            leftIla.get(array, arrayOffsetInBits, ilaStartInBits, lengthInBits);
            rightIla.get(rightArray, rightOffsetInBits, ilaStartInBits, lengthInBits);

            xor(array, arrayOffsetInBits, rightArray, rightOffsetInBits, lengthInBits);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy