Libraryless. Click here for Pure Java version (3354L/19K).
1  | // you can arrange the entries in a self-chosen order  | 
2  | // so it's like a very compact LinkedHashMap  | 
3  | // Only good for very small maps  | 
4  | sclass SmallArrayBasedMap<A, B> extends CompactAbstractMap<A, B> {
 | 
5  | O[] entries; // key, value, ... (any order of entries)  | 
6  | |
7  |   *() {}
 | 
8  |   *(Map<A, B> map) {
 | 
9  | int n = l(map);  | 
10  | if (n == 0) ret;  | 
11  | entries = new O[n*2];  | 
12  | int i = 0;  | 
13  |     fOr (A key, B value : map) {
 | 
14  | entries[i++] = key;  | 
15  | entries[i++] = value;  | 
16  | }  | 
17  | }  | 
18  | |
19  |   public int size() { ret l(entries)/2; }
 | 
20  |   public Set<Map.Entry<A, B>> entrySet() {
 | 
21  |     ret new AbstractSet<Map.Entry<A, B>>() {
 | 
22  |       public int size() { ret SmallArrayBasedMap.this.size(); }
 | 
23  | |
24  |       public Iterator<Map.Entry<A, B>> iterator() {
 | 
25  |         ret new Iterator<Map.Entry<A, B>>() {
 | 
26  | int idx;  | 
27  | |
28  |           public bool hasNext() {
 | 
29  | ret idx < l(entries);  | 
30  | }  | 
31  | |
32  |           public Map.Entry<A, B> next() {
 | 
33  | SimpleEntry<A, B> e = new SimpleEntry<A, B>((A) entries[idx], (B) entries[idx+1]);  | 
34  | idx += 2;  | 
35  | ret e;  | 
36  | }  | 
37  | };  | 
38  | }  | 
39  | };  | 
40  | }  | 
41  | |
42  | // returns actual index in entries array  | 
43  |   int find(O key) {
 | 
44  | int n = l(entries);  | 
45  | //if (n == 0) ret -1;  | 
46  | //int hash = _hashCode(key);  | 
47  | for (int i = 0; i < n; i += 2)  | 
48  | //if (_hashCode(entries[i]) == hash  | 
49  | if (eq(key, entries[i]))  | 
50  | ret i;  | 
51  | ret -1;  | 
52  | }  | 
53  | |
54  |   public bool containsKey(O o) {
 | 
55  | ret find(o) >= 0;  | 
56  | }  | 
57  | |
58  | @Override  | 
59  |   public B get(O o) {
 | 
60  | int i = find(o);  | 
61  | ret i >= 0 ? (B) entries[i+1] : null;  | 
62  | }  | 
63  | }  | 
Began life as a copy of #1031058
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1031784 | 
| Snippet name: | SmallArrayBasedMap - map based on an array in any order [slow! O(n) access. Use ony for small maps. immutable] | 
| Eternal ID of this version: | #1031784/1 | 
| Text MD5: | 51e1dfec40d32a1b6f94124d30fcf519 | 
| Transpilation MD5: | d182fa19d47e5825428b326c8f5caf01 | 
| Author: | stefan | 
| Category: | javax | 
| Type: | JavaX fragment (include) | 
| Public (visible to everyone): | Yes | 
| Archived (hidden from active list): | No | 
| Created/modified: | 2021-06-29 18:43:22 | 
| Source code size: | 1630 bytes / 63 lines | 
| Pitched / IR pitched: | No / No | 
| Views / Downloads: | 499 / 683 | 
| Referenced in: | [show references] |