[m-rev.] for review: improvements to bitmap.m [1]

Simon Taylor staylr at gmail.com
Mon Feb 12 23:12:52 AEDT 2007


On 12-Feb-2007, Ralph Becket <rafe at csse.unimelb.edu.au> wrote:
> Simon Taylor, Sunday, 11 February 2007:
> > Improvements for bitmap.m, to make it more useable as a general container
> > for binary data.
> 
> Thanks for doing this.  I can't see any problems in the code.
> One thing: there have been several requests over the years for a byte
> array data type.  Would a bitmap be a suitable implementation for a
> byte array or vice versa?

That was pretty much why I did this; a bitmap with a size that is
a multiple of eight is a byte array (hence the `byte' field and the
byte copy functions).  I've seen a few hacked up byte array modules,
and decided not to create another one.
 
> > Index: library/bitmap.m
> > ===================================================================
> > +            OldLastByte = byte_index_for_bit(OldSize - 1),
> > +            NewLastByte = byte_index_for_bit(NewSize - 1),
> 
> I would have used an name like OldLastByteIdx or something like that.
> OldLastByte sounds like it refers to a byte rather than a byte index.

Done (and in other places).
 
> The convention in the original bitmap.m was to use if-then-else rather
> than ( -> ; ).  It's a good idea to just use one style in the same
> module.

Fixed.

Simon.



diff -u library/bitmap.m library/bitmap.m
--- library/bitmap.m
+++ library/bitmap.m
@@ -421,11 +421,11 @@
             % Fill in the trailing bits in the previous final byte.
             !:BM = set_trailing_bits_in_byte(!.BM, OldSize - 1,
                         InitializerByte),
-            OldLastByte = byte_index_for_bit(OldSize - 1),
-            NewLastByte = byte_index_for_bit(NewSize - 1),
-            ( if NewLastByte > OldLastByte then
-                !:BM = initialize_bitmap_bytes(!.BM, OldLastByte + 1,
-                            NewLastByte, InitializerByte)
+            OldLastByteIndex = byte_index_for_bit(OldSize - 1),
+            NewLastByteIndex = byte_index_for_bit(NewSize - 1),
+            ( if NewLastByteIndex > OldLastByteIndex then
+                !:BM = initialize_bitmap_bytes(!.BM, OldLastByteIndex + 1,
+                            NewLastByteIndex, InitializerByte)
               else
                 true
             )
@@ -475,12 +475,12 @@
     byte) = bitmap.
 :- mode initialize_bitmap_bytes(bitmap_di, in, in, in) = bitmap_uo is det.
 
-initialize_bitmap_bytes(BM, Byte, LastByte, Init) =
-    ( Byte > LastByte ->
+initialize_bitmap_bytes(BM, ByteIndex, LastByteIndex, Init) =
+    ( if ByteIndex > LastByteIndex then
         BM
-    ;
-        initialize_bitmap_bytes(BM ^ unsafe_byte(Byte) := Init,
-            Byte + 1, LastByte, Init)
+      else
+        initialize_bitmap_bytes(BM ^ unsafe_byte(ByteIndex) := Init,
+            ByteIndex + 1, LastByteIndex, Init)
     ).
 
 %-----------------------------------------------------------------------------%
@@ -525,9 +525,9 @@
     ).
 
 BM ^ unsafe_bits(FirstBit, NumBits) = Bits :-
-    FirstByte = byte_index_for_bit(FirstBit),
+    FirstByteIndex = byte_index_for_bit(FirstBit),
     FirstBitIndex = bit_index_in_byte(FirstBit),
-    extract_bits_from_bytes(FirstByte, FirstBitIndex,
+    extract_bits_from_bytes(FirstByteIndex, FirstBitIndex,
         NumBits, BM, 0, Bits).
 
     % Extract the given number of bits starting at the most significant.
@@ -536,18 +536,22 @@
 %:- mode extract_bits_from_bytes(in, in, in, bitmap_ui, in, out) is det.
 :- mode extract_bits_from_bytes(in, in, in, in, in, out) is det.
 
-extract_bits_from_bytes(FirstByte, FirstBitIndex, NumBits, BM, !Bits) :-
+extract_bits_from_bytes(FirstByteIndex, FirstBitIndex, NumBits, BM, !Bits) :-
     RemainingBitsInByte = bits_per_byte - FirstBitIndex,
-    ( NumBits > RemainingBitsInByte ->
+    ( if
+        NumBits > RemainingBitsInByte
+      then
         NumBitsThisByte = RemainingBitsInByte,
-        extract_bits_from_byte_index(FirstByte, FirstBitIndex,
+        extract_bits_from_byte_index(FirstByteIndex, FirstBitIndex,
             NumBitsThisByte, BM, !Bits),
-        extract_bits_from_bytes(FirstByte + 1, 0,
+        extract_bits_from_bytes(FirstByteIndex + 1, 0,
             NumBits - NumBitsThisByte, BM, !Bits)
-    ; NumBits > 0 ->
-        extract_bits_from_byte_index(FirstByte, FirstBitIndex,
+      else if
+        NumBits > 0
+      then
+        extract_bits_from_byte_index(FirstByteIndex, FirstBitIndex,
             NumBits, BM, !Bits)
-    ;
+      else
         true
     ).
 
@@ -578,27 +582,27 @@
 
 (BM0 ^ unsafe_bits(FirstBit, NumBits) := Bits) = BM :-
     LastBit = FirstBit + NumBits - 1,
-    LastByte = byte_index_for_bit(LastBit),
+    LastByteIndex = byte_index_for_bit(LastBit),
     LastBitIndex = bit_index_in_byte(LastBit),
-    set_bits_in_bytes(LastByte, LastBitIndex, NumBits, Bits, BM0, BM).
+    set_bits_in_bytes(LastByteIndex, LastBitIndex, NumBits, Bits, BM0, BM).
 
     % Set the given number of bits starting at the least significant.
 :- pred set_bits_in_bytes(byte_index, bit_index_in_byte, num_bits,
     word, bitmap, bitmap).
 :- mode set_bits_in_bytes(in, in, in, in, bitmap_di, bitmap_uo) is det.
 
-set_bits_in_bytes(LastByte, LastBitIndex, NumBits, Bits, !BM) :-
+set_bits_in_bytes(LastByteIndex, LastBitIndex, NumBits, Bits, !BM) :-
     RemainingBitsInByte = LastBitIndex + 1,
-    ( NumBits > RemainingBitsInByte ->
+    ( if NumBits > RemainingBitsInByte then
         NumBitsThisByte = RemainingBitsInByte,
-        set_bits_in_byte_index(LastByte, LastBitIndex, NumBitsThisByte,
+        set_bits_in_byte_index(LastByteIndex, LastBitIndex, NumBitsThisByte,
             Bits, !BM),
-        set_bits_in_bytes(LastByte - 1, bits_per_byte - 1,
+        set_bits_in_bytes(LastByteIndex - 1, bits_per_byte - 1,
             NumBits - NumBitsThisByte,
             Bits `unchecked_right_shift` NumBitsThisByte, !BM)
-    ; NumBits > 0 ->
-        set_bits_in_byte_index(LastByte, LastBitIndex, NumBits, Bits, !BM)
-    ;
+      else if NumBits > 0 then
+        set_bits_in_byte_index(LastByteIndex, LastBitIndex, NumBits, Bits, !BM)
+      else
         true
     ).
 
@@ -757,7 +761,7 @@
       else zip2(byte_index_for_bit(num_bits(BMb) - 1), Fn, BMa, BMb)
     ).
 
-:- func zip2(int, func(byte, byte) = byte,
+:- func zip2(byte_index, func(byte, byte) = byte,
     bitmap, bitmap) = bitmap.
 %:- mode zip2(in, func(in, in) = out is det,
 %    bitmap_ui, bitmap_di) = bitmap_uo is det.
@@ -787,16 +791,16 @@
 :- mode copy_bits(in, in, in, bitmap_di, in, in) = bitmap_uo is det.
 
 copy_bits(SameBM, SrcBM, SrcStartBit, DestBM, DestStartBit, NumBits) =
-    (
+    ( if
         NumBits >= 0,
-        in_range(SrcBM, SrcStartBit),
+        SrcStartBit >= 0,
         in_range(SrcBM, SrcStartBit + NumBits - 1),
-        in_range(DestBM, DestStartBit),
+        DestStartBit >= 0,
         in_range(DestBM, DestStartBit + NumBits - 1)
-    ->
+      then
         unsafe_copy_bits(SameBM, SrcBM, SrcStartBit,
             DestBM, DestStartBit, NumBits)
-    ; 
+      else
         throw_bitmap_error(
           "bitmap.copy_bits_in_bitmap: out of range")
     ).
@@ -812,18 +816,14 @@
         !.NumBits) = !:DestBM :-
     SrcStartIndex = bit_index_in_byte(SrcStartBit),
     DestStartIndex = bit_index_in_byte(DestStartBit),
-    (
-        !.NumBits < 2 * bits_per_byte
-    ->
+    ( if !.NumBits < 2 * bits_per_byte then
         %
         % The alternatives below don't handle ranges that don't
         % span a byte boundary.
         % 
         !:DestBM = !.DestBM ^ unsafe_bits(DestStartBit, !.NumBits) :=
                         SrcBM ^ unsafe_bits(SrcStartBit, !.NumBits)
-    ;
-        SrcStartIndex = DestStartIndex
-    ->
+      else if SrcStartIndex = DestStartIndex then
         %
         % Handle the common case where the bits to be moved
         % have the same offsets in each byte, so we can do a
@@ -833,46 +833,47 @@
         StartIndex = SrcStartIndex,
         SrcEndBit = SrcStartBit + !.NumBits - 1,
         EndIndex = bit_index_in_byte(SrcEndBit),
-        (
+        ( if
             StartIndex = 0,
             EndIndex = bits_per_byte - 1
-        ->
+         then
             %
             % It's an aligned block of bytes, move it.
             %
             NumBytes = !.NumBits `unchecked_quotient` bits_per_byte,
-            SrcStartByte = SrcStartBit `unchecked_quotient` bits_per_byte,
-            DestStartByte = DestStartBit `unchecked_quotient` bits_per_byte,
-            !:DestBM = unsafe_copy_bytes(SameBM, SrcBM, SrcStartByte,
-                            !.DestBM, DestStartByte, NumBytes)
-        ;
+            SrcStartByteIndex = SrcStartBit `unchecked_quotient` bits_per_byte,
+            DestStartByteIndex =
+                DestStartBit `unchecked_quotient` bits_per_byte,
+            !:DestBM = unsafe_copy_bytes(SameBM, SrcBM, SrcStartByteIndex,
+                            !.DestBM, DestStartByteIndex, NumBytes)
+         else
             % 
             % Grab the odd bits at each end of the block to move,
             % leaving a block of aligned bytes to move.
             %
-            ( StartIndex = 0 ->
+            ( if StartIndex = 0 then
                 NumBitsAtStart = 0,
                 StartBitsToSet = 0
-            ;
+              else
                 NumBitsAtStart = bits_per_byte - StartIndex,
-                SrcPartialStartByte = byte_index_for_bit(SrcStartBit),
+                SrcPartialStartByteIndex = byte_index_for_bit(SrcStartBit),
                 StartBitsToSet =
                     extract_bits_from_byte(
-                        SrcBM ^ unsafe_byte(SrcPartialStartByte),
+                        SrcBM ^ unsafe_byte(SrcPartialStartByteIndex),
                         StartIndex, NumBitsAtStart),
 
                 !:NumBits = !.NumBits - NumBitsAtStart
             ),
 
-            ( EndIndex = bits_per_byte - 1 ->
+            ( if EndIndex = bits_per_byte - 1 then 
                 NumBitsAtEnd = 0,
                 EndBitsToSet = 0
-            ;
+              else
                 NumBitsAtEnd = EndIndex + 1,
-                SrcPartialEndByte = byte_index_for_bit(SrcEndBit),
+                SrcPartialEndByteIndex = byte_index_for_bit(SrcEndBit),
                 EndBitsToSet =
                     extract_bits_from_byte(
-                        SrcBM ^ unsafe_byte(SrcPartialEndByte),
+                        SrcBM ^ unsafe_byte(SrcPartialEndByteIndex),
                         0, NumBitsAtEnd),
 
                 !:NumBits = !.NumBits - NumBitsAtEnd
@@ -882,48 +883,50 @@
             % Do the block copy.
             %
             NumBytes = !.NumBits `unchecked_quotient` bits_per_byte,
-            SrcStartByte = (SrcStartBit + NumBitsAtStart)
+            SrcStartByteIndex = (SrcStartBit + NumBitsAtStart)
                                 `unchecked_quotient` bits_per_byte,
-            DestStartByte = (DestStartBit + NumBitsAtStart)
+            DestStartByteIndex = (DestStartBit + NumBitsAtStart)
                                 `unchecked_quotient` bits_per_byte,
-            !:DestBM = unsafe_copy_bytes(SameBM, SrcBM, SrcStartByte,
-                            !.DestBM, DestStartByte, NumBytes),
+            !:DestBM = unsafe_copy_bytes(SameBM, SrcBM, SrcStartByteIndex,
+                            !.DestBM, DestStartByteIndex, NumBytes),
 
             %
             % Fill in the partial bytes at the start and end of the range.
             %
-            ( NumBitsAtStart \= 0 ->       
-                DestPartialStartByte = DestStartByte - 1,
+            ( if NumBitsAtStart \= 0 then
+                DestPartialStartByteIndex = DestStartByteIndex - 1,
                 !:DestBM =
-                    !.DestBM ^ unsafe_byte(DestPartialStartByte) :=
+                    !.DestBM ^ unsafe_byte(DestPartialStartByteIndex) :=
                         set_bits_in_byte(
-                            !.DestBM ^ unsafe_byte(DestPartialStartByte),
+                            !.DestBM ^ unsafe_byte(DestPartialStartByteIndex),
                             StartIndex, NumBitsAtStart, StartBitsToSet)
-            ;
+              else
                 true
             ),
 
-            ( NumBitsAtEnd \= 0 ->
-                DestPartialEndByte = DestStartByte + NumBytes,
+            ( if NumBitsAtEnd \= 0 then
+                DestPartialEndByteIndex = DestStartByteIndex + NumBytes,
                 !:DestBM =
-                    !.DestBM ^ unsafe_byte(DestPartialEndByte) :=
+                    !.DestBM ^ unsafe_byte(DestPartialEndByteIndex) :=
                         set_bits_in_byte(
-                            !.DestBM ^ unsafe_byte(DestPartialEndByte),
+                            !.DestBM ^ unsafe_byte(DestPartialEndByteIndex),
                             0, NumBitsAtEnd, EndBitsToSet)
-            ;
+              else
                 true
             )
         )
-    ;
+      else
         !:DestBM = unsafe_copy_unaligned_bits(SameBM, SrcBM, SrcStartBit,
                         !.DestBM, DestStartBit, !.NumBits)
     ).
 
-copy_bytes(SrcBM, SrcStartByte, DestBM, DestStartByte, NumBytes) =
-    copy_bytes(0, SrcBM, SrcStartByte, DestBM, DestStartByte, NumBytes).
-
-copy_bytes_in_bitmap(SrcBM, SrcStartByte, DestStartByte, NumBytes) =
-    copy_bytes(1, SrcBM, SrcStartByte, SrcBM, DestStartByte, NumBytes).
+copy_bytes(SrcBM, SrcStartByteIndex, DestBM, DestStartByteIndex, NumBytes) =
+    copy_bytes(0, SrcBM, SrcStartByteIndex,
+        DestBM, DestStartByteIndex, NumBytes).
+
+copy_bytes_in_bitmap(SrcBM, SrcStartByteIndex, DestStartByteIndex, NumBytes) =
+    copy_bytes(1, SrcBM, SrcStartByteIndex,
+        SrcBM, DestStartByteIndex, NumBytes).
 
     % The SameBM parameter is 1 if we are copying within the same bitmap
     % bitmap.  We use an `int' rather than a `bool' for easier interfacing
@@ -936,16 +939,16 @@
     bitmap_di, in, in) = bitmap_uo is det.
 
 copy_bytes(SameBM, SrcBM, SrcStartByte, DestBM, DestStartByte, NumBytes) =
