Debian Codegraph @

clib/segmenttree.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (** This module is a very simple implementation of "segment trees".
12
13 A segment tree of type ['a t] represents a mapping from a union of
14 disjoint segments to some values of type 'a.
15 *)
16
17 (** Misc. functions. *)
18 let list_iteri f l =
19 let rec loop i = function
20 | [] -> ()
21 | x :: xs -> f i x; loop (i + 1) xs
22 in
23 loop 0 l
24
25 let log2 x = log x /. log 2.
26
27 let log2n x = int_of_float (ceil (log2 (float_of_int x)))
28
29 (** We focus on integers but this module can be generalized. *)
30 type elt = int
31
32 (** A value of type [domain] is interpreted differently given its position
33 in the tree. On internal nodes, a domain represents the set of
34 integers which are _not_ in the set of keys handled by the tree. On
35 leaves, a domain represents the st of integers which are in the set of
36 keys. *)
37 type domain =
38 | Interval of elt * elt
39 (** On internal nodes, a domain [Interval (a, b)] represents
40 the interval [a + 1; b - 1]. On leaves, it represents [a; b].
41 We always have [a] <= [b]. *)
42 | Universe
43 (** On internal node or root, a domain [Universe] represents all
44 the integers. When the tree is not a trivial root,
45 [Universe] has no interpretation on leaves. (The lookup
46 function should never reach the leaves.) *)
47
48 (** We use an array to store the almost complete tree. This array
49 contains at least one element. *)
50 type 'a t = (domain * 'a option) array
51
52 (** The root is the first item of the array. *)
53
54 (** Standard layout for left child. *)
55 let left_child i = 2 * i + 1
56
57 (** Standard layout for right child. *)
58 let right_child i = 2 * i + 2
59
60 (** Extract the annotation of a node, be it internal or a leaf. *)
61 let value_of i t = match t.(i) with (_, Some x) -> x | _ -> raise Not_found
62
63 (** Initialize the array to store [n] leaves. *)
64 let create n init =
65 Array.make (1 lsl (log2n n + 1) - 1) init
66
67 (** Make a complete interval tree from a list of disjoint segments.
68 Precondition : the segments must be sorted. *)
69 let make segments =
70 let nsegments = List.length segments in
71 let tree = create nsegments (Universe, None) in
72 let leaves_offset = (1 lsl (log2n nsegments)) - 1 in
73
74 (* The algorithm proceeds in two steps using an intermediate tree
75 to store minimum and maximum of each subtree as annotation of
76 the node. *)
77
78 (* We start from leaves: the last level of the tree is initialized
79 with the given segments... *)
80 list_iteri
81 (fun i ((start, stop), value) ->
82 let k = leaves_offset + i in
83 let i = Interval (start, stop) in
84 tree.(k) <- (i, Some i))
85 segments;
86 (* ... the remaining leaves are initialized with neutral information. *)
87 for k = leaves_offset + nsegments to Array.length tree -1 do
88 tree.(k) <- (Universe, Some Universe)
89 done;
90
91 (* We traverse the tree bottom-up and compute the interval and
92 annotation associated to each node from the annotations of its
93 children. *)
94 for k = leaves_offset - 1 downto 0 do
95 let node, annotation =
96 match value_of (left_child k) tree, value_of (right_child k) tree with
97 | Interval (left_min, left_max), Interval (right_min, right_max) ->
98 (Interval (left_max, right_min), Interval (left_min, right_max))
99 | Interval (min, max), Universe ->
100 (Interval (max, max), Interval (min, max))
101 | Universe, Universe -> Universe, Universe
102 | Universe, _ -> assert false
103 in
104 tree.(k) <- (node, Some annotation)
105 done;
106
107 (* Finally, annotation are replaced with the image related to each leaf. *)
108 let final_tree =
109 Array.mapi (fun i (segment, value) -> (segment, None)) tree
110 in
111 list_iteri
112 (fun i ((start, stop), value) ->
113 final_tree.(leaves_offset + i)
114 <- (Interval (start, stop), Some value))
115 segments;
116 final_tree
117
118 (** [lookup k t] looks for an image for key [k] in the interval tree [t].
119 Raise [Not_found] if it fails. *)
120 let lookup k t =
121 let i = ref 0 in
122 while (snd t.(!i) = None) do
123 match fst t.(!i) with
124 | Interval (start, stop) ->
125 if k <= start then i := left_child !i
126 else if k >= stop then i:= right_child !i
127 else raise Not_found
128 | Universe -> raise Not_found
129 done;
130 match fst t.(!i) with
131 | Interval (start, stop) ->
132 if k >= start && k <= stop then
133 match snd t.(!i) with
134 | Some v -> v
135 | None -> assert false
136 else
137 raise Not_found
138 | Universe -> assert false
139
140