Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

81
LINES

< > BotCompany Repo | #1005534 // analyzeBitMap

JavaX fragment (include)

1  
static O analyzeBitMap_idToText; // func(long) -> S
2  
3  
// Looks only at the values
4  
static void analyzeBitMap(MultiMap<Long, Long> bitMap) {
5  
  analyzeBitMap(values(bitMap.data));
6  
}
7  
8  
static void analyzeBitMap(Collection<L<Long>> bitMap) {
9  
  if (bitMap.isEmpty()) ret;
10  
  
11  
  new HashSet<Long> allSeen;
12  
  for (L<Long> l : bitMap)
13  
    allSeen.addAll(l);
14  
  printStruct(+allSeen);
15  
  MultiMap<Long, Long> implies = fullSelfMap(allSeen);
16  
  MultiMap<Long, Long> eq = fullSelfMap(allSeen);
17  
  
18  
  for (L<Long> l : bitMap) {
19  
    HashSet<Long> set = new HashSet(l);
20  
    for (Long a : cloneList(implies.keySet())) {
21  
      bool ax = set.contains(a);
22  
      for (Long b : cloneList(implies.get(a))) {
23  
        bool bx = set.contains(b);
24  
        if (ax && !bx)
25  
          implies.remove(a, b);
26  
      }
27  
    }
28  
    
29  
    for (Long a : cloneList(eq.keySet())) {
30  
      bool ax = set.contains(a);
31  
      for (Long b : cloneList(eq.get(a))) {
32  
        bool bx = set.contains(b);
33  
        if (ax != bx)
34  
          eq.remove(a, b);
35  
      }
36  
    }
37  
  }
38  
  
39  
  printStruct(+implies);
40  
  //printStruct(+eq);
41  
  
42  
  L<L<Long>> clusters = multiMapToClusters(eq);
43  
  for i over clusters:
44  
    print("Cluster \* i+1 */: " + struct(mapIfFunctionNotNull(analyzeBitMap_idToText, clusters.get(i))));
45  
}
46  
47  
static MultiMap<Long, Long> fullSelfMap(Collection<Long> l) {
48  
  new MultiMap<Long, Long> mm;
49  
  for (Long x : l) mm.putAll(x, l);
50  
  ret mm;
51  
}
52  
53  
static L<L<Long>> multiMapToClusters(MultiMap<Long, Long> mm) {
54  
  // a cluster is a list of concepts.
55  
  // first, make trivial clusters (one per item).
56  
  
57  
  Map<Long, L<Long>> clusters = new TreeMap<Long, L<Long>>();
58  
  for (Long a : mm.keySet()) {
59  
    if (!clusters.containsKey(a))
60  
      clusters.put(a, ll(a));
61  
  }
62  
  
63  
  // then, merge the clusters.
64  
  
65  
  for (Long a : mm.keySet())
66  
    for (Long b : mm.get(a)) {
67  
      L<Long> c1 = clusters.get(a);
68  
      L<Long> c2 = clusters.get(b);
69  
      if (c1 != c2) // different clusters, need to merge!
70  
        mergeClusters(clusters, c1, c2);
71  
    }
72  
  
73  
  ret asList(new HashSet<L<Long>>(clusters.values()));
74  
}
75  
76  
static void mergeClusters(Map<Long, L<Long>> clusters, L<Long> c1, L<Long> c2) {
77  
  for (Long x : c2) {
78  
    clusters.put(x, c1);
79  
    c1.add(x);
80  
  }
81  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1005534
Snippet name: analyzeBitMap
Eternal ID of this version: #1005534/1
Text MD5: a41a73c41a52102f55e6478537f2e7b0
Author: stefan
Category: javax / logic
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-27 14:04:39
Source code size: 2265 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 438 / 656
Referenced in: [show references]