for review: add bag__count_value

Simon Taylor stayl at cs.mu.OZ.AU
Thu Dec 3 11:50:58 AEDT 1998


Estimated hours taken: 0.1

library/bag.m:
	Add a new predicate bag__count_value/3 which returns how
	many of a value a bag contains.

NEWS:
	Mention bag__count_value/3.


Index: bag.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/bag.m,v
retrieving revision 1.16
diff -u -t -u -r1.16 bag.m
--- bag.m	1998/03/10 05:40:21	1.16
+++ bag.m	1998/12/03 00:23:30
@@ -116,6 +116,11 @@
 :- pred bag__contains(bag(T), T).
 :- mode bag__contains(in, in) is semidet.
 
+        % Count how many occurrences of the value the bag contains.
+        %
+:- pred bag__count_value(bag(T), T, int).
+:- mode bag__count_value(in, in, out) is det.
+
         % bag__subtract(Bag0, SubBag, Bag)
         % subtracts SubBag from Bag0 to produce Bag
         % each element in SubBag is removed from Bag0 to produce Bag.
@@ -302,6 +307,15 @@
 
 bag__contains(Bag, Item) :-
         map__contains(Bag, Item).
+
+%---------------------------------------------------------------------------%
+
+bag__count_value(Bag, Item, Count) :-
+        ( map__search(Bag, Item, Count0) ->
+                Count = Count0
+        ;
+                Count = 0
+        ).
 
 %---------------------------------------------------------------------------%
 


Index: NEWS
===================================================================
NEWS since Mercury release 0.8:
-------------------------------

* We've added a new predicate to the Mercury standard library:
	bag__count_value/3.	



More information about the developers mailing list