]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/store/IndexMappingUtils.java
Check statement collisions
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / store / IndexMappingUtils.java
1 package org.simantics.graph.store;\r
2 \r
3 import gnu.trove.list.array.TIntArrayList;\r
4 import gnu.trove.map.hash.TIntIntHashMap;\r
5 import gnu.trove.map.hash.TIntObjectHashMap;\r
6 import gnu.trove.map.hash.TObjectIntHashMap;\r
7 import gnu.trove.procedure.TIntObjectProcedure;\r
8 import gnu.trove.procedure.TIntProcedure;\r
9 import gnu.trove.procedure.TObjectIntProcedure;\r
10 import gnu.trove.set.hash.TIntHashSet;\r
11 \r
12 public class IndexMappingUtils {\r
13         \r
14         public static <T> TIntObjectHashMap<T> map(final TIntIntHashMap map, TIntObjectHashMap<T> target,\r
15                         final TIntHashSet collisions) {\r
16                 final TIntObjectHashMap<T> newTarget = new TIntObjectHashMap<T>();\r
17                 target.forEachEntry(new TIntObjectProcedure<T>() {\r
18                         @Override\r
19                         public boolean execute(int a, T b) {\r
20                                 if(map.contains(a))\r
21                                         a = map.get(a);\r
22                                 if(newTarget.put(a, b) != null)\r
23                                         collisions.add(a);\r
24                                 return true;\r
25                         }\r
26                 });\r
27                 return newTarget;\r
28         }\r
29         \r
30         public static void map(TIntIntHashMap map, TIntArrayList target) {\r
31                 int size = target.size();\r
32                 for(int i=0;i<size;++i) {\r
33                         int id = target.getQuick(i);\r
34                         if(map.contains(id))\r
35                                 target.setQuick(i, map.get(id));\r
36                 }\r
37         }       \r
38         \r
39         public static <T> void map(final TIntIntHashMap map, \r
40                         final TObjectIntHashMap<T> target) {\r
41                 target.forEachEntry(new TObjectIntProcedure<T>() {\r
42                         @Override\r
43                         public boolean execute(T a, int b) {\r
44                                 if(map.containsKey(b))\r
45                                         target.put(a, map.get(b));\r
46                                 return true;\r
47                         }\r
48                 });\r
49         }\r
50         \r
51         public static TIntHashSet map(final TIntIntHashMap map, TIntHashSet target) {\r
52                 final TIntHashSet newTarget = new TIntHashSet();\r
53                 target.forEach(new TIntProcedure() {\r
54                         @Override\r
55                         public boolean execute(int a) {\r
56                                 if(map.contains(a))\r
57                                         a = map.get(a);\r
58                                 newTarget.add(a);\r
59                                 return true;\r
60                         }\r
61                 });\r
62                 return newTarget;\r
63         }\r
64 \r
65         public static void map(TIntIntHashMap map, int[] target) {\r
66                 for(int i=0;i<target.length;++i) {\r
67                         int temp = target[i];\r
68                         if(map.containsKey(temp))\r
69                                 target[i] = map.get(temp);\r
70                 }\r
71         }       \r
72 \r
73 }\r