#ifndef INTEGER_H #define INTEGER_H #include #include #include struct Integer { // --- constructors ---------------------------- Integer() { mpz_init(value); } Integer(unsigned long int intval) { mpz_init_set_ui(value, intval); } Integer(signed long int intval) { mpz_init_set_si(value, intval); } Integer(char* s, int base = 10) { mpz_init_set_str(value, s, base); } Integer(const Integer& other) { mpz_init_set(value, other.value); } // --- deconstructor ---------------------------- ~Integer() { mpz_clear(value); } // --- assignment operators ---------------------------- Integer& operator=(mpz_t& otherval) { mpz_set(value, otherval); return *this; } Integer& operator=(const Integer& other) { mpz_set(value, other.value); return *this; } Integer& operator=(unsigned long int intval) { mpz_set_ui(value, intval); return *this; } Integer& operator=(signed long int intval) { mpz_set_si(value, intval); return *this; } Integer& operator=(char* str) { mpz_set_str(value, str, 10); return *this; } Integer& operator+=(const Integer& other) { mpz_add(value, value, other.value); return *this; } Integer& operator+=(unsigned long int intval) { mpz_add_ui(value, value, intval); return *this; } // --- comparison operators ---------------------------- bool operator<(const Integer& other) { return mpz_cmp(value, other.value) < 0; } bool operator>(const Integer& other) { return mpz_cmp(value, other.value) > 0; } bool operator==(const Integer& other) { return mpz_cmp(value, other.value) == 0; } // --- type conversion ---------------------------- operator mpz_t&() { return value; } // --- export ---------------------------- typedef unsigned int Word; Word* get_export(size_t& len) const { return (Word*) mpz_export(0, &len, 1, sizeof(Word), 1, 0, value); } void import(const Word* words, size_t len) { mpz_import(value, len, 1, sizeof(Word), 1, 0, (const void*) words); } // --- value ---------------------------- mpz_t value; }; std::ostream& operator<<(std::ostream& out, const Integer& i) { char* s = mpz_get_str(0, 10, i.value); out << s; delete s; return out; } #endif