]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/MMultiMap.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / MMultiMap.scl
diff --git a/bundles/org.simantics.scl.runtime/scl/MMultiMap.scl b/bundles/org.simantics.scl.runtime/scl/MMultiMap.scl
new file mode 100644 (file)
index 0000000..88bc371
--- /dev/null
@@ -0,0 +1,21 @@
+import "Prelude" as Prelude
+import "MMap" as MMap
+import "MList" as MList
+
+type T a b = MMap.T a (MList.T b)
+
+add :: MMap.T a (MList.T b) -> a -> b -> <Proc> ()
+add m k v = match MMap.get m k with
+    Just l -> MList.add l v
+    Nothing -> Prelude.ignore (MMap.put m k (MList.singleton v))
+
+get :: MMap.T a (MList.T b) -> a -> <Proc> [b]
+get m k = match MMap.get m k with
+    Just l -> MList.freeze l
+    Nothing -> []
+
+indexBy :: (v -> <e> k) -> [v] -> <Proc,e> MMap.T k (MList.T v)
+indexBy f l = do
+    result = MMap.createC (Prelude.length l)
+    Prelude.iter (\v -> add result (f v) v) l
+    result