A preliminary implementation of multisets in terms of maps from

bag.nl:
	A preliminary implementation of multisets in terms of maps from
	T to int. Not yet used anywhere, nor added to the makefile.
	Maybe we should split the makefile into library stuff and compiler
	stuff?
This commit is contained in:
Thomas Conway
1994-06-15 13:26:25 +00:00
parent d20aaf2a5b
commit 22a39de53f

87
library/bag.m Normal file
View File

@@ -0,0 +1,87 @@
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
%
% file: bag.nl
% An implementation of multisets.
% main author: conway.
%
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- module bag.
:- interface.
:- type bag(T).
:- pred bag__init(bag(T)).
:- mode bag__init(out) is det.
:- pred bag__insert(bag(T), T, bag(T)).
:- mode bag__insert(in, in, out) is det.
:- pred bag__remove(bag(T), T, bag(T)).
:- mode bag__remove(in, in, out) is det.
:- pred bag__remove_all(bag(T), T, bag(T)).
:- mode bag__remove_all(in, in, out) is det.
:- pred bag__contains(T, bag(T)).
:- mode bag__contains(in, in) is semidet.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module map, int.
:- type bag(T) == map(T, int).
%---------------------------------------------------------------------------%
bag__init(B) :-
map__init(B).
%---------------------------------------------------------------------------%
bag__insert(B0, I, B) :-
(
map__search(B0, I, C0)
->
C is C0 + 1
;
C = 1
),
map__set(B0, I, C, B).
%---------------------------------------------------------------------------%
bag__remove(B0, I, B) :-
(
map__search(B0, I, C0)
->
C is C0 - 1,
(
C > 0
->
map__set(B0, I, C, B)
;
map__delete(B0, I, B)
)
;
B = B0
).
/*
%---------------------------------------------------------------------------%
bag__remove_all(B0, I, B) :-
map__delete(B0, I, B).
%---------------------------------------------------------------------------%
bag__contains(I, B) :-
map__contains(B, I).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
*/