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

39
LINES

< > BotCompany Repo | #1031664 // ManagedStringPool (is itself not persistent)

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

Uses 11335K of libraries. Click here for Pure Java version (5385L/33K).

1  
sclass ManagedStringPool {
2  
  replace Addr with int.
3  
  
4  
  ManagedIntObjects_v1 mem;
5  
  
6  
  *(ManagedIntObjects_v1 *mem) {}
7  
  
8  
  sclass Entry {
9  
    Addr addr;
10  
    int references;
11  
    
12  
    *(Addr *addr, int *references) {}
13  
  }
14  
  
15  
  new Map<S, Entry> map;
16  
  
17  
  // stores a string (creates if necessary and registers a reference)
18  
  Addr store(S s) {
19  
    Entry e = map.get(s);
20  
    if (e == null)
21  
      e = new Entry(mem.newString(s), 0);
22  
    e.references++;
23  
    ret e.addr;
24  
  }
25  
  
26  
  // just queries
27  
  Addr peek(S s) {
28  
    Entry e = map.get(s);
29  
    ret e == null ? 0 : e.addr;
30  
  }
31  
  
32  
  // remove a reference
33  
  void unstore(S s) {
34  
    Entry e = map.get(s);
35  
    if (e == null) fail("ManagedStringPool user error");
36  
    if (--e.references == 0)
37  
      map.remove(s);
38  
  }
39  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031664
Snippet name: ManagedStringPool (is itself not persistent)
Eternal ID of this version: #1031664/3
Text MD5: 6aac11b847bf90ddce95dfff9ba5de3b
Transpilation MD5: 4fd313d5043749c8b048e8ee4be04ffb
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-25 09:17:46
Source code size: 797 bytes / 39 lines
Pitched / IR pitched: No / No
Views / Downloads: 194 / 273
Version history: 2 change(s)
Referenced in: [show references]