[m-rev.] diff: implement bitmap for erlang

Peter Wang wangp at students.csse.unimelb.edu.au
Mon Jul 30 16:01:22 AEST 2007


Estimated hours taken: 1
Branches: main

library/bitmap.m:
	Implement bitmaps in Erlang in terms of binaries.

README.Erlang:
	Delete `bitmap' from the list of unimplemented modules.


Index: README.Erlang
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/README.Erlang,v
retrieving revision 1.1
diff -u -r1.1 README.Erlang
--- README.Erlang	20 Jul 2007 01:21:59 -0000	1.1
+++ README.Erlang	27 Jul 2007 07:48:17 -0000
@@ -193,7 +193,6 @@
 
 	benchmarking
 	bit_buffer
-	bitmap
 	construct
 	deconstruct
 	dir
Index: library/bitmap.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/bitmap.m,v
retrieving revision 1.23
diff -u -r1.23 bitmap.m
--- library/bitmap.m	17 Jul 2007 06:22:52 -0000	1.23
+++ library/bitmap.m	27 Jul 2007 07:48:17 -0000
@@ -1570,6 +1570,13 @@
     SUCCESS_INDICATOR = MR_bitmap_eq(BM1, BM2);
 ").
 
+:- pragma foreign_proc("Erlang",
+    bitmap_equal(BM1::in, BM2::in),
+    [will_not_call_mercury, thread_safe, promise_pure, will_not_modify_trail],
+"
+    SUCCESS_INDICATOR = (BM1 =:= BM2)
+").
+
 bitmap_equal(BM1, BM2) :-
     BM1 ^ num_bits = (BM2 ^ num_bits) @ NumBits,
     bytes_equal(0, byte_index_for_bit(NumBits), BM1, BM2).
@@ -1600,6 +1607,20 @@
                 : MR_COMPARE_GREATER);
 ").
 
+:- pragma foreign_proc("Erlang",
+    bitmap_compare(Result::uo, BM1::in, BM2::in),
+    [will_not_call_mercury, thread_safe, promise_pure, will_not_modify_trail],
+"
+    if
+        BM1 =:= BM2 ->
+            Result = {'='};
+        BM1 < BM2 ->
+            Result = {'<'};
+        true ->
+            Result = {'>'}
+    end
+").
+
 bitmap_compare(Result, BM1, BM2) :-
     compare(Result0, BM1 ^ num_bits, (BM2 ^ num_bits) @ NumBits),
     ( if Result0 = (=) then
@@ -1663,6 +1684,13 @@
     NumBits = BM.num_bits;
 ").
 
+:- pragma foreign_proc("Erlang",
+    num_bits(BM::in) = (NumBits::out),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
+"
+    {_, NumBits} = BM
+").
+
 %-----------------------------------------------------------------------------%
 
 :- func 'num_bits :='(bitmap, num_bits) = bitmap.
@@ -1691,6 +1719,14 @@
     BM.num_bits = NumBits;
 ").
 
+:- pragma foreign_proc("Erlang",
+    'num_bits :='(BM0::bitmap_di, NumBits::in) = (BM::bitmap_uo),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
+"
+    {Bin, _} = BM0,
+    BM = {Bin, NumBits}
+").
+
 %-----------------------------------------------------------------------------%
 
 BM ^ byte(N) = 
@@ -1722,6 +1758,17 @@
     Byte = BM.elements[N];
 ").
 
+:- pragma foreign_proc("Erlang",
+    unsafe_byte(N::in, BM::in) = (Byte::out),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
+"
+    {Bin, _} = BM,
+    Size = size(Bin),
+    Left = N * 8,
+    Right = (Size - N - 1) * 8,
+    <<_:Left, Byte:8, _:Right>> = Bin
+").
+
 %-----------------------------------------------------------------------------%
 
 (BM ^ byte(N) := Byte) =
@@ -1757,6 +1804,17 @@
     BM.elements[N] = (byte) Byte;
 ").
 
+:- pragma foreign_proc("Erlang",
+    'unsafe_byte :='(N::in, BM0::bitmap_di, Byte::in) = (BM::bitmap_uo),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
+"
+    {Bin0, NumBits} = BM0,
+    {Left, Mid} = split_binary(Bin0, N),
+    {_, Right} = split_binary(Mid, 1),
+    Bin = list_to_binary([Left, Byte, Right]),
+    BM = {Bin, NumBits}
+").
+
 %-----------------------------------------------------------------------------%
 
 :- func allocate_bitmap(num_bits) = bitmap.
@@ -1783,6 +1841,15 @@
     BM = new MercuryBitmap(N);
 ").
 
+:- pragma foreign_proc("Erlang",
+    allocate_bitmap(N::in) = (BM::bitmap_uo),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
+"
+    Roundup = 8 * ((N + 7) div 8),
+    Bin = <<0:Roundup>>,
+    BM = {Bin, N}
+").
+
 :- func resize_bitmap(bitmap, num_bits) = bitmap.
 :- mode resize_bitmap(bitmap_di, in) = bitmap_uo is det.
 
@@ -1799,6 +1866,13 @@
     MR_copy_bitmap(BM, BM0);
 ").
 
+:- pragma foreign_proc("Erlang",
+    copy(BM0::in) = (BM::bitmap_uo),
+    [will_not_call_mercury, promise_pure, thread_safe, will_not_modify_trail],
+"
+    BM = BM0
+").
+
 copy(BM0) = BM :-
     NumBits = BM0 ^ num_bits,
     BM = clear_filler_bits(
--------------------------------------------------------------------------
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