package grenier
A collection of various algorithms in OCaml
Install
Dune Dependency
Authors
Maintainers
Sources
grenier-0.15.tbz
sha256=dec7f84b9e93d5825f10c7dea84d5a74d7365ede45664ae63c26b5e8045c1c44
sha512=b8aa1569c2e24b89674d1b34de34cd1798896bb6a53aa5a1287f68cee880125e6b687f66ad73da9069a01cc3ece1f0684f48328b099d43529bff736b772c8fd8
doc/src/grenier.orderme/order_list.ml.html
Source file order_list.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
type t = { mutable tag: int; mutable prev: t; mutable next: t; counter: int ref; } let average x y = (x land y) + (x lxor y) / 2 let curr_index t = t.tag let rec sentinel = { tag = 0; prev = sentinel; next = sentinel; counter = ref 0 } let is_first t = t.prev == t let is_last t = t == t.next let is_valid t = t.next != sentinel let prev_index t = if is_first t then min_int else t.prev.tag let next_index t = if is_last t then max_int else t.next.tag let check t = assert (is_valid t); assert (is_valid t.prev); assert (is_valid t.next); assert (t == t.prev || t.prev.next == t); assert (t == t.next || t.next.prev == t); if t.next != t then assert (t.next.tag > t.tag); if t.prev != t then assert (t.prev.tag < t.tag) let consistent _ = () let consistents _ _ = () (*let rec consistents t = function | 0 -> () | 1 -> consistent t | n -> consistent t; assert (t.next != t); consistents t (n - 1)*) let root () = let rec t = { prev = t; next = t; tag = 0; counter = ref 1 } in consistent t; t let forget t = if is_valid t then begin let {prev; next; counter; _} = t in if is_first t then next.prev <- next else if is_last t then prev.next <- prev else ( prev.next <- next; next.prev <- prev; ); decr counter; t.next <- sentinel; t.prev <- sentinel; consistent prev; consistent next; assert (not (is_valid t)); end let same_order t1 t2 = is_valid t1 && is_valid t2 && t1.counter == t2.counter let compare t1 t2 = assert (same_order t1 t2); compare t1.tag t2.tag let cardinal t = if is_valid t then !(t.counter) else 0 let uint_size = Sys.word_size - 2 let pow = 2.0 ** float uint_size let inv = 1.0 /. float uint_size let optimal_t count = (pow /. float count) ** inv let find_span n = let t = optimal_t !(n.counter) in let count = ref 1 and left = ref n and right = ref n and tag = n.tag and low = ref n.tag and high = ref n.tag and bit = ref 1 and thresh = ref 1.0 in while !bit > 0 && (float !count >= float !bit *. !thresh) do let to_left = (tag land !bit) <> 0 in if to_left then begin low := !low lxor !bit; while !left.tag > !low && not (is_first !left) do left := !left.prev; incr count; done end else begin high := !high lxor !bit; while !right.tag < !high && not (is_last !right) do right := !right.next; incr count; done end; bit := !bit lsl 1; thresh := !thresh /. t; done; !left, !low, (!bit lsr 1), !count let rec relabel_span_big root step tag = function | 1 -> root.tag <- tag; assert (tag < next_index root || is_last root) | n -> root.tag <- tag; assert (tag > prev_index root); relabel_span_big root.next step (tag + step) (n - 1) let rec relabel_span_small node root slack tag = function | 1 -> root.tag <- tag; assert (tag < next_index root || is_last root) | n -> root.tag <- tag; (*Printf.eprintf "assert (%d > %d); slack = %d\n" tag (prev_index root) slack;*) assert (tag > prev_index root); relabel_span_small node root.next slack (tag + if node == root then slack + 1 else 1) (n - 1) let relabel node = let root, tag, range, count = find_span node in let step = range / count in (*Printf.eprintf "range = %d, count = %d\n" range count;*) if step <= 1 then (assert (range >= count); relabel_span_small node root (range - count) (tag + 1) count) else relabel_span_big root step (tag + step) count; consistents root count let after t = assert (is_valid t); let tag = average (curr_index t) (next_index t) in (* IMPORTANT Allocation must be done before reading t.prev/t.next. It might trigger a garbage collection which can invalidate the linked list (e.g if used through Order_managed). *) let t' = {prev = t; next = t; tag; counter = t.counter} in let {next; counter; _} = t in if t == next then t'.next <- t' else ( t'.next <- next; next.prev <- t' ); t.next <- t'; incr counter; if t'.tag = prev_index t' then relabel t'; consistent t; consistent t'; t' let before t = assert (is_valid t); let tag = average (prev_index t) (curr_index t) in (* IMPORTANT Allocation must be done before reading t.prev/t.next. It might trigger a garbage collection which can invalidate the linked list (e.g if used through Order_managed). *) let t' = {prev = t; next = t; tag; counter = t.counter} in let {prev; counter; _} = t in if t == prev then t'.prev <- t' else ( t'.prev <- prev; prev.next <- t' ); t.prev <- t'; incr counter; if t'.tag = prev_index t' then relabel t'; consistent t; consistent t'; t' let unsafe_next t = t.next let unsafe_prev t = t.prev let unsafe_check t msg = try if is_valid t then check t else begin assert (t.prev == sentinel); assert (t.next == sentinel); end with Assert_failure (file, line, col) -> raise (Assert_failure (msg ^ ": " ^ file, line, col))
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>