View source with raw comments or as raw
    1/*  Part of CHR (Constraint Handling Rules)
    2
    3    Author:        Tom Schrijvers
    4    E-mail:        Tom.Schrijvers@cs.kuleuven.be
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2004-2011, K.U. Leuven
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   36% Binomial Heap imlementation based on
   37%
   38%	Functional Binomial Queues
   39%	James F. King
   40%	University of Glasgow
   41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   42
   43:- module(binomialheap,
   44	[
   45		empty_q/1,
   46		insert_q/3,
   47		insert_list_q/3,
   48		delete_min_q/3,
   49		find_min_q/2
   50	]).   51
   52:- use_module(library(lists),[reverse/2]).   53
   54% data Tree a = Node a [Tree a]
   55% type BinQueue a = [Maybe (Tree a)]
   56% data Maybe a = Zero | One a
   57% type Item = (Entry,Key)
   58
   59key(_-Key,Key).
   60
   61empty_q([]).
   62
   63meld_q(P,Q,R) :-
   64	meld_qc(P,Q,zero,R).
   65
   66meld_qc([],Q,zero,Q) :- !.
   67meld_qc([],Q,C,R) :- !,
   68	meld_q(Q,[C],R).
   69meld_qc(P,[],C,R) :- !,
   70	meld_qc([],P,C,R).
   71meld_qc([zero|Ps],[zero|Qs],C,R) :- !,
   72	R = [C | Rs],
   73	meld_q(Ps,Qs,Rs).
   74meld_qc([one(node(X,Xs))|Ps],[one(node(Y,Ys))|Qs],C,R) :- !,
   75	key(X,KX),
   76	key(Y,KY),
   77	( KX < KY ->
   78		T = node(X,[node(Y,Ys)|Xs])
   79	;
   80		T = node(Y,[node(X,Xs)|Ys])
   81	),
   82	R = [C|Rs],
   83	meld_qc(Ps,Qs,one(T),Rs).
   84meld_qc([P|Ps],[Q|Qs],C,Rs) :-
   85	meld_qc([Q|Ps],[C|Qs],P,Rs).
   86
   87insert_q(Q,I,NQ) :-
   88	meld_q([one(node(I,[]))],Q,NQ).
   89
   90insert_list_q([],Q,Q).
   91insert_list_q([I|Is],Q,NQ) :-
   92	insert_q(Q,I,Q1),
   93	insert_list_q(Is,Q1,NQ).
   94
   95min_tree([T|Ts],MT) :-
   96	min_tree_acc(Ts,T,MT).
   97
   98min_tree_acc([],MT,MT).
   99min_tree_acc([T|Ts],Acc,MT) :-
  100	least(T,Acc,NAcc),
  101	min_tree_acc(Ts,NAcc,MT).
  102
  103least(zero,T,T) :- !.
  104least(T,zero,T) :- !.
  105least(one(node(X,Xs)),one(node(Y,Ys)),T) :-
  106	key(X,KX),
  107	key(Y,KY),
  108	( KX < KY ->
  109		T = one(node(X,Xs))
  110	;
  111		T = one(node(Y,Ys))
  112	).
  113
  114remove_tree([],_,[]).
  115remove_tree([T|Ts],I,[NT|NTs]) :-
  116	( T == zero ->
  117		NT = T
  118	;
  119		T = one(node(X,_)),
  120		( X == I ->
  121			NT = zero
  122		;
  123			NT = T
  124		)
  125	),
  126	remove_tree(Ts,I,NTs).
  127
  128delete_min_q(Q,NQ,Min) :-
  129	min_tree(Q,one(node(Min,Ts))),
  130	remove_tree(Q,Min,Q1),
  131	reverse(Ts,RTs),
  132	make_ones(RTs,Q2),
  133	meld_q(Q2,Q1,NQ).
  134
  135make_ones([],[]).
  136make_ones([N|Ns],[one(N)|RQ]) :-
  137	make_ones(Ns,RQ).
  138
  139find_min_q(Q,I) :-
  140	min_tree(Q,one(node(I,_)))