Anonymous avatar Anonymous committed 347ea80

Subarray_cat..

Comments (0)

Files changed (1)

 module S = Subarray
 ;
 
+(* invariant: subarrays are not empty *)
 type t 'a = array (Subarray.t 'a)
 ;
 
-value (array_filter : ('a -> bool) -> array 'a -> array 'a) pred arr =
-  let bads = Array.fold_left
-    (fun count x -> count + if not & pred x then 1 else 0)
-    0
-    arr
-  in
-  if bads = 0
-  then arr
-  else
-    let new_len = Array.length arr - bads in
-    if new_len = 0
-    then [| |]
-    else
-      let res = Array.make new_len arr.(0)
-      and i = ref 0 in
-      ( Array.iter
-          (fun x ->
-             if pred x
-             then
-               ( res.(i.val) := x
-               ; incr i
-               )
-             else ()
-          )
-          arr
-      ; res
-      )
-;
-
-value make_of_array arr =
-  array_filter (fun s -> S.length s <> 0) arr
-;
-
-value make lst = make_of_array & Array.of_list lst
+value make lst = Array.of_list (List.filter (fun s -> S.length s <> 0) lst)
 ;
 
 value (snoc_array : array 'a -> 'a -> array 'a) sc s =
   let sc_len = Array.length sc in
-  Array.init (sc_len + 1) & fun i ->
-    if i = sc_len
-    then s
-    else sc.(i)
+  let res = Array.make (sc_len + 1) s  (* filling last element here *) in
+  let () = Array.blit
+    sc 0
+    res 0
+    sc_len
+  in
+    res
 ;
 
 value snoc sc s =
-  make_of_array & snoc_array sc s
+  if S.length s = 0
+  then sc
+  else snoc_array sc s
 ;
 
 value length sc =
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.