Package com.alexkasko.unsafe.bytearray

Examples of com.alexkasko.unsafe.bytearray.ByteArrayTool


     *
     * @param col collection for index access
     * @param comp comparator
     */
    OffHeapStructComparator(OffHeapStructCollection col, Comparator<OffHeapStructAccessor> comp) {
        ByteArrayTool bt = ByteArrayTool.get();
        this.ia1 = new OffHeapStructIndexAccessor(col);
        this.ia2 = new OffHeapStructIndexAccessor(col);
        this.baa1 = new OffHeapStructByteArrayAccessor(bt);
        this.baa2 = new OffHeapStructByteArrayAccessor(bt);
        this.comp = comp;
View Full Code Here


//  private static final int LENGTH = 1000000;
    private static final int LENGTH = 10000;

    @Test
    public void testLong() {
        ByteArrayTool bt = ByteArrayTool.get();
        byte[] buf = new byte[8];
        OffHeapStructArray oha = null;
        try {
            oha = new OffHeapStructArray(LENGTH, 8);
            Random random = new Random(42);
            long[] arr = new long[LENGTH];
            long val4242 = -1;
            long val4243 = -1;
            for (int i = 0; i < LENGTH; i++) {
                long ra = random.nextInt();
                arr[i] = ra;
                bt.putLong(buf, 0, ra);
                oha.set(i, buf);
                if (4242 == i) val4242 = ra;
                if (4243 == i) val4243 = ra;
            }
//            System.out.println(Arrays.toString(arr));
            Arrays.sort(arr);
//            System.out.println(Arrays.toString(arr));
            Comparator<OffHeapStructAccessor> comp = new LongComp();
//            System.out.println(toLongList(oha));
            OffHeapStructSorter.sort(oha, comp);
//            System.out.println(toLongList(oha));
            OffHeapStructBinarySearchWithComparator searcher = new OffHeapStructBinarySearchWithComparator(oha, comp);
            long ind4242e = Arrays.binarySearch(arr, val4242);
            bt.putLong(buf, 0, val4242);
            long ind4242a = searcher.binarySearch(buf);
            long ind4243e = Arrays.binarySearch(arr, val4243);
            bt.putLong(buf, 0, val4243);
            long ind4243a = searcher.binarySearch(buf);
            assertEquals(ind4242e, ind4242a);
            assertEquals(ind4243e, ind4243a);
        } finally {
            OffHeapUtils.free(oha);
View Full Code Here

    @Test
    public void testArray() {
        OffHeapStructArray arr = null;
        try {
            ByteArrayTool bat = ByteArrayTool.get();
            arr = new OffHeapStructArray(3, 8);
            byte[] buf42 = new byte[8];
            bat.putInt(buf42, 0, 42);
            arr.set(0, buf42);
            byte[] buf43 = new byte[8];
            bat.putInt(buf43, 0, 43);
            arr.set(1, buf43);
            byte[] buf44 = new byte[8];
            bat.putInt(buf44, 0, 44);
            arr.set(2, buf44);

            Iterator<byte[]> iter = arr.iterator();
            assertTrue(iter.hasNext());
            assertArrayEquals("Contents fail", buf42, iter.next());
View Full Code Here

    @Test
    public void testArrayList() {
        OffHeapStructArrayList list = null;
        try {
            ByteArrayTool bat = ByteArrayTool.get();
            list = new OffHeapStructArrayList(8);
            byte[] buf = new byte[8];
            for (int i = 0; i < 42; i++) {
                bat.putLong(buf, 0, i);
                list.add(buf);
            }
            bat.putLong(buf, 0, 1L << 42);
            list.add(buf);

            Iterator<byte[]> iter = list.iterator();
            // next values must be ignored by iterator
            list.add(buf);
            list.add(buf);
            list.add(buf);
            list.get(0, buf);
            assertEquals("Payload fail", 0, bat.getLong(buf, 0));
            list.get(41, buf);
            assertEquals("Payload fail", 41, bat.getLong(buf, 0));
            list.get(42, buf);
            assertEquals("Payload fail", 1L << 42, bat.getLong(buf, 0));
        } finally {
            free(list);
        }
    }
View Full Code Here

//    private static final int LENGTH = 1000000;
    private static final int LENGTH = 10000;

    @Test
    public void testLong() {
        ByteArrayTool bat = ByteArrayTool.get();
        byte[] buf = new byte[8];
        OffHeapStructArray oha = null;
        try {
            oha = new OffHeapStructArray(LENGTH, 8);
            Random random = new Random(42);
            long[] arr = new long[LENGTH];
            long val4242 = -1;
            long val4243 = -1;
            for (int i = 0; i < LENGTH; i++) {
                long ra = random.nextLong();
                arr[i] = ra;
                bat.putLong(buf, 0, ra);
                oha.set(i, buf);
                if (4242 == i) val4242 = ra;
                if (4243 == i) val4243 = ra;
            }
            Arrays.sort(arr);
View Full Code Here

        }
    }

    @Test
    public void testLongRange() {
        ByteArrayTool bat = ByteArrayTool.get();
        byte[] buf = new byte[8];
        OffHeapStructArray oha = null;
        try {
            oha = new OffHeapStructArray(6, 8);
            bat.putLong(buf, 0, 41);
            oha.set(0, buf);
            bat.putLong(buf, 0, 41);
            oha.set(1, buf);
            bat.putLong(buf, 0, 42);
            oha.set(2, buf);
            bat.putLong(buf, 0, 42);
            oha.set(3, buf);
            bat.putLong(buf, 0, 42);
            oha.set(4, buf);
            bat.putLong(buf, 0, 43);
            oha.set(5, buf);
            OffHeapStructBinarySearch.IndexRange range41 = new OffHeapStructBinarySearch.IndexRange();
            OffHeapStructBinarySearch.binarySearchRangeByLongKey(oha, 41, 0, range41);
            assertTrue(range41.isNotEmpty());
            assertEquals("Start fail", 0, range41.getFromIndex());
View Full Code Here

        }
    }

    @Test
    public void testInt() {
        ByteArrayTool bat = ByteArrayTool.get();
        byte[] buf = new byte[8];
        OffHeapStructArray oha = null;
        try {
            oha = new OffHeapStructArray(LENGTH, 8);
            Random random = new Random(42);
            int[] arr = new int[LENGTH];
            int val4242 = -1;
            int val4243 = -1;
            for (int i = 0; i < LENGTH; i++) {
                int ra = random.nextInt();
                arr[i] = ra;
                bat.putLong(buf, 0, ra);
                oha.set(i, buf);
                if (4242 == i) val4242 = ra;
                if (4243 == i) val4243 = ra;
            }
            Arrays.sort(arr);
View Full Code Here

        }
    }

    @Test
    public void testIntRange() {
        ByteArrayTool bat = ByteArrayTool.get();
        byte[] buf = new byte[8];
        OffHeapStructArray oha = null;
        try {
            oha = new OffHeapStructArray(6, 8);
            bat.putLong(buf, 0, 41);
            oha.set(0, buf);
            bat.putLong(buf, 0, 41);
            oha.set(1, buf);
            bat.putLong(buf, 0, 42);
            oha.set(2, buf);
            bat.putLong(buf, 0, 42);
            oha.set(3, buf);
            bat.putLong(buf, 0, 42);
            oha.set(4, buf);
            bat.putLong(buf, 0, 43);
            oha.set(5, buf);
            OffHeapStructBinarySearch.IndexRange range41 = new OffHeapStructBinarySearch.IndexRange();
            OffHeapStructBinarySearch.binarySearchRangeByIntKey(oha, 41, 0, range41);
            assertTrue(range41.isNotEmpty());
            assertEquals("Start fail", 0, range41.getFromIndex());
View Full Code Here

public class OffHeapStructArrayTest {
    @Test
    public void test() {
        OffHeapStructArray arr = null;
        try {
            ByteArrayTool bat = ByteArrayTool.get();
            arr = new OffHeapStructArray(3, 8);
            byte[] buf42 = new byte[8];
            bat.putInt(buf42, 0, 42);
            arr.set(0, buf42);
            byte[] buf43 = new byte[8];
            bat.putInt(buf43, 0, 43);
            arr.set(1, buf43);
            byte[] buf44 = new byte[8];
            bat.putInt(buf44, 0, 44);
            arr.set(2, buf44);

            byte[] buf = new byte[8];
            arr.get(0, buf);
            assertArrayEquals("Contents fail", buf42, buf);
View Full Code Here

    @Test
    public void test() {
        OffHeapStructArrayList list = null;
        try {
            ByteArrayTool bat = ByteArrayTool.get();
            list = new OffHeapStructArrayList(8);
            byte[] buf = new byte[8];
            for (int i = 0; i < 42; i++) {
                bat.putLong(buf, 0, i);
                list.add(buf);
            }
            bat.putLong(buf, 0, 1L << 42);
            list.add(buf);
            assertEquals("Size fail", 43, list.size());
            assertTrue("Capacity fail", list.capacity() >= 43);
            list.get(0, buf);
            assertEquals("Payload fail", 0, bat.getLong(buf, 0));
            list.get(41, buf);
            assertEquals("Payload fail", 41, bat.getLong(buf, 0));
            list.get(42, buf);
            assertEquals("Payload fail", 1L << 42, bat.getLong(buf, 0));
        } finally {
            free(list);
        }
    }
View Full Code Here

TOP

Related Classes of com.alexkasko.unsafe.bytearray.ByteArrayTool

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.