void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::false_ const&, mpl::false_ const&) { // Load. // Non-trivial. // Non binary. bool s; ar & s; std::size_t limb_count; std::size_t byte_count; ar & byte_count; limb_count = byte_count / sizeof(limb_type) + (byte_count % sizeof(limb_type) ? 1 : 0); val.resize(limb_count, limb_count); limb_type* pl = val.limbs(); for(std::size_t i = 0; i < limb_count; ++i) { pl[i] = 0; for(std::size_t j = 0; (j < sizeof(limb_type)) && byte_count; ++j) { unsigned char byte; ar & byte; pl[i] |= static_cast<limb_type>(byte) << (j * CHAR_BIT); --byte_count; } } if(s != val.sign()) val.negate(); val.normalize(); }
std::string Num_to_Words(Int toconvert){ std::string toreturn(toconvert.sign() == -1 ? "negative " : ""); toconvert = toconvert.magnitude(); while(toconvert > 0){ size_t i( toconvert.count_digits() ); //key1 accesses words of order 10, like "thousand" //key2 accesses the single digits words, like "one" Int key1(Math::exponentiate(Int(k_ten), Int(i-1))), key2(toconvert / key1) ; switch(i){ case 1: //One digit toreturn += NumberWordBank.at(key2); break; case 2: //Two digits if(toconvert >= 2*k_ten) toreturn += NumberWordBank.at(key2 * k_ten) + (toconvert%k_ten > 0 ? "-" : "") ; else if(toconvert >= k_ten){ toreturn += NumberWordBank.at(toconvert); return toreturn; } break; case 3: //Three digits toreturn += NumberWordBank.at(key2) + ' '; toreturn += NumberWordBank.at(key1) + ' '; break; default: //Four or more digits key2 = toconvert; key1 = 1; while(key2.count_digits() > 3){ key1 *= k_thousand; key2 /= k_thousand; } toreturn += Num_to_Words(key2) + ' '; toreturn += NumberWordBank.at(key1) + ' '; break; } //Stop adding things once toconvert is 0 if(key1*key2 == toconvert) break; //Inserting "and" if( (i == 3 && toconvert%100 > 0) || (i >= 4 && toconvert%k_thousand < 100) ) toreturn += k_and; toconvert -= key1*key2; } while(toreturn.back() == ' ') toreturn.pop_back(); return toreturn; }
void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::true_ const&, mpl::true_ const&) { // Store. // Trivial. // Binary. bool s = val.sign(); ar & s; ar.save_binary(val.limbs(), sizeof(*val.limbs())); }
void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::true_ const&, mpl::true_ const&) { // Load. // Trivial. // Binary. bool s; ar & s; ar.load_binary(val.limbs(), sizeof(*val.limbs())); if(s != val.sign()) val.negate(); }
void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::false_ const&, mpl::true_ const&) { // Store. // Non-trivial. // Binary. bool s = val.sign(); std::size_t c = val.size(); ar & s; ar & c; ar.save_binary(val.limbs(), c * sizeof(limb_type)); }
void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::false_ const&, mpl::true_ const&) { // Load. // Non-trivial. // Binary. bool s; std::size_t c; ar & s; ar & c; val.resize(c, c); ar.load_binary(val.limbs(), c * sizeof(limb_type)); if(s != val.sign()) val.negate(); val.normalize(); }
void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::true_ const&, mpl::false_ const&) { // Store. // Trivial. // Non binary. bool s = val.sign(); typename Int::local_limb_type l = *val.limbs(); ar & s; std::size_t limb_count = sizeof(l); ar & limb_count; for(std::size_t i = 0; i < limb_count; ++i) { unsigned char b = static_cast<unsigned char>(static_cast<typename Int::local_limb_type>(l >> (i * CHAR_BIT)) & static_cast<typename Int::local_limb_type>((1u << CHAR_BIT) - 1)); ar & b; } }
void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::true_ const&, mpl::false_ const&) { // Load. // Trivial. // Non binary. bool s; typename Int::local_limb_type l = 0; ar & s; std::size_t byte_count; ar & byte_count; for(std::size_t i = 0; i < byte_count; ++i) { unsigned char b; ar & b; l |= static_cast<typename Int::local_limb_type>(b) << (i * CHAR_BIT); } *val.limbs() = l; if(s != val.sign()) val.negate(); }
void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::false_ const&, mpl::false_ const&) { // Store. // Non-trivial. // Non binary. bool s = val.sign(); ar & s; limb_type* pl = val.limbs(); std::size_t limb_count = val.size(); std::size_t byte_count = limb_count * sizeof(limb_count); ar & byte_count; for(std::size_t i = 0; i < limb_count; ++i) { limb_type l = pl[i]; for(std::size_t j = 0; j < sizeof(limb_type); ++j) { unsigned char byte = static_cast<unsigned char>((l >> (j * CHAR_BIT)) & ((1u << CHAR_BIT) - 1)); ar & byte; } } }