Question to the SCOOP experts out there:
I have a class BIG_NUMBER implemented as an ARRAY [NATURAL_8] where each array `item' represents a "digit" in base-256. To naively add two numbers, a and b:
loop
sum [i] := a [i] + b [i] + carry
if sum [i] < a [i] then -- NATURAL wraps around past zero if too big
carry := 1
else
carry := 0
end
i := i + 1
end
The above algorithm is O(n), because the value of a particular digit must know the carry-in from the previous calculation, requiring the carry calculation to propagate from low-order to high-order digit. But, can we add all the digits concurrently, similarly to a carry look-ahead adder, in O(1) time?
We know, without looking at the whole number, that the addition of two digits at a particular place in the numbers will "generate" a carry to the next place if that sum is greater than the base. Using NATURAL_8 the sum of two digits wraps around past zero if the sum is greater than 255 so that the sum is less than both [or either] of the digits. We also can determine from the two digits if that sum could "propagate" a carry (i.e. there will be a carry-out if there was a carry-in). For NATURAL_8 that happens if the sum of the digits is equal to 255, because adding one from a carry-in makes the sum zero with a carry-out of one. So:
sum [i] := a[i] + b[i] + carry (a, b, i - 1)
where feature `carry' is something like:
carry (a_num, a_other: BIG_NUMBER; index: INTEGER): NATURAL_8
-- The carry-out generated at the `index'th digits of the two numbers
local
s: NATURAL_8
do
s := a_num [i] + a_other [i]
if s < a_num [i] or else
s = 255 and then carry (a_num, a_other, index - 1) = 1 then
Result := 1
end
end
So, my question. Is there a way to parallel this computation with SCOOP? What objects are separate, one or both BIG_NUMBERs? the Result of the computation? the indexes? What is signature of the separate calls and declarations?
Thanks for reading this far,
jjj