| 20 | |
| 21 | template <typename T, typename U> |
| 22 | typename T::value_type::second_type distance_squared ( |
| 23 | const T& a, |
| 24 | const U& b |
| 25 | ) |
| 26 | { |
| 27 | typedef typename T::value_type::second_type scalar_type; |
| 28 | typedef typename U::value_type::second_type scalar_typeU; |
| 29 | // Both T and U must contain the same kinds of elements |
| 30 | COMPILE_TIME_ASSERT((is_same_type<scalar_type, scalar_typeU>::value)); |
| 31 | |
| 32 | typename T::const_iterator ai = a.begin(); |
| 33 | typename U::const_iterator bi = b.begin(); |
| 34 | |
| 35 | scalar_type sum = 0, temp = 0; |
| 36 | while (ai != a.end() && bi != b.end()) |
| 37 | { |
| 38 | if (ai->first == bi->first) |
| 39 | { |
| 40 | temp = ai->second - bi->second; |
| 41 | ++ai; |
| 42 | ++bi; |
| 43 | } |
| 44 | else if (ai->first < bi->first) |
| 45 | { |
| 46 | temp = ai->second; |
| 47 | ++ai; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | temp = bi->second; |
| 52 | ++bi; |
| 53 | } |
| 54 | |
| 55 | sum += temp*temp; |
| 56 | } |
| 57 | |
| 58 | while (ai != a.end()) |
| 59 | { |
| 60 | sum += ai->second*ai->second; |
| 61 | ++ai; |
| 62 | } |
| 63 | while (bi != b.end()) |
| 64 | { |
| 65 | sum += bi->second*bi->second; |
| 66 | ++bi; |
| 67 | } |
| 68 | |
| 69 | return sum; |
| 70 | } |
| 71 | |
| 72 | // ------------------------------------------------------------------------------------ |
| 73 |
no test coverage detected