]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/Compactify.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / procedures / Compactify.java
diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/Compactify.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/Compactify.java
new file mode 100644 (file)
index 0000000..82fd579
--- /dev/null
@@ -0,0 +1,32 @@
+package org.simantics.graph.compiler.internal.procedures;\r
+\r
+import gnu.trove.map.hash.TIntIntHashMap;\r
+\r
+import org.simantics.graph.store.GraphStore;\r
+\r
+public class Compactify implements Runnable {\r
+       GraphStore store;\r
+\r
+       public Compactify(GraphStore store) {\r
+               this.store = store;\r
+       }\r
+\r
+       @Override\r
+       public void run() {\r
+               // Mark all used resources\r
+               int resourceCount = store.identities.getResourceCount();\r
+               final boolean[] temp = new boolean[resourceCount];\r
+               store.collectReferences(temp);\r
+               \r
+               // Create a map\r
+               int newResourceCount = 0;\r
+               TIntIntHashMap map = new TIntIntHashMap(resourceCount);\r
+               for(int i=0;i<resourceCount;++i)\r
+                       if(temp[i])\r
+                               map.put(i, newResourceCount++);\r
+               if(newResourceCount < resourceCount) {\r
+                       store.map(map);\r
+                       store.identities.setResourceCount(newResourceCount);\r
+               }\r
+       }\r
+}\r