diff options
-rw-r--r-- | gr-error-correcting-codes/src/lib/libecc/mld/n2bs.cc | 75 | ||||
-rw-r--r-- | gr-error-correcting-codes/src/lib/libecc/mld/n2bs.h | 205 |
2 files changed, 13 insertions, 267 deletions
diff --git a/gr-error-correcting-codes/src/lib/libecc/mld/n2bs.cc b/gr-error-correcting-codes/src/lib/libecc/mld/n2bs.cc index d32dec865..d64626c64 100644 --- a/gr-error-correcting-codes/src/lib/libecc/mld/n2bs.cc +++ b/gr-error-correcting-codes/src/lib/libecc/mld/n2bs.cc @@ -23,78 +23,15 @@ #include <n2bs.h> #include <iostream> -static const int g_num_bits_per_byte = 8; - -std::string DoIt (long long number, char digits) +std::string _n2bs_helper(unsigned long long v, size_t nbinary_digits) { - std::string retVal (digits, '0'); - if (number != 0) - for (char n = digits-1; n >= 0; n--) { - if (number & 1) { + std::string retVal (nbinary_digits, '0'); + if (v != 0) + for (int n = int(nbinary_digits) - 1; n >= 0; n--) { + if (v & 1) { retVal[n] = '1'; } - number >>= 1; + v >>= 1; } return (retVal); } - -std::string n2bs (char number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (char) * g_num_bits_per_byte)) - digits = sizeof (char) * g_num_bits_per_byte; - return DoIt ((long long) number, (char) digits); -} -std::string n2bs (short number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (short) * g_num_bits_per_byte)) - digits = sizeof (short) * g_num_bits_per_byte; - return DoIt ((long long) number, (char) digits); -} -std::string n2bs (int number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (int) * g_num_bits_per_byte)) - digits = sizeof (int) * g_num_bits_per_byte; - return DoIt ((long long) number, (char) digits); -} -std::string n2bs (long number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (long long) * g_num_bits_per_byte)) - digits = sizeof (long long) * g_num_bits_per_byte; - return DoIt (number, (char) digits); -} -std::string n2bs (long long number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (long long) * g_num_bits_per_byte)) - digits = sizeof (long long) * g_num_bits_per_byte; - return DoIt (number, (char) digits); -} -std::string n2bs (unsigned char number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (unsigned char) * g_num_bits_per_byte)) - digits = sizeof (unsigned char) * g_num_bits_per_byte; - return DoIt ((long long) number, (char) digits); -} -std::string n2bs (unsigned short number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (unsigned short) * g_num_bits_per_byte)) - digits = sizeof (unsigned short) * g_num_bits_per_byte; - return DoIt ((long long) number, (char) digits); -} -std::string n2bs (unsigned int number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (unsigned int) * g_num_bits_per_byte)) - digits = sizeof (unsigned int) * g_num_bits_per_byte; - return DoIt ((long long) number, (char) digits); -} -std::string n2bs (unsigned long number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (unsigned long long) * g_num_bits_per_byte)) - digits = sizeof (unsigned long long) * g_num_bits_per_byte; - return DoIt ((long long) number, (char) digits); -} -std::string n2bs (unsigned long long number, unsigned char digits) -{ - if (digits > (unsigned char)(sizeof (unsigned long long) * g_num_bits_per_byte)) - digits = sizeof (unsigned long long) * g_num_bits_per_byte; - return DoIt ((long long) number, (char) digits); -} diff --git a/gr-error-correcting-codes/src/lib/libecc/mld/n2bs.h b/gr-error-correcting-codes/src/lib/libecc/mld/n2bs.h index 5bd95c991..bc225e0f7 100644 --- a/gr-error-correcting-codes/src/lib/libecc/mld/n2bs.h +++ b/gr-error-correcting-codes/src/lib/libecc/mld/n2bs.h @@ -24,205 +24,14 @@ #define INCLUDED_N2BS_H #include <string> +#include <algorithm> -std::string n2bs (char number, unsigned char digits); -std::string n2bs (short number, unsigned char digits); -std::string n2bs (int number, unsigned char digits); -std::string n2bs (long number, unsigned char digits); -std::string n2bs (long long number, unsigned char digits); -std::string n2bs (unsigned char number, unsigned char digits); -std::string n2bs (unsigned short number, unsigned char digits); -std::string n2bs (unsigned int number, unsigned char digits); -std::string n2bs (unsigned long number, unsigned char digits); -std::string n2bs (unsigned long long number, unsigned char digits); +std::string _n2bs_helper(unsigned long long v, size_t nbinary_digits); -inline std::string n2bs (char number, char digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (short number, char digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (int number, char digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long number, char digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long long number, char digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned char number, char digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned short number, char digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned int number, char digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long number, char digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long long number, char digits) -{return (n2bs (number, (unsigned char) digits));}; - -inline std::string n2bs (char number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (short number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (int number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long long number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned char number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned short number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned int number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long long number, unsigned short digits) -{return (n2bs (number, (unsigned char) digits));}; - -inline std::string n2bs (char number, short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (short number, short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (int number, short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long number, short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long long number, short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned char number, short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned short number, short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned int number, short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long number, short digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long long number, short digits) -{return (n2bs (number, (unsigned char) digits));}; - -inline std::string n2bs (char number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (short number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (int number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long long number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned char number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned short number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned int number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long long number, unsigned int digits) -{return (n2bs (number, (unsigned char) digits));}; - -inline std::string n2bs (char number, int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (short number, int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (int number, int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long number, int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long long number, int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned char number, int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned short number, int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned int number, int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long number, int digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long long number, int digits) -{return (n2bs (number, (unsigned char) digits));}; - -inline std::string n2bs (char number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (short number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (int number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long long number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned char number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned short number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned int number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long long number, unsigned long digits) -{return (n2bs (number, (unsigned char) digits));}; - -inline std::string n2bs (char number, long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (short number, long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (int number, long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long number, long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long long number, long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned char number, long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned short number, long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned int number, long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long number, long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long long number, long digits) -{return (n2bs (number, (unsigned char) digits));}; - -inline std::string n2bs (char number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (short number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (int number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long long number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned char number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned short number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned int number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long long number, unsigned long long digits) -{return (n2bs (number, (unsigned char) digits));}; - -inline std::string n2bs (char number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (short number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (int number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (long long number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned char number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned short number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned int number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; -inline std::string n2bs (unsigned long long number, long long digits) -{return (n2bs (number, (unsigned char) digits));}; +template<class T> +std::string n2bs(T v, size_t nbinary_digits) +{ + return _n2bs_helper((unsigned long long)v, std::min(nbinary_digits, 8 * sizeof(T))); +} #endif /* INCLUDED_N2BS_H */ |