--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.datastructures;\r
+\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.TreeMap;\r
+\r
+/**\r
+ * MapSet is an associative data structure a key type on the left side (L) and a\r
+ * set element type of the right side.\r
+ * \r
+ * <p>\r
+ * Based on {@link MapList} by Toni Kalajainen.\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class MapSet<L, R> {\r
+\r
+ protected Map<L, Set<R>> sets;\r
+ \r
+ public static class Hash<L, R> extends MapSet<L, R> {\r
+ public Hash() {\r
+ sets = new HashMap<L, Set<R>>();\r
+ }\r
+ protected Set<R> getOrCreateSet(L key) {\r
+ Set<R> set = sets.get(key);\r
+ if (set == null) {\r
+ set = new HashSet<R>();\r
+ sets.put(key, set);\r
+ }\r
+ return set;\r
+ }\r
+ }\r
+\r
+ public static class Tree<L, R> extends MapSet<L, R> {\r
+ public Tree() {\r
+ sets = new TreeMap<L, Set<R>>();\r
+ }\r
+ public Tree(Comparator<? super L> comparator) {\r
+ sets = new TreeMap<L, Set<R>>(comparator);\r
+ }\r
+ protected Set<R> getOrCreateSet(L key) {\r
+ Set<R> set = sets.get(key);\r
+ if (set == null) {\r
+ set = new HashSet<R>();\r
+ sets.put(key, set);\r
+ }\r
+ return set;\r
+ }\r
+ }\r
+ \r
+ public boolean add(L key, R value) {\r
+ Set<R> set = getOrCreateSet(key);\r
+ return set.add(value);\r
+ }\r
+\r
+ protected abstract Set<R> getOrCreateSet(L key);\r
+\r
+ private Set<R> getSet(L key) {\r
+ return sets.get(key);\r
+ }\r
+\r
+ /**\r
+ * @param key\r
+ * @return a valid set, empty if no values exist\r
+ */\r
+ public Set<R> removeValues(L key) {\r
+ Set<R> set = sets.remove(key);\r
+ if (set == null)\r
+ return Collections.emptySet();\r
+ return set;\r
+ }\r
+\r
+ public boolean remove(L key, R value) {\r
+ Set<R> set = getSet(key);\r
+ if (set == null)\r
+ return false;\r
+ boolean result = set.remove(value);\r
+ if (set.isEmpty())\r
+ sets.remove(key);\r
+ return result;\r
+ }\r
+\r
+ public void clear() {\r
+ sets.clear();\r
+ }\r
+\r
+ public L[] getKeys(L[] list) {\r
+ return sets.keySet().toArray(list);\r
+ }\r
+\r
+ public Set<L> getKeys() {\r
+ return sets.keySet();\r
+ }\r
+\r
+ public boolean hasValues(L key) {\r
+ return sets.containsKey(key);\r
+ }\r
+\r
+ public R[] getValues(L key, R[] list) {\r
+ Set<R> l = sets.get(key);\r
+ if (l == null)\r
+ return null;\r
+ return l.toArray(list);\r
+ }\r
+\r
+ public Set<R> getValues(L key) {\r
+ Set<R> l = sets.get(key);\r
+ if (l == null)\r
+ return null;\r
+ return new HashSet<R>(l);\r
+ }\r
+\r
+ public Set<R> getValuesUnsafe(L key) {\r
+ return sets.get(key);\r
+ }\r
+\r
+}\r