Libraryless. Click here for Pure Java version (3408L/21K).
1 | sclass StringClustersWithIDs { |
2 | new L<Cluster> clusters; |
3 | |
4 | !include #1025301 // change()/onChange() |
5 | |
6 | sclass Cluster { |
7 | GlobalID globalID = aGlobalIDObject(); |
8 | Set<S> synonyms = ciSet(); |
9 | |
10 | *() {} |
11 | *(S s) { synonyms.add(s); } |
12 | |
13 | toString { ret "Cluster " + globalID + ": " + synonyms; } |
14 | |
15 | bool add(S s) { |
16 | ret synonyms.add(s); |
17 | } |
18 | } |
19 | |
20 | L<Cluster> searchForCluster(S s) { |
21 | ret filter(clusters, c -> contains(c.synonyms, s)); |
22 | } |
23 | |
24 | Cluster clusterWith(S s) { |
25 | ret first(searchForCluster(s)); |
26 | } |
27 | |
28 | void add(Cluster cluster) { |
29 | clusters.add(cluster); |
30 | change(); |
31 | } |
32 | |
33 | void remove(Cluster cluster) { |
34 | clusters.remove(cluster); |
35 | change(); |
36 | } |
37 | |
38 | // also adds to list |
39 | Cluster newCluster(S s) { |
40 | Cluster c = new(s); |
41 | add(c); |
42 | ret c; |
43 | } |
44 | |
45 | // also adds to list |
46 | Cluster newCluster(Cl<S> synonyms) { |
47 | new Cluster c; |
48 | addAll(c.synonyms, synonyms); |
49 | add(c); |
50 | ret c; |
51 | } |
52 | |
53 | // also merges clusters |
54 | void addPair(S a, S b) { |
55 | Cluster c1 = clusterWith(a), c2 = clusterWith(b); |
56 | if (c1 != null) |
57 | if (c2 != null) |
58 | mergeClusters(c1, c2); |
59 | else { c1.add(b); change(); } |
60 | else |
61 | if (c2 != null) { c2.add(a); change(); } |
62 | else |
63 | newCluster(ll(a, b)); |
64 | } |
65 | |
66 | void mergeClusters(Cluster a, Cluster b) { |
67 | if (a == b) ret; |
68 | a.synonyms.addAll(b.synonyms); |
69 | remove(b); |
70 | } |
71 | |
72 | // either return s or all synonyms from the first cluster containing s |
73 | Set<S> extend(S s) { |
74 | Cluster c = clusterWith(s); |
75 | ret c == null ? litciset(s) : c.synonyms; |
76 | } |
77 | |
78 | int totalCount() { |
79 | ret intSum(map(clusters, c -> l(c.synonyms))); |
80 | } |
81 | |
82 | bool eqicOrInSameCluster(S a, S b) { |
83 | if (eqic(a, b)) true; |
84 | if (a == null || b == null) false; |
85 | for (Cluster c : clusters) |
86 | if (contains(c.synonyms, a) && contains(c.synonyms, b)) |
87 | true; |
88 | false; |
89 | } |
90 | } |
Began life as a copy of #1025290
download show line numbers debug dex old transpilations
Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1025302 |
Snippet name: | StringClustersWithIDs |
Eternal ID of this version: | #1025302/18 |
Text MD5: | 934f5ea6e9be14f3ccc6d5e7c685e536 |
Transpilation MD5: | 2242325a8c27f02a0c69c2c0db998cdd |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-10-26 02:13:25 |
Source code size: | 1979 bytes / 90 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 352 / 815 |
Version history: | 17 change(s) |
Referenced in: | [show references] |