-   ( 
+   ( if
         NumBytes >= 0,
-        byte_in_range(SrcBM, SrcStartByte),
+        SrcStartByte >= 0,
         byte_in_range(SrcBM, SrcStartByte + NumBytes - 1),
-        byte_in_range(DestBM, DestStartByte),
+        DestStartByte >= 0,
         byte_in_range(DestBM, DestStartByte + NumBytes - 1)
-    ->
+      then
         unsafe_copy_bytes(SameBM, SrcBM, SrcStartByte, DestBM,
             DestStartByte, NumBytes)
-    ;
+      else
         throw_bitmap_error("bitmap.copy_bytes: out of range")
     ).
 
@@ -957,33 +960,35 @@
     bitmap_di, in, in) = bitmap_uo is det.
 
 :- pragma foreign_proc("C",
-    unsafe_copy_bytes(SameBM::in, SrcBM::in, SrcFirstByte::in,
-        DestBM0::bitmap_di, DestFirstByte::in,
+    unsafe_copy_bytes(SameBM::in, SrcBM::in, SrcFirstByteIndex::in,
+        DestBM0::bitmap_di, DestFirstByteIndex::in,
         NumBytes::in) = (DestBM::bitmap_uo),
     [will_not_call_mercury, thread_safe, promise_pure, will_not_modify_trail],
 "
     DestBM = DestBM0;
     if (SameBM) {
-        memmove(DestBM->elements + DestFirstByte,
-            SrcBM->elements + SrcFirstByte, NumBytes);
+        memmove(DestBM->elements + DestFirstByteIndex,
+            SrcBM->elements + SrcFirstByteIndex, NumBytes);
     } else {
-        memcpy(DestBM->elements + DestFirstByte,
-            SrcBM->elements + SrcFirstByte, NumBytes);
+        memcpy(DestBM->elements + DestFirstByteIndex,
+            SrcBM->elements + SrcFirstByteIndex, NumBytes);
     }
 
 ").
 
-unsafe_copy_bytes(SameBM, SrcBM, SrcFirstByte,
-        !.DestBM, DestFirstByte, NumBytes) = !:DestBM :-
-    Direction = choose_copy_direction(SameBM, SrcFirstByte, DestFirstByte),
+unsafe_copy_bytes(SameBM, SrcBM, SrcFirstByteIndex,
+        !.DestBM, DestFirstByteIndex, NumBytes) = !:DestBM :-
+    Direction = choose_copy_direction(SameBM, SrcFirstByteIndex,
+                    DestFirstByteIndex),
     (
         Direction = left_to_right,
-        !:DestBM = unsafe_do_copy_bytes(SrcBM, SrcFirstByte,
-            !.DestBM, DestFirstByte, NumBytes, 1)
+        !:DestBM = unsafe_do_copy_bytes(SrcBM, SrcFirstByteIndex,
+            !.DestBM, DestFirstByteIndex, NumBytes, 1)
     ;
         Direction = right_to_left,
-        !:DestBM = unsafe_do_copy_bytes(SrcBM, SrcFirstByte + NumBytes - 1,
-            !.DestBM, DestFirstByte + NumBytes - 1, NumBytes, -1)
+        !:DestBM = unsafe_do_copy_bytes(SrcBM,
+            SrcFirstByteIndex + NumBytes - 1,
+            !.DestBM, DestFirstByteIndex + NumBytes - 1, NumBytes, -1)
     ).
 
 :- func unsafe_do_copy_bytes(bitmap, byte_index,
@@ -993,14 +998,15 @@
 :- mode unsafe_do_copy_bytes(in, in,
     bitmap_di, in, in, in) = bitmap_uo is det.
 
-unsafe_do_copy_bytes(SrcBM, SrcByteNo, DestBM, DestByteNo,
+unsafe_do_copy_bytes(SrcBM, SrcByteIndex, DestBM, DestByteIndex,
         NumBytes, AddForNext) =
-    ( NumBytes = 0 ->
+    ( if NumBytes = 0 then
         DestBM
-    ;
-        unsafe_do_copy_bytes(SrcBM, SrcByteNo + AddForNext, 
-            DestBM ^ unsafe_byte(DestByteNo) := SrcBM ^ unsafe_byte(SrcByteNo),
-            DestByteNo + AddForNext, NumBytes - 1, AddForNext)
+      else
+        unsafe_do_copy_bytes(SrcBM, SrcByteIndex + AddForNext, 
+            DestBM ^ unsafe_byte(DestByteIndex) :=
+                SrcBM ^ unsafe_byte(SrcByteIndex),
+            DestByteIndex + AddForNext, NumBytes - 1, AddForNext)
     ).
 
     %
@@ -1022,10 +1028,10 @@
     %
     DestStartIndex = bit_index_in_byte(DestStartBit),
     DestEndBit = DestStartBit + !.NumBits - 1,
-    ( DestStartIndex = 0 ->
+    ( if DestStartIndex = 0 then
         NumBitsAtStart = 0,
         StartBits = 0
-    ;
+      else
         NumBitsAtStart = bits_per_byte - DestStartIndex,
         StartBits = SrcBM ^ unsafe_bits(SrcStartBit, NumBitsAtStart),
         !:NumBits = !.NumBits - NumBitsAtStart
@@ -1035,10 +1041,10 @@
     NewDestStartBit = (DestStartBit + NumBitsAtStart),
 
     DestEndIndex = bit_index_in_byte(DestEndBit),
-    ( DestEndIndex = bits_per_byte - 1 ->
+    ( if DestEndIndex = bits_per_byte - 1 then
         NumBitsAtEnd = 0,
         EndBits = 0
-    ;
+      else
         NumBitsAtEnd = DestEndIndex + 1,
         SrcEndBit = NewSrcStartBit + !.NumBits - 1,
         EndBits =
@@ -1055,42 +1061,45 @@
 
     (
         Direction = left_to_right,
-        SrcStartByte = byte_index_for_bit(NewSrcStartBit),
-        DestStartByte = byte_index_for_bit(NewDestStartBit),
+        SrcStartByteIndex = byte_index_for_bit(NewSrcStartBit),
+        DestStartByteIndex = byte_index_for_bit(NewDestStartBit),
         !:DestBM = unsafe_copy_unaligned_bytes_ltor(SrcBM,
-                    SrcStartByte + 1, SrcBitIndex,
-                    SrcBM ^ unsafe_byte(SrcStartByte),
-                    !.DestBM, DestStartByte, NumBytes)
+                    SrcStartByteIndex + 1, SrcBitIndex,
+                    SrcBM ^ unsafe_byte(SrcStartByteIndex),
+                    !.DestBM, DestStartByteIndex, NumBytes)
     ;
         Direction = right_to_left,
-        SrcStartByte = byte_index_for_bit(NewSrcStartBit + !.NumBits - 1),
-        DestStartByte = byte_index_for_bit(NewDestStartBit + !.NumBits - 1),
+        SrcStartByteIndex = byte_index_for_bit(NewSrcStartBit + !.NumBits - 1),
+        DestStartByteIndex =
+            byte_index_for_bit(NewDestStartBit + !.NumBits - 1),
         !:DestBM = unsafe_copy_unaligned_bytes_rtol(SrcBM,
-                    SrcStartByte - 1, SrcBitIndex,
-                    SrcBM ^ unsafe_byte(SrcStartByte),
-                    !.DestBM, DestStartByte, NumBytes)
+                    SrcStartByteIndex - 1, SrcBitIndex,
+                    SrcBM ^ unsafe_byte(SrcStartByteIndex),
+                    !.DestBM, DestStartByteIndex, NumBytes)
     ),
 
     %
     % Fill in the partial bytes at the start and end of the range.
     %
-    ( NumBitsAtStart \= 0 ->       
-        PartialDestStartByte = byte_index_for_bit(DestStartBit),
+    ( if NumBitsAtStart \= 0 then
+        PartialDestStartByteIndex = byte_index_for_bit(DestStartBit),
         !:DestBM =
-            !.DestBM ^ unsafe_byte(PartialDestStartByte) :=
-                set_bits_in_byte(!.DestBM ^ unsafe_byte(PartialDestStartByte),
+            !.DestBM ^ unsafe_byte(PartialDestStartByteIndex) :=
+                set_bits_in_byte(
+                    !.DestBM ^ unsafe_byte(PartialDestStartByteIndex),
                     DestStartIndex, NumBitsAtStart, StartBits)
-    ;
+      else
         true
     ),
 
-    ( NumBitsAtEnd \= 0 ->
-        PartialDestEndByte = byte_index_for_bit(DestEndBit),
+    ( if NumBitsAtEnd \= 0 then
+        PartialDestEndByteIndex = byte_index_for_bit(DestEndBit),
         !:DestBM =
-            !.DestBM ^ unsafe_byte(PartialDestEndByte) :=
-                set_bits_in_byte(!.DestBM ^ unsafe_byte(PartialDestEndByte),
+            !.DestBM ^ unsafe_byte(PartialDestEndByteIndex) :=
+                set_bits_in_byte(
+                    !.DestBM ^ unsafe_byte(PartialDestEndByteIndex),
                     0, NumBitsAtEnd, EndBits)
-    ;
+      else
         true
     ).
 
@@ -1103,9 +1112,9 @@
 
 unsafe_copy_unaligned_bytes_ltor(SrcBM, SrcByteIndex, SrcBitIndex,
         PrevSrcByteBits, !.DestBM, DestByteIndex, NumBytes) = !:DestBM :-
-    ( NumBytes =< 0 ->
+    ( if NumBytes =< 0 then
         true
-    ;
+      else
         %
         % Combine parts of two adjacent bytes in the source bitmap
         % into one byte of the destination.
@@ -1144,9 +1153,9 @@
 
 unsafe_copy_unaligned_bytes_rtol(SrcBM, SrcByteIndex, SrcBitIndex,
         PrevSrcByteBits, !.DestBM, DestByteIndex, NumBytes) = !:DestBM :-
-    ( NumBytes =< 0 ->
+    ( if NumBytes =< 0 then
         true
-    ;
+      else
         %
         % Combine parts of two adjacent bytes in the source bitmap
         % into one byte of the destination.
@@ -1190,13 +1199,9 @@
 :- func choose_copy_direction(int, bit_index, bit_index) = copy_direction.
 
 choose_copy_direction(SameBM, SrcStartBit, DestStartBit) =
-    (
-        SameBM = 1,
-        SrcStartBit < DestStartBit
-    ->
-        right_to_left
-    ;
-        left_to_right
+    ( if SameBM = 1, SrcStartBit < DestStartBit
+      then right_to_left
+      else left_to_right
     ).
 
 %-----------------------------------------------------------------------------%
@@ -1216,39 +1221,39 @@
 :- mode to_string_chars(in, in, in, out) is det.
 
 to_string_chars(Index, BM, !Chars) :-
-    ( Index < 0 ->
+    ( if Index < 0 then
         true
-    ;
+      else
         Byte = BM ^ unsafe_byte(Index),
         Mask = n_bit_mask(4),
-        (
+        ( if
             char.int_to_hex_char((Byte `unchecked_right_shift` 4) /\ Mask,
                 HighChar),
             char.int_to_hex_char(Byte /\ Mask, LowChar)
-        ->
+          then
             !:Chars = [HighChar, LowChar | !.Chars],
             to_string_chars(Index - 1, BM, !Chars)
-        ;
+          else
             error("bitmap.to_string: internal error")
         )
     ).
 
 from_string(Str) = BM :-
     Len = length(Str),
-    ( Len >= 4 ->
+    ( if Len >= 4 then
         Str ^ unsafe_elem(0) = ('<'),
         char.is_digit(Str ^ unsafe_elem(1)),
         Str ^ unsafe_elem(Len - 1) = ('>'),
         string.sub_string_search(Str, ":", Colon),
         SizeStr = string.unsafe_substring(Str, 1, Colon - 1),
         string.to_int(SizeStr, Size),
-        ( Size >= 0 ->
+        ( if Size >= 0 then
             BM0 = allocate_bitmap(Size),
             hex_chars_to_bitmap(Str, Colon + 1, Len - 1, 0, BM0, BM)
-        ;
+          else
             fail
         )
-    ;
+      else
         fail
     ).
     
@@ -1256,12 +1261,12 @@
             bitmap::bitmap_di, bitmap::bitmap_uo) is semidet.
 
 hex_chars_to_bitmap(Str, Index, End, ByteIndex, !BM) :-
-    ( Index = End ->
+    ( if Index = End then
         true
-    ; Index + 1 = End ->
+      else if Index + 1 = End then
         % Each byte of the bitmap should have mapped to a pair of characters.
         fail
-    ;
+      else    
         char.is_hex_digit(Str ^ unsafe_elem(Index), HighNibble),
         char.is_hex_digit(Str ^ unsafe_elem(Index + 1), LowNibble),
         Byte = (HighNibble `unchecked_left_shift` 4) \/ LowNibble,
@@ -1286,15 +1291,15 @@
 :- mode bitmap_to_byte_strings(in, in, in) = out is det.
 
 bitmap_to_byte_strings(BM, NumBits, !.Strs) = !:Strs :-
-    ( NumBits =< 0 ->
+    ( if NumBits =< 0 then
         true
-    ;  
+      else
         ThisByte0 = BM ^ unsafe_byte(byte_index_for_bit(NumBits - 1)),
         LastBitIndex = bit_index_in_byte(NumBits - 1),
-        ( LastBitIndex = bits_per_byte - 1 ->
+        ( if LastBitIndex = bits_per_byte - 1 then
             BitsThisByte = bits_per_byte,
             ThisByte = ThisByte0
-        ;
+         else
             BitsThisByte = LastBitIndex + 1,
             ThisByte = ThisByte0 `unchecked_right_shift`
                             (bits_per_byte - BitsThisByte)
@@ -1314,21 +1319,21 @@
 hash(BM) = HashVal :-
     NumBits = BM ^ num_bits,
     NumBytes0 = NumBits `unchecked_quotient` bits_per_byte,
-    ( NumBits `unchecked_rem` bits_per_byte = 0 ->
+    ( if NumBits `unchecked_rem` bits_per_byte = 0 then
         NumBytes = NumBytes0
-    ;
+      else
         NumBytes = NumBytes0 + 1
     ),
     hash_2(BM, 0, NumBytes, 0, HashVal0),
     HashVal = HashVal0 `xor` NumBits.
 
-:- pred hash_2(bitmap::in, int::in, int::in, int::in, int::out) is det.
+:- pred hash_2(bitmap::in, byte_index::in, int::in, int::in, int::out) is det.
 
 hash_2(BM, Index, Length, !HashVal) :-
-    ( Index < Length ->
+    ( if Index < Length then
         combine_hash(BM ^ unsafe_byte(Index), !HashVal),
         hash_2(BM, Index + 1, Length, !HashVal)
-    ;
+      else
         true
     ).
 
@@ -1356,10 +1361,10 @@
 
 :- pragma foreign_code("Java", "
 public static class MercuryBitmap {
-    int num_bits;
-    byte[] elements;
+    public int num_bits;
+    public byte[] elements;
 
-    MercuryBitmap(int numBits) {
+    public MercuryBitmap(int numBits) {
         num_bits = numBits;
         elements = new byte[numBits / 8 + (((numBits % 8) != 0) ? 1 : 0)];
     }
@@ -1367,15 +1372,15 @@
 ").
 
 /* XXX UNTESTED
-:- pragma foreign_code("C#", "
+:- pragma foreign_decl("C#", "
 namespace mercury {
   namespace bitmap__csharp_code {
 
     public class MercuryBitmap {
-        int num_bits;
-        byte[] elements;
+        public int num_bits;
+        public byte[] elements;
 
-        MercuryBitmap(int numBits) {
+        public MercuryBitmap(int numBits) {
             num_bits = numBits;
             elements = new byte[numBits / 8 + (((numBits % 8) != 0) ? 1: 0)];
         }
@@ -1413,10 +1418,10 @@
 :- mode bytes_equal(in, in, in, in) is semidet.
 
 bytes_equal(Index, MaxIndex, BM1, BM2) :-
-    ( Index =< MaxIndex ->
+    ( if Index =< MaxIndex then
         BM1 ^ unsafe_byte(Index) = BM2 ^ unsafe_byte(Index),
         bytes_equal(Index + 1, MaxIndex, BM1, BM2)
-    ;
+      else
         true
     ).
 
@@ -1436,9 +1441,9 @@
 
 bitmap_compare(Result, BM1, BM2) :-
     compare(Result0, BM1 ^ num_bits, (BM2 ^ num_bits) @ NumBits),
-    ( Result0 = (=) ->
+    ( if Result0 = (=) then
         bytes_compare(Result, 0, byte_index_for_bit(NumBits), BM1, BM2)
-    ;
+      else
         Result = Result0
     ).
 
@@ -1447,14 +1452,14 @@
 :- mode bytes_compare(uo, in, in, in, in) is det.
 
 bytes_compare(Result, Index, MaxIndex, BM1, BM2) :-
-    ( Index =< MaxIndex ->
+    ( if Index =< MaxIndex then
         compare(Result0, BM1 ^ unsafe_byte(Index), BM2 ^ unsafe_byte(Index)),
-        ( Result0 = (=) ->
+        ( if Result0 = (=) then
             bytes_compare(Result, Index + 1, MaxIndex, BM1, BM2)
-        ;
+          else
             Result = Result0
         )
-    ;
+      else
         Result = (=)
     ).
 
@@ -1466,9 +1471,9 @@
     Bytes = NumBits `unchecked_quotient` bits_per_byte.
 
 det_num_bytes(BM) = Bytes :-
-    ( Bytes0 = num_bytes(BM) ->
+    ( if Bytes0 = num_bytes(BM) then
         Bytes = Bytes0
-    ;
+      else
         throw_bitmap_error("det_num_bytes: bitmap has a partial final byte")
     ).
 
@@ -1674,9 +1679,9 @@
 floor_to_multiple_of_bits_per_byte(X) = Floor :-
     Trunc = unchecked_quotient(X, bits_per_byte),
     Floor0 = Trunc * bits_per_byte,
-    ( Floor0 > X ->
+    ( if Floor0 > X then
         Floor = Floor0 - bits_per_byte
-    ;
+      else
         Floor = Floor0
     ).
 
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list