Commits

Sébastien Ferré committed ac1a82a

Add of iterator.
Git

Comments (0)

Files changed (1)

 
     let diff bmp1 bmp2 = bmp1 land (lnot bmp2)
 
-(*
-    let fold f init bmp = Common.prof "Intmap.Bitmap_base.fold" (fun () ->
-      let res = ref init in
-      for x1 = 0 to base - 1 do
-	if mem x1 bmp
-	then res := f !res x1
-      done;
-      !res)
-*)
     let fold f init bmp = Common.prof "Intmap.Bitmap_base.fold" (fun () ->
       let res = ref init in
       let bmp1 = ref bmp in
 	then f x1
       done
 
+    class iterator bmp =
+      object (self)
+	inherit [int] Iterator.iterator
+	val mutable bmp1 = bmp
+	val mutable x1 = 0
+	method init = bmp1 <- bmp; x1 <- 0
+	method next = Common.prof "Intmap.Bitmap.iterator#next" (fun () ->
+	  if bmp1 <> 0
+	  then
+	    let ok = bmp1 land 1 <> 0 in
+	    let elt = x1 in
+	    bmp1 <- bmp1 lsr 1;
+	    x1 <- x1+1;
+	    if ok
+	    then Some elt
+	    else self#next
+	  else None)
+      end
+    let iterator bmp = new iterator bmp
+
     let filter f bmp = fold (fun res x -> if f x then res else remove x res) bmp bmp
   end
 
 	then Block (Block.remove_value b x1)
 	else Block (Block.replace_field b x1 (fun m_x1 -> remove_repr x2 m_x1))
 
+(* in progress
+    class ['a] iterator (m : 'a t) =
+      object
+	inherit [int * 'a] Iterator.iterator
+	val mutable pos = 0
+	val mutable lrepr = obj false m
+	method next =
+	  match repr with
+	  | Empty -> Iterator.empty ()
+	  | Full l -> Iterator.range pos (pos + exp_base.(l) - 1)
+	  | Single y -> Iterator.single y
+	  | Bitmap_base bmp -> Bitmap_base.iterator bmp
+	  | Block b ->
+      end
+*)
 
     let rec fold f init (m : 'a t) = Common.prof "Intmap.fold" (fun () ->
       fold_repr f init 0 (obj false m))