Skip to content

Commit

Permalink
BREAKING: LinkedHash(Map/Set) -> Ordered(Map/Set)
Browse files Browse the repository at this point in the history
This can be fixed in your code, probably, by doing a search/replace for
"LinkedHash" and replacing with "Ordered", then importing
`squidpony.squidmath.OrderedMap`, `squidpony.squidmath.OrderedSet`, or
`squidpony.squidmath.*`, and removing any appearances of
`java.util.OrderedMap` or `java.util.OrderedSet` that mistakenly
appeared as a result of the replace. You may also need to change calls
to `Maker.makeLHM()` to `Maker.makeOM()`, but both methods are
available. There aren't many places where a LinkedHashMap was returned
as a specific return type of a SquidLib method, but Placement,
RegionMap, ModularMapGenerator, MonsterGen, some of CoordPacker, and
virtually all of the AOE-related classes (including AreaUtils,
Technique, and the related parts of DijkstraMap) now return
`OrderedMap`s instead of `LinkedHashMap`s. SoundMap mistakenly returned
`HashMap`s when the ordering did matter, so it now returns
`OrderedMap`s.

This isn't a strictly necessary change to make in the library, but the
memory consumption of OrderedSet is better than LinkedHashSet (because
it doesn't store wasteful references to unused values in an internal
LinkedHashMap), and both Ordered collections allow better
random-access-in-some-order traversal of their data. We also gain the
ability to change the ordering, possibly in constant time, and the
shuffle() method on both collections does this already (though shuffling
takes more time on larger collections, and so is not constant-time). An
early case where a "better way" is made possible by these data
structures is the removal of `convertThreats()` in the display module's
CoveredPathDemo, where it was creating a LinkedHashMap with sequential
Integer keys to correspond to the already-unique Threat values in a
List. This is represented better with an OrderedSet since the sequential
ints associated with Threat values are already present in an OrderedSet
for indexing; this is also not possible with a LinkedHashSet. OrderedMap
and OrderedSet are likely to get continued improvements over time. If
this commit breaks more than it should, please post an issue on GitHub.
  • Loading branch information
tommyettinger committed Jun 20, 2016
1 parent ff209d1 commit 22c770b
Show file tree
Hide file tree
Showing 51 changed files with 913 additions and 939 deletions.
Expand Up @@ -51,7 +51,7 @@

