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

21
LINES

< > BotCompany Repo | #1032122 // CalculateEachElementOnce

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

Libraryless. Click here for Pure Java version (4478L/26K).

1  
// TODO: it isn't actually multithreaded at all
2  
sclass CalculateEachElementOnce<A, B> {
3  
  Map<A, Flag> calculating = syncMap();
4  
  Map<A, OKOrError<B>> calculated = syncMap();
5  
  
6  
  synchronized B get(A a, IF1<A, B> f) {
7  
    Flag flag = calculating.get(a);
8  
    if (flag != null) {
9  
      flag.waitUntilUp();
10  
      ret unpackOKOrError(calculated.get(a));
11  
    } else {
12  
      flag = new Flag;
13  
      calculating.put(a, flag);
14  
      OKOrError<B> b = okOrError(() -> f.get(a));
15  
      calculated.put(a, b);
16  
      calculating.remove(a);
17  
      flag.raise();
18  
      ret b!;
19  
    }
20  
  }
21  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1032122
Snippet name: CalculateEachElementOnce
Eternal ID of this version: #1032122/7
Text MD5: 63af94d1bcb880ad6923f1c1a977bb6c
Transpilation MD5: 1696c8636c54a28d9fb2abc5b957a0d1
Author: stefan
Category: javax / gazelles
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-14 03:10:53
Source code size: 591 bytes / 21 lines
Pitched / IR pitched: No / No
Views / Downloads: 171 / 314
Version history: 6 change(s)
Referenced in: [show references]