LCOV - code coverage report
Current view: top level - globalscripts - WeightedList.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 4 20 20.0 %
Date: 2023-04-02 12:52:40 Functions: 0 5 0.0 %

          Line data    Source code
       1             : function WeightedList()
       2             : {
       3           0 :         this.elements = new Map();
       4           0 :         this.totalWeight = 0;
       5             : };
       6             : 
       7          72 : WeightedList.prototype.length = function()
       8             : {
       9           0 :         return this.elements.size;
      10             : };
      11             : 
      12          72 : WeightedList.prototype.push = function(item, weight = 1)
      13             : {
      14           0 :         this.elements.set(item, weight);
      15           0 :         this.totalWeight += weight;
      16             : };
      17             : 
      18          72 : WeightedList.prototype.remove = function(item)
      19             : {
      20           0 :         const weight = this.elements.get(item);
      21           0 :         if (weight)
      22           0 :                 this.totalWeight -= weight;
      23           0 :         this.elements.delete(item);
      24             : };
      25             : 
      26          72 : WeightedList.prototype.randomItem = function()
      27             : {
      28           0 :         const targetWeight = randFloat(0, this.totalWeight);
      29           0 :         let cumulativeWeight = 0;
      30           0 :         for (let [item, weight] of this.elements)
      31             :         {
      32           0 :                 cumulativeWeight += weight;
      33           0 :                 if (cumulativeWeight >= targetWeight)
      34           0 :                         return item;
      35             :         }
      36           0 :         return undefined;
      37             : };

Generated by: LCOV version 1.14