src/Pure/General/balanced_tree.ML
author wenzelm
Fri, 15 Aug 2008 22:16:14 +0200
changeset 27905 070b4a6a9d58
parent 23427 26202f4e663d
child 29606 fedb8be05f24
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23427
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/balanced_tree.ML
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson and Makarius
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
     4
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
     5
Balanced binary trees.
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
     6
*)
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
     7
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
     8
signature BALANCED_TREE =
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
     9
sig
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    10
  val make: ('a * 'a -> 'a) -> 'a list -> 'a
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    11
  val dest: ('a -> 'a * 'a) -> int -> 'a -> 'a list
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    12
  val access: {left: 'a -> 'a, right: 'a -> 'a, init: 'a} -> int -> int -> 'a
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    13
  val accesses: {left: 'a -> 'a, right: 'a -> 'a, init: 'a} -> int -> 'a list
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    14
end;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    15
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    16
structure BalancedTree: BALANCED_TREE =
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    17
struct
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    18
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    19
fun make _ [] = raise Empty
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    20
  | make _ [x] = x
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    21
  | make f xs =
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    22
      let
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    23
        val m = length xs div 2;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    24
        val (ps, qs) = chop m xs;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    25
      in f (make f ps, make f qs) end;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    26
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    27
fun dest f n x =
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    28
  if n <= 0 then raise Empty
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    29
  else if n = 1 then [x]
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    30
  else
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    31
    let
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    32
      val m = n div 2;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    33
      val (left, right) = f x;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    34
    in dest f m left @ dest f (n - m) right end;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    35
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    36
(*construct something of the form f(...g(...(x)...)) for balanced access*)
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    37
fun access {left = f, right = g, init = x} len i =
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    38
  let
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    39
    fun acc 1 _ = x
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    40
      | acc n i =
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    41
          let val m = n div 2 in
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    42
            if i <= m then f (acc m i)
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    43
            else g (acc (n - m) (i - m))
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    44
          end;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    45
  in if 1 <= i andalso i <= len then acc len i else raise Subscript end;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    46
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    47
(*construct ALL such accesses; could try harder to share recursive calls!*)
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    48
fun accesses {left = f, right = g, init = x} len =
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    49
  let
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    50
    fun acc 1 = [x]
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    51
      | acc n =
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    52
          let
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    53
            val m = n div 2;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    54
            val accs_left = acc m;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    55
            val accs_right =
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    56
              if n - m = m then accs_left
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    57
              else acc (n - m);
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    58
          in map f accs_left @ map g accs_right end;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    59
  in if 1 <= len then acc len else raise Subscript end;
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    60
26202f4e663d Balanced binary trees (material from library.ML);
wenzelm
parents:
diff changeset
    61
end;