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).

// TODO: it isn't actually multithreaded at all
sclass CalculateEachElementOnce<A, B> {
  Map<A, Flag> calculating = syncMap();
  Map<A, OKOrError<B>> calculated = syncMap();
  
  synchronized B get(A a, IF1<A, B> f) {
    Flag flag = calculating.get(a);
    if (flag != null) {
      flag.waitUntilUp();
      ret unpackOKOrError(calculated.get(a));
    } else {
      flag = new Flag;
      calculating.put(a, flag);
      OKOrError<B> b = okOrError(() -> f.get(a));
      calculated.put(a, b);
      calculating.remove(a);
      flag.raise();
      ret b!;
    }
  }
}

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: 98 / 212
Version history: 6 change(s)
Referenced in: [show references]