|
21 | 21 | #include <algorithm> |
22 | 22 | #include <cassert> |
23 | 23 | #include <chrono> |
| 24 | +#include <iterator> |
24 | 25 | #include <ostream> |
25 | 26 | #include <string> |
26 | 27 |
|
@@ -573,6 +574,52 @@ std::ostream& operator<<(std::ostream& os, |
573 | 574 | return os; |
574 | 575 | } |
575 | 576 |
|
| 577 | +bool ConsecutiveStringsOfMaxLen(std::string const& a, std::string const& b, |
| 578 | + std::size_t max_len) { |
| 579 | + assert(a.length() <= max_len); |
| 580 | + assert(b.length() <= max_len); |
| 581 | + |
| 582 | + if (max_len == 0) { |
| 583 | + return false; |
| 584 | + } |
| 585 | + |
| 586 | + if (a.length() < max_len) { |
| 587 | + return internal::ConsecutiveRowKeys(a, b); |
| 588 | + } |
| 589 | + |
| 590 | + // Note that at this point we are guaranteed that a is not empty but |
| 591 | + // let us be sure. |
| 592 | + assert(!a.empty()); |
| 593 | + |
| 594 | + // What is the rightmost index that we can increment by 1 to get |
| 595 | + // the next string in lexicographic order. |
| 596 | + absl::optional<std::size_t> index_to_inc = absl::nullopt; |
| 597 | + |
| 598 | + for (auto it = a.rbegin(); it != a.rend(); it++) { |
| 599 | + char c = *it; |
| 600 | + if (c < CHAR_MAX) { |
| 601 | + index_to_inc = (a.length() - 1) - std::distance(a.rbegin(), it); |
| 602 | + break; |
| 603 | + } |
| 604 | + } |
| 605 | + |
| 606 | + if (!index_to_inc.has_value()) { |
| 607 | + // a is the last string in lexicographical order for strings of |
| 608 | + // max_len chars, so it has no successor. |
| 609 | + return false; |
| 610 | + } |
| 611 | + |
| 612 | + std::string successor_to_a; |
| 613 | + // A copy but if we want we could fix that by a comparison in 3 |
| 614 | + // parts. However, row keys that are at maximum length are the rare |
| 615 | + // exception, presumably. |
| 616 | + successor_to_a = a; |
| 617 | + |
| 618 | + successor_to_a[index_to_inc.value()] += 1; |
| 619 | + |
| 620 | + return successor_to_a == b; |
| 621 | +} |
| 622 | + |
576 | 623 | } // namespace emulator |
577 | 624 | } // namespace bigtable |
578 | 625 | } // namespace cloud |
|
0 commit comments