:- interface.
%==============================================================================%
+:- use_module bitmap.
:- use_module bool.
:- use_module rbtree.
:- use_module pair.
:- instance compare(int).
:- instance compare(string).
:- instance compare(float).
+:- instance compare(bitmap.bitmap).
+:- instance compare(bitmap.slice).
:- instance compare(bool.bool).
:- instance compare(maybe.maybe(T)) <= (to_string(T), compare(T)).
:- instance compare(pair.pair(A, B)) <= (to_string(A), to_string(B), compare(A), compare(B)).
%==============================================================================%
:- import_module float.
+:- use_module exception.
:- use_module int.
:- use_module string.
:- use_module std_util.
+%-----------------------------------------------------------------------------%
+
+:- func compare_bits(int, int, int, bitmap.bitmap, bitmap.bitmap) = maybe.maybe_error.
+:- mode compare_bits(in, in, in, bitmap.bitmap_ui, bitmap.bitmap_ui) = (uo) is det.
+:- mode compare_bits(di, di, di, bitmap.bitmap_ui, bitmap.bitmap_ui) = (uo) is det.
+
+compare_bits(N, I1, I2, BMP1, BMP2) = Result :-
+ builtin.compare(Cmp, N, 0),
+ (
+ Cmp = (=),
+ Result = maybe.ok
+ ;
+ Cmp = (<),
+ exception.throw(exception.software_error("Invalid index in compare_bits"))
+ ;
+ Cmp = (>),
+ Bit1 = BMP1 ^ bitmap.unsafe_bit(I1),
+ Bit2 = BMP2 ^ bitmap.unsafe_bit(I2),
+ ( if
+ Bit1 = Bit2
+ then
+ Result = compare_bits(int.minus(N, 1), int.plus(I1, 1), int.plus(I2, 1), BMP1, BMP2)
+ else
+ Result = maybe.error(string.append(string.append(string.append(
+ "Bit mismatch at bit ", string.from_int(I1)), ", "), string.from_int(I2)))
+ )
+ ).
+
%------------------------------------------------------------------------------%
:- instance to_string(int) where [
)
].
+:- instance transunit.compare(bitmap.bitmap) where [
+ ( compare(BMP1, BMP2) = Result :-
+ Size1 = BMP1 ^ bitmap.num_bits,
+ ( if
+ maybe.error(E) =
+ transunit.compare(Size1, BMP2 ^ bitmap.num_bits)
+ then
+ Result = maybe.error(string.append("Bitmap size mismatch: ", E))
+ else
+ Result = compare_bits(Size1, 0, 0, BMP1, BMP2)
+ )
+ )
+].
+
+:- instance transunit.compare(bitmap.slice) where [
+ ( compare(Slice1, Slice2) = Result :-
+ Size1 = Slice1 ^ bitmap.slice_num_bits,
+ ( if
+ maybe.error(E) =
+ transunit.compare(Size1, Slice2 ^ bitmap.slice_num_bits)
+ then
+ Result = maybe.error(string.append("Bitmap slice size mismatch: ", E))
+ else
+ Result = compare_bits(Size1,
+ Slice1 ^ bitmap.slice_start_bit_index,
+ Slice2 ^ bitmap.slice_start_bit_index,
+ Slice1 ^ bitmap.slice_bitmap,
+ Slice2 ^ bitmap.slice_bitmap)
+ )
+ )
+].
+
:- instance compare(bool.bool) where [
( compare(bool.yes, bool.yes) = maybe.ok ),
( compare(bool.no, bool.no) = maybe.ok ),