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