diff options
author | 2021-05-13 11:31:11 -0700 | |
---|---|---|
committer | 2021-05-13 15:59:01 -0400 | |
commit | aad2c8a3966424bbaa2f26027d104d17f9c1c1ae (patch) | |
tree | ab4f61bfb0ec4af44fbdd08bb0be7a44f8335a2d /absl/strings/internal/resize_uninitialized.h | |
parent | ce42de10fbea616379826e91c7c23c16bffe6e61 (diff) |
Export of internal Abseil changes
--
912c205cf80c4ed24a08000c04263403857b7f75 by Abseil Team <absl-team@google.com>:
Internal change
PiperOrigin-RevId: 373620391
--
d454f10549d27d7b025d1ce0ef90a0cdec42c361 by Martijn Vels <mvels@google.com>:
Add SetCordRepForTesting() helper to CordzInfo
PiperOrigin-RevId: 373602832
--
7a2d7bdd2e60fb51333e81cd71ebd0a4edb60704 by Evan Brown <ezb@google.com>:
For StrAppend, make sure to follow exponential growth like std::string::append.
PiperOrigin-RevId: 373589332
--
24397f19bce45133a42feaa7c883009ef9325095 by Greg Falcon <gfalcon@google.com>:
Import of CCTZ from GitHub.
PiperOrigin-RevId: 373569246
GitOrigin-RevId: 912c205cf80c4ed24a08000c04263403857b7f75
Change-Id: I5444f7ca2c0980685ca5c51596c259b845d69673
Diffstat (limited to 'absl/strings/internal/resize_uninitialized.h')
-rw-r--r-- | absl/strings/internal/resize_uninitialized.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/absl/strings/internal/resize_uninitialized.h b/absl/strings/internal/resize_uninitialized.h index e42628e3..749c66e7 100644 --- a/absl/strings/internal/resize_uninitialized.h +++ b/absl/strings/internal/resize_uninitialized.h @@ -17,6 +17,7 @@ #ifndef ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_ #define ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_ +#include <algorithm> #include <string> #include <type_traits> #include <utility> @@ -66,6 +67,28 @@ inline void STLStringResizeUninitialized(string_type* s, size_t new_size) { ResizeUninitializedTraits<string_type>::Resize(s, new_size); } +// Used to ensure exponential growth so that the amortized complexity of +// increasing the string size by a small amount is O(1), in contrast to +// O(str->size()) in the case of precise growth. +template <typename string_type> +void STLStringReserveAmortized(string_type* s, size_t new_size) { + const size_t cap = s->capacity(); + if (new_size > cap) { + // Make sure to always grow by at least a factor of 2x. + s->reserve((std::max)(new_size, 2 * cap)); + } +} + +// Like STLStringResizeUninitialized(str, new_size), except guaranteed to use +// exponential growth so that the amortized complexity of increasing the string +// size by a small amount is O(1), in contrast to O(str->size()) in the case of +// precise growth. +template <typename string_type> +void STLStringResizeUninitializedAmortized(string_type* s, size_t new_size) { + STLStringReserveAmortized(s, new_size); + STLStringResizeUninitialized(s, new_size); +} + } // namespace strings_internal ABSL_NAMESPACE_END } // namespace absl |