YN55.5.0+dev0-2025-04-28/(Dynarray!tA;!a@@A@A@O@B@@@9../../stdlib/dynarray.mli}  }  $@@@@(Dynarray@@A@&create@$unit@@@(Dynarray!t!a@B@@@@@@@"I  #I  @@!A@@$make@#int@@@@!a@B@(Dynarray!t@@@@@@@@DL  EL  @@CB@@$init@"@@@@@+@@@!a@B@@@(Dynarray!t@@@@@@@@lR  mR  @@kC@@#get@(Dynarray!t!a@B@@@@@[@@@@@@@@^^3@@D@@#set@(Dynarray!t!a@B@@@@@|@@@@@@@@@@@@@@cc@@E@@&length@(Dynarray!t!a@B@@@@@@@@@@kk@@F@@(is_empty@(Dynarray!t!a@B@@@@$bool@@@@@@nn0@@G@@(get_last@(Dynarray!t!a@B@@@@@@@q}}q}@@ H@@)find_last@(Dynarray!t!a@B@@@@&optionL @@@@@@/w  0w *@@.I@@$copy@(Dynarray!t!a@B@@@@(Dynarray!t@@@@@@R{S{@@QJ@@(add_last@(Dynarray!t!a@B@@@@@ a@@@@@@@@st@@rK@@,append_array@(Dynarray!t!a@B@@@@@%array@@@@@@@@@@@   6@@L@@+append_list@(Dynarray!t!a@ B@@@@@$list@@@@@@ @@ @@ @DDDm@@M@@&append @(Dynarray!t!a@B@@@@@(Dynarray!t@@@@@@@@@@@@@N@@*append_seq`@(Dynarray!t!a@ DB@@@@@&Stdlib#Seq!t@@@ ?@@@ @@@ A@@ B@@@O@@+append_itera@(Dynarray!t!a@ SB@ E@@@ G@@@ ,@@@ H@@ I@!x@ UB@ J9@@@ K@@ L@@ M@ @@@@ N@@ O@@ P@@ Q@RDDS@@QP@@$blitb#src(Dynarray!t!a@ eB@ V@@@ X'src_posE@@@ Y#dst(Dynarray!t@@@ ['dst_pos[@@@ \#lend@@@ ]@@@ ^@@ _@@ `@@ a@@ b@@ c@@@Q@@,pop_last_optc@(Dynarray!t!a@ mB@ f@@@ h @@@ j@@ k@xxx@@R@@(pop_lastd@(Dynarray!t!a@ sB@ n@@@ p@@ q@!@@S@@+remove_laste@(Dynarray!t!a@ zB@ t@@@ v@@@ w@@ x@@@T@@(truncatef@(Dynarray!t!a@ B@ {@@@ }@@@@ ~@@@ @@ @@ @4@@U@@%clearg@(Dynarray!t!a@ B@ @@@ &@@@ @@ @8  9  @@7V@@$iterh@@!a@ B@ >@@@ @@ @(Dynarray!t@@@ N@@@ @@ @@ @`""a"# @@_W@@%iterii@@@@@@ @!a@ B@ m@@@ @@ @@ @(Dynarray!t@@@ }@@@ @@ @@ @#A#A#A#p@@X@@#mapj@@!a@ B@ !b@ B@ @@ @(Dynarray!t@@@ (Dynarray!t@@@ @@ @@ @####@@Y@@$mapik@@@@@ @!a@ B@ !b@ B@ @@ @@ @(Dynarray!t@@@ (Dynarray!t@@@ @@ @@ @$$$$@@Z@@)fold_leftl@@#acc@ B@ @!a@ B@ @@ @@ @@(Dynarray!t@@@ @@ @@ @@ @%%%& @@[@@*fold_rightm@@!a@ B@ @#acc@ B@ @@ @@ @(Dynarray!t@@@ @@@ @@ @@ @@' ' A' 'I@@?\@@&filtern@@!a@ B@ e@@@ @@ @(Dynarray!t@@@ (Dynarray!t@@@ @@ @@ @l"''m"''@@k]@@*filter_mapo@@!a@ B@ W!b@ B@ @@@ @@ @(Dynarray!t@@@ (Dynarray!t@@@ @@ @@ @+)@)@+)@)r@@^@@&existsp@@!a@ B@ @@@ @@ @(Dynarray!t@@@ @@@ @@ @@ @;++;++@@_@@'for_allq@@!a@ B@ @@@ @@ @(Dynarray!t@@@ @@@ @@ @@ @B,,B,,@@`@@'exists2r@@!a@ B@ @!b@ B@ @@@ @@ @@ @(Dynarray!t@@@ @(Dynarray!t @@@ 6@@@ @@ @@ @@ @)J--*J--@@(a@@(for_all2s@@!a@ $B@ @!b@ &B@ V@@@ @@ @@ @(Dynarray!t@@@ @(Dynarray!t @@@ q@@@ @@ @@ !@@ "@dR._._eR._.@@cb@@#memt@!a@ .B@ '@(Dynarray!t@@@ )@@@ *@@ +@@ ,@Z/,/,Z/,/H@@c@@$memqu@!a@ 6B@ /@(Dynarray!t@@@ 1@@@ 2@@ 3@@ 4@b//b/0@@d@@(find_optv@@!a@ AB@ 7@@@ 8@@ 9@(Dynarray!t@@@ ;@@@ =@@ >@@ ?@i00i00@@e@@*find_indexw@@!a@ MB@ B@@@ C@@ D@(Dynarray!t@@@ Fɠ@@@ G@@@ I@@ J@@ K@q11q11@@f@@(find_mapx@@!a@ ZB@ N!b@ \B@ O@@@ Q@@ R@(Dynarray!t@@@ T@@@ V@@ W@@ X@){22*{22@@(g@@)find_mapiy@@ @@@ ]@!a@ kB@ ^!b@ mB@ _@@@ a@@ b@@ c@(Dynarray!t@@@ e1@@@ g@@ h@@ i@^33_33@@]h@@%equalz@@!a@ {B@ n@@@@ o@@ p@@ q@(Dynarray!t@@@ s@(Dynarray!t"@@@ u@@@ v@@ w@@ x@@ y@5e5e5e5@@i@@'compare{@@!a@ B@ |@{@@@ }@@ ~@@ @(Dynarray!t@@@ @(Dynarray!t"@@@ @@@ @@ @@ @@ @6.6.6.6d@@j@@(of_array|@D!a@ B@ @@@ (Dynarray!t@@@ @@ @9~9~9~9@@k@@(to_array}@(Dynarray!t!a@ B@ @@@ s @@@ @@ @:':' :':F@@l@@'of_list~@\!a@ B@ @@@ (Dynarray!t@@@ @@ @(::):;@@'m@@'to_list@(Dynarray!t!a@ B@ @@@  @@@ @@ @H;[;[I;[;x@@Gn@@&of_seq@&Stdlib#Seq!t!a@ B@ @@@ (Dynarray!t@@@ @@ @m;;n;;@@lo@@&to_seq@(Dynarray!t!a@ B@ @@@ &Stdlib#Seq!t@@@ @@ @>>>@@r@@4to_seq_rev_reentrant@(Dynarray!t!a@ B@ @@@ &Stdlib#Seq!t@@@ @@ @????@@s@@(capacity@(Dynarray!t!a@ B@ @@@  @@@ @@ @ F,F,!F,FF@@t@@/ensure_capacity@(Dynarray !t!a@ B@ @@@ @ @@@ 4 @@@ @@ @@ @FFFGFF@@Eu@@5ensure_extra_capacity@(Dynarray !t!a@ B@ @@@ @5@@@ Z@@@ @@ @@ @lIImII@@kv@@,fit_capacity@(Dynarray!t!a@ B@ @@@ y@@@ @@ @(KK(KK@@w@@,set_capacity@(Dynarray!t!a@ B@ @@@ @z@@@ @@@ @@ @@ @:NN:NN@@x@@%reset@(Dynarray!t!a@ B@ @@@ @@@ @@ @KQQKQQ@@y@@0unsafe_to_iarray(capacity@@@ @@(Dynarray!t!a@ B@ @@@ @@@ @@ &iarray@@@ @@ @@ @ YStSt YStS@@ z@@@3/Dynamic arrays.@) The %Array@@ ( module provide arrays of fixed length.  &@@ s provides arrays whose length can change over time, by adding or removing elements at the end of the array.@ This is typically used to accumulate elements whose number is not known in advance or changes during computation, while also providing fast access to elements at arbitrary indices.@! let dynarray_of_list li = let arr = Dynarray.create () in List.iter (fun v -> Dynarray.add_last arr v) li; arr @) The &Buffer@@  module provides similar features, but it is specialized for accumulating characters into a dynamically-resized string.@) The %Stack@@ p module provides a last-in first-out data structure that can be easily implemented on top of dynamic arrays.@@@@#5.2@@@@@@5unsynchronized_access BUnsynchronized accesses to dynamic arrays are a programming error.@A9../../stdlib/dynarray.mli7Unsynchronized accesses@@ SConcurrent accesses to dynamic arrays must be synchronized (for instance with a 'Mutex.t@@ ). Unsynchronized accesses to a dynamic array are a programming error that may lead to an invalid dynamic array state, on which some operations would fail with an 0Invalid_argument+ exception.@A)dynarrays.Dynamic arrays@@#*Dynarray.t3 *A dynamic array containing values of type "'a!.@5 A dynamic array !a8 provides constant-time #get% and #set # operations on indices between !0% and 5Dynarray.length a - 13 included. Its /Dynarray.lengthD@ Q may change over time by adding or removing elements to the end of the array.@ * We say that an index into a dynarray !a: is valid if it is in 10 .. length a - 17 and invalid otherwise.@@@@@@@@@@@@ O@@A@@  @@/Dynarray.create3)create ()7 is a new, empty array.@@@@@@@@@@@@ @  @@@@ -Dynarray.make3(make n x: is a new array of length !n., filled with !x!.@@@@@@@@0Invalid_argument#if %n < 0$ or 8n > Sys.max_array_length!.@@@@@ @1 @4 @@@@ -Dynarray.init3(init n f0 is a new array !a+ of length !n0, such that 'get a i$ is #f i &. In other words, the elements of !a% are #f 0', then #f 1+, then #f 2(... and )f (n - 1) # last, evaluated in that order.@8 This is similar to *Array.init@@!.@@@@@@@@0Invalid_argument#if %n < 0$ or 8n > Sys.max_array_length!.@@@@@ @ @ @@@@ ,Dynarray.get3'get a i( is the !i/-th element of !a6, starting with index !0!.@@@@@@@@0Invalid_argument7if the index is invalid@@@@@ @ @ @@@@ ,Dynarray.set3)set a i x* sets the !i/-th element of !a' to be !x!.@% !i8 must be a valid index. #set 3 does not add new elements to the array -- see 1Dynarray.add_lastD@3 to add an element.@@@@@@@@0Invalid_argument8if the index is invalid.@@@@@ 2@  3@  %@ 1@@@@ >3(length a ( is the number of elements in the array.@@@@@@@@@@@@ !@  "@@@@ 1Dynarray.is_empty3*is_empty a$ is $true$ if !a7 is empty, that is, if ,length a = 0!.@@@@@@@@@@@@ &@D '@@@@ 1Dynarray.get_last3*get_last a3 is the element of !a* at index ,length a - 1!.@@@@@@@@0Invalid_argument#if !a* is empty.@@@@@ 0@n 1@@@@ $2Dynarray.find_last3+find_last a$ is $None$ if !a2 is empty and 1Some (get_last a)+ otherwise.@@@@@@@@@@@@ :@ ;@@@@ '-Dynarray.copy3© a6 is a shallow copy of !a 2, a new array containing the same elements as !a!.@@@@@@@@@@@@ 7@ 8@@@@ "A&adding/Adding elements@@ 0 Note: all operations adding elements raise 0Invalid_argument ( if the length needs to grow beyond 4Sys.max_array_length@@!.@Ӑ3,add_last a x2 adds the element !x9 at the end of the array !a!.@@@@@@@@@@@@ J@ K@ F@@@@ :5Dynarray.append_array30append_array a b6 adds all elements of !b/ at the end of !a ", in the order they appear in !b!.@6 For example: let a = Dynarray.of_list [1;2] in Dynarray.append_array a [|3; 4|]; assert (Dynarray.to_list a = [1; 2; 3; 4]) @@@@@@@@@@@@ W@ X@ J@@@@ @4Dynarray.append_list3%Like 7D@1 but with a list.@@@@@@@@@@@@ G@, H@/ :@@@@ 0/Dynarray.append3*append a b) is like 0append_array a b*, but !b 9 is itself a dynamic array instead of a fixed-size array.@. Warning: *append a a 3 is a programming error because it iterates on !a 9 and adds elements to it at the same time -- see the 2Dynarray.iteration)Iteration@* Iteration@ " section below. It fails with 0Invalid_argument -. If you really want to append a copy of !a< to itself, you can use -Dynarray.append_array a (Dynarray.to_array a). which copies !a< into a temporary array.@@@@@@@@@@@@ w@ x@ j@@@@ ^3Dynarray.append_seq3%Like D@5 but with a sequence.@. Warning: !append_seq a (to_seq_reentrant a)> simultaneously traverses !a and adds element to it; the ordering of those operations is unspecified, and may result in an infinite loop -- the new elements may in turn be produced by 2to_seq_reentrant a # and get added again and again.@@@@@@@@@@@@ {@ |@ n@@@@ `4Dynarray.append_iter34append_iter a iter x6 adds each element of !x/ to the end of !a.. This is 3iter (add_last a) x!.@2 For example, ?append_iter a List.iter [1;2;3]8 would add elements !1", !2+, and then !3/ at the end of !a&. :append_iter a Queue.iter q> adds elements from the queue !q!.@@@@@@@@@@@@ @ @  @  @@@@ -Dynarray.blit3 %blit ~src ~src_pos ~dst ~dst_pos ~len( copies #len % elements from a source dynarray #src4, starting at index 'src_pos , to a destination dynarray #dst4, starting at index 'dst_pos !. It works correctly even if #src% and #dst G are the same array, and the source and destination chunks overlap.@, Unlike *Array.blit@@", @D@ M can extend the destination array with new elements: it is valid to call $blit/ even when -dst_pos + len0 is larger than *length dst #. The only requirement is that 'dst_pos1 must be at most *length dst ^ (included), so that there is no gap between the current elements and the blit region.@@@@@@@@0Invalid_argument#if 'src_pos% and #len * do not designate a valid subarray of #src(, or if 'dst_pos3 is strictly below !07 or strictly above *length dst!.@@@@@ @  @  @  @  @  @@@@ ֠A(removing1Removing elements@@5Dynarray.pop_last_opt3.pop_last_opt a ) removes and returns the last element of !a), or $None7 if the array is empty.@@@@@@@@@@@@ @ @@@@ ޠ1Dynarray.pop_last3*pop_last a ) removes and returns the last element of !a!.@@@@@@@@)Not_found2on an empty array.@@@@@ @ @@@@ ⠕4Dynarray.remove_last3-remove_last a= removes the last element of !a !, if any. It does nothing if !a* is empty.@@@@@@@@@@@@ @ @@@@ ᠕1Dynarray.truncate3,truncate a n+ truncates !a1 to have at most !n* elements.@ < It removes elements whose index is greater or equal to !n9. It does nothing if -n >= length a!.@% ,truncate a n7 is equivalent to: n if n < 0 then invalid_argument "..."; while length a > n do remove_last a done @@@@@@@@0Invalid_argument#if %n < 0!.@@@@@ @W @Z @@@@ .Dynarray.clear3'clear a$ is ,truncate a 0 !, it removes all the elements of !a!.@@@@@@@@@@@@ @x @@@@ A)iteration!@@ Y The iteration functions traverse the elements of a dynamic array. Traversals of !a G are computed in increasing index order: from the element of index !09 to the element of index ,length a - 1!.@ It is a programming error to change the length of an array (by adding or removing elements) during an iteration on the array. Any iteration function will fail with 0Invalid_argument ( if it detects such a length change.@-Dynarray.iter3(iter f a' calls !f4 on each element of !a!.@@@@@@@@@@@@ ;@ <@ 0@@@@ $.Dynarray.iteri3)iteri f a' calls %f i x* for each !x* at index !i$ in !a!.@@@@@@@@@@@@ @@ A@ .@@@@ ",Dynarray.map3'map f a ( is a new array of elements of the form #f x6 for each element !x$ of !a!.@ % For example, if the elements of !a% are "x0", "x1", "x2;, then the elements of !b% are $f x0", $f x1", $f x2!.@@@@@@@@@@@@ l@E m@H `@@@@ P-Dynarray.mapi3(mapi f a ( is a new array of elements of the form %f i x6 for each element !x$ of !a* at index !i!.@ % For example, if the elements of !a% are "x0", "x1", "x2;, then the elements of !b% are &f 0 x0", &f 1 x1", &f 2 x2!.@@@@@@@@@@@@ @ @ @@@@ }2Dynarray.fold_left31fold_left f acc a' folds !f& over !a ) in order, starting with accumulator #acc!.@ % For example, if the elements of !a% are "x0", "x1+, then ,fold f acc a( is G let acc = f acc x0 in let acc = f acc x1 in acc @@@@@@@@@@@@ @ @ @ @@@@ 3Dynarray.fold_right32fold_right f a acc. computes f x0 (f x1 (... (f xn acc) ...))+ where "x0", "x1', ..., "xn5 are the elements of !a!.@@@@@@@@@@@@ @" @% @( @@@@ /Dynarray.filter3*filter f a ' is a new array of all the elements of !a. that satisfy !f %. In other words, it is an array !b= such that, for each element !x( in !a+ in order, !x- is added to !b$ if #f x$ is $true!.@2 For example, :filter (fun x -> x >= 0) a 4 is a new array of all non-negative elements of !a+, in order.@@@@@@@@@@@@ @ @ @@@@ ۠3Dynarray.filter_map3.filter_map f a< is a new array of elements !y/ such that #f x$ is &Some y0 for an element !x$ of !a &. In others words, it is an array !b ! such that, for each element !x$ of !a/ in order: #if ,f x = Some y', then !y- is added to !b!,@#if *f x = None>, then no element is added to !b!.@@@2 For example, #filter_map int_of_string_opt inputs > returns a new array of integers read from the strings in &inputs <, ignoring strings that cannot be converted to integers.@@@@@@@@@@@@ G@ H@ 6@@@@ &A1dynarray_scanning2Dynarray scanning @@/Dynarray.exists3*exists f a$ is $true4 if some element of !a+ satisfies !f!.@ % For example, if the elements of !a% are "x0", "x1", "x2+, then *exists f a$ is 4f x0 || f x1 || f x2!.@@@@@@@@@@@@ m@U n@X b@@@@ V0Dynarray.for_all3+for_all f a$ is $true4 if all elements of !a) satisfy !f #. This includes the case where !a* is empty.@ % For example, if the elements of !a% are "x0", "x1", "x2+, then +for_all f a$ is 4f x0 && f x1 && f x2!.@@@@@@@@@@@@ @ @ @@@@ 0Dynarray.exists23(Same as D@ #, but for a two-argument predicate.@@@@#5.4@@@0Invalid_argument )if the two arrays have different lengths.@@@@@ @ @ @ w@@@@ k1Dynarray.for_all23(Same as D@ #, but for a two-argument predicate.@@@@#5.4@@@0Invalid_argument )if the two arrays have different lengths.@@@@@ z@ {@ g@ _@@@@ S,Dynarray.mem3)mem a set8 is true if and only if !a , is structurally equal to an element of #set3 (i.e. there is an !x$ in #set/ such that /compare a x = 0").@@@@#5.3@@@@@@@ w@% x@( s@@@@ g-Dynarray.memq3(Same as >D@ Z, but uses physical equality instead of structural equality to compare array elements.@@@@#5.3@@@@@@@ p@? q@B l@@@@ `1Dynarray.find_opt3,find_opt f a ( returns the first element of the array !a " that satisfies the predicate !f%, or $None % if there is no value that satisfies !f2 in the array !a!.@@@@#5.3@@@@@@@ @t @w y@@@@ m3Dynarray.find_index3.find_index f a) returns &Some i(, where !i 4 is the index of the first element of the array !a0 that satisfies #f x ", if there is such an element.@0 It returns $None= if there is no such element.@@@@#5.3@@@@@@@ @ @ @@@@ y1Dynarray.find_map3,find_map f a) applies !f4 to the elements of !a 8 in order, and returns the first result of the form &Some v%, or $None/ if none exist.@@@@#5.3@@@@@@@ @ @ @@@@ z2Dynarray.find_mapi3(Same as (find_map , but the predicate is applied to the index of the element as first argument (counting from 0), and the element itself as second argument.@@@@#5.3@@@@@@@ @ @ k@@@@ _A*comparison4Comparison functions@@ Comparison functions iterate over their arguments; it is a programming error to change their length during the iteration, see the 2Dynarray.iteration* Iteration@/ section above.@.Dynarray.equal3,equal eq a b, holds when !a% and !b / have the same length, and for all indices !i) we have 6eq (get a i) (get b i)!.@@@@#5.3@@@@@@@ @ > @ A @ D @@@@ u0Dynarray.compare3/compare cmp a b* compares !a% and !b according to the shortlex order, that is, shorter arrays are smaller and equal-sized arrays are compared in lexicographic order using #cmp5 to compare elements.@ 3 For more details on comparison functions, see *Array.sort@@!.@@@@#5.3@@@@@@@ @ t @ w @ z @@@@ vA+conversions $Conversions to other data structures@@/ Note: the $of_*1 functions raise 0Invalid_argument ( if the length needs to grow beyond 4Sys.max_array_length@@!.@) The $to_* functions, except those specifically marked "reentrant", iterate on their dynarray argument. In particular it is a programming error if the length of the dynarray changes during their execution, and the conversion functions raise 0Invalid_argument? if they observe such a change.@1Dynarray.of_array3,of_array arr D returns a dynamic array corresponding to the fixed-sized array !a.. Operates in $O(n)7 time by making a copy.@@@@@@@@@@@@ @  @@@@ 1Dynarray.to_array3*to_array a D returns a fixed-sized array corresponding to the dynamic array !a C. This always allocate a new array and copies elements into it.@@@@@@@@@@@@ @  @@@@ 0Dynarray.of_list3)of_list l ) is the array containing the elements of !l7 in the same order.@@@@@@@@@@@@ @  @@@@ 0Dynarray.to_list3)to_list a 4 is a list with the elements contained in the array !a!.@@@@@@@@@@@@ @  @@@@ /Dynarray.of_seq3*of_seq seq - is an array containing the same elements as #seq!.@2 It traverses #seq ! once and will terminate only if #seq+ is finite.@@@@@@@@@@@@ @ 7 @@@@ /Dynarray.to_seq3(to_seq a ! is the sequence of elements 'get a 0", 'get a 1$... 4get a (length a - 1)!.@@@@@@@@@@@@ @ [ @@@@ 9Dynarray.to_seq_reentrant32to_seq_reentrant a; is a reentrant variant of 0D@ R, in the sense that one may still access its elements after the length of !a- has changed.@3 Demanding the !i h-th element of the resulting sequence (which can happen zero, one or several times) will access the !i3-th element of !a 2 at the time of the demand. The sequence stops if !a3 has less than !i8 elements at this point.@@@@@@@@@@@@ @  @@@@ 3Dynarray.to_seq_rev3,to_seq_rev a ! is the sequence of elements -get a (l - 1)", -get a (l - 2)$... 'get a 0,, where !l$ is (length a- at the time *to_seq_rev, is invoked.@@@@@@@@@@@@ @  @@@@ =Dynarray.to_seq_rev_reentrant36to_seq_rev_reentrant a; is a reentrant variant of BD@ R, in the sense that one may still access its elements after the length of !a- has changed.@ r Elements that have been removed from the array by the time they are demanded in the sequence are skipped.@@@@@@@@@@@@ @  @@@@ A(advanced?Advanced topics for performance@@B(capacity7Backing array, capacity@@ ( Internally, a dynamic array uses a -backing array@ , (a fixed-size array as provided by the %Array@@ ` module) whose length is greater or equal to the length of the dynamic array. We define the , capacity@ 7 of a dynamic array as the length of its backing array.@ The capacity of a dynamic array is relevant in advanced scenarios, when reasoning about the performance of dynamic array programs: cThe memory usage of a dynamic array is proportional to its capacity, rather than its length.@ When there is no empty space left at the end of the backing array, adding elements requires allocating a new, larger backing array.@@@ 4 The implementation uses a standard exponential reallocation strategy which guarantees amortized constant-time operation; in particular, the total capacity of all backing arrays allocated over the lifetime of a dynamic array is at worst proportional to the total number of elements added.@  In other words, users need not care about capacity and reallocations, and they will get reasonable behavior by default. However, in some performance-sensitive scenarios the functions below can help control memory usage or guarantee an optimal number of reallocations.@1Dynarray.capacity3*capacity a2 is the length of !a1's backing array.@@@@@@@@@@@@ @ M @@@@ 8Dynarray.ensure_capacity33ensure_capacity a n ! makes sure that the capacity of !a1 is at least !n!.@@@@@@@@0Invalid_argument 5if the requested capacity is outside the range 90 .. Sys.max_array_length!.@ ( An example would be to reimplement ̐D@/ without using  GD@&: let of_array arr = let a = Dynarray.create () in Dynarray.ensure_capacity a (Array.length arr); Array.iter (fun v -> add_last a v) arr @+ Using /ensure_capacity [ guarantees that at most one reallocation will take place, instead of possibly several.@2 Without this /ensure_capacity G hint, the number of resizes would be logarithmic in the length of #arr :, creating a constant-factor slowdown noticeable when #arr* is large.@@@@@ @@  A@  3@@@@ +>Dynarray.ensure_extra_capacity39ensure_extra_capacity a n$ is ensure_capacity a (length a + n)9, it makes sure that !a. has room for !n- extra items.@@@@@@@@0Invalid_argument ;if the total requested capacity is outside the range 90 .. Sys.max_array_length!.@ & A use case would be to implement  D@&: let append_array a arr = ensure_extra_capacity a (Array.length arr); Array.iter (fun v -> add_last a v) arr @@@@@ Z@  [@  M@@@@ E5Dynarray.fit_capacity3.fit_capacity a Y reallocates a backing array if necessary, so that the resulting capacity is exactly (length a , with no additional empty space at the end. This can be useful to make sure there is no memory wasted on a long-lived array.@7 Note that calling ,fit_capacity breaks the amortized complexity guarantees provided by the default reallocation strategy. Calling it repeatedly on an array may have quadratic complexity, both in time and in total number of words allocated.@ If you know that a dynamic array has reached its final length, which will remain fixed in the future, it is sufficient to call (to_array 3 and only keep the resulting fixed-size array. ,fit_capacity Q is useful when you need to keep a dynamic array for eventual future resizes.@@@@@@@@@@@@ i@  j@@@@ X5Dynarray.set_capacity30set_capacity a n Y reallocates a backing array if necessary, so that the resulting capacity is exactly !n +. In particular, all elements of index !n8 or greater are removed.@* Like ND@ , this function breaks the amortized complexity guarantees provided by the reallocation strategy. Calling it repeatedly on an array may have quadratic complexity, both in time and in total number of words allocated.@ 2 This is an advanced function; in particular, D@ b should be preferred to increase the capacity, as it preserves those amortized guarantees.@@@@@@@@0Invalid_argument#if %n < 0!.@@@@@ @ ] @ ` {@@@@ s.Dynarray.reset3'reset a( clears !a 2 and replaces its backing array by an empty array.@9 It is equivalent to 0set_capacity a 0$ or 7clear a; fit_capacity a!.@@@@@@@@@@@@ @  @@@@ |B'noleaks )No leaks: preservation of memory liveness@@ = The user-provided values reachable from a dynamic array !a - are exactly the elements in the indices !0$ to ,length a - 1 m. In particular, no user-provided values are "leaked" by being present in the backing array at index (length a* or later.@9Dynarray.unsafe_to_iarray3 t val add : t -> Elem.t -> unit val pop_min : t -> Elem.t option end = struct (* Our priority queues are implemented using the standard "min heap" data structure, a dynamic array representing a binary tree. *) type t = Elem.t Dynarray.t let create = Dynarray.create (* The node of index [i] has as children the nodes of index [2 * i + 1] and [2 * i + 2] -- if they are valid indices in the dynarray. *) let left_child i = 2 * i + 1 let right_child i = 2 * i + 2 let parent_node i = (i - 1) / 2 (* We use indexing operators for convenient notations. *) let ( .!() ) = Dynarray.get let ( .!()<- ) = Dynarray.set (* Auxiliary functions to compare and swap two elements in the dynamic array. *) let order h i j = Elem.compare h.!(i) h.!(j) let swap h i j = let v = h.!(i) in h.!(i) <- h.!(j); h.!(j) <- v (* We say that a heap respects the "heap ordering" if the value of each node is smaller than the value of its children. The algorithm manipulates arrays that respect the heap algorithm, except for one node whose value may be too small or too large. The auxiliary functions [heap_up] and [heap_down] take such a misplaced value, and move it "up" (respectively: "down") the tree by permuting it with its parent value (respectively: a child value) until the heap ordering is restored. *) let rec heap_up h i = if i = 0 then () else let parent = parent_node i in if order h i parent < 0 then (swap h i parent; heap_up h parent) and heap_down h ~len i = let left, right = left_child i, right_child i in if left >= len then () (* no child, stop *) else let smallest = if right >= len then left (* no right child *) else if order h left right < 0 then left else right in if order h i smallest > 0 then (swap h i smallest; heap_down h ~len smallest) let add h s = let i = Dynarray.length h in Dynarray.add_last h s; heap_up h i let pop_min h = if Dynarray.is_empty h then None else begin (* Standard trick: swap the 'best' value at index 0 with the last value of the array. *) let last = Dynarray.length h - 1 in swap h 0 last; (* At this point [pop_last] returns the 'best' value, and leaves a heap with one misplaced element at index [0]. *) let best = Dynarray.pop_last h in (* Restore the heap ordering -- does nothing if the heap is empty. *) heap_down h ~len:last 0; Some best end end @ The production code from which this example was inspired includes logic to free the backing array when the heap becomes empty, only in the case where the capacity is above a certain threshold. This can be done by calling the following function from #pop!:@! g let shrink h = if Dynarray.length h = 0 && Dynarray.capacity h > 1 lsl 18 then Dynarray.reset h @% The $Heap functor can be used to implement a sorting function, by adding all elements into a priority queue and then extracting them in order.@! let heap_sort (type a) cmp li = let module Heap = Heap(struct type t = a let compare = cmp end) in let heap = Heap.create () in List.iter (Heap.add heap) li; List.map (fun _ -> Heap.pop_min heap |> Option.get) li @@@ @@A#Seq@@@@@