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

29
LINES

< > BotCompany Repo | #1009481 // clusters_add - add relations to a cluster list (Map<A, L<A>>)

JavaX fragment (include)

static <A> L<A> clusters_add(Map<A, L<A>> clusters, L<A> l) {
  for (int i = 0; i < l(l)-1; i++)
    clusters_add(clusters, l.get(i), l.get(i+1));
  ret clusters.get(first(l));
}

static <A> void clusters_add(Map<A, L<A>> clusters, A a, A b) {
  if (eq(a, b)) {
    L<A> cluster = clusters.get(a);
    if (cluster == null)
      clusters.put(a, litlist(a));
    ret;
  }
  
  L<A> clusterA = clusters.get(a);
  L<A> clusterB = clusters.get(b);
  if (clusterA == null && clusterB == null) {
    L<A> cluster = ll(a, b);
    clusters.put(a, cluster);
    clusters.put(b, cluster);
  } else if (clusterA == null) {
    clusterB.add(a);
    clusters.put(a, clusterB);
  } else if (clusterB == null) {
    clusterA.add(b);
    clusters.put(b, clusterA);
  } else if (clusterA != clusterB)
    clusters_merge(clusters, clusterA, clusterB);
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1009481
Snippet name: clusters_add - add relations to a cluster list (Map<A, L<A>>)
Eternal ID of this version: #1009481/4
Text MD5: 2c271f9a9faa09827d7d1ffb34b49dd5
Author: stefan
Category: javax / math
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-09-07 16:52:10
Source code size: 863 bytes / 29 lines
Pitched / IR pitched: No / No
Views / Downloads: 521 / 537
Version history: 3 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1009482 - clusters_merge - helper function for clusters_add
#1010458 - treeSetClusters_add - add relations to a cluster list (Map<A, Collection<A>>)