| 89 | } |
| 90 | |
| 91 | size_t ComputeMaxElements(uint32_t bits, size_t capacity, uint32_t fpbits) { |
| 92 | if (bits == 0) return 0; |
| 93 | // Start with max_elements=capacity, and decrease max_elements until the corresponding capacity is capacity. |
| 94 | size_t max_elements = capacity; |
| 95 | while (true) { |
| 96 | size_t capacity_for_max_elements = ComputeCapacity(bits, max_elements, fpbits); |
| 97 | CHECK_SAFE(capacity_for_max_elements >= capacity); |
| 98 | if (capacity_for_max_elements <= capacity) return max_elements; |
| 99 | size_t adjust = capacity_for_max_elements - capacity; |
| 100 | // Decrementing max_elements by N will at most decrement the corresponding capacity by N. |
| 101 | // As the observed capacity is adjust too high, we can safely decrease max_elements by adjust. |
| 102 | // If that brings us into negative max_elements territory, no solution exists and we return 0. |
| 103 | if (max_elements < adjust) return 0; |
| 104 | max_elements -= adjust; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | } // namespace |
| 109 |
no test coverage detected