public class DijkstraBenchmark {

public static final int DIMENSION = 60, PATH_LENGTH = (DIMENSION - 2) * (DIMENSION - 2);
public static final int DIMENSION = 100, PATH_LENGTH = (DIMENSION - 2) * (DIMENSION - 2);
public static DungeonGenerator dungeonGen =
new DungeonGenerator(DIMENSION, DIMENSION, new StatefulRNG(0x1337BEEFDEAL));
public static SerpentMapGenerator serpent = new SerpentMapGenerator(DIMENSION, DIMENSION,
Expand Down

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions squidlib-util/src/main/java/squidpony/FakeLanguageGen.java
Expand Up @@ -1670,12 +1670,12 @@ public String word(RNG rng, boolean capitalize) {
StringBuilder sb = new StringBuilder(20);
double syllableChance = rng.nextDouble(totalSyllableFrequency);
int syllables = 1, i = 0;
for (Map.Entry<Integer, Double> kv : syllableFrequencies.entrySet()) {
if (syllableChance < kv.getValue()) {
syllables = kv.getKey();
for (IntDoubleOrderedMap.MapEntry kv : syllableFrequencies.mapEntrySet()) {
if (syllableChance < kv.getDoubleValue()) {
syllables = kv.getIntKey();
break;
} else
syllableChance -= kv.getValue();
syllableChance -= kv.getDoubleValue();
}
if (rng.nextDouble() < vowelStartFrequency) {
sb.append(rng.getRandomElement(openingVowels));
Expand Down
2 changes: 1 addition & 1 deletion squidlib-util/src/main/java/squidpony/LanguageCipher.java
Expand Up @@ -46,7 +46,7 @@ public class LanguageCipher implements Serializable{
*/
public FakeLanguageGen language;
private StatefulRNG rng;
// not a LinkedHashMap because this should never be need a random element to be requested
// not an OrderedMap because this should never be need a random element to be requested
/**
* The mapping of lower-case word keys to lower-case word values, where keys are in the source language and values
* are generated by language.
Expand Down
42 changes: 20 additions & 22 deletions squidlib-util/src/main/java/squidpony/MonsterGen.java
@@ -1,8 +1,6 @@
package squidpony;

import squidpony.squidmath.CrossHash;
import squidpony.squidmath.RNG;
import squidpony.squidmath.StatefulRNG;
import squidpony.squidmath.*;

import java.util.*;

Expand Down Expand Up @@ -40,8 +38,8 @@ public class MonsterGen {
*/
public static class Chimera
{
public LinkedHashMap<String, List<String>> parts;
public LinkedHashSet<String> unsaidAdjectives, wholeAdjectives, powerAdjectives, powerPhrases;
public OrderedMap<String, List<String>> parts;
public OrderedSet<String> unsaidAdjectives, wholeAdjectives, powerAdjectives, powerPhrases;
public String name, mainForm, unknown;

/**
Expand All @@ -57,13 +55,13 @@ public Chimera(String name, Chimera other)
mainForm = unknown;
else
mainForm = other.name;
parts = new LinkedHashMap<>(other.parts);
parts = new OrderedMap<>(other.parts);
List<String> oldParts = new ArrayList<>(parts.remove(mainForm));
parts.put(name, oldParts);
unsaidAdjectives = new LinkedHashSet<>(other.unsaidAdjectives);
wholeAdjectives = new LinkedHashSet<>(other.wholeAdjectives);
powerAdjectives = new LinkedHashSet<>(other.powerAdjectives);
powerPhrases = new LinkedHashSet<>(other.powerPhrases);
unsaidAdjectives = new OrderedSet<>(other.unsaidAdjectives);
wholeAdjectives = new OrderedSet<>(other.wholeAdjectives);
powerAdjectives = new OrderedSet<>(other.powerAdjectives);
powerPhrases = new OrderedSet<>(other.powerPhrases);
}

/**
Expand Down Expand Up @@ -100,11 +98,11 @@ public Chimera(String name, String unknown, String... terms)
mainForm = unknown;
else
mainForm = name;
parts = new LinkedHashMap<>();
unsaidAdjectives = new LinkedHashSet<>();
wholeAdjectives = new LinkedHashSet<>();
powerAdjectives = new LinkedHashSet<>();
powerPhrases = new LinkedHashSet<>();
parts = new OrderedMap<>();
unsaidAdjectives = new OrderedSet<>();
wholeAdjectives = new OrderedSet<>();
powerAdjectives = new OrderedSet<>();
powerPhrases = new OrderedSet<>();
ArrayList<String> selfParts = new ArrayList<>();
int t = 0;
for (; t < terms.length; t++) {
Expand Down Expand Up @@ -183,11 +181,11 @@ public Chimera(String name, String unknown, Collection<String> parts, Collection
mainForm = unknown;
else
mainForm = name;
this.parts = new LinkedHashMap<String, List<String>>();
unsaidAdjectives = new LinkedHashSet<String>(unsaid);
wholeAdjectives = new LinkedHashSet<String>(whole);
powerAdjectives = new LinkedHashSet<String>(powerAdj);
powerPhrases = new LinkedHashSet<String>(powerPhr);
this.parts = new OrderedMap<String, List<String>>();
unsaidAdjectives = new OrderedSet<String>(unsaid);
wholeAdjectives = new OrderedSet<String>(whole);
powerAdjectives = new OrderedSet<String>(powerAdj);
powerPhrases = new OrderedSet<String>(powerPhr);
ArrayList<String> selfParts = new ArrayList<String>(parts);
this.parts.put(name, selfParts);
}
Expand All @@ -205,7 +203,7 @@ public String present(boolean capitalize)
else
sb.append('a');
int i = 0;
LinkedHashSet<String> allAdjectives = new LinkedHashSet<>(wholeAdjectives);
OrderedSet<String> allAdjectives = new OrderedSet<>(wholeAdjectives);
if(unknown != null)
allAdjectives.addAll(unsaidAdjectives);
allAdjectives.addAll(powerAdjectives);
Expand Down Expand Up @@ -288,7 +286,7 @@ public String presentVisible(boolean capitalize)
sb.append('a');
int i = 0;

LinkedHashSet<String> allAdjectives = new LinkedHashSet<>(wholeAdjectives);
OrderedSet<String> allAdjectives = new OrderedSet<>(wholeAdjectives);
if(unknown != null)
allAdjectives.addAll(unsaidAdjectives);
for(String adj : allAdjectives)
Expand Down
18 changes: 9 additions & 9 deletions squidlib-util/src/main/java/squidpony/squidai/AOE.java
Expand Up @@ -4,9 +4,9 @@
import squidpony.squidgrid.FOVCache;
import squidpony.squidgrid.Radius;
import squidpony.squidmath.Coord;
import squidpony.squidmath.OrderedMap;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Set;

/**
Expand Down Expand Up @@ -37,7 +37,7 @@ public interface AOE {
boolean mayContainTarget(Set<Coord> targets);

/**
* Returns a LinkedHashMap of Coord keys and ArrayList of Coord values, where each Coord key is an ideal location to
* Returns a OrderedMap of Coord keys and ArrayList of Coord values, where each Coord key is an ideal location to
* hit as many of the Points in targets as possible without hitting any Points in requiredExclusions, and each value
* is the collection of targets that will be hit if the associated key is used. The length of any ArrayList in the
* returned collection's values will be the number of targets likely to be affected by the AOE when shift() is
Expand All @@ -54,9 +54,9 @@ public interface AOE {
* shift(Coord) with the location of some enemy (probably the closest) as its argument.
* @param targets a Set of Points that are desirable targets to include in this AOE
* @param requiredExclusions a Set of Points that this tries strongly to avoid including in this AOE
* @return a LinkedHashMap of Coord keys and ArrayList of Coord values where keys are ideal locations and values are the target points that will be hit when that key is used.
* @return a OrderedMap of Coord keys and ArrayList of Coord values where keys are ideal locations and values are the target points that will be hit when that key is used.
*/
LinkedHashMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> targets, Set<Coord> requiredExclusions);
OrderedMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> targets, Set<Coord> requiredExclusions);

/**
* A variant of idealLocations that takes two groups of desirable targets, and will rate locations by how many
Expand All @@ -69,9 +69,9 @@ public interface AOE {
* @param priorityTargets A Set of Points that are the most-wanted targets to include in this AOE
* @param lesserTargets A Set of Points that are the less-wanted targets to include in this AOE, should not overlap with priorityTargets
* @param requiredExclusions a Set of Points that this tries strongly to avoid including in this AOE
* @return a LinkedHashMap of Coord keys and ArrayList of Coord values where keys are ideal locations and values are the target points that will be hit when that key is used.
* @return a OrderedMap of Coord keys and ArrayList of Coord values where keys are ideal locations and values are the target points that will be hit when that key is used.
*/
LinkedHashMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> priorityTargets, Set<Coord> lesserTargets, Set<Coord> requiredExclusions);
OrderedMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> priorityTargets, Set<Coord> lesserTargets, Set<Coord> requiredExclusions);

/**
* This must be called before any other methods, and takes a char[][] with '#' for walls, anything else for floors.
Expand All @@ -86,12 +86,12 @@ public interface AOE {
* the map. The map must be bounded by walls, which DungeonGenerator does automatically and other generators can
* easily add with two loops.
*
* This returns a HashMap of Coord keys to Double values; if a cell is 100% affected by the AOE then the value
* This returns an OrderedMap of Coord keys to Double values; if a cell is 100% affected by the AOE then the value
* should be 1.0; if it is 50% affected it should be 0.5, if unaffected should be 0.0, etc. The Coord keys should
* have the same x and y as the x,y map positions they correspond to.
* @return a HashMap of Coord keys to Double values from 1.0 (fully affected) to 0.0 (unaffected).
* @return an OrderedMap of Coord keys to Double values from 1.0 (fully affected) to 0.0 (unaffected).
*/
LinkedHashMap<Coord, Double> findArea();
OrderedMap<Coord, Double> findArea();

/**
* Get the position from which the AOE originates, which may be related to the location of the AOE's effect, as for
Expand Down
18 changes: 9 additions & 9 deletions squidlib-util/src/main/java/squidpony/squidai/AreaUtils.java
@@ -1,8 +1,8 @@
package squidpony.squidai;

import squidpony.squidmath.Coord;
import squidpony.squidmath.OrderedMap;

import java.util.LinkedHashMap;

/**
* Static utilities for use in AOE and anything else that might need HashMaps of Coord keys to Double values.
Expand All @@ -15,9 +15,9 @@ public class AreaUtils {
* @param map width by height, commonly generated by FOV methods
* @return a HashMap of Coord keys to Double values, but the only value used is 1.0
*/
public static LinkedHashMap<Coord, Double> arrayToHashMap(boolean[][] map)
public static OrderedMap<Coord, Double> arrayToHashMap(boolean[][] map)
{
LinkedHashMap<Coord, Double> ret = new LinkedHashMap<>();
OrderedMap<Coord, Double> ret = new OrderedMap<>();
for(int i = 0; i < map.length; i++)
{
for(int j = 0; j < map[i].length; j++)
Expand All @@ -35,9 +35,9 @@ public static LinkedHashMap<Coord, Double> arrayToHashMap(boolean[][] map)
* @param map width by height, commonly generated by FOV methods
* @return a HashMap of Coord keys to Double values, with values all greater than 0.0
*/
public static LinkedHashMap<Coord, Double> arrayToHashMap(double[][] map)
public static OrderedMap<Coord, Double> arrayToHashMap(double[][] map)
{
LinkedHashMap<Coord, Double> ret = new LinkedHashMap<>();
OrderedMap<Coord, Double> ret = new OrderedMap<>();
for(int i = 0; i < map.length; i++)
{
for(int j = 0; j < map[i].length; j++)
Expand All @@ -58,9 +58,9 @@ public static LinkedHashMap<Coord, Double> arrayToHashMap(double[][] map)
* @param cutoff any elements greater than this will be 1.0 in the return, anything else will be ignored
* @return a HashMap of Coord keys to Double values, but the only value used is 1.0
*/
public static LinkedHashMap<Coord, Double> arrayToHashMap(double[][] map, double cutoff)
public static OrderedMap<Coord, Double> arrayToHashMap(double[][] map, double cutoff)
{
LinkedHashMap<Coord, Double> ret = new LinkedHashMap<>();
OrderedMap<Coord, Double> ret = new OrderedMap<>();
for(int i = 0; i < map.length; i++)
{
for(int j = 0; j < map[i].length; j++)
Expand All @@ -78,9 +78,9 @@ public static LinkedHashMap<Coord, Double> arrayToHashMap(double[][] map, double
* @param map a double[][] returned by a DijkstraMap running its scan()
* @return a HashMap of Coord keys to Double values, with values of 1.0 only
*/
public static LinkedHashMap<Coord, Double> dijkstraToHashMap(double[][] map)
public static OrderedMap<Coord, Double> dijkstraToHashMap(double[][] map)
{
LinkedHashMap<Coord, Double> ret = new LinkedHashMap<>();
OrderedMap<Coord, Double> ret = new OrderedMap<>();
for(int i = 0; i < map.length; i++)
{
for(int j = 0; j < map[i].length; j++)
Expand Down
23 changes: 14 additions & 9 deletions squidlib-util/src/main/java/squidpony/squidai/BeamAOE.java
Expand Up @@ -6,8 +6,13 @@
import squidpony.squidgrid.Radius;
import squidpony.squidgrid.mapping.DungeonUtility;
import squidpony.squidmath.Coord;
import squidpony.squidmath.OrderedMap;
import squidpony.squidmath.OrderedSet;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Queue;
import java.util.Set;

import static java.lang.Math.*;

Expand Down Expand Up @@ -302,15 +307,15 @@ public boolean mayContainTarget(Set<Coord> targets) {
}

@Override
public LinkedHashMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> targets, Set<Coord> requiredExclusions) {
public OrderedMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> targets, Set<Coord> requiredExclusions) {
if(targets == null)
return new LinkedHashMap<>();
if(requiredExclusions == null) requiredExclusions = new LinkedHashSet<>();
return new OrderedMap<>();
if(requiredExclusions == null) requiredExclusions = new OrderedSet<>();

//requiredExclusions.remove(origin);

int totalTargets = targets.size();
LinkedHashMap<Coord, ArrayList<Coord>> bestPoints = new LinkedHashMap<>(totalTargets * 8);
OrderedMap<Coord, ArrayList<Coord>> bestPoints = new OrderedMap<>(totalTargets * 8);

if(totalTargets == 0)
return bestPoints;
Expand Down Expand Up @@ -447,14 +452,14 @@ else if(qualityMap[x][y] == bestQuality)
}

@Override
public LinkedHashMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> priorityTargets, Set<Coord> lesserTargets, Set<Coord> requiredExclusions) {
public OrderedMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> priorityTargets, Set<Coord> lesserTargets, Set<Coord> requiredExclusions) {
if(priorityTargets == null)
return idealLocations(lesserTargets, requiredExclusions);
if(requiredExclusions == null) requiredExclusions = new LinkedHashSet<>();
if(requiredExclusions == null) requiredExclusions = new OrderedSet<>();

//requiredExclusions.remove(origin);
int totalTargets = priorityTargets.size() + lesserTargets.size();
LinkedHashMap<Coord, ArrayList<Coord>> bestPoints = new LinkedHashMap<>(totalTargets * 8);
OrderedMap<Coord, ArrayList<Coord>> bestPoints = new OrderedMap<>(totalTargets * 8);

if(totalTargets == 0)
return bestPoints;
Expand Down Expand Up @@ -742,7 +747,7 @@ public void setMap(char[][] map) {
}

@Override
public LinkedHashMap<Coord, Double> findArea() {
public OrderedMap<Coord, Double> findArea() {
double[][] dmap = initDijkstra();
dmap[origin.x][origin.y] = DijkstraMap.DARK;
dijkstra.resetMap();
Expand Down
19 changes: 11 additions & 8 deletions squidlib-util/src/main/java/squidpony/squidai/BlastAOE.java
Expand Up @@ -7,6 +7,9 @@
import squidpony.squidgrid.mapping.DungeonUtility;
import squidpony.squidmath.Coord;

import squidpony.squidmath.OrderedMap;
import squidpony.squidmath.OrderedSet;

import java.util.*;

/**
Expand Down Expand Up @@ -91,14 +94,14 @@ public boolean mayContainTarget(Set<Coord> targets) {
}

@Override
public LinkedHashMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> targets, Set<Coord> requiredExclusions) {
public OrderedMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> targets, Set<Coord> requiredExclusions) {
if(targets == null)
return new LinkedHashMap<>();
if(requiredExclusions == null) requiredExclusions = new LinkedHashSet<>();
return new OrderedMap<>();
if(requiredExclusions == null) requiredExclusions = new OrderedSet<>();

//requiredExclusions.remove(origin);
int totalTargets = targets.size();
LinkedHashMap<Coord, ArrayList<Coord>> bestPoints = new LinkedHashMap<>(totalTargets * 8);
OrderedMap<Coord, ArrayList<Coord>> bestPoints = new OrderedMap<>(totalTargets * 8);

if(totalTargets == 0)
return bestPoints;
Expand Down Expand Up @@ -233,14 +236,14 @@ else if(qualityMap[x][y] == bestQuality) {
}

@Override
public LinkedHashMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> priorityTargets, Set<Coord> lesserTargets, Set<Coord> requiredExclusions) {
public OrderedMap<Coord, ArrayList<Coord>> idealLocations(Set<Coord> priorityTargets, Set<Coord> lesserTargets, Set<Coord> requiredExclusions) {
if(priorityTargets == null)
return idealLocations(lesserTargets, requiredExclusions);
if(requiredExclusions == null) requiredExclusions = new LinkedHashSet<>();
if(requiredExclusions == null) requiredExclusions = new OrderedSet<>();

//requiredExclusions.remove(origin);
int totalTargets = priorityTargets.size() + lesserTargets.size();
LinkedHashMap<Coord, ArrayList<Coord>> bestPoints = new LinkedHashMap<>(totalTargets * 8);
OrderedMap<Coord, ArrayList<Coord>> bestPoints = new OrderedMap<>(totalTargets * 8);

if(totalTargets == 0)
return bestPoints;
Expand Down Expand Up @@ -510,7 +513,7 @@ public void setMap(char[][] map) {
}

@Override
public LinkedHashMap<Coord, Double> findArea() {
public OrderedMap<Coord, Double> findArea() {
return AreaUtils.arrayToHashMap(fov.calculateFOV(map, center.x, center.y, radius, radiusType));
}

Expand Down

0 comments on commit 22c770b

Please sign in to comment.