diff options
| author | Ben Culkin <scorpress@gmail.com> | 2022-09-27 19:09:11 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2022-09-27 19:09:11 -0400 |
| commit | 2442e05e638a61dd1bfbd6b95cb3544b6a327af9 (patch) | |
| tree | 7d332d1b84bc9338d38664de64c166f1641f3fe9 /src/main/java/bjc/esodata/Multimap.java | |
| parent | 2d5c3288134f19088941c980e852521e9838db56 (diff) | |
GPLize project
Finally deciding to move things into a proper license; which I can do
since I've never accepted (or had :( ) a pull request from another
person
Diffstat (limited to 'src/main/java/bjc/esodata/Multimap.java')
| -rw-r--r-- | src/main/java/bjc/esodata/Multimap.java | 149 |
1 files changed, 49 insertions, 100 deletions
diff --git a/src/main/java/bjc/esodata/Multimap.java b/src/main/java/bjc/esodata/Multimap.java index e18ed49..d0c0b4b 100644 --- a/src/main/java/bjc/esodata/Multimap.java +++ b/src/main/java/bjc/esodata/Multimap.java @@ -1,153 +1,102 @@ +/* + * esodata - data structures and other things, of varying utility + * Copyright 2022, Ben Culkin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ package bjc.esodata; import java.util.*; -import java.util.Map.Entry; import bjc.data.Pair; /** - * A map that has support for multiple values for a given key. - * - * Whenever you give another value for a key, that is then returned for that - * key. About the only somewhat complex thing, is that, if you add the same - * key-value pair multiple times, it will only show up once. However, you will - * have to remove that pair as many times as you added it. + * A map that supports multiple values for a given key. + * + * The thing that will tend to vary with the implementation is what they do with + * duplicate values for the same key. + * + * @author bjcul * - * @author Ben Culkin - * @param <KeyType> The type of keys in the map. - * @param <ValueType> The type of values in the map. + * @param <KeyType> The key type of the map + * @param <ValueType> The value type of the map. */ -public class Multimap<KeyType, ValueType> implements Iterable<Pair<KeyType, ValueType>> { - private Map<KeyType, ThresholdSet<ValueType>> backing; - - /** - * Create a new empty multimap. - */ - public Multimap() { - backing = new HashMap<>(); - } - +public interface Multimap<KeyType, ValueType> { /** * Add a key-value mapping to the map. * - * @param key - * The key to store the value under. + * @param key The key to store the value under. * - * @param value - * The value to store. + * @param value The value to store. */ - public void add(KeyType key, ValueType value) { - ThresholdSet<ValueType> container - = backing.computeIfAbsent(key, k -> new ThresholdSet<>()); - - container.add(value); - } + void add(KeyType key, ValueType value); /** * Delete a particular key-value mapping from the map. * - * @param key - * The key of the mapping to remove. + * @param key The key of the mapping to remove. * - * @param value - * The value of the mapping to remove. + * @param value The value of the mapping to remove. */ - public void remove(KeyType key, ValueType value) { - // We have no values for that key; bail. - if (!backing.containsKey(key)) return; - - backing.get(key).remove(value); - } + void remove(KeyType key, ValueType value); /** * Delete all of the values associated with a particular key. * - * @param key - * The key to remove values for. + * @param key The key to remove values for. */ - public void remove(KeyType key) { - backing.remove(key); - } + void remove(KeyType key); /** * Get a set containing all of the values that are recorded for that key. * - * @param key - * The key to look up values for. + * @param key The key to look up values for. * * @return A set containing all of the values that have been mapped to that key. */ - public Set<ValueType> get(KeyType key) { - if (!backing.containsKey(key)) return new HashSet<>(); - else return backing.get(key).values(); - } + Set<ValueType> get(KeyType key); /** * Get the single value in the map, if there is one. + * * @param key The key to look up - * @return An optional containing the key if it is there once, or empty if it is there either no or more than one times + * @return An optional containing the key if it is there once, or empty if it is + * there either no or more than one times */ - public Optional<ValueType> getSingle(KeyType key) { - Set<ValueType> set = get(key); - - if (set.size() == 1) return Optional.of(set.iterator().next()); - return Optional.empty(); - } + Optional<ValueType> getSingle(KeyType key); + /** * Check if there is at least one value mapped to the given key. * - * @param key - * The key to check for mappings for. + * @param key The key to check for mappings for. * * @return Whether or not there is at least one value mapped to the key. */ - public boolean contains(KeyType key) { - return backing.containsKey(key); - } + boolean contains(KeyType key); /** * Check if there is at least one instance of a particular key-value mapping. * - * @param key - * The key to check for mappings for. + * @param key The key to check for mappings for. * - * @param value - * The value to check for mappings for. + * @param value The value to check for mappings for. * * @return Whether or not there is at least one instance of the given key-value * mapping. */ - public boolean contains(KeyType key, ValueType value) { - if (!backing.containsKey(key)) return false; + boolean contains(KeyType key, ValueType value); + + Iterator<Pair<KeyType, ValueType>> iterator(); - return backing.get(key).contains(value) > 0; - } - - @Override - public Iterator<Pair<KeyType, ValueType>> iterator() { - return new Iterator<>() { - private Iterator<Entry<KeyType, ThresholdSet<ValueType>>> mapIter = backing.entrySet().iterator(); - private KeyType currKey; - private Iterator<ValueType> setIter; - - @Override - public boolean hasNext() { - while (setIter == null || !setIter.hasNext()) { - if (!mapIter.hasNext()) return false; - Entry<KeyType,ThresholdSet<ValueType>> entry = mapIter.next(); - - currKey = entry.getKey(); - setIter = entry.getValue().setView().iterator(); - } - - return setIter.hasNext(); - } - - @Override - public Pair<KeyType, ValueType> next() { - if (setIter == null || !setIter.hasNext()) throw new NoSuchElementException(); - return Pair.pair(currKey, setIter.next()) ; - } - }; - } -} +}
\ No newline at end of file |
