1. Dmitry Grebeniuk
  2. cadastr

Commits

Dmitry Grebeniuk  committed ae5c1a4

Array.GrowingArray

  • Participants
  • Parent commits d6dd464
  • Branches default

Comments (0)

Files changed (1)

File src/cd_Array.ml

View file
             invalid_arg "Array.GrowingArray.with_array: requested_size"
           else
             let arr =
-              if requested_size <= ga.cur_size
+              let cur_size = ga.cur_size in
+              if requested_size <= cur_size
               then
                 ga.cur_array
               else
-                let new_size = min (2 * ga.cur_size) Sys.max_array_length in
+                let new_size =
+                  let rec loop sz =
+                    let sz2 = 2 * sz in
+                    let new_size = min sz2 Sys.max_array_length in
+                    if new_size < requested_size
+                    then
+                      loop sz2
+                    else
+                      new_size
+                  in
+                    loop cur_size
+                in
                 let new_array = Array.make new_size ga.default_element in
                 ( ga.cur_size := new_size
                 ; ga.cur_array := new_array
                 ; new_array
                 )
             in
+            let () = assert (Array.length arr >= requested_size) in
               user_func arr
         ;