1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
2012-10-17 14:01:34 -07:00
|
|
|
(* Francois Pottier, projet Cristal, INRIA Rocquencourt *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
2002-05-06 05:11:08 -07:00
|
|
|
(* Copyright 2002 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
exception Empty
|
|
|
|
|
2011-12-21 07:37:54 -08:00
|
|
|
(* OCaml currently does not allow the components of a sum type to be
|
2002-05-06 05:11:08 -07:00
|
|
|
mutable. Yet, for optimal space efficiency, we must have cons cells
|
|
|
|
whose [next] field is mutable. This leads us to define a type of
|
|
|
|
cyclic lists, so as to eliminate the [Nil] case and the sum
|
|
|
|
type. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-05-06 05:11:08 -07:00
|
|
|
type 'a cell = {
|
|
|
|
content: 'a;
|
|
|
|
mutable next: 'a cell
|
2005-08-26 05:10:47 -07:00
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-05-06 05:11:08 -07:00
|
|
|
(* A queue is a reference to either nothing or some cell of a cyclic
|
|
|
|
list. By convention, that cell is to be viewed as the last cell in
|
|
|
|
the queue. The first cell in the queue is then found in constant
|
|
|
|
time: it is the next cell in the cyclic list. The queue's length is
|
|
|
|
also recorded, so as to make [length] a constant-time operation.
|
|
|
|
|
|
|
|
The [tail] field should really be of type ['a cell option], but
|
|
|
|
then it would be [None] when [length] is 0 and [Some] otherwise,
|
|
|
|
leading to redundant memory allocation and accesses. We avoid this
|
|
|
|
overhead by filling [tail] with a dummy value when [length] is 0.
|
|
|
|
Of course, this requires bending the type system's arm slightly,
|
|
|
|
because it does not have dependent sums. *)
|
|
|
|
|
|
|
|
type 'a t = {
|
|
|
|
mutable length: int;
|
|
|
|
mutable tail: 'a cell
|
2005-08-26 05:10:47 -07:00
|
|
|
}
|
2002-05-06 05:11:08 -07:00
|
|
|
|
|
|
|
let create () = {
|
|
|
|
length = 0;
|
|
|
|
tail = Obj.magic None
|
2005-08-26 05:10:47 -07:00
|
|
|
}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let clear q =
|
2002-05-06 05:11:08 -07:00
|
|
|
q.length <- 0;
|
|
|
|
q.tail <- Obj.magic None
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let add x q =
|
2012-02-18 01:36:13 -08:00
|
|
|
if q.length = 0 then
|
2002-05-06 05:11:08 -07:00
|
|
|
let rec cell = {
|
|
|
|
content = x;
|
|
|
|
next = cell
|
|
|
|
} in
|
2012-02-18 01:36:13 -08:00
|
|
|
q.length <- 1;
|
2002-05-06 05:11:08 -07:00
|
|
|
q.tail <- cell
|
|
|
|
else
|
|
|
|
let tail = q.tail in
|
|
|
|
let head = tail.next in
|
|
|
|
let cell = {
|
|
|
|
content = x;
|
|
|
|
next = head
|
|
|
|
} in
|
2012-02-18 01:36:13 -08:00
|
|
|
q.length <- q.length + 1;
|
2002-05-06 05:11:08 -07:00
|
|
|
tail.next <- cell;
|
|
|
|
q.tail <- cell
|
|
|
|
|
|
|
|
let push =
|
|
|
|
add
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let peek q =
|
2002-05-06 05:11:08 -07:00
|
|
|
if q.length = 0 then
|
|
|
|
raise Empty
|
|
|
|
else
|
|
|
|
q.tail.next.content
|
|
|
|
|
|
|
|
let top =
|
|
|
|
peek
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let take q =
|
2005-08-26 05:10:47 -07:00
|
|
|
if q.length = 0 then raise Empty;
|
|
|
|
q.length <- q.length - 1;
|
|
|
|
let tail = q.tail in
|
|
|
|
let head = tail.next in
|
|
|
|
if head == tail then
|
|
|
|
q.tail <- Obj.magic None
|
2002-05-06 05:11:08 -07:00
|
|
|
else
|
2005-08-26 05:10:47 -07:00
|
|
|
tail.next <- head.next;
|
|
|
|
head.content
|
2002-05-06 05:11:08 -07:00
|
|
|
|
|
|
|
let pop =
|
|
|
|
take
|
|
|
|
|
|
|
|
let copy q =
|
|
|
|
if q.length = 0 then
|
|
|
|
create()
|
|
|
|
else
|
|
|
|
let tail = q.tail in
|
|
|
|
|
|
|
|
let rec tail' = {
|
|
|
|
content = tail.content;
|
|
|
|
next = tail'
|
|
|
|
} in
|
|
|
|
|
|
|
|
let rec copy cell =
|
|
|
|
if cell == tail then tail'
|
|
|
|
else {
|
2002-07-23 07:12:03 -07:00
|
|
|
content = cell.content;
|
|
|
|
next = copy cell.next
|
2002-05-06 05:11:08 -07:00
|
|
|
} in
|
|
|
|
|
|
|
|
tail'.next <- copy tail.next;
|
|
|
|
{
|
|
|
|
length = q.length;
|
|
|
|
tail = tail'
|
2005-08-26 05:10:47 -07:00
|
|
|
}
|
2002-05-06 05:11:08 -07:00
|
|
|
|
2002-06-27 01:48:26 -07:00
|
|
|
let is_empty q =
|
|
|
|
q.length = 0
|
|
|
|
|
2002-05-06 05:11:08 -07:00
|
|
|
let length q =
|
|
|
|
q.length
|
|
|
|
|
|
|
|
let iter f q =
|
|
|
|
if q.length > 0 then
|
|
|
|
let tail = q.tail in
|
|
|
|
let rec iter cell =
|
|
|
|
f cell.content;
|
|
|
|
if cell != tail then
|
2002-07-23 07:12:03 -07:00
|
|
|
iter cell.next in
|
2002-05-06 05:11:08 -07:00
|
|
|
iter tail.next
|
|
|
|
|
|
|
|
let fold f accu q =
|
|
|
|
if q.length = 0 then
|
|
|
|
accu
|
|
|
|
else
|
|
|
|
let tail = q.tail in
|
|
|
|
let rec fold accu cell =
|
|
|
|
let accu = f accu cell.content in
|
|
|
|
if cell == tail then
|
2002-07-23 07:12:03 -07:00
|
|
|
accu
|
2002-05-06 05:11:08 -07:00
|
|
|
else
|
2002-07-23 07:12:03 -07:00
|
|
|
fold accu cell.next in
|
2002-05-06 05:11:08 -07:00
|
|
|
fold accu tail.next
|
|
|
|
|
|
|
|
let transfer q1 q2 =
|
|
|
|
let length1 = q1.length in
|
|
|
|
if length1 > 0 then
|
|
|
|
let tail1 = q1.tail in
|
|
|
|
clear q1;
|
|
|
|
if q2.length > 0 then begin
|
|
|
|
let tail2 = q2.tail in
|
|
|
|
let head1 = tail1.next in
|
|
|
|
let head2 = tail2.next in
|
|
|
|
tail1.next <- head2;
|
|
|
|
tail2.next <- head1
|
|
|
|
end;
|
|
|
|
q2.length <- q2.length + length1;
|
|
|
|
q2.tail <- tail1
|