Topic 27 of 27 · Expert
Append a bounded batch with the strong exception guarantee
Append all batch values or leave the original buffer unchanged on capacity or allocation failure.
C++20; compile with g++ -std=c++20.
Problem statement
Append all batch values or leave the original buffer unchanged on capacity or allocation failure.
Publish a complete in-memory batch without exposing partial mutation.
Input: Capacity and vector<int> batches.
Output: Updated immutable-view data after success.
- Existing size never exceeds limit.
- Single-threaded.
- Element type fixed to int.
Sample 1
Input: limit3 existing[1,2],batch[3,4] Output: error; data[1,2]
Oversized batch changes nothing.
Sample 2
Input: limit3 existing[1,2],batch[3] Output: [1,2,3]
Exactly fills capacity.
Brute-force solution
Append one value at a time and discover failure after partial insertion; this violates atomic batch semantics.
O(existing size + batch size)
#include <vector>
#include <stdexcept>
#include <iostream>
#include <cassert>
class BoundedBuffer{std::size_t limit;std::vector<int> items;public:explicit BoundedBuffer(std::size_t n):limit(n){items.reserve(n);}void append_batch(const std::vector<int>& batch){if(batch.size()>limit-items.size())throw std::length_error("capacity");auto next=items;next.insert(next.end(),batch.begin(),batch.end());items.swap(next);}const std::vector<int>& data()const{return items;}};
int main() {
BoundedBuffer b(3);b.append_batch({1,2});try{b.append_batch({3,4});assert(false);}catch(const std::length_error&){}assert((b.data()==std::vector<int>{1,2}));b.append_batch({3});assert(b.data().size()==3);std::cout<<b.data().size()<<"\n";
return 0;
}
Optimized solution
Build a replacement vector then swap only after complete insertion.
- Check remaining capacity with subtraction.
- Copy original data to temporary.
- Append entire batch there.
- Swap as the commit step.
#include <vector>
#include <stdexcept>
#include <iostream>
#include <cassert>
class BoundedBuffer{std::size_t limit;std::vector<int> items;public:explicit BoundedBuffer(std::size_t n):limit(n){items.reserve(n);}void append_batch(const std::vector<int>& batch){if(batch.size()>limit-items.size())throw std::length_error("capacity");auto next=items;next.insert(next.end(),batch.begin(),batch.end());items.swap(next);}const std::vector<int>& data()const{return items;}};
int main() {
BoundedBuffer b(3);b.append_batch({1,2});try{b.append_batch({3,4});assert(false);}catch(const std::length_error&){}assert((b.data()==std::vector<int>{1,2}));b.append_batch({3});assert(b.data().size()==3);std::cout<<b.data().size()<<"\n";
return 0;
}
Complexity analysis
Time: O(existing size + batch size)
Auxiliary space: O(existing size + batch size) temporary.
Test cases
- Zero capacity.
- Empty batch.
- Oversize.
- Allocation failure.
Supplied main asserts unchanged data after rejection and successful exact fill.Common mistakes
- Claiming in-place partial append has the strong guarantee.
- Using unchecked size+batch arithmetic.
Key takeaways
- Validate capacity first.
- Commit complete state.
- Explain the extra-copy trade-off.
Follow-up challenges
- How would generic throwing elements affect the guarantee?
- Can a reserved int vector support a cheaper commit strategy?