Construct a binary summation tree to sum all the values
(api frontend.API, rs []RationalNumber)
| 27 | |
| 28 | // Construct a binary summation tree to sum all the values |
| 29 | func SumRationalNumbers(api frontend.API, rs []RationalNumber) RationalNumber { |
| 30 | n := len(rs) |
| 31 | if n == 0 { |
| 32 | return RationalNumber{Numerator: 0, Denominator: 1} |
| 33 | } |
| 34 | |
| 35 | if !IsPowerOf2(n) { |
| 36 | fmt.Println(n) |
| 37 | panic("The length of rs should be a power of 2") |
| 38 | } |
| 39 | |
| 40 | cur := rs |
| 41 | next := make([]RationalNumber, 0) |
| 42 | |
| 43 | for n > 1 { |
| 44 | n >>= 1 |
| 45 | for i := 0; i < n; i++ { |
| 46 | next = append(next, cur[i*2].Add(api, &cur[i*2+1])) |
| 47 | } |
| 48 | cur = next |
| 49 | next = next[:0] |
| 50 | } |
| 51 | |
| 52 | if len(cur) != 1 { |
| 53 | panic("Summation code may be wrong.") |
| 54 | } |
| 55 | |
| 56 | return cur[0] |
| 57 | } |
| 58 | |
| 59 | func SimpleMin(a uint, b uint) uint { |
| 60 | if a < b { |
no test coverage detected