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

27
LINES

< > BotCompany Repo | #1031861 // BigLittleMapPolicy - uses two different map classes for small and big maps respectively

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

Libraryless. Click here for Pure Java version (3167L/18K).

// threshold must be at least 2
// not synchronized
// untested
srecord BigLittleMapPolicy(Class<? extends Map> smallClass, Class<? extends Map> bigClass, int threshold) implements IMapImplementationPolicy {
  bool nullOnEmpty = false;
  
  public <A, B> Map<A, B> makeSingletonMap(A key, B value) {
    ret put(null, key, value);
  }
  
  public <A, B> Map<A, B> put(Map<A, B> map, A key, B value) {
    if (map == null) map = newInstance(smallClass);
    if (l(map) == threshold-1 && !map.containsKey(key))
      map = putAll(newInstance(bigClass), map);
    map.put(key, value);
    ret map;
  }
  
  public <A, B> Map<A, B> remove(Map<A, B> map, O key) {
    if (map == null || !map.containsKey(key)) ret map;
    map.remove(key);
    int n = l(map);
    if (n == threshold-1)
      map = putAll(newInstance(smallClass), map);
    ret nullOnEmpty && n == 0 ? null : map;
  }
}

Author comment

Began life as a copy of #1031860

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031861
Snippet name: BigLittleMapPolicy - uses two different map classes for small and big maps respectively
Eternal ID of this version: #1031861/6
Text MD5: 30961b7883a186ca8bca57c435a31b32
Transpilation MD5: dcf698d285a16ad354b3a5be4c5766d3
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-16 07:08:24
Source code size: 906 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 149 / 283
Version history: 5 change(s)
Referenced in: [show references]