[llvm] [LLVM][ADT] Explicitly convert size_t values to SmallVector's size type (PR #77939)
Andrei Golubev via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 16 05:29:08 PST 2024
================
@@ -100,7 +100,18 @@ template <class Size_T> class SmallVectorBase {
/// This does not construct or destroy any elements in the vector.
void set_size(size_t N) {
assert(N <= capacity());
- Size = N;
+ Size = static_cast<Size_T>(N);
+ assert(size() == N);
+ }
+
+ /// Set the array capacity to \p M, which the current array size must
+ /// not be greater than.
+ ///
+ /// This does not construct or destroy any elements in the vector.
+ void set_capacity(size_t M) {
+ assert(size() <= M);
----------------
andrey-golubev wrote:
thanks, the assertion you propose would indeed be useful!
`size() <= M` is to just guarantee that we don't end up in a weird container state where `Size > Capacity`. but then using set_size() together with set_capacity() in the current sense would be problematic:
```
// capacity = 50, size = 40
// grow:
// set_size(70); // asserts: 70 <= capacity() fails
set_capacity(100); // OK
set_size(70); // OK
// shrink:
// set_capacity(10); // asserts: size() <= 10 fails
set_size(2); // OK
set_capacity(10); // OK
```
I guess due to this "swap" in the order, it might make it harder to write generic code? but then I again think of `Size > Capacity` problem.
https://github.com/llvm/llvm-project/pull/77939
More information about the llvm-commits
mailing list