public static BitSet toBitSet(IntBag bag, BitSet out) { int[] data = bag.getData(); for (int i = 0, s = bag.size(); s > i; i++) { out.set(data[i]); } return out; }
/** Ensures that the bag contains the default value by default */ @Test public void testDefault() { IntBag bag = new IntBag(); assertEquals(0, bag.get(0)); bag.set(0, 1); assertEquals(0, bag.get(1)); assertEquals(0, bag.get(2)); assertEquals(0, bag.get(3)); }
public static IntBag toIntBag(BitSet bs, IntBag out) { if (bs.isEmpty()) { out.setSize(0); return out; } int size = bs.cardinality(); out.setSize(size); out.ensureCapacity(size); int[] activesArray = out.getData(); for (int i = 0, id = -1, s = size; s > i; i++) { id = bs.nextSetBit(id + 1); activesArray[i] = id; } return out; }
/** When a negative index is used, an {@link ArrayIndexOutOfBoundsException} should be thrown. */ @Test public void testArrayIndexOutOfBoundsException() { IntBag bag = new IntBag(); for (int i = 0; i < 32; i++) { try { bag.set(-(1 << i), 0); } catch (ArrayIndexOutOfBoundsException ex) { continue; } fail("ArrayIndexOutOfBoundsException expected for index " + (-(1 << i))); } for (int i = 0; i < 32; i++) { try { bag.get(-(1 << i)); } catch (ArrayIndexOutOfBoundsException ex) { continue; } fail("ArrayIndexOutOfBoundsException expected for index " + (-(1 << i))); } }
/** * Gets all neighbors of a location that satisfy abs(x-X) + abs(y-Y) + abs(z-Z) <= dist. This * region forms an <a href="http://images.google.com/images?q=octahedron">octohedron</a> 2*dist+1 * cells from point to opposite point inclusive, centered at (X,Y,Y). If dist==1 this is * equivalent to the six neighbors above, below, left, and right, front, and behind (X,Y,Z)), plus * (X,Y,Z) itself. Places each x, y, and z value of these locations in the provided IntBags xPos, * yPos, and zPos, clearing the bags first. Then places into the result IntBag the elements at * each of those <x,y,z> locations clearning it first. Returns the result IntBag (constructing one * if null had been passed in). null may be passed in for the various bags, though it is more * efficient to pass in a 'scratch bag' for each one. */ public final void getNeighborsHamiltonianDistance( final int x, final int y, final int z, final int dist, final boolean toroidal, IntBag result, IntBag xPos, IntBag yPos, IntBag zPos) { if (xPos == null) xPos = new IntBag(); if (yPos == null) yPos = new IntBag(); if (zPos == null) zPos = new IntBag(); getNeighborsHamiltonianDistance(x, y, z, dist, toroidal, xPos, yPos, zPos); if (result != null) { result.clear(); result.resize(xPos.size()); } else result = new IntBag(xPos.size()); for (int i = 0; i < xPos.numObjs; i++) result.add(field[xPos.objs[i]][yPos.objs[i]][zPos.objs[i]]); }
public void getNeighborsHamiltonianDistance( final int x, final int y, final int z, final int dist, final boolean toroidal, IntBag xPos, IntBag yPos, IntBag zPos) { // won't work for negative distances if (dist < 0) { throw new RuntimeException( "Runtime exception in method getNeighborsHamiltonianDistance: Distance must be positive"); } if (xPos == null || yPos == null || zPos == null) { throw new RuntimeException( "Runtime exception in method getNeighborsHamiltonianDistance: xPos and yPos should not be null"); } xPos.clear(); yPos.clear(); zPos.clear(); // local variables are faster final int height = this.height; final int width = this.width; final int length = this.length; // for toroidal environments the code will be different because of wrapping arround if (toroidal) { // compute xmin and xmax for the neighborhood final int xmax = x + dist; final int xmin = x - dist; for (int x0 = xmin; x0 <= xmax; x0++) { final int x_0 = stx(x0, width); // compute ymin and ymax for the neighborhood; they depend on the curreny x0 value final int ymax = y + (dist - ((x0 - x >= 0) ? x0 - x : x - x0)); final int ymin = y - (dist - ((x0 - x >= 0) ? x0 - x : x - x0)); for (int y0 = ymin; y0 <= ymax; y0++) { final int y_0 = sty(y0, height); final int zmax = z + (dist - ((x0 - x >= 0) ? x0 - x : x - x0) - ((y0 - y >= 0) ? y0 - y : y - y0)); final int zmin = z - (dist - ((x0 - x >= 0) ? x0 - x : x - x0) - ((y0 - y >= 0) ? y0 - y : y - y0)); for (int z0 = zmin; z0 <= zmax; z0++) { final int z_0 = stz(z0, length); if (x_0 != x || y_0 != y || z_0 != z) { xPos.add(x_0); yPos.add(y_0); zPos.add(z_0); } } } } } else // not toroidal { // compute xmin and xmax for the neighborhood such that they are within boundaries final int xmax = ((x + dist <= width - 1) ? x + dist : width - 1); final int xmin = ((x - dist >= 0) ? x - dist : 0); for (int x0 = xmin; x0 <= xmax; x0++) { final int x_0 = x0; // compute ymin and ymax for the neighborhood such that they are within boundaries // they depend on the curreny x0 value final int ymax = ((y + (dist - ((x0 - x >= 0) ? x0 - x : x - x0)) <= height - 1) ? y + (dist - ((x0 - x >= 0) ? x0 - x : x - x0)) : height - 1); final int ymin = ((y - (dist - ((x0 - x >= 0) ? x0 - x : x - x0)) >= 0) ? y - (dist - ((x0 - x >= 0) ? x0 - x : x - x0)) : 0); for (int y0 = ymin; y0 <= ymax; y0++) { final int y_0 = y0; final int zmin = ((z - (dist - ((x0 - x >= 0) ? x0 - x : x - x0) - ((y0 - y >= 0) ? y0 - y : y - y0)) >= 0) ? z - (dist - ((x0 - x >= 0) ? x0 - x : x - x0) - ((y0 - y >= 0) ? y0 - y : y - y0)) : 0); final int zmax = ((z + (dist - ((x0 - x >= 0) ? x0 - x : x - x0) - ((y0 - y >= 0) ? y0 - y : y - y0)) <= length - 1) ? z + (dist - ((x0 - x >= 0) ? x0 - x : x - x0) - ((y0 - y >= 0) ? y0 - y : y - y0)) : length - 1); for (int z0 = zmin; z0 <= zmax; z0++) { final int z_0 = z0; if (x_0 != x || y_0 != y || z_0 != z) { xPos.add(x_0); yPos.add(y_0); zPos.add(z_0); } } } } } }
/** Ensures that the elements of a bag are actually stored */ @Test public void testStorage() { IntBag bag = new IntBag(); bag.set(0, 0); assertEquals(0, bag.get(0)); assertEquals(0, bag.get(1)); assertEquals(0, bag.get(2)); assertEquals(0, bag.get(3)); assertEquals(0, bag.get(4)); assertEquals(0, bag.get(5)); assertEquals(0, bag.get(6)); assertEquals(0, bag.get(7)); bag.set(1, 1); assertEquals(0, bag.get(0)); assertEquals(1, bag.get(1)); assertEquals(0, bag.get(2)); assertEquals(0, bag.get(3)); assertEquals(0, bag.get(4)); assertEquals(0, bag.get(5)); assertEquals(0, bag.get(6)); assertEquals(0, bag.get(7)); bag.set(2, 2); assertEquals(0, bag.get(0)); assertEquals(1, bag.get(1)); assertEquals(2, bag.get(2)); assertEquals(0, bag.get(3)); assertEquals(0, bag.get(4)); assertEquals(0, bag.get(5)); assertEquals(0, bag.get(6)); assertEquals(0, bag.get(7)); bag.set(3, 3); assertEquals(0, bag.get(0)); assertEquals(1, bag.get(1)); assertEquals(2, bag.get(2)); assertEquals(3, bag.get(3)); assertEquals(0, bag.get(4)); assertEquals(0, bag.get(5)); assertEquals(0, bag.get(6)); assertEquals(0, bag.get(7)); bag.set(4, 4); assertEquals(0, bag.get(0)); assertEquals(1, bag.get(1)); assertEquals(2, bag.get(2)); assertEquals(3, bag.get(3)); assertEquals(4, bag.get(4)); assertEquals(0, bag.get(5)); assertEquals(0, bag.get(6)); assertEquals(0, bag.get(7)); bag.set(5, 5); assertEquals(0, bag.get(0)); assertEquals(1, bag.get(1)); assertEquals(2, bag.get(2)); assertEquals(3, bag.get(3)); assertEquals(4, bag.get(4)); assertEquals(5, bag.get(5)); assertEquals(0, bag.get(6)); assertEquals(0, bag.get(7)); bag.set(6, 6); assertEquals(0, bag.get(0)); assertEquals(1, bag.get(1)); assertEquals(2, bag.get(2)); assertEquals(3, bag.get(3)); assertEquals(4, bag.get(4)); assertEquals(5, bag.get(5)); assertEquals(6, bag.get(6)); assertEquals(0, bag.get(7)); bag.set(7, 7); assertEquals(0, bag.get(0)); assertEquals(1, bag.get(1)); assertEquals(2, bag.get(2)); assertEquals(3, bag.get(3)); assertEquals(4, bag.get(4)); assertEquals(5, bag.get(5)); assertEquals(6, bag.get(6)); assertEquals(7, bag.get(7)); bag.clear(); assertEquals(0, bag.get(0)); assertEquals(0, bag.get(1)); assertEquals(0, bag.get(2)); assertEquals(0, bag.get(3)); assertEquals(0, bag.get(4)); assertEquals(0, bag.get(5)); assertEquals(0, bag.get(6)); assertEquals(0, bag.get(7)); }
/** * Ensures that the bag resizes correctly when out of capacity, that it does not resize when * queried for non-existing elements, and that it does not resize when the default value is set. */ @Test public void testCapacity() { IntBag bag; bag = new IntBag(); assertEquals(0, bag.getCapacity()); bag.set(0, 1); assertEquals(1, bag.getCapacity()); bag.set(1, 2); assertEquals(2, bag.getCapacity()); bag.set(2, 3); assertEquals(4, bag.getCapacity()); bag.set(3, 4); assertEquals(4, bag.getCapacity()); bag.set(4, 5); assertEquals(8, bag.getCapacity()); bag.set(8, 6); assertEquals(16, bag.getCapacity()); bag.set(35, 7); assertEquals(64, bag.getCapacity()); bag = new IntBag(); for (int i = 0; i < 32; i++) { bag.get((1 << i) - 1); assertEquals(0, bag.getCapacity()); } bag.get(Integer.MAX_VALUE); assertEquals(0, bag.getCapacity()); bag = new IntBag(); for (int i = 0; i < 31; i++) { bag.set((1 << i) - 1, 0); assertEquals(0, bag.getCapacity()); } bag.set(Integer.MAX_VALUE, 0); assertEquals(0, bag.getCapacity()); }