src/Pure/General/queue.ML
author wenzelm
Thu, 09 Feb 2017 22:40:15 +0100
changeset 65014 97a622d01609
parent 47060 e2741ec9ae36
permissions -rw-r--r--
support for type Date;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28127
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/queue.ML
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
     3
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
     4
Efficient queues.
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
     5
*)
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
     6
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
     7
signature QUEUE =
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
     8
sig
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
     9
  type 'a T
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    10
  val empty: 'a T
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    11
  val is_empty: 'a T -> bool
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    12
  val content: 'a T -> 'a list
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    13
  val enqueue: 'a -> 'a T -> 'a T
47060
e2741ec9ae36 prefer explicitly qualified exception List.Empty;
wenzelm
parents: 29606
diff changeset
    14
  val dequeue: 'a T -> 'a * 'a T  (*exception List.Empty*)
28127
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    15
end;
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    16
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    17
structure Queue: QUEUE =
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    18
struct
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    19
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    20
datatype 'a T = Queue of 'a list * 'a list;
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    21
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    22
val empty = Queue ([], []);
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    23
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    24
fun is_empty (Queue ([], [])) = true
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    25
  | is_empty _ = false;
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    26
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    27
fun content (Queue (xs, ys)) = ys @ rev xs;
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    28
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    29
fun enqueue x (Queue (xs, ys)) = Queue (x :: xs, ys);
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    30
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    31
fun dequeue (Queue (xs, y :: ys)) = (y, Queue (xs, ys))
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    32
  | dequeue (Queue (xs as _ :: _, [])) = let val y :: ys = rev xs in (y, Queue ([], ys)) end
47060
e2741ec9ae36 prefer explicitly qualified exception List.Empty;
wenzelm
parents: 29606
diff changeset
    33
  | dequeue (Queue ([], [])) = raise List.Empty;
28127
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    34
93ee30a54c9a Efficient queues.
wenzelm
parents:
diff changeset
    35
end;