[7576857] | 1 | /* |
---|
| 2 | Copyright (C) 2015 |
---|
| 3 | Alejandro Mujica (amujica en cenditel.gob.ve) |
---|
| 4 | José Angel Contreras (jancontreras en cenditel.gob.ve) |
---|
| 5 | Antonio Araujo (aaraujo en cenditel.gob.ve) |
---|
| 6 | Pedro Buitrago (pbuitrago en cenditel.gob.ve) |
---|
| 7 | |
---|
| 8 | CENDITEL Fundación Centro Nacional de Desarrollo e Investigación en |
---|
| 9 | Tecnologías Libres |
---|
| 10 | |
---|
| 11 | Este programa es software libre; Usted puede usarlo bajo los términos de la |
---|
| 12 | licencia de software GPL versión 2.0 de la Free Software Foundation. |
---|
| 13 | |
---|
| 14 | Este programa se distribuye con la esperanza de que sea útil, pero SIN |
---|
| 15 | NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o |
---|
| 16 | ADECUACIÓN A UN PROPÓSITO PARTICULAR. |
---|
| 17 | Consulte la licencia GPL para más detalles. Usted debe recibir una copia |
---|
| 18 | de la GPL junto con este programa; si no, escriba a la Free Software |
---|
| 19 | Foundation Inc. 51 Franklin Street,5 Piso, Boston, MA 02110-1301, USA. |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | /* |
---|
| 23 | Este archivo contiene la definiciones y (en algunos casos) implementaciones |
---|
| 24 | de funciones utilitarias para problemas presentes en el proyecto. |
---|
| 25 | |
---|
| 26 | Creado por: Alejandro J. Mujica |
---|
| 27 | Fecha de creación: |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | # ifndef UTIL_H |
---|
| 31 | # define UTIL_H |
---|
| 32 | |
---|
| 33 | # include <type_traits> |
---|
| 34 | |
---|
| 35 | /** Calcula la potencia @f$x^y@f$ con @f$x, y \in \mathcal{Z}^+@f$. |
---|
| 36 | * |
---|
| 37 | * @tparam NumberType |
---|
| 38 | * |
---|
| 39 | * @pre NumberType debe corresponder a alguno de los tipos enteros sin signo |
---|
| 40 | * de la biblioteca estándar de C++. |
---|
| 41 | * |
---|
| 42 | * |
---|
| 43 | * @param base Base de la operación. |
---|
| 44 | * @param exp Exponente de la operación. |
---|
| 45 | * |
---|
| 46 | * @return el resultado de la operación de elevar base al exponente. |
---|
| 47 | */ |
---|
| 48 | template <typename NumberType> |
---|
| 49 | NumberType positive_int_power(const NumberType & base, const NumberType & exp) |
---|
| 50 | { |
---|
| 51 | static_assert(std::is_integral<NumberType>::value and |
---|
| 52 | std::is_unsigned<NumberType>::value, |
---|
| 53 | "Template argument is not an unsigned integral type"); |
---|
| 54 | |
---|
| 55 | NumberType ret_val(1); |
---|
| 56 | |
---|
| 57 | for (NumberType i = 0; i < exp; ++i) |
---|
| 58 | ret_val *= base; |
---|
| 59 | |
---|
| 60 | return ret_val; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | # endif // UTIL_H |
---|