Skip to content

Commit 5eaf2bb

Browse files
committed
Remove unnecessary complexity from functions
1 parent 630bba6 commit 5eaf2bb

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

arby/include/arby/Nat.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -686,18 +686,18 @@ namespace com::saxbophone::arby {
686686
}
687687
/**
688688
* @brief bitwise AND-assignment
689-
* @note Complexity is @f$ \mathcal{O(n^2)} @f$ but should be @f$ \mathcal{O(n)} @f$
689+
* @note Complexity: @f$ \mathcal{O(n)} @f$
690690
*/
691691
constexpr Nat& operator&=(const Nat& rhs) {
692692
/*
693693
* if rhs has fewer digits than this, we can remove this' leading
694694
* digits because they would be AND'ed with implicit zero which is
695695
* always zero
696696
*/
697-
if (rhs._digits.size() < _digits.size()) { // XXX: redundant if!?
698-
// TODO: just work out how many times to remove, don't keep checking
699-
// .size() is O(n)
700-
while (_digits.size() > rhs._digits.size()) {
697+
std::size_t lhs_size = _digits.size();
698+
std::size_t rhs_size = rhs._digits.size();
699+
if (lhs_size > rhs_size) {
700+
for (std::size_t i = 0; i < lhs_size - rhs_size; i++) {
701701
_digits.pop_front();
702702
}
703703
}
@@ -713,14 +713,14 @@ namespace com::saxbophone::arby {
713713
*it &= *rhs_it;
714714
}
715715
// remove any leading zeroes
716-
while (_digits.size() > 0 and _digits.front() == 0) {
716+
while (not _digits.empty() and _digits.front() == 0) {
717717
_digits.pop_front();
718718
}
719719
return *this;
720720
}
721721
/**
722722
* @brief bitwise AND operator for Nat
723-
* @note Complexity is @f$ \mathcal{O(n^2)} @f$ but should be @f$ \mathcal{O(n)} @f$
723+
* @note Complexity: @f$ \mathcal{O(n)} @f$
724724
*/
725725
friend constexpr Nat operator&(Nat lhs, const Nat& rhs) {
726726
lhs &= rhs; // reuse member operator

0 commit comments

Comments
 (0)