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

71
LINES

< > BotCompany Repo | #1031058 // SortedArrayBasedMap - map based on a sorted list [O(log n) access, immutable]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3313L/19K).

1  
sclass SortedArrayBasedMap<A, B> extends CompactAbstractMap<A, B> {
2  
  O[] entries; // key, value, ... with keys being sorted
3  
  
4  
  *() {}
5  
  *(Map<A, B> map) {
6  
    LPair<A, B> l = pairsSortedByA(mapToPairs(map));
7  
    int n = l(l);
8  
    entries = new O[n*2];
9  
    for i to n: {
10  
      Pair<A, B> p = l.get(i);
11  
      entries[i*2] = p.a;
12  
      entries[i*2+1] = p.b;
13  
    }
14  
  }
15  
  
16  
  public int size() { ret l(entries)/2; }
17  
  public Set<Map.Entry<A, B>> entrySet() {
18  
    ret new AbstractSet<Map.Entry<A, B>>() {
19  
      public int size() { ret SortedArrayBasedMap.this.size(); }
20  
      
21  
      public Iterator<Map.Entry<A, B>> iterator() {
22  
        ret new Iterator<Map.Entry<A, B>>() {
23  
          int idx;
24  
          
25  
          public bool hasNext() {
26  
            ret idx < l(entries);
27  
          }
28  
          
29  
          public Map.Entry<A, B> next() {
30  
            SimpleEntry<A, B> e = new SimpleEntry<A, B>((A) entries[idx], (B) entries[idx+1]);
31  
            idx += 2;
32  
            ret e;
33  
          }
34  
        };
35  
      }
36  
    };
37  
  }
38  
  
39  
  int find(O key) {
40  
    ret find(0, size(), key);
41  
  }
42  
  
43  
  int find(int fromIndex, int toIndex, O key) {
44  
    int low = fromIndex;
45  
    int high = toIndex - 1;
46  
47  
    while (low <= high) {
48  
      int mid = (low + high) >>> 1;
49  
      O midVal = entries[mid*2];
50  
  
51  
      int c = cmp(midVal, key);
52  
      if (c < 0)
53  
        low = mid + 1;
54  
      else if (c > 0)
55  
        high = mid - 1;
56  
      else
57  
        ret mid; // key found
58  
    }
59  
    ret -(low + 1);  // key not found.
60  
  }
61  
  
62  
  public bool containsKey(O o) {
63  
    ret find(o) >= 0;
64  
  }
65  
  
66  
  @Override
67  
  public B get(O o) {
68  
    int i = find(o)*2;
69  
    ret i >= 0 ? (B) entries[i+1] : null;
70  
  }
71  
}

Author comment

Began life as a copy of #1031042

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031058
Snippet name: SortedArrayBasedMap - map based on a sorted list [O(log n) access, immutable]
Eternal ID of this version: #1031058/10
Text MD5: b86e7d5186c157a8df0bfc97f6320311
Transpilation MD5: 5d61e61408dbf6f6ff3366f7303fb88b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-04-26 17:06:28
Source code size: 1719 bytes / 71 lines
Pitched / IR pitched: No / No
Views / Downloads: 133 / 642
Version history: 9 change(s)
Referenced in: [show references]