static StringDoubleMap test(int n, int range) { // Generate random data Random rand = new Random(); Set<Integer> set = new HashSet<Integer>(); for (int i = 0; i < n; i++) set.add(rand.nextInt(range)); // Make the map StringDoubleMap map = new StringDoubleMap(0); // map.switchToSortedList(); for (int x : set) { map.put("" + x, 1.0 * x); } // System.out.println("here"); check(set, map); map.switchToSortedList(); check(set, map); map.switchToHashTable(); check(set, map); map.lock(); for (int x : set) map.put("" + x, 1.0 * x); assert set.size() == map.size(); return map; }
// Return a map with only keys in the set public StringDoubleMap restrict(Set<String> set) { StringDoubleMap newMap = new StringDoubleMap(); newMap.mapType = mapType; if (mapType == MapType.SORTED_LIST) { newMap.allocate(getCapacity(num, false)); for (int i = 0; i < keys.length; i++) { if (set.contains(keys[i])) { newMap.keys[newMap.num] = keys[i]; newMap.values[newMap.num] = values[i]; newMap.num++; } } } else if (mapType == MapType.HASH_TABLE) { for (int i = 0; i < keys.length; i++) if (keys[i] != null && set.contains(keys[i])) newMap.put(keys[i], values[i]); } newMap.locked = locked; return newMap; }
static void check(Set<Integer> set, StringDoubleMap map) { for (int x : set) { double value = map.getSure("" + x); assert value == 1.0 * x; } for (StringDoubleMap.Entry e : map) { int x = Integer.parseInt(e.getKey()); assert set.contains(x); assert e.getValue() == 1.0 * x; } }