Attached Files  new_queue_functions.patch [^] (1,999 bytes) 20130730 12:48 [Show Content] [Hide Content]From 5126c5b64cd78ef6e32571c0d3ae31d59ef9d6e8 Mon Sep 17 00:00:00 2001
From: nickgian <nick.giannarakis@gmail.com>
Date: Tue, 30 Jul 2013 13:42:16 +0300
Subject: [PATCH] Added Queue.to_list and Queue.rev_iter

stdlib/queue.ml  12 ++++++++++++
stdlib/queue.mli  9 +++++++++
2 files changed, 21 insertions(+)
diff git a/stdlib/queue.ml b/stdlib/queue.ml
index fb920d8..765e7ba 100644
 a/stdlib/queue.ml
+++ b/stdlib/queue.ml
@@ 136,6 +136,15 @@ let iter f q =
iter cell.next in
iter tail.next
+let rev_iter f q =
+ if q.length > 0 then
+ let tail = q.tail in
+ let rec iter cell =
+ if cell != tail then
+ iter cell.next;
+ f cell.content in
+ iter tail.next
+
let fold f accu q =
if q.length = 0 then
accu
@@ 163,3 +172,6 @@ let transfer q1 q2 =
end;
q2.length < q2.length + length1;
q2.tail < tail1
+
+let to_list q =
+ List.rev (fold (fun acc x > x :: acc) [] q)
diff git a/stdlib/queue.mli b/stdlib/queue.mli
index 3542712..ebea3c4 100644
 a/stdlib/queue.mli
+++ b/stdlib/queue.mli
@@ 64,6 +64,11 @@ val iter : ('a > unit) > 'a t > unit
from the least recently entered to the most recently entered.
The queue itself is unchanged. *)
+val rev_iter : ('a > unit) > 'a t > unit
+(** [rev_iter f q] applies [f] in turn to all elements of [q],
+ from the most recently entered to the least recently entered.
+ The queue itself is unchanged. Not tailrecursive. *)
+
val fold : ('b > 'a > 'b) > 'b > 'a t > 'b
(** [fold f accu q] is equivalent to [List.fold_left f accu l],
where [l] is the list of [q]'s elements. The queue remains
@@ 74,3 +79,7 @@ val transfer : 'a t > 'a t > unit
the queue [q2], then clears [q1]. It is equivalent to the
sequence [iter (fun x > add x q2) q1; clear q1], but runs
in constant time. *)
+
+val to_list : 'a t > 'a list
+(** [Queue.to_list q] returns the list of all the elements of [q]. *)
+

1.7.12.4 (Apple Git37)
