@Override public String getValueForKey(int key) { return keyToValue.get((short) key); }
private void put(short key, String value) { keyToValue.put(key, value); valueToKey.put(value, key); }
@Override public void clear() { values.clear(); keyToValue.clear(); valueToKey.clear(); }
/** * If the specified key is not already associated with a value, associates it * with the given value and returns the {@linkplain #defaultReturnValue() * default return value}, else returns the current value. * * @param key * key with which the specified value is to be associated. * @param value * value to be associated with the specified key. * * @return the previous value associated with the specified key, or the * {@linkplain #defaultReturnValue() default return value} if there was * no mapping for the key. * * @see java.util.Map#putIfAbsent(Object, Object) * @since 8.0.0 */ default V putIfAbsent(final short key, final V value) { final V v = get(key), drv = defaultReturnValue(); if (v != drv || containsKey(key)) return v; put(key, value); return drv; } /**
public boolean addType(final Object label, final int id) { short givenId = intToShort(id); short foundId = labelMap.getShort(label); if (foundId != NULL_SHORT && foundId != givenId) { throw new RuntimeException("This label '" + label + "' is already assigned to a different id"); } else if (idMap.containsKey(givenId)) { if ((label == null && idMap.get(givenId) == null) || idMap.get(givenId).equals(label)) { return false; } else { throw new RuntimeException("This id '" + id + "' is already assigned to a different label"); } } if (garbageQueue.contains(givenId)) { garbageQueue.remove(givenId); labelMap.put(label, givenId); idMap.put(givenId, label); } else { short insertId = intToShort(length++); while (insertId < givenId) { garbageQueue.add(insertId); insertId = intToShort(length++); if (length >= MAX_SIZE) { throw new RuntimeException("Maximum number of edge types reached at " + MAX_SIZE); } } labelMap.put(label, givenId); idMap.put(givenId, label); } return true; }
/** * Replaces the entry for the specified key only if it is currently mapped to * some value. * * @param key * key with which the specified value is associated. * @param value * value to be associated with the specified key. * * @return the previous value associated with the specified key, or the * {@linkplain #defaultReturnValue() default return value} if there was * no mapping for the key. * * @see java.util.Map#replace(Object, Object) * @since 8.0.0 */ default V replace(final short key, final V value) { return containsKey(key) ? put(key, value) : defaultReturnValue(); } /**
/** * Returns the value to which the specified key is mapped, or the * {@code defaultValue} if this map contains no mapping for the key. * * @param key * the key. * @param defaultValue * the default mapping of the key. * * @return the value to which the specified key is mapped, or the * {@code defaultValue} if this map contains no mapping for the key. * * @see java.util.Map#getOrDefault(Object, Object) * @since 8.0.0 */ default V getOrDefault(final short key, final V defaultValue) { final V v; return ((v = get(key)) != defaultReturnValue() || containsKey(key)) ? v : defaultValue; } /**
@SuppressWarnings("unchecked") @Override public boolean contains(final Object o) { if (!(o instanceof Map.Entry)) return false; if (o instanceof Short2ObjectMap.Entry) { final Short2ObjectMap.Entry<V> e = (Short2ObjectMap.Entry<V>) o; final short k = e.getShortKey(); return map.containsKey(k) && java.util.Objects.equals(map.get(k), e.getValue()); } final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; final Object key = e.getKey(); if (key == null || !(key instanceof Short)) return false; final short k = ((Short) (key)).shortValue(); final Object value = e.getValue(); return map.containsKey(k) && java.util.Objects.equals(map.get(k), (value)); } @SuppressWarnings("unchecked")
/** * Returns a list of boolean columns suitable for use as dummy variables in, for example, regression analysis, * select a column of categorical data must be encoded as a list of columns, such that each column represents * a single category and indicates whether it is present (1) or not present (0) * * @return a list of {@link BooleanColumn} */ @Override public List<BooleanColumn> getDummies() { List<BooleanColumn> results = new ArrayList<>(); // createFromCsv the necessary columns for (Short2ObjectMap.Entry<String> entry : keyToValueMap().short2ObjectEntrySet()) { BooleanColumn column = BooleanColumn.create(entry.getValue()); results.add(column); } // iterate over the values, updating the dummy variable columns as appropriate for (short next : values) { String category = getValueForKey(next); for (BooleanColumn column : results) { if (category.equals(column.name())) { //TODO(lwhite): update the correct row more efficiently, by using set rather than add & only // updating true column.append(true); } else { column.append(false); } } } return results; }
@Override public int countUnique() { return keyToValueMap().size(); }
@Override public boolean remove(final short key, final Object value) { synchronized (sync) { return map.remove(key, value); } } @Override
private void checkIdExists(final int id) { if (!idMap.containsKey(intToShort(id))) { throw new IllegalArgumentException("The id " + id + " doesn' exist"); } }
@Override public ShortSet keySet() { synchronized (sync) { if (keys == null) keys = ShortSets.synchronize(map.keySet(), sync); return keys; } } @Override
@Override public V compute(final short key, final java.util.function.BiFunction<? super Short, ? super V, ? extends V> remappingFunction) { synchronized (sync) { return map.compute(key, remappingFunction); } } @Override
/** * Removes the entry for the specified key only if it is currently mapped to the * specified value. * * @param key * key with which the specified value is associated. * @param value * value expected to be associated with the specified key. * * @return {@code true} if the value was removed. * * @see java.util.Map#remove(Object, Object) * @since 8.0.0 */ default boolean remove(final short key, final Object value) { final V curValue = get(key); if (!java.util.Objects.equals(curValue, value) || (curValue == defaultReturnValue() && !containsKey(key))) return false; remove(key); return true; } /**
public boolean addType(final Object label, final int id) { short givenId = intToShort(id); short foundId = labelMap.getShort(label); if (foundId != NULL_SHORT && foundId != givenId) { throw new RuntimeException("This label '" + label + "' is already assigned to a different id"); } else if (idMap.containsKey(givenId)) { if ((label == null && idMap.get(givenId) == null) || idMap.get(givenId).equals(label)) { return false; } else { throw new RuntimeException("This id '" + id + "' is already assigned to a different label"); } } if (garbageQueue.contains(givenId)) { garbageQueue.remove(givenId); labelMap.put(label, givenId); idMap.put(givenId, label); } else { short insertId = intToShort(length++); while (insertId < givenId) { garbageQueue.add(insertId); insertId = intToShort(length++); if (length >= MAX_SIZE) { throw new RuntimeException("Maximum number of edge types reached at " + MAX_SIZE); } } labelMap.put(label, givenId); idMap.put(givenId, label); } return true; }