From 88a53f71eff680249473fe7111157dba0eeec5d8 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 23 Apr 2015 22:00:03 -0400 Subject: [PATCH] lib/counter: introduce Counter::dec Signed-off-by: Alexandre Terrasa --- lib/counter.nit | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/counter.nit b/lib/counter.nit index 6545b73..6d5729d 100644 --- a/lib/counter.nit +++ b/lib/counter.nit @@ -93,6 +93,22 @@ class Counter[E] for e in es do inc(e) end + # Decrement the value of `e` by 1 + fun dec(e: E) do + if not has_key(e) then + self.map[e] = 0 + else + self.map[e] = self[e] - 1 + sum += - 1 + end + end + + # Decrement the value for each element of `es` + fun dec_all(es: Collection[E]) + do + for e in es do dec(e) + end + # A new Counter initialized with `inc_all`. init from(es: Collection[E]) do -- 1.7.9.5