X-Git-Url: https://git.llucax.com/z.facultad/75.29/dale.git/blobdiff_plain/b81213323fe3a25ea2f5f1dd4f95c3800854bb43..99ab093c910f1fdddfb7328f3ea8e9fff484e928:/src/number.h?ds=sidebyside diff --git a/src/number.h b/src/number.h index 4a52029..7df04e1 100644 --- a/src/number.h +++ b/src/number.h @@ -7,11 +7,17 @@ #include #include #include -#include +#include /* sizeof(E) tiene que ser 2*sizeof(N); y son los tipos nativos con los cuales * se haran las operaciones mas basicas. */ +template < typename N, typename E > +struct number; + +template < typename N, typename E > +std::ostream& operator<< (std::ostream& os, const number< N, E >& n); + template < typename N = uint32_t, typename E = uint64_t > struct number { @@ -75,6 +81,10 @@ struct number const_reverse_iterator rbegin() const { return chunk.rbegin(); } const_reverse_iterator rend() const { return chunk.rend(); } + // Friends + template < typename NN, typename EE > + friend std::ostream& operator<< (std::ostream& os, const number< NN, EE>& n); + private: // Atributos chunk_type chunk; @@ -205,7 +215,9 @@ template < typename N, typename E > std::ostream& operator<< (std::ostream& os, const number< N, E >& n) { // FIXME sacar una salida bonita en ASCII =) - std::copy(n.begin(), n.end(), std::ostream_iterator< N >(os, " ")); + for (typename number< N, E >::const_iterator i = n.chunk.begin(); + i != n.chunk.end(); ++i) + os << std::hex << *i << " "; return os; } @@ -273,7 +285,7 @@ std::pair< number< N, E >, number< N, E > > number< N, E >::split() const // es el algoritmo de divisiĆ³n y conquista, que se llama recursivamente template < typename N, typename E > -number < N, E > divide_n_conquer(number< N, E > u, number< N, E > v) +number < N, E > karatsuba(number< N, E > u, number< N, E > v) { typedef number< N, E > num_type; @@ -296,9 +308,9 @@ number < N, E > divide_n_conquer(number< N, E > u, number< N, E > v) // m = u1*v1 // d = u2*v2 // h = (u1+v1)*(u2+v2) = u1*u2+u1*v2+u2*v1+u2*v2 - num_type m = divide_n_conquer(u12.first, v12.first); - num_type d = divide_n_conquer(u12.second, v12.second); - num_type h = divide_n_conquer(u12.first + v12.first, + num_type m = karastuba(u12.first, v12.first); + num_type d = karastuba(u12.second, v12.second); + num_type h = karastuba(u12.first + v12.first, u12.second + v12.second); // H-D-M = u1*u2+u1*v2+u2*v1+u2*v2 - u2*v2 - u1*v1 = u1*v2+u2*v1