Explorar el Código

Add a method in RNG and correct a serious bug in Zone::translate

smelc hace 7 años
padre
commit
9bf121146d

+ 2 - 1
squidlib-util/src/main/java/squidpony/squidgrid/zone/Zone.java

@@ -297,12 +297,13 @@ public interface Zone extends Serializable, Iterable<Coord> {
 		/* Convenience implementation, feel free to override. */
 		public Zone translate(int x, int y) {
 			final List<Coord> initial = getAll();
-			final List<Coord> shifted = new ArrayList<Coord>(initial);
 			final int sz = initial.size();
+			final List<Coord> shifted = new ArrayList<Coord>(sz);
 			for (int i = 0; i < sz; i++) {
 				final Coord c = initial.get(i);
 				shifted.add(Coord.get(c.x + x, c.y + y));
 			}
+			assert shifted.size() == sz;
 			return new ListZone(shifted);
 		}
 

+ 12 - 2
squidlib-util/src/main/java/squidpony/squidmath/RNG.java

@@ -556,14 +556,24 @@ public class RNG implements Serializable {
 	 *            can be any non-primitive type.
 	 * @return elements after shuffling it in-place
 	 */
-	public <T> T[] shuffleInPlace(T[] elements) {
+	public <T> void shuffleInPlace(T[] elements) {
 		for (int i = elements.length - 1; i > 0; i--) {
 			int r = nextInt(i + 1);
 			T t = elements[r];
 			elements[r] = elements[i];
 			elements[i] = t;
 		}
-		return elements;
+	}
+
+	public <T> void shuffleInPlace(List<T> elements) {
+		final int sz = elements.size();
+		for (int i = sz - 1; i > 0; i--) {
+			int r = nextInt(i + 1);
+			final T atr = elements.get(r);
+			final T ati = elements.get(i);
+			elements.set(r, ati);
+			elements.set(i, atr);
+		}
 	}
 
 	/**