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

33
LINES

< > BotCompany Repo | #1001512 // Synonyms (class)

JavaX fragment (include)

1  
static class Synonyms {
2  
  Map<S, L<S>> synMap = new TreeMap<S, L<S>>();
3  
  
4  
  void addSynonym(S a, S b) {
5  
    L<S> la = synMap.get(a), lb = synMap.get(b);
6  
    if (la == null && lb == null) {
7  
      L<S> l = litlist(a, b);
8  
      synMap.put(a, l);
9  
      synMap.put(b, l);
10  
    } else if (la == lb) {
11  
      // nothing to do, they're already synonyms
12  
    } else if (lb == null) {
13  
      la.add(b);
14  
      synMap.put(b, la);
15  
    } else if (la == null) {
16  
      lb.add(a);
17  
      synMap.put(a, lb);
18  
    } else
19  
      mergeLists(la, lb);
20  
  }
21  
  
22  
  void mergeLists(L<S> la, L<S> lb) {
23  
    for (S b : lb) {
24  
      la.add(b);
25  
      synMap.put(b, la);
26  
    }
27  
  }
28  
  
29  
  // returns null or a list
30  
  L<S> getSynonyms(S x) {
31  
    ret synMap.get(x);
32  
  }
33  
}

Author comment

Began life as a copy of #1001511

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: #1001512
Snippet name: Synonyms (class)
Eternal ID of this version: #1001512/1
Text MD5: b8c64d583a65db814d05b01a5de3c85a
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-21 16:34:24
Source code size: 758 bytes / 33 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 619 / 792
Referenced in: [show references]