| 1 | % (c) 2009-2019 Lehrstuhl fuer Softwaretechnik und Programmiersprachen, | |
| 2 | % Heinrich Heine Universitaet Duesseldorf | |
| 3 | % This software is licenced under EPL 1.0 (http://www.eclipse.org/org/documents/epl-v10.html) | |
| 4 | ||
| 5 | ||
| 6 | :- module(tools_lists, | |
| 7 | [count_occurences/2, | |
| 8 | ord_member_nonvar_chk/2 | |
| 9 | ]). | |
| 10 | ||
| 11 | :- use_module(module_information). | |
| 12 | ||
| 13 | :- module_info(group,infrastructure). | |
| 14 | :- module_info(description,'A few utilities on lists seperated out from tools.pl to avoid cyclic module dependencies.'). | |
| 15 | ||
| 16 | ||
| 17 | % self-check in tools to avoid dependency on self_check module | |
| 18 | %:- assert_must_succeed((tools:count_occurences([a,b,a,a,b],R),R == [a-3,b-2])). | |
| 19 | :- use_module(library(avl), [avl_fetch/3, avl_to_list/2, avl_store/4,empty_avl/1]). | |
| 20 | ||
| 21 | % count number of occurences inside a list | |
| 22 | count_occurences(L,R) :- empty_avl(E), count_occ_aux(L,E,R). | |
| 23 | count_occ_aux([Term|T],A,Res) :- (avl_fetch(Term,A,Count) -> C is Count+1 ; C = 1), | |
| 24 | avl_store(Term,A,C,A2), | |
| 25 | count_occ_aux(T,A2,Res). | |
| 26 | count_occ_aux([],A,L) :- avl_to_list(A,L). | |
| 27 | ||
| 28 | % like ord_member but also allows nonvar lookup terms | |
| 29 | % note that this test fails for ord_member: | |
| 30 | %:- assert_must_succeed((tools:ord_member_nonvar_chk(p(b,X),[p(a,c),p(b,d)]),R == d)). | |
| 31 | ord_member_nonvar_chk(X, [H|T]) :- | |
| 32 | (X=H -> true | |
| 33 | ; X @>H -> ord_member_nonvar_chk(X,T)). |