Transunit repository
Revision | 6d6bf3c888ef75164954dcb6dcc7539487800c2c (tree) |
---|---|
Zeit | 2020-05-25 11:47:06 |
Autor | AlaskanEmily <emily@alas...> |
Commiter | AlaskanEmily |
Add compare instances for bitmap.bitmap and bitmap.slice
@@ -28,6 +28,7 @@ | ||
28 | 28 | :- interface. |
29 | 29 | %==============================================================================% |
30 | 30 | |
31 | +:- use_module bitmap. | |
31 | 32 | :- use_module bool. |
32 | 33 | :- use_module rbtree. |
33 | 34 | :- use_module pair. |
@@ -53,6 +54,8 @@ | ||
53 | 54 | :- instance compare(int). |
54 | 55 | :- instance compare(string). |
55 | 56 | :- instance compare(float). |
57 | +:- instance compare(bitmap.bitmap). | |
58 | +:- instance compare(bitmap.slice). | |
56 | 59 | :- instance compare(bool.bool). |
57 | 60 | :- instance compare(maybe.maybe(T)) <= (to_string(T), compare(T)). |
58 | 61 | :- instance compare(pair.pair(A, B)) <= (to_string(A), to_string(B), compare(A), compare(B)). |
@@ -116,10 +119,39 @@ | ||
116 | 119 | %==============================================================================% |
117 | 120 | |
118 | 121 | :- import_module float. |
122 | +:- use_module exception. | |
119 | 123 | :- use_module int. |
120 | 124 | :- use_module string. |
121 | 125 | :- use_module std_util. |
122 | 126 | |
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 | + | |
123 | 155 | %------------------------------------------------------------------------------% |
124 | 156 | |
125 | 157 | :- instance to_string(int) where [ |
@@ -244,6 +276,38 @@ accumulate_mismatch(A, B, !List, I, int.plus(I, 1)) :- | ||
244 | 276 | ) |
245 | 277 | ]. |
246 | 278 | |
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 | + | |
247 | 311 | :- instance compare(bool.bool) where [ |
248 | 312 | ( compare(bool.yes, bool.yes) = maybe.ok ), |
249 | 313 | ( compare(bool.no, bool.no) = maybe.ok ), |