Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
dll.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
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright (c) 2020 Nomadic Labs, <contact@nomadic-labs.com> *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) (* to deal in the Software without restriction, including without limitation *) (* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) (* and/or sell copies of the Software, and to permit persons to whom the *) (* Software is furnished to do so, subject to the following conditions: *) (* *) (* The above copyright notice and this permission notice shall be included *) (* in all copies or substantial portions of the Software. *) (* *) (* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) (* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) (* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) (* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) (* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) (* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) type 'a node = { data: 'a; mutable prev: 'a node option; mutable next: 'a node option; } let data {data; _} = data type 'a tt = | Empty of {capacity: int} | Inited of { capacity: int; mutable size: int; mutable first: 'a node; mutable last: 'a node; } type 'a t = 'a tt ref let create capacity = if capacity <= 0 then raise (Invalid_argument "Ringo.Dll.create: negative or null capacity") else ref (Empty {capacity}) let capacity dll = match !dll with | Empty {capacity} | Inited {capacity; _} -> capacity let add_and_return_erased dll data = match !dll with | Empty {capacity} -> let node = {data; prev = None; next = None} in dll := Inited {capacity; size = 1; first = node; last = node;}; (node, None) | Inited ({capacity = 1; size; first; last;} as dll) -> assert (size = 1); assert (first == last); assert (first.next = None); assert (first.prev = None); assert (last.next = None); assert (last.prev = None); let pops = last in let node = {data; prev = None; next = None} in dll.first <- node; dll.last <- node; (node, Some pops.data) | Inited ({capacity; size; first; last;} as dll) -> assert (first.prev = None); assert (last.next = None); if size < capacity then begin let node = {data; prev = None; next = Some first} in first.prev <- Some node; dll.first <- node; dll.size <- succ dll.size; (node, None) end else begin let pops = last in ( match last.prev with | Some new_last -> dll.last <- new_last; new_last.next <- None | None -> (* This requires (1) to have a single element, (2) to have reached capacity, and (3) to have a capacity > 1 *) assert false ); pops.prev <- None; pops.next <- None; let node = {data; prev = None; next = Some dll.first} in first.prev <- Some node; dll.first <- node; (node, Some pops.data) end let add dll data = fst @@ add_and_return_erased dll data let add_list dll l = let capacity = capacity dll in let length = List.length l in if length < capacity then begin List.map (add dll) l |> List.rev end else begin List.fold_left (fun (index, acc) x -> if index < length - capacity then (index + 1, acc) else (index + 1, add dll x:: acc)) (0, []) l |> fun (_, acc) -> List.rev acc end let clear dll = match !dll with | Empty _ -> () | Inited {capacity; _} -> dll := Empty {capacity} let rec fold_node f acc node = let acc = f acc node in match node.next with | None -> acc | Some next -> fold_node f acc next let fold dll ~init ~f = match !dll with | Empty _ -> init | Inited {first; _} -> fold_node f init first let elements t = fold t ~init:[] ~f:(fun acc elt -> elt:: acc) let elements_data t = fold t ~init:[] ~f:(fun acc elt -> elt.data:: acc) let remove dll node = match !dll with | Empty _ -> assert false | Inited dll -> begin match (node.prev, node.next) with | (None, None) -> assert false | (None, Some next) -> next.prev <- None; dll.first <- next; | (Some prev, None) -> prev.next <- None; dll.last <- prev | (Some prev, Some next) -> prev.next <- node.next; next.prev <- node.prev end; node.prev <- None; node.next <- None; dll.size <- pred dll.size let promote dll node = match !dll with | Empty _ -> assert false | Inited dll -> if dll.first == node then () else begin let prev_first = dll.first in (* first, promote neighbors *) begin match (node.prev, node.next) with | (None, None) -> assert false | (None, Some _next) -> assert false | (Some prev, None) -> prev.next <- None; dll.last <- prev | (Some prev, Some next) -> prev.next <- node.next; next.prev <- node.prev end; (* promote node to first *) prev_first.prev <- Some node; node.prev <- None; node.next <- Some dll.first; dll.first <- node end