From 4c095bbc460facf2de6ff8317dde0c26404fb993 Mon Sep 17 00:00:00 2001 From: OndrejSladky Date: Thu, 21 Mar 2024 15:02:42 +0100 Subject: [PATCH] Minor cleanups. --- Makefile | 2 +- src/kmers.h | 4 +- src/prophasm.h | 4 +- src/uint256_t/uint256_t.cpp | 265 -------------------------------- src/uint256_t/uint256_t.include | 110 +------------ 5 files changed, 9 insertions(+), 376 deletions(-) diff --git a/Makefile b/Makefile index 8e9b52b..1331ab6 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ verify: $(PROG) $(SCRIPTS)/verify.py $(DATA)/spneumoniae.fa quick-verify: $(PROG) $(SCRIPTS)/verify.py $(DATA)/spneumoniae.fa python $(SCRIPTS)/verify.py $(DATA)/spneumoniae.fa --quick --interpath $(DATA)/spyogenes.fa -$(PROG): $(SRC)/main.cpp $(SRC)/$(wildcard *.cpp *.h *.hpp) src/version.h +$(PROG): $(SRC)/main.cpp $(SRC)/$(wildcard *.cpp *.h *.hpp) src/version.h $(wildcard $(UINT256)/*.cpp $(UINT256)/*.h $(UINT256)/*.include) ./create-version.sh $(CXX) $(CXXFLAGS) $(SRC)/main.cpp $(UINT256)/uint256_t.cpp $(SRC)/kthread.c -o $@ $(LDFLAGS) diff --git a/src/kmers.h b/src/kmers.h index 27040ea..7bf2422 100644 --- a/src/kmers.h +++ b/src/kmers.h @@ -114,7 +114,7 @@ const char letters[4] {'A', 'C', 'G', 'T'}; /// Return the index-th nucleotide from the encoded k-mer. template inline char NucleotideAtIndex(kmer_t encoded, int k, int index) { - return letters[(unsigned long)(encoded >> ((k - index - kmer_t(1)) << kmer_t(1))) & kmer_t(3)]; + return letters[(uint32_t)(encoded >> ((k - index - kmer_t(1)) << kmer_t(1))) & kmer_t(3)]; } /// Convert the encoded KMer representation to string. @@ -123,7 +123,7 @@ std::string NumberToKMer(kmer_t encoded, int length) { std::string ret(length, 'N'); for (int i = 0; i < length; ++i) { // The last two bits correspond to one nucleotide. - ret[length - i -1] = letters[(unsigned long)encoded & 3]; + ret[length - i -1] = letters[(uint32_t)(encoded & 3)]; // Move to the next letter. encoded >>= 2; } diff --git a/src/prophasm.h b/src/prophasm.h index d099cfb..6220942 100644 --- a/src/prophasm.h +++ b/src/prophasm.h @@ -60,7 +60,7 @@ void NextSimplitig(KHT *kMers, kmer_t begin, std::ostream& of, int k, bool comp } else { // Extend the simplitig to the right. eraseKMer(kMers, next, k, complements); - simplitig.emplace_back(letters[(unsigned int)ext]); + simplitig.emplace_back(letters[(uint32_t)ext]); last = next; } } else { @@ -71,7 +71,7 @@ void NextSimplitig(KHT *kMers, kmer_t begin, std::ostream& of, int k, bool comp } else { // Extend the simplitig to the left. eraseKMer(kMers, next, k, complements); - simplitig.emplace_front(letters[(unsigned int)ext]); + simplitig.emplace_front(letters[(uint32_t)ext]); first = next; } } diff --git a/src/uint256_t/uint256_t.cpp b/src/uint256_t/uint256_t.cpp index 0cc4858..82bcc00 100644 --- a/src/uint256_t/uint256_t.cpp +++ b/src/uint256_t/uint256_t.cpp @@ -10,39 +10,9 @@ const uint256_t uint256_0(0); const uint256_t uint256_1(1); const uint256_t uint256_max(uint128_t(-1), uint128_t(-1)); -/* -uint256_t::uint256_t(const std::string & s, uint8_t base) { - init_from_base(s.c_str(), base); -} - -uint256_t::uint256_t(const char * s, uint8_t base) { - init_from_base(s, base); -} -*/ - uint256_t::uint256_t(const bool & b) : uint256_t((uint8_t) b) {} -/* -void uint256_t::init_from_base(const char * s, uint8_t base) { - *this = 0; - - uint256_t power(1); - uint8_t digit; - int pos = strlen(s) - 1; - while(pos >= 0) { - digit = 0; - if('0' <= s[pos] && s[pos] <= '9') { - digit = s[pos] - '0'; - } else if('a' <= s[pos] && s[pos] <= 'z') { - digit = s[pos] - 'a' + 10; - } - *this += digit * power; - pos--; - power *= base; - } -} -*/ uint256_t & uint256_t::operator=(const bool & rhs) { UPPER = 0; @@ -321,133 +291,6 @@ uint256_t & uint256_t::operator-=(const uint256_t & rhs){ return *this; } -/* -uint256_t uint256_t::operator*(const uint128_t & rhs) const{ - return *this * uint256_t(rhs); -} - */ - -/* -uint256_t uint256_t::operator*(const uint256_t & rhs) const{ - // split values into 4 64-bit parts - uint128_t top[4] = {UPPER.upper(), UPPER.lower(), LOWER.upper(), LOWER.lower()}; - uint128_t bottom[4] = {rhs.upper().upper(), rhs.upper().lower(), rhs.lower().upper(), rhs.lower().lower()}; - uint128_t products[4][4]; - - // multiply each component of the values - for(int y = 3; y > -1; y--){ - for(int x = 3; x > -1; x--){ - products[3 - y][x] = top[x] * bottom[y]; - } - } - - // first row - uint128_t fourth64 = uint128_t(products[0][3].lower()); - uint128_t third64 = uint128_t(products[0][2].lower()) + uint128_t(products[0][3].upper()); - uint128_t second64 = uint128_t(products[0][1].lower()) + uint128_t(products[0][2].upper()); - uint128_t first64 = uint128_t(products[0][0].lower()) + uint128_t(products[0][1].upper()); - - // second row - third64 += uint128_t(products[1][3].lower()); - second64 += uint128_t(products[1][2].lower()) + uint128_t(products[1][3].upper()); - first64 += uint128_t(products[1][1].lower()) + uint128_t(products[1][2].upper()); - - // third row - second64 += uint128_t(products[2][3].lower()); - first64 += uint128_t(products[2][2].lower()) + uint128_t(products[2][3].upper()); - - // fourth row - first64 += uint128_t(products[3][3].lower()); - - // combines the values, taking care of carry over - return uint256_t(first64 << uint128_64, uint128_0) + - uint256_t(third64.upper(), third64 << uint128_64) + - uint256_t(second64, uint128_0) + - uint256_t(fourth64); -} - */ - -/* -uint256_t & uint256_t::operator*=(const uint128_t & rhs){ - return *this *= uint256_t(rhs); -} - -uint256_t & uint256_t::operator*=(const uint256_t & rhs){ - *this = *this * rhs; - return *this; -} - */ - -/* -std::pair uint256_t::divmod(const uint256_t & lhs, const uint256_t & rhs) const{ - // Save some calculations ///////////////////// - if (rhs == uint256_0){ - throw std::domain_error("Error: division or modulus by 0"); - } - else if (rhs == uint256_1){ - return std::pair (lhs, uint256_0); - } - else if (lhs == rhs){ - return std::pair (uint256_1, uint256_0); - } - else if ((lhs == uint256_0) || (lhs < rhs)){ - return std::pair (uint256_0, lhs); - } - - std::pair qr(uint256_0, lhs); - uint256_t copyd = rhs << (lhs.bits() - rhs.bits()); - uint256_t adder = uint256_1 << (lhs.bits() - rhs.bits()); - if (copyd > qr.second){ - copyd >>= uint256_1; - adder >>= uint256_1; - } - while (qr.second >= rhs){ - if (qr.second >= copyd){ - qr.second -= copyd; - qr.first |= adder; - } - copyd >>= uint256_1; - adder >>= uint256_1; - } - return qr; -} - - -uint256_t uint256_t::operator/(const uint128_t & rhs) const{ - return *this / uint256_t(rhs); -} - -uint256_t uint256_t::operator/(const uint256_t & rhs) const{ - return divmod(*this, rhs).first; -} - -uint256_t & uint256_t::operator/=(const uint128_t & rhs){ - return *this /= uint256_t(rhs); -} - -uint256_t & uint256_t::operator/=(const uint256_t & rhs){ - *this = *this / rhs; - return *this; -} - -uint256_t uint256_t::operator%(const uint128_t & rhs) const{ - return *this % uint256_t(rhs); -} - -uint256_t uint256_t::operator%(const uint256_t & rhs) const{ - return *this - (rhs * (*this / rhs)); -} - -uint256_t & uint256_t::operator%=(const uint128_t & rhs){ - return *this %= uint256_t(rhs); -} - -uint256_t & uint256_t::operator%=(const uint256_t & rhs){ - *this = *this % rhs; - return *this; -} - */ - uint256_t & uint256_t::operator++(){ *this += uint256_1; return *this; @@ -486,70 +329,6 @@ const uint128_t & uint256_t::lower() const { return LOWER; } -/* -std::vector uint256_t::export_bits() const { - std::vector ret; - ret.reserve(32); - UPPER.export_bits(ret); - LOWER.export_bits(ret); - return ret; -} - -std::vector uint256_t::export_bits_truncate() const { - std::vector ret = export_bits(); - - //prune the zeroes - int i = 0; - while (ret[i] == 0 && i < 64) i++; - ret.erase(ret.begin(), ret.begin() + i); - - return ret; -} - -uint16_t uint256_t::bits() const{ - uint16_t out = 0; - if (UPPER){ - out = 128; - uint128_t up = UPPER; - while (up){ - up >>= uint128_1; - out++; - } - } - else{ - uint128_t low = LOWER; - while (low){ - low >>= uint128_1; - out++; - } - } - return out; -} - */ - -/* -std::string uint256_t::str(uint8_t base, const unsigned int & len) const{ - if ((base < 2) || (base > 36)){ - throw std::invalid_argument("Base must be in the range 2-36"); - } - std::string out = ""; - if (!(*this)){ - out = "0"; - } - else{ - std::pair qr(*this, uint256_0); - do{ - qr = divmod(qr.first, base); - out = "0123456789abcdefghijklmnopqrstuvwxyz"[(uint8_t) qr.second] + out; - } while (qr.first); - } - if (out.size() < len){ - out = std::string(len - out.size(), '0') + out; - } - return out; -} -*/ - uint256_t operator&(const uint128_t & lhs, const uint256_t & rhs){ return rhs & lhs; } @@ -710,47 +489,3 @@ uint128_t & operator-=(uint128_t & lhs, const uint256_t & rhs){ lhs = (-(rhs - lhs)).lower(); return lhs; } - -/* -uint256_t operator*(const uint128_t & lhs, const uint256_t & rhs){ - return rhs * lhs; -} - -uint128_t & operator*=(uint128_t & lhs, const uint256_t & rhs){ - lhs = (rhs * lhs).lower(); - return lhs; -} - -uint256_t operator/(const uint128_t & lhs, const uint256_t & rhs){ - return uint256_t(lhs) / rhs; -} - -uint128_t & operator/=(uint128_t & lhs, const uint256_t & rhs){ - lhs = (uint256_t(lhs) / rhs).lower(); - return lhs; -} - -uint256_t operator%(const uint128_t & lhs, const uint256_t & rhs){ - return uint256_t(lhs) % rhs; -} - -uint128_t & operator%=(uint128_t & lhs, const uint256_t & rhs){ - lhs = (uint256_t(lhs) % rhs).lower(); - return lhs; -} - */ - -/* -std::ostream & operator<<(std::ostream & stream, const uint256_t & rhs){ - if (stream.flags() & stream.oct){ - stream << rhs.str(8); - } - else if (stream.flags() & stream.dec){ - stream << rhs.str(10); - } - else if (stream.flags() & stream.hex){ - stream << rhs.str(16); - } - return stream; -} - */ diff --git a/src/uint256_t/uint256_t.include b/src/uint256_t/uint256_t.include index 5dfa739..1e719a6 100644 --- a/src/uint256_t/uint256_t.include +++ b/src/uint256_t/uint256_t.include @@ -26,6 +26,10 @@ With much help from Auston Sterling Thanks to François Dessenne for convincing me to do a general rewrite of this class. + + +Ondřej Sladký: removed parts of this file as they were not needed and were +not compatible with __uint128_t. */ #ifndef __UINT256_T__ @@ -124,11 +128,6 @@ class uint256_t{ #endif {} - /* - // RHS input args only - std::vector export_bits() const; - std::vector export_bits_truncate() const; - */ // Assignment Operator uint256_t & operator=(const uint256_t & rhs) = default; uint256_t & operator=(uint256_t && rhs) = default; @@ -348,61 +347,6 @@ class uint256_t{ return *this = *this - uint256_t(rhs); } - /* - uint256_t operator*(const uint128_t & rhs) const; - uint256_t operator*(const uint256_t & rhs) const; - - template ::value, T>::type > - uint256_t operator*(const T & rhs) const{ - return *this * uint256_t(rhs); - } - uint256_t & operator*=(const uint128_t & rhs); - uint256_t & operator*=(const uint256_t & rhs); - - template ::value, T>::type > - uint256_t & operator*=(const T & rhs){ - return *this = *this * uint256_t(rhs); - } -*/ - - private: - std::pair divmod(const uint256_t & lhs, const uint256_t & rhs) const; - void init(const char * s); - void init_from_base(const char * s, uint8_t base); - - public: - uint256_t operator/(const uint128_t & rhs) const; - uint256_t operator/(const uint256_t & rhs) const; - - template ::value, T>::type > - uint256_t operator/(const T & rhs) const{ - return *this / uint256_t(rhs); - } - - uint256_t & operator/=(const uint128_t & rhs); - uint256_t & operator/=(const uint256_t & rhs); - - template ::value, T>::type > - uint256_t & operator/=(const T & rhs){ - return *this = *this / uint256_t(rhs); - } - - uint256_t operator%(const uint128_t & rhs) const; - uint256_t operator%(const uint256_t & rhs) const; - - template ::value, T>::type > - uint256_t operator%(const T & rhs) const{ - return *this % uint256_t(rhs); - } - - uint256_t & operator%=(const uint128_t & rhs); - uint256_t & operator%=(const uint256_t & rhs); - - template ::value, T>::type > - uint256_t & operator%=(const T & rhs){ - return *this = *this % uint256_t(rhs); - } - // Increment Operators uint256_t & operator++(); uint256_t operator++(int); @@ -590,50 +534,4 @@ T & operator-=(T & lhs, const uint256_t & rhs){ return lhs = static_cast (-(rhs - lhs)); } -/* -UINT256_T_EXTERN uint256_t operator*(const uint128_t & lhs, const uint256_t & rhs); - -template ::value, T>::type > -uint256_t operator*(const T & lhs, const uint256_t & rhs){ - return rhs * lhs; -} - -UINT256_T_EXTERN uint128_t & operator*=(uint128_t & lhs, const uint256_t & rhs); - -template ::value, T>::type > -T & operator*=(T & lhs, const uint256_t & rhs){ - return lhs = static_cast (rhs * lhs); -} - */ - -UINT256_T_EXTERN uint256_t operator/(const uint128_t & lhs, const uint256_t & rhs); - -template ::value, T>::type > -uint256_t operator/(const T & lhs, const uint256_t & rhs){ - return uint256_t(lhs) / rhs; -} - -UINT256_T_EXTERN uint128_t & operator/=(uint128_t & lhs, const uint256_t & rhs); - -template ::value, T>::type > -T & operator/=(T & lhs, const uint256_t & rhs){ - return lhs = static_cast (uint256_t(lhs) / rhs); -} - -UINT256_T_EXTERN uint256_t operator%(const uint128_t & lhs, const uint256_t & rhs); - -template ::value, T>::type > -uint256_t operator%(const T & lhs, const uint256_t & rhs){ - return uint256_t(lhs) % rhs; -} - -UINT256_T_EXTERN uint128_t & operator%=(uint128_t & lhs, const uint256_t & rhs); - -template ::value, T>::type > -T & operator%=(T & lhs, const uint256_t & rhs){ - return lhs = static_cast (uint256_t(lhs) % rhs); -} - -// IO Operator -UINT256_T_EXTERN std::ostream & operator<<(std::ostream & stream, const uint256_t & rhs); #endif