llvm.org GIT mirror llvm / c2da6fb
Add SmallVector::{capacity,set_size}. - These allow clients to make use of the extra elements in the vector which have already been allocated, without requiring them to be value initialized. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79433 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 11 years ago
2 changed file(s) with 39 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
121121
122122
123123 reference operator[](unsigned idx) {
124 assert (Begin + idx < End);
124 assert(Begin + idx < End);
125125 return Begin[idx];
126126 }
127127 const_reference operator[](unsigned idx) const {
128 assert (Begin + idx < End);
128 assert(Begin + idx < End);
129129 return Begin[idx];
130130 }
131131
396396 bool operator<(const SmallVectorImpl &RHS) const {
397397 return std::lexicographical_compare(begin(), end(),
398398 RHS.begin(), RHS.end());
399 }
400
401 /// capacity - Return the total number of elements in the currently allocated
402 /// buffer.
403 size_t capacity() const { return Capacity - Begin; }
404
405 /// set_size - Set the array size to \arg N, which the current array must have
406 /// enough capacity for.
407 ///
408 /// This does not construct or destroy any elements in the vector.
409 ///
410 /// Clients can use this in conjunction with capacity() to write past the end
411 /// of the buffer when they know that more elements are available, and only
412 /// update the size later. This avoids the cost of value initializing elements
413 /// which will only be overwritten.
414 void set_size(unsigned N) {
415 assert(N <= capacity());
416 End = Begin + N;
399417 }
400418
401419 private:
380380 EXPECT_TRUE(constVector.begin() == constVector.end());
381381 }
382382
383 }
383 // Direct array access.
384 TEST_F(SmallVectorTest, DirectVectorTest) {
385 EXPECT_EQ(0u, theVector.size());
386 EXPECT_EQ(4u, theVector.capacity());
387 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
388 theVector.end()[0] = 1;
389 theVector.end()[1] = 2;
390 theVector.end()[2] = 3;
391 theVector.end()[3] = 4;
392 theVector.set_size(4);
393 EXPECT_EQ(4u, theVector.size());
394 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
395 EXPECT_EQ(1, theVector[0].getValue());
396 EXPECT_EQ(2, theVector[1].getValue());
397 EXPECT_EQ(3, theVector[2].getValue());
398 EXPECT_EQ(4, theVector[3].getValue());
399 }
400
401 }