mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-18 15:26:31 +00:00
Added to_list, put_on_front, put_list_on_front, and get_from_back.
Estimated hours taken: 0.75 Branches: main library/queue.m: library/svqueue.m: Added to_list, put_on_front, put_list_on_front, and get_from_back. Added svqueue.put_on_front, svqueue.put_list_on_front, and svqueue.get_from_back. Cosmetic changes to comments in svqueue.m. NEWS: Mention the new additions.
This commit is contained in:
7
NEWS
7
NEWS
@@ -130,7 +130,12 @@ Changes to the Mercury language:
|
||||
Changes to the Mercury standard library:
|
||||
|
||||
* We've add the function queue.from_list/1 as a synonym for
|
||||
queue.list_to_queue/1.
|
||||
queue.list_to_queue/1, function queue.to_list/1 (and predicate
|
||||
queue.to_list/2) as the converse of queue.from_list/1, queue.put_on_front/2
|
||||
(and predicate queue.put_on_front/3) to put items on to the front of a
|
||||
queue, queue.put_list_on_front/2 (and predicate queue.put_list_on_front/3)
|
||||
to put a list of items on to the front of a queue, and predicate
|
||||
queue.get_from_back/3 which removes the last element from a queue.
|
||||
|
||||
* We've added the function pqueue.from_assoc_list/1 as a synonym
|
||||
for pqueue.assoc_list_to_pqueue/1.
|
||||
|
||||
@@ -88,6 +88,10 @@
|
||||
%
|
||||
:- func queue__from_list(list(T)) = queue(T).
|
||||
|
||||
% `queue__to_list(Queue) = List' is the inverse of queue__from_list/1.
|
||||
%
|
||||
:- func queue__to_list(queue(T)) = list(T).
|
||||
|
||||
% `queue__delete_all(Queue0, Elem, Queue)' is true iff `Queue' is
|
||||
% the same queue as `Queue0' with all occurrences of `Elem' removed
|
||||
% from it.
|
||||
@@ -95,6 +99,25 @@
|
||||
:- pred queue__delete_all(queue(T)::in, T::in, queue(T)::out) is det.
|
||||
:- func queue__delete_all(queue(T), T) = queue(T).
|
||||
|
||||
% `queue__put_on_front(Queue0, Elem) = Queue' pushes `Elem' on to
|
||||
% the front of `Queue0', giving `Queue'.
|
||||
%
|
||||
:- func queue__put_on_front(queue(T), T) = queue(T).
|
||||
:- pred queue__put_on_front(queue(T)::in, T::in, queue(T)::out) is det.
|
||||
|
||||
% `queue__put_list_on_front(Queue0, Elems) = Queue' pushes `Elems'
|
||||
% on to the front of `Queue0', giving `Queue' (the Nth member
|
||||
% of `Elems' becomes the Nth member from the front of `Queue').
|
||||
%
|
||||
:- func queue__put_list_on_front(queue(T), list(T)) = queue(T).
|
||||
:- pred queue__put_list_on_front(queue(T)::in, list(T)::in, queue(T)::out)
|
||||
is det.
|
||||
|
||||
% `queue__get_from_back(Queue0, Elem, Queue)' removes `Elem' from
|
||||
% the back of `Queue0', giving `Queue'.
|
||||
%
|
||||
:- pred queue__get_from_back(queue(T)::in, T::out, queue(T)::out) is semidet.
|
||||
|
||||
%--------------------------------------------------------------------------%
|
||||
%--------------------------------------------------------------------------%
|
||||
|
||||
@@ -166,6 +189,8 @@ queue__list_to_queue(List, [] - List).
|
||||
|
||||
queue__from_list(List) = [] - List.
|
||||
|
||||
queue__to_list(On - Off) = Off ++ list__reverse(On).
|
||||
|
||||
queue__delete_all(On0 - Off0, Elem, On - Off) :-
|
||||
list__delete_all(On0, Elem, On1),
|
||||
list__delete_all(Off0, Elem, Off1),
|
||||
@@ -177,6 +202,45 @@ queue__delete_all(On0 - Off0, Elem, On - Off) :-
|
||||
Off = Off1
|
||||
).
|
||||
|
||||
queue__put_on_front(On - Off, Elem, On - [Elem | Off]).
|
||||
|
||||
queue__put_on_front(Queue0, Elem) = Queue :-
|
||||
queue__put_on_front(Queue0, Elem, Queue).
|
||||
|
||||
queue__put_list_on_front(On - Off, Elems, On - (Elems ++ Off)).
|
||||
|
||||
queue__put_list_on_front(Queue0, Elems) = Queue :-
|
||||
queue__put_list_on_front(Queue0, Elems, Queue).
|
||||
|
||||
queue__get_from_back(On0 - Off0, Elem, On - Off) :-
|
||||
(
|
||||
% The On list is non-empty and the last element
|
||||
% in the queue is the head of the On list.
|
||||
%
|
||||
On0 = [Elem | On],
|
||||
Off = Off0
|
||||
;
|
||||
% The On list is empty.
|
||||
%
|
||||
On0 = [],
|
||||
(
|
||||
% The Off list contains a single element.
|
||||
%
|
||||
Off0 = [Elem],
|
||||
On = [],
|
||||
Off = []
|
||||
;
|
||||
% The Off list contains two or more elements.
|
||||
% We split it in two and take the head of the
|
||||
% new On list as Elem.
|
||||
%
|
||||
Off0 = [_, _ | _],
|
||||
N = list__length(Off0),
|
||||
list__split_list(N / 2, Off0, Off, RevOn),
|
||||
[Elem | On] = list__reverse(RevOn)
|
||||
)
|
||||
).
|
||||
|
||||
%--------------------------------------------------------------------------%
|
||||
%--------------------------------------------------------------------------%
|
||||
% Ralph Becket <rwab1@cl.cam.ac.uk> 29/04/99
|
||||
|
||||
@@ -46,6 +46,23 @@
|
||||
%
|
||||
:- pred svqueue__delete_all(T::in, queue(T)::in, queue(T)::out) is det.
|
||||
|
||||
% `svqueue__put_on_front(Elem, Queue0, Queue)' pushes `Elem' on to
|
||||
% the front of `Queue0', giving `Queue'.
|
||||
%
|
||||
:- pred svqueue__put_on_front(T::in, queue(T)::in, queue(T)::out) is det.
|
||||
|
||||
% `svqueue__put_list_on_front(Queue0, Elems, Queue)' pushes `Elems'
|
||||
% on to the front of `Queue0', giving `Queue' (the Nth member
|
||||
% of `Elems' becomes the Nth member from the front of `Queue').
|
||||
%
|
||||
:- pred svqueue__put_list_on_front(list(T)::in, queue(T)::in, queue(T)::out)
|
||||
is det.
|
||||
|
||||
% `queue__get_from_back(Elem, Queue0, Queue)' removes `Elem' from
|
||||
% the back of `Queue0', giving `Queue'.
|
||||
%
|
||||
:- pred svqueue__get_from_back(T::out, queue(T)::in, queue(T)::out) is semidet.
|
||||
|
||||
%--------------------------------------------------------------------------%
|
||||
%--------------------------------------------------------------------------%
|
||||
|
||||
@@ -62,3 +79,13 @@ svqueue__get(Elem, Queue0, Queue) :-
|
||||
|
||||
svqueue__delete_all(Elem, Queue0, Queue) :-
|
||||
queue__delete_all(Queue0, Elem, Queue).
|
||||
|
||||
svqueue__put_on_front(Elem, Queue0, Queue) :-
|
||||
queue__put_on_front(Queue0, Elem, Queue).
|
||||
|
||||
svqueue__put_list_on_front(Elems, Queue0, Queue) :-
|
||||
queue__put_list_on_front(Queue0, Elems, Queue).
|
||||
|
||||
svqueue__get_from_back(Elem, Queue0, Queue) :-
|
||||
queue__get_from_back(Queue0, Elem, Queue).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user