-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistRND.min.js
6 lines (6 loc) · 1.29 KB
/
DistRND.min.js
1
2
3
4
5
6
/*!
* DistRND.js v1.0
* Copyright (c) 2019 Lukas Dachtler
* Licensed under MIT (https://github.com/lukas-dachtler/DistRND.js/blob/master/LICENSE.md)
*/
function DistRND(i,u){var e,r,a=2<arguments.length&&void 0!==arguments[2]?arguments[2]:0,c=new DistRND_DataStructure,v=new DistRND_DataStructure,o=u-i+1;function t(){for(var t=i;t<=u;t++)c.add(t,0);r=e=0}function d(t,n){c.remove(t),v.add(t,n)}function f(t,n){v.remove(t),c.add(t,n)}t(),this.next=function(){e++;var t=c.getRandom();c.increase(t.value),t.count+1>r+a&&d(t.value,t.count+1);var n=Math.floor(e/o);return r<n&&(r=n,function(){for(var t=i;t<=u;t++)if(0===a){var n=v.getCount(t);f(t,n)}else{var e=c.getCount(t);if(null!=e)r+a<e&&d(t,e);else{var o=v.getCount(t);o<=r+a&&f(t,o)}}}()),t.value},this.reset=function(){c.clear(),v.clear(),t()}}function DistRND_DataStructure(){var o=[],i={};this.add=function(t,n){o.push(t),i[t]={index:o.length-1,count:n}},this.remove=function(t){var n=i[t];if(void 0!==n){i[t]=void 0;var e=o.length-1;n.index!==e?(o[n.index]=o[e],o.pop(),i[o[n.index]].index=n.index):o.pop()}},this.increase=function(t){i[t].count++},this.getCount=function(t){return void 0!==i[t]?i[t].count:null},this.getRandom=function(){var t=o[Math.floor(Math.random()*o.length)];return{value:t,count:i[t].count}},this.clear=function(){o=[],i={}}}