1 /** 2 Utility class for keeping a count of items. 3 */ 4 module dcord.util.counter; 5 6 import std.algorithm; 7 8 class Counter(T) { 9 uint total; 10 uint[T] storage; 11 12 uint get(T v) { 13 return this.storage[v]; 14 } 15 16 void tick(T v) { 17 this.total += 1; 18 this.storage[v] += 1; 19 } 20 21 void reset(T v) { 22 this.total -= this.storage[v]; 23 this.storage[v] = 0; 24 } 25 26 void resetAll() { 27 foreach (ref k; this.storage.keys) { 28 this.reset(k); 29 } 30 this.total = 0; 31 } 32 33 auto mostCommon(uint limit) { 34 auto res = schwartzSort!(k => this.storage[k], "a > b")(this.storage.keys); 35 if (res.length > limit) { 36 return res[0..limit]; 37 } else { 38 return res; 39 } 40 } 41 }