Search in sources :

Example 1 with Entry

use of fr.neatmonster.nocheatplus.utilities.ds.map.CoordMap.Entry in project NoCheatPlus by NoCheatPlus.

the class TestCoordMap method testLinkedCoordHashMap.

@Test
public void testLinkedCoordHashMap() {
    final Random random = new Random(System.nanoTime() - (System.currentTimeMillis() % 2 == 1 ? 37 : 137));
    // Number of coordinates.
    final int n = suggestedSamples;
    // Coordinate maximum.
    final int max = 800;
    // Preparecoordinates.
    int[][] coords = getUniqueRandomCoords(n, max, random);
    LinkedCoordHashMap<Integer> map = new LinkedCoordHashMap<Integer>(1, 0.75f);
    // Use a map with these coordinates.
    fillMap(map, coords);
    // Initial iteration order.
    testIterationOrder(map, coords, 1);
    // Re-put, moving to end.
    for (int i = 0; i < coords.length; i++) {
        map.put(coords[i][0], coords[i][1], coords[i][2], i, MoveOrder.END);
        testLast(map, coords[i], i);
    }
    if (map.size() != coords.length) {
        fail("Map different size than coords.");
    }
    testIterationOrder(map, coords, 1);
    // Re-put, moving to front.
    for (int i = coords.length - 1; i >= 0; i--) {
        map.put(coords[i][0], coords[i][1], coords[i][2], i, MoveOrder.FRONT);
        testFirst(map, coords[i], i);
    }
    if (map.size() != coords.length) {
        fail("Map different size than coords.");
    }
    testIterationOrder(map, coords, 1);
    // Map.clear
    map.clear();
    if (map.size() != 0) {
        fail("Expect map size to be 0 after clear.");
    }
    if (map.iterator(false).hasNext()) {
        fail("Expect no first element on iteration after clear.");
    }
    if (map.iterator(true).hasNext()) {
        fail("Expect no last element on iteration after clear.");
    }
    // New map with all coordinates.
    fillMap(map, coords);
    // Half the coordinates.
    int[][] halfCoords = new int[n / 2][3];
    for (int i = 0; i < n / 2; i++) {
        for (int j = 0; j < 3; j++) {
            halfCoords[i][j] = coords[i * 2][j];
        }
    }
    // Test remove every second entry.
    for (int i = 0; i < n / 2; i++) {
        map.remove(coords[i * 2 + 1][0], coords[i * 2 + 1][1], coords[i * 2 + 1][2]);
        if (map.contains(coords[i * 2 + 1][0], coords[i * 2 + 1][1], coords[i * 2 + 1][2])) {
            fail("Expect removed entries not to be contained in the map.");
        }
    }
    if (map.size() != n / 2) {
        fail("Map size should be halfed after removing every second element (" + map.size() + " instead of " + n / 2 + ").");
    }
    testIterationOrder(map, halfCoords, 2);
    // Test iterator.remove every second entry.
    map.clear();
    fillMap(map, coords);
    int i = 0;
    Iterator<Entry<Integer>> it = map.iterator(false);
    while (it.hasNext()) {
        Entry<Integer> entry = it.next();
        if (i % 2 == 1) {
            it.remove();
            if (map.contains(entry.getX(), entry.getY(), entry.getZ())) {
                fail("Expect entries removed by iterator not to be in the map.");
            }
        }
        i++;
    }
    if (map.size() != n / 2) {
        fail("Map size should be halfed after removing every second element with an iterator (" + map.size() + " instead of " + n / 2 + ").");
    }
    testIterationOrder(map, halfCoords, 2);
// TODO: Some random mixtures.
}
Also used : Entry(fr.neatmonster.nocheatplus.utilities.ds.map.CoordMap.Entry) Random(java.util.Random) LinkedCoordHashMap(fr.neatmonster.nocheatplus.utilities.ds.map.LinkedCoordHashMap) Test(org.junit.Test)

Example 2 with Entry

use of fr.neatmonster.nocheatplus.utilities.ds.map.CoordMap.Entry in project NoCheatPlus by NoCheatPlus.

the class FakeBlockCache method toJava.

/**
 * Return a line of java code to construct a new FakeBlockCache with the
 * same content (no newlines).
 *
 * @param builder
 *            the builder
 * @param fbcName
 *            Variable name of the FakeBlockCache instance.
 * @param boundsPrefix
 *            A prefix for bounds variables for the case of repeated
 *            content. If set to null, no optimization will be performed.
 */
public void toJava(final StringBuilder builder, final String fbcName, final String boundsPrefix) {
    builder.append("FakeBlockCache " + fbcName + " = new FakeBlockCache();");
    final String fullBounds;
    if (boundsPrefix != null) {
        fullBounds = boundsPrefix + "_fb";
        builder.append(" double[] " + fullBounds + " = new double[]{0.0, 0.0, 0.0, 1.0, 1.0, 1.0};");
    } else {
        fullBounds = null;
    }
    // Assume id is always set.
    final Iterator<Entry<Material>> it = idMapStored.iterator();
    while (it.hasNext()) {
        Entry<Material> entry = it.next();
        final int x = entry.getX();
        final int y = entry.getY();
        final int z = entry.getZ();
        final Material id = entry.getValue();
        if (id == Material.AIR) {
            builder.append(fbcName + ".set(" + x + ", " + y + ", " + z + ", " + id + ");");
        } else {
            final Integer data = dataMapStored.get(x, y, z);
            final double[] bounds = boundsMapStored.get(x, y, z);
            if (bounds == null) {
                if (data == null) {
                    // Consider 0 too.
                    builder.append(fbcName + ".set(" + x + ", " + y + ", " + z + ", " + id + ");");
                } else {
                    builder.append(fbcName + ".set(" + x + ", " + y + ", " + z + ", " + id + ", " + data + ");");
                }
            } else if (boundsPrefix != null && MapUtil.isFullBounds(bounds)) {
                builder.append(fbcName + ".set(" + x + ", " + y + ", " + z + ", " + id + ", " + data + ", " + fullBounds + ");");
                ;
            } else {
                builder.append(fbcName + ".set(" + x + ", " + y + ", " + z + ", " + id + ", " + data + ", ");
                DebugUtil.toJava(bounds, builder);
                builder.append(");");
            }
        }
    }
}
Also used : Entry(fr.neatmonster.nocheatplus.utilities.ds.map.CoordMap.Entry) Material(org.bukkit.Material)

Aggregations

Entry (fr.neatmonster.nocheatplus.utilities.ds.map.CoordMap.Entry)2 LinkedCoordHashMap (fr.neatmonster.nocheatplus.utilities.ds.map.LinkedCoordHashMap)1 Random (java.util.Random)1 Material (org.bukkit.Material)1 Test (org.junit.Test)1