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

51
LINES

< > BotCompany Repo | #1031511 // test_ProbabilisticList (TODO: reflect new policy of replacing elements)

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

Transpiled version (5483L) is out of date.

sclass Test_ProbabilisticList {
  MultiSetMap<Double, S> a = multiSetMap_outerDescTreeMap_innerBetterLinkedHashSet();
  new ProbabilisticList<S> b;

  int nProbabilities = 4;
  int nObjects = 10;
  int n = 1000;
  
  WithProbability<S> makeEntry() {
    ret withProbability(
      (double) random(nProbabilities),
      str(charPlus('A', random(nObjects)));
  }
  
  run {
    repeat n {
      // add one
      WithProbability<S> e = makeEntry();
      print("Adding " + e);
      
      if (e.probability() > 0) // simulate default cut-off at probability 0
      // Simulate the right behavior for adding same object again
        if (!a.contains(e.probability(), e!)) {
          a.remove(a.keyForValue(e!), e!);
            a.put(e.probability(), e!);
        }
      
      b.add(e); // automatically overwrites the old probability
      
      check();
      
      // remove one
      e = makeEntry(); // probability doesn't matter
      print("Removing " + e!);
      a.remove(a.keyForValue(e!), e!);
      b.remove(e);
      
      check();
    }
  }
    
  void check {
    LPair<Double, S> aEntries = asList(a.allEntries());
    print(takeFirst(10, aEntries);
    assertEquals(mapPairsToList(aEntries, (p, x) -> WithProbability(p, x)), asList(b.iterator());
  }
}

svoid test_ProbabilisticList() {
  new Test_ProbabilisticList().run();
}

Author comment

Began life as a copy of #1031508

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1031511
Snippet name: test_ProbabilisticList (TODO: reflect new policy of replacing elements)
Eternal ID of this version: #1031511/18
Text MD5: 4fa1fb796d7fa73135f2443617f0f99f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-30 16:40:21
Source code size: 1397 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 119 / 241
Version history: 17 change(s)
Referenced in: [show references]