• R/O
  • HTTP
  • SSH
  • HTTPS

transunit: Commit

Transunit repository


Commit MetaInfo

Revision6d6bf3c888ef75164954dcb6dcc7539487800c2c (tree)
Zeit2020-05-25 11:47:06
AutorAlaskanEmily <emily@alas...>
CommiterAlaskanEmily

Log Message

Add compare instances for bitmap.bitmap and bitmap.slice

Ändern Zusammenfassung

Diff

--- a/transunit.compare.m
+++ b/transunit.compare.m
@@ -28,6 +28,7 @@
2828 :- interface.
2929 %==============================================================================%
3030
31+:- use_module bitmap.
3132 :- use_module bool.
3233 :- use_module rbtree.
3334 :- use_module pair.
@@ -53,6 +54,8 @@
5354 :- instance compare(int).
5455 :- instance compare(string).
5556 :- instance compare(float).
57+:- instance compare(bitmap.bitmap).
58+:- instance compare(bitmap.slice).
5659 :- instance compare(bool.bool).
5760 :- instance compare(maybe.maybe(T)) <= (to_string(T), compare(T)).
5861 :- instance compare(pair.pair(A, B)) <= (to_string(A), to_string(B), compare(A), compare(B)).
@@ -116,10 +119,39 @@
116119 %==============================================================================%
117120
118121 :- import_module float.
122+:- use_module exception.
119123 :- use_module int.
120124 :- use_module string.
121125 :- use_module std_util.
122126
127+%-----------------------------------------------------------------------------%
128+
129+:- func compare_bits(int, int, int, bitmap.bitmap, bitmap.bitmap) = maybe.maybe_error.
130+:- mode compare_bits(in, in, in, bitmap.bitmap_ui, bitmap.bitmap_ui) = (uo) is det.
131+:- mode compare_bits(di, di, di, bitmap.bitmap_ui, bitmap.bitmap_ui) = (uo) is det.
132+
133+compare_bits(N, I1, I2, BMP1, BMP2) = Result :-
134+ builtin.compare(Cmp, N, 0),
135+ (
136+ Cmp = (=),
137+ Result = maybe.ok
138+ ;
139+ Cmp = (<),
140+ exception.throw(exception.software_error("Invalid index in compare_bits"))
141+ ;
142+ Cmp = (>),
143+ Bit1 = BMP1 ^ bitmap.unsafe_bit(I1),
144+ Bit2 = BMP2 ^ bitmap.unsafe_bit(I2),
145+ ( if
146+ Bit1 = Bit2
147+ then
148+ Result = compare_bits(int.minus(N, 1), int.plus(I1, 1), int.plus(I2, 1), BMP1, BMP2)
149+ else
150+ Result = maybe.error(string.append(string.append(string.append(
151+ "Bit mismatch at bit ", string.from_int(I1)), ", "), string.from_int(I2)))
152+ )
153+ ).
154+
123155 %------------------------------------------------------------------------------%
124156
125157 :- instance to_string(int) where [
@@ -244,6 +276,38 @@ accumulate_mismatch(A, B, !List, I, int.plus(I, 1)) :-
244276 )
245277 ].
246278
279+:- instance transunit.compare(bitmap.bitmap) where [
280+ ( compare(BMP1, BMP2) = Result :-
281+ Size1 = BMP1 ^ bitmap.num_bits,
282+ ( if
283+ maybe.error(E) =
284+ transunit.compare(Size1, BMP2 ^ bitmap.num_bits)
285+ then
286+ Result = maybe.error(string.append("Bitmap size mismatch: ", E))
287+ else
288+ Result = compare_bits(Size1, 0, 0, BMP1, BMP2)
289+ )
290+ )
291+].
292+
293+:- instance transunit.compare(bitmap.slice) where [
294+ ( compare(Slice1, Slice2) = Result :-
295+ Size1 = Slice1 ^ bitmap.slice_num_bits,
296+ ( if
297+ maybe.error(E) =
298+ transunit.compare(Size1, Slice2 ^ bitmap.slice_num_bits)
299+ then
300+ Result = maybe.error(string.append("Bitmap slice size mismatch: ", E))
301+ else
302+ Result = compare_bits(Size1,
303+ Slice1 ^ bitmap.slice_start_bit_index,
304+ Slice2 ^ bitmap.slice_start_bit_index,
305+ Slice1 ^ bitmap.slice_bitmap,
306+ Slice2 ^ bitmap.slice_bitmap)
307+ )
308+ )
309+].
310+
247311 :- instance compare(bool.bool) where [
248312 ( compare(bool.yes, bool.yes) = maybe.ok ),
249313 ( compare(bool.no, bool.no) = maybe.ok ),
Show on old repository browser