/** */ @Override public Table countByCategory(String columnName) { Table t = Table.create("Column: " + columnName); StringColumn categories = StringColumn.create("Category"); IntColumn counts = IntColumn.create("Count"); Short2IntMap valueToCount = new Short2IntOpenHashMap(); for (short next : values) { if (valueToCount.containsKey(next)) { valueToCount.put(next, valueToCount.get(next) + 1); } else { valueToCount.put(next, 1); } } for (Map.Entry<Short, Integer> entry : valueToCount.short2IntEntrySet()) { categories.append(getValueForKey(entry.getKey())); counts.append(entry.getValue()); } if (countMissing() > 0) { categories.append("* missing values"); counts.append(countMissing()); } t.addColumns(categories); t.addColumns(counts); return t; }
/** * 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 int value) { final int curValue = get(key); if (!((curValue) == (value)) || (curValue == defaultReturnValue() && !containsKey(key))) return false; remove(key); return true; } /**
/** * 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 int getOrDefault(final short key, final int defaultValue) { final int v; return ((v = get(key)) != defaultReturnValue() || containsKey(key)) ? v : defaultValue; } /**
/** * 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 int replace(final short key, final int value) { return containsKey(key) ? put(key, value) : defaultReturnValue(); } /**
/** * Returns an iterable yielding an iterator that will be * {@linkplain FastEntrySet fast}, if possible, on the * {@linkplain Map#entrySet() entry set} of the provided {@code map}. * * @param map * a map from which we will try to extract an iterable yielding a * (fast) iterator on the entry set. * @return an iterable yielding an iterator on the entry set of the given map * that will be fast, if possible. * @since 8.0.0 */ public static ObjectIterable<Short2IntMap.Entry> fastIterable(Short2IntMap map) { final ObjectSet<Short2IntMap.Entry> entries = map.short2IntEntrySet(); return entries instanceof Short2IntMap.FastEntrySet ? new ObjectIterable<Short2IntMap.Entry>() { public ObjectIterator<Short2IntMap.Entry> iterator() { return ((Short2IntMap.FastEntrySet) entries).fastIterator(); } public void forEach(final Consumer<? super Short2IntMap.Entry> consumer) { ((Short2IntMap.FastEntrySet) entries).fastForEach(consumer); } } : entries; } /**
@Override public boolean contains(final Object o) { if (!(o instanceof Map.Entry)) return false; if (o instanceof Short2IntMap.Entry) { final Short2IntMap.Entry e = (Short2IntMap.Entry) o; final short k = e.getShortKey(); return map.containsKey(k) && ((map.get(k)) == (e.getIntValue())); } 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(); if (value == null || !(value instanceof Integer)) return false; return map.containsKey(k) && ((map.get(k)) == (((Integer) (value)).intValue())); }
@Override public boolean containsValue(final int v) { synchronized (sync) { return map.containsValue(v); } } /**
@Override public int computeIfPresent(final short key, final java.util.function.BiFunction<? super Short, ? super Integer, ? extends Integer> remappingFunction) { synchronized (sync) { return map.computeIfPresent(key, remappingFunction); } } @Override
@Override public int compute(final short key, final java.util.function.BiFunction<? super Short, ? super Integer, ? extends Integer> remappingFunction) { synchronized (sync) { return map.compute(key, remappingFunction); } } @Override
@Override public int computeIfAbsent(final short key, final java.util.function.IntUnaryOperator mappingFunction) { synchronized (sync) { return map.computeIfAbsent(key, mappingFunction); } } @Override
@Override public int computeIfAbsentPartial(final short key, final Short2IntFunction mappingFunction) { synchronized (sync) { return map.computeIfAbsentPartial(key, mappingFunction); } } @Override
@Override public int computeIfAbsentNullable(final short key, final java.util.function.IntFunction<? extends Integer> mappingFunction) { synchronized (sync) { return map.computeIfAbsentNullable(key, mappingFunction); } } @Override
/** * Replaces the entry for the specified key only if currently mapped to the * specified value. * * @param key * key with which the specified value is associated. * @param oldValue * value expected to be associated with the specified key. * @param newValue * value to be associated with the specified key. * * @return {@code true} if the value was replaced. * * @see java.util.Map#replace(Object, Object, Object) * @since 8.0.0 */ default boolean replace(final short key, final int oldValue, final int newValue) { final int curValue = get(key); if (!((curValue) == (oldValue)) || (curValue == defaultReturnValue() && !containsKey(key))) return false; put(key, newValue); return true; } /**
/** * Returns a set view of the mappings contained in this map. * <p> * Note that this specification strengthens the one given in * {@link Map#entrySet()}. * * @return a set view of the mappings contained in this map. * @see Map#entrySet() * @deprecated Please use the corresponding type-specific method instead. */ @Deprecated @Override @SuppressWarnings({"unchecked", "rawtypes"}) default ObjectSet<Map.Entry<Short, Integer>> entrySet() { return (ObjectSet) short2IntEntrySet(); } /**
@Override public boolean containsValue(final int v) { return map.containsValue(v); } /**
/** * {@inheritDoc} * * @deprecated Please use the corresponding type-specific method instead. */ @Deprecated @Override public Integer computeIfPresent(final Short key, final java.util.function.BiFunction<? super Short, ? super Integer, ? extends Integer> remappingFunction) { synchronized (sync) { return map.computeIfPresent(key, remappingFunction); } } /**
/** * {@inheritDoc} * * @deprecated Please use the corresponding type-specific method instead. */ @Deprecated @Override public Integer compute(final Short key, final java.util.function.BiFunction<? super Short, ? super Integer, ? extends Integer> remappingFunction) { synchronized (sync) { return map.compute(key, remappingFunction); } } /**
/** * {@inheritDoc} * * @deprecated Please use the corresponding type-specific method instead. */ @Deprecated @Override public Integer computeIfAbsent(final Short key, final java.util.function.Function<? super Short, ? extends Integer> mappingFunction) { synchronized (sync) { return map.computeIfAbsent(key, mappingFunction); } } /**
/** * 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 int putIfAbsent(final short key, final int value) { final int v = get(key), drv = defaultReturnValue(); if (v != drv || containsKey(key)) return v; put(key, value); return drv; } /**
/** */ @Override public Table countByCategory(String columnName) { Table t = Table.create("Column: " + columnName); StringColumn categories = StringColumn.create("Category"); IntColumn counts = IntColumn.create("Count"); Short2IntMap valueToCount = new Short2IntOpenHashMap(); for (short next : values) { if (valueToCount.containsKey(next)) { valueToCount.put(next, valueToCount.get(next) + 1); } else { valueToCount.put(next, 1); } } for (Map.Entry<Short, Integer> entry : valueToCount.short2IntEntrySet()) { categories.append(getValueForKey(entry.getKey())); counts.append(entry.getValue()); } if (countMissing() > 0) { categories.append("* missing values"); counts.append(countMissing()); } t.addColumns(categories); t.addColumns(counts); return t; }