From 2442e05e638a61dd1bfbd6b95cb3544b6a327af9 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Tue, 27 Sep 2022 19:09:11 -0400 Subject: 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 --- src/main/java/bjc/esodata/AbbrevMap2.java | 19 ++- src/main/java/bjc/esodata/AbbrevTree.java | 23 +++- src/main/java/bjc/esodata/DefaultList.java | 17 +++ src/main/java/bjc/esodata/Directory.java | 17 +++ src/main/java/bjc/esodata/DoubleSided.java | 17 +++ src/main/java/bjc/esodata/DoubleTape.java | 17 +++ src/main/java/bjc/esodata/FlatNestIterator.java | 17 +++ src/main/java/bjc/esodata/KeyedList.java | 123 +++++++++++++++++++ src/main/java/bjc/esodata/MapSet.java | 17 +++ src/main/java/bjc/esodata/MinMaxList.java | 17 +++ src/main/java/bjc/esodata/Multimap.java | 149 ++++++++---------------- src/main/java/bjc/esodata/NestList.java | 17 +++ src/main/java/bjc/esodata/PairMap.java | 25 +++- src/main/java/bjc/esodata/PushdownMap.java | 17 +++ src/main/java/bjc/esodata/QueueStack.java | 17 +++ src/main/java/bjc/esodata/SimpleDirectory.java | 17 +++ src/main/java/bjc/esodata/SimpleStack.java | 17 +++ src/main/java/bjc/esodata/SingleTape.java | 17 +++ src/main/java/bjc/esodata/SpaghettiStack.java | 17 +++ src/main/java/bjc/esodata/Stack.java | 17 +++ src/main/java/bjc/esodata/TSetMultimap.java | 124 ++++++++++++++++++++ src/main/java/bjc/esodata/Tape.java | 17 +++ src/main/java/bjc/esodata/TapeLibrary.java | 17 +++ src/main/java/bjc/esodata/TapeView.java | 17 +++ src/main/java/bjc/esodata/ThresholdSet.java | 17 +++ src/main/java/bjc/esodata/UnifiedDirectory.java | 17 +++ src/main/java/bjc/esodata/spool/Spool.java | 17 +++ src/main/java/bjc/esodata/spool/Spooler.java | 17 +++ 28 files changed, 729 insertions(+), 108 deletions(-) create mode 100644 src/main/java/bjc/esodata/KeyedList.java create mode 100644 src/main/java/bjc/esodata/TSetMultimap.java (limited to 'src/main/java/bjc/esodata') diff --git a/src/main/java/bjc/esodata/AbbrevMap2.java b/src/main/java/bjc/esodata/AbbrevMap2.java index 341a28d..72bd4c8 100644 --- a/src/main/java/bjc/esodata/AbbrevMap2.java +++ b/src/main/java/bjc/esodata/AbbrevMap2.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.*; @@ -25,7 +42,7 @@ public class AbbrevMap2 { * Create a new abbreviation map. */ public AbbrevMap2() { - backing = new Multimap<>(); + backing = new TSetMultimap<>(); } /** diff --git a/src/main/java/bjc/esodata/AbbrevTree.java b/src/main/java/bjc/esodata/AbbrevTree.java index 35c44f0..c2ad8db 100644 --- a/src/main/java/bjc/esodata/AbbrevTree.java +++ b/src/main/java/bjc/esodata/AbbrevTree.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.*; @@ -22,7 +39,7 @@ import bjc.funcdata.ListEx; * @param The type of data contained in the nodes. */ public class AbbrevTree implements Iterable> { - private Multimap> labelledNodes; + private TSetMultimap> labelledNodes; private Map> children; private AbbrevTree parent; @@ -34,7 +51,7 @@ public class AbbrevTree implements Iterable(); + labelledNodes = new TSetMultimap<>(); children = new HashMap<>(); } @@ -57,7 +74,7 @@ public class AbbrevTree implements Iterable parent) { - labelledNodes = new Multimap<>(); + labelledNodes = new TSetMultimap<>(); children = new HashMap<>(); this.parent = parent; diff --git a/src/main/java/bjc/esodata/DefaultList.java b/src/main/java/bjc/esodata/DefaultList.java index e622301..727eafd 100644 --- a/src/main/java/bjc/esodata/DefaultList.java +++ b/src/main/java/bjc/esodata/DefaultList.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.AbstractList; diff --git a/src/main/java/bjc/esodata/Directory.java b/src/main/java/bjc/esodata/Directory.java index fa47b6f..feeb25c 100644 --- a/src/main/java/bjc/esodata/Directory.java +++ b/src/main/java/bjc/esodata/Directory.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; /** diff --git a/src/main/java/bjc/esodata/DoubleSided.java b/src/main/java/bjc/esodata/DoubleSided.java index 8ac6684..0ec417a 100644 --- a/src/main/java/bjc/esodata/DoubleSided.java +++ b/src/main/java/bjc/esodata/DoubleSided.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; /** diff --git a/src/main/java/bjc/esodata/DoubleTape.java b/src/main/java/bjc/esodata/DoubleTape.java index 30d94a1..e7ba98c 100644 --- a/src/main/java/bjc/esodata/DoubleTape.java +++ b/src/main/java/bjc/esodata/DoubleTape.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; /** diff --git a/src/main/java/bjc/esodata/FlatNestIterator.java b/src/main/java/bjc/esodata/FlatNestIterator.java index 42981f5..60463d8 100644 --- a/src/main/java/bjc/esodata/FlatNestIterator.java +++ b/src/main/java/bjc/esodata/FlatNestIterator.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.*; diff --git a/src/main/java/bjc/esodata/KeyedList.java b/src/main/java/bjc/esodata/KeyedList.java new file mode 100644 index 0000000..5b0d6cc --- /dev/null +++ b/src/main/java/bjc/esodata/KeyedList.java @@ -0,0 +1,123 @@ +/* + * 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 . + */ +package bjc.esodata; + +import java.util.*; + +public class KeyedList implements Iterable { + private List backing; + private Map indices; + + private int currIdx = 0; + + public KeyedList() { + backing = new ArrayList<>(); + indices = new HashMap<>(); + } + + /** + * Add a item to this list. + * + * If an item already exists with the given key, the current one will not be + * added. Use set to handle that. + * + * @param key The key for this item. + * @param val The value for this item + * + * @return Whether or not this item was added to the list + */ + public boolean add(Key key, Val val) { + // TODO: Determine if this is the desired behavior + if (indices.containsKey(key)) + return false; + + backing.add(val); + indices.put(key, currIdx++); + return true; + } + + /** + * Set the item associated with a given key. + * + * @param key The key to set + * @param newVal The new value for the key + * + * @return The previous value for the key, if there was one + */ + public Val set(Key key, Val newVal) { + if (indices.containsKey(key)) { + return backing.set(indices.get(key), newVal); + } + + add(key, newVal); + return null; + } + + /** + * Retrieve all of the keys for this list. + * + * @return An immutable set of the keys for this list + */ + public Set keys() { + // TODO: write mutable wrapper which will update the list appropriately + return Collections.unmodifiableSet(indices.keySet()); + } + + /** + * Retrieve the value associated with the given key. + * + * @param key The key to look up. + * + * @return The value for the given key. + */ + public Val get(Key key) { + return backing.get(indices.get(key)); + } + + /** + * Check if this list contains a value for a given key. + * + * @param key The key to look up. + * + * @return Whether this list contains a value for the given key. + */ + public boolean containsKey(Key key) { + return indices.containsKey(key); + } + + @Override + public Iterator iterator() { + return backing.iterator(); + } + + /** + * Return an iterator that starts at the value for the given key.' + * + * @param key The key to start at. + * + * @return An iterator starting at the given key, or null if the key isn't + * present. + */ + public ListIterator iteratorFrom(Key key) { + if (indices.containsKey(key)) { + return backing.listIterator(indices.get(key)); + } + + return null; + } +} diff --git a/src/main/java/bjc/esodata/MapSet.java b/src/main/java/bjc/esodata/MapSet.java index 7d77ad6..ade6219 100644 --- a/src/main/java/bjc/esodata/MapSet.java +++ b/src/main/java/bjc/esodata/MapSet.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.AbstractMap; diff --git a/src/main/java/bjc/esodata/MinMaxList.java b/src/main/java/bjc/esodata/MinMaxList.java index 6747831..50b0ea0 100644 --- a/src/main/java/bjc/esodata/MinMaxList.java +++ b/src/main/java/bjc/esodata/MinMaxList.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.*; 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 . + */ 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 The type of keys in the map. - * @param The type of values in the map. + * @param The key type of the map + * @param The value type of the map. */ -public class Multimap implements Iterable> { - private Map> backing; - - /** - * Create a new empty multimap. - */ - public Multimap() { - backing = new HashMap<>(); - } - +public interface Multimap { /** * 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 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 get(KeyType key) { - if (!backing.containsKey(key)) return new HashSet<>(); - else return backing.get(key).values(); - } + Set 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 getSingle(KeyType key) { - Set set = get(key); - - if (set.size() == 1) return Optional.of(set.iterator().next()); - return Optional.empty(); - } + Optional 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> iterator(); - return backing.get(key).contains(value) > 0; - } - - @Override - public Iterator> iterator() { - return new Iterator<>() { - private Iterator>> mapIter = backing.entrySet().iterator(); - private KeyType currKey; - private Iterator setIter; - - @Override - public boolean hasNext() { - while (setIter == null || !setIter.hasNext()) { - if (!mapIter.hasNext()) return false; - Entry> entry = mapIter.next(); - - currKey = entry.getKey(); - setIter = entry.getValue().setView().iterator(); - } - - return setIter.hasNext(); - } - - @Override - public Pair next() { - if (setIter == null || !setIter.hasNext()) throw new NoSuchElementException(); - return Pair.pair(currKey, setIter.next()) ; - } - }; - } -} +} \ No newline at end of file diff --git a/src/main/java/bjc/esodata/NestList.java b/src/main/java/bjc/esodata/NestList.java index 9d9149c..c1544ca 100644 --- a/src/main/java/bjc/esodata/NestList.java +++ b/src/main/java/bjc/esodata/NestList.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import static bjc.functypes.Combinators.*; diff --git a/src/main/java/bjc/esodata/PairMap.java b/src/main/java/bjc/esodata/PairMap.java index ba13a2f..0e01bd3 100644 --- a/src/main/java/bjc/esodata/PairMap.java +++ b/src/main/java/bjc/esodata/PairMap.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.*; @@ -29,8 +46,8 @@ public class PairMap implements Map, Value public PairMap() { this.backing = new HashMap<>(); - this.leftTracker = new Multimap<>(); - this.rightTracker = new Multimap<>(); + this.leftTracker = new TSetMultimap<>(); + this.rightTracker = new TSetMultimap<>(); } /** @@ -122,8 +139,8 @@ public class PairMap implements Map, Value public void clear() { backing.clear(); - leftTracker = new Multimap<>(); - rightTracker = new Multimap<>(); + leftTracker = new TSetMultimap<>(); + rightTracker = new TSetMultimap<>(); } // TODO: Update these to not break the tracking invariants diff --git a/src/main/java/bjc/esodata/PushdownMap.java b/src/main/java/bjc/esodata/PushdownMap.java index 76dd2db..a6e153c 100644 --- a/src/main/java/bjc/esodata/PushdownMap.java +++ b/src/main/java/bjc/esodata/PushdownMap.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.*; diff --git a/src/main/java/bjc/esodata/QueueStack.java b/src/main/java/bjc/esodata/QueueStack.java index e310f16..22b849a 100644 --- a/src/main/java/bjc/esodata/QueueStack.java +++ b/src/main/java/bjc/esodata/QueueStack.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.Deque; diff --git a/src/main/java/bjc/esodata/SimpleDirectory.java b/src/main/java/bjc/esodata/SimpleDirectory.java index 13a9f3e..4cc6f35 100644 --- a/src/main/java/bjc/esodata/SimpleDirectory.java +++ b/src/main/java/bjc/esodata/SimpleDirectory.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import bjc.funcdata.FunctionalMap; diff --git a/src/main/java/bjc/esodata/SimpleStack.java b/src/main/java/bjc/esodata/SimpleStack.java index 9c016c3..55d4000 100644 --- a/src/main/java/bjc/esodata/SimpleStack.java +++ b/src/main/java/bjc/esodata/SimpleStack.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.Deque; diff --git a/src/main/java/bjc/esodata/SingleTape.java b/src/main/java/bjc/esodata/SingleTape.java index 7c3a34f..ff34178 100644 --- a/src/main/java/bjc/esodata/SingleTape.java +++ b/src/main/java/bjc/esodata/SingleTape.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.ArrayList; diff --git a/src/main/java/bjc/esodata/SpaghettiStack.java b/src/main/java/bjc/esodata/SpaghettiStack.java index 4bd77d3..ab57646 100644 --- a/src/main/java/bjc/esodata/SpaghettiStack.java +++ b/src/main/java/bjc/esodata/SpaghettiStack.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.Arrays; diff --git a/src/main/java/bjc/esodata/Stack.java b/src/main/java/bjc/esodata/Stack.java index 9dfee17..6027a23 100644 --- a/src/main/java/bjc/esodata/Stack.java +++ b/src/main/java/bjc/esodata/Stack.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.*; diff --git a/src/main/java/bjc/esodata/TSetMultimap.java b/src/main/java/bjc/esodata/TSetMultimap.java new file mode 100644 index 0000000..1a04a41 --- /dev/null +++ b/src/main/java/bjc/esodata/TSetMultimap.java @@ -0,0 +1,124 @@ +/* + * 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 . + */ +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. + * + * @author Ben Culkin + * + * @param The type of keys in the map. + * @param The type of values in the map. + */ +public class TSetMultimap implements Iterable>, Multimap { + private final class SetMultimapIterator implements Iterator> { + private Iterator>> mapIter = backing.entrySet().iterator(); + private KeyType currKey; + private Iterator setIter; + + @Override + public boolean hasNext() { + while (setIter == null || !setIter.hasNext()) { + if (!mapIter.hasNext()) return false; + Entry> entry = mapIter.next(); + + currKey = entry.getKey(); + setIter = entry.getValue().setView().iterator(); + } + + return setIter.hasNext(); + } + + @Override + public Pair next() { + if (setIter == null || !setIter.hasNext()) throw new NoSuchElementException(); + return Pair.pair(currKey, setIter.next()) ; + } + } + + private Map> backing; + + /** + * Create a new empty multimap. + */ + public TSetMultimap() { + backing = new HashMap<>(); + } + + @Override + public void add(KeyType key, ValueType value) { + ThresholdSet container + = backing.computeIfAbsent(key, k -> new ThresholdSet<>()); + + container.add(value); + } + + @Override + 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); + } + + @Override + public void remove(KeyType key) { + backing.remove(key); + } + + @Override + public Set get(KeyType key) { + if (!backing.containsKey(key)) return new HashSet<>(); + else return backing.get(key).values(); + } + + @Override + public Optional getSingle(KeyType key) { + Set set = get(key); + + if (set.size() == 1) return Optional.of(set.iterator().next()); + return Optional.empty(); + } + + @Override + public boolean contains(KeyType key) { + return backing.containsKey(key); + } + + @Override + public boolean contains(KeyType key, ValueType value) { + if (!backing.containsKey(key)) return false; + + return backing.get(key).contains(value) > 0; + } + + @Override + public Iterator> iterator() { + return new SetMultimapIterator(); + } +} diff --git a/src/main/java/bjc/esodata/Tape.java b/src/main/java/bjc/esodata/Tape.java index 134ba62..efc3df7 100644 --- a/src/main/java/bjc/esodata/Tape.java +++ b/src/main/java/bjc/esodata/Tape.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; /** diff --git a/src/main/java/bjc/esodata/TapeLibrary.java b/src/main/java/bjc/esodata/TapeLibrary.java index 922833f..5fec28a 100644 --- a/src/main/java/bjc/esodata/TapeLibrary.java +++ b/src/main/java/bjc/esodata/TapeLibrary.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.*; diff --git a/src/main/java/bjc/esodata/TapeView.java b/src/main/java/bjc/esodata/TapeView.java index bfc33a9..94192d8 100644 --- a/src/main/java/bjc/esodata/TapeView.java +++ b/src/main/java/bjc/esodata/TapeView.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; /** diff --git a/src/main/java/bjc/esodata/ThresholdSet.java b/src/main/java/bjc/esodata/ThresholdSet.java index c13bad3..645d4d3 100644 --- a/src/main/java/bjc/esodata/ThresholdSet.java +++ b/src/main/java/bjc/esodata/ThresholdSet.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import java.util.*; diff --git a/src/main/java/bjc/esodata/UnifiedDirectory.java b/src/main/java/bjc/esodata/UnifiedDirectory.java index 1b630eb..3b521c0 100644 --- a/src/main/java/bjc/esodata/UnifiedDirectory.java +++ b/src/main/java/bjc/esodata/UnifiedDirectory.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata; import bjc.funcdata.FunctionalMap; diff --git a/src/main/java/bjc/esodata/spool/Spool.java b/src/main/java/bjc/esodata/spool/Spool.java index dbd2ce8..80e8a8c 100644 --- a/src/main/java/bjc/esodata/spool/Spool.java +++ b/src/main/java/bjc/esodata/spool/Spool.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata.spool; public interface Spool { diff --git a/src/main/java/bjc/esodata/spool/Spooler.java b/src/main/java/bjc/esodata/spool/Spooler.java index a222a25..133df2e 100644 --- a/src/main/java/bjc/esodata/spool/Spooler.java +++ b/src/main/java/bjc/esodata/spool/Spooler.java @@ -1,3 +1,20 @@ +/* + * 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 . + */ package bjc.esodata.spool; public interface Spooler { -- cgit v1.2.3