Chromium Code Reviews| Index: base/stl_util-inl.h |
| diff --git a/base/stl_util-inl.h b/base/stl_util-inl.h |
| index 2161c59a0330ee241888d22ef07ed2e6d0b398a2..ee74df1ee15356213f26f931fbec08eedaa74d70 100644 |
| --- a/base/stl_util-inl.h |
| +++ b/base/stl_util-inl.h |
| @@ -9,12 +9,13 @@ |
| #define BASE_STL_UTIL_INL_H_ |
| #pragma once |
| +#include <assert.h> |
| #include <string.h> // for memcpy |
| + |
| #include <functional> |
| #include <set> |
| #include <string> |
| #include <vector> |
| -#include <cassert> |
| // Clear internal memory of an STL object. |
| // STL clear()/reserve(0) does not always free internal memory allocated |
| @@ -22,29 +23,9 @@ |
| template<class T> void STLClearObject(T* obj) { |
| T tmp; |
| tmp.swap(*obj); |
| - obj->reserve(0); // this is because sometimes "T tmp" allocates objects with |
| - // memory (arena implementation?). use reserve() |
| - // to clear() even if it doesn't always work |
| -} |
| - |
| -// Reduce memory usage on behalf of object if it is using more than |
| -// "bytes" bytes of space. By default, we clear objects over 1MB. |
| -template <class T> inline void STLClearIfBig(T* obj, size_t limit = 1<<20) { |
| - if (obj->capacity() >= limit) { |
| - STLClearObject(obj); |
| - } else { |
| - obj->clear(); |
| - } |
| -} |
| - |
| -// Reserve space for STL object. |
| -// STL's reserve() will always copy. |
| -// This function avoid the copy if we already have capacity |
| -template<class T> void STLReserveIfNeeded(T* obj, int new_size) { |
| - if (obj->capacity() < new_size) // increase capacity |
| - obj->reserve(new_size); |
| - else if (obj->size() > new_size) // reduce size |
| - obj->resize(new_size); |
| + // Sometimes "T tmp" allocates objects with memory (arena implementation?). |
| + // Hence using additional reserve(0) even if it doesn't always work. |
| + obj->reserve(0); |
| } |
| // STLDeleteContainerPointers() |
| @@ -111,42 +92,9 @@ void STLDeleteContainerPairSecondPointers(ForwardIterator begin, |
| } |
| } |
| -template<typename T> |
| -inline void STLAssignToVector(std::vector<T>* vec, |
| - const T* ptr, |
| - size_t n) { |
| - vec->resize(n); |
| - memcpy(&vec->front(), ptr, n*sizeof(T)); |
| -} |
| - |
| -/***** Hack to allow faster assignment to a vector *****/ |
| - |
| -// This routine speeds up an assignment of 32 bytes to a vector from |
| -// about 250 cycles per assignment to about 140 cycles. |
| -// |
| -// Usage: |
| -// STLAssignToVectorChar(&vec, ptr, size); |
| -// STLAssignToString(&str, ptr, size); |
| - |
| -inline void STLAssignToVectorChar(std::vector<char>* vec, |
| - const char* ptr, |
| - size_t n) { |
| - STLAssignToVector(vec, ptr, n); |
| -} |
| - |
| -inline void STLAssignToString(std::string* str, const char* ptr, size_t n) { |
| - str->resize(n); |
| - memcpy(&*str->begin(), ptr, n); |
| -} |
| - |
| // To treat a possibly-empty vector as an array, use these functions. |
| // If you know the array will never be empty, you can use &*v.begin() |
| -// directly, but that is allowed to dump core if v is empty. This |
| -// function is the most efficient code that will work, taking into |
| -// account how our STL is actually implemented. THIS IS NON-PORTABLE |
| -// CODE, so call us instead of repeating the nonportable code |
| -// everywhere. If our STL implementation changes, we will need to |
| -// change this as well. |
| +// directly, but that is undefined behaviour if v is empty. |
| template<typename T> |
| inline T* vector_as_array(std::vector<T>* v) { |
| @@ -167,7 +115,7 @@ inline const T* vector_as_array(const std::vector<T>* v) { |
| } |
| // Return a mutable char* pointing to a string's internal buffer, |
| -// which may not be null-terminated. Writing through this pointer will |
| +// which may be not null-terminated. Writing through this pointer will |
|
brettw
2011/07/15 05:32:38
Seems like the old comment was fine (and grammatic
Denis Lagno
2011/07/18 16:12:38
Reverted.
|
| // modify the string. |
| // |
| // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the |
| @@ -179,10 +127,21 @@ inline const T* vector_as_array(const std::vector<T>* v) { |
| // proposes this as the method. According to Matt Austern, this should |
| // already work on all current implementations. |
| inline char* string_as_array(std::string* str) { |
| - // DO NOT USE const_cast<char*>(str->data())! See the unittest for why. |
| + // DO NOT USE const_cast<char*>(str->data()) |
| return str->empty() ? NULL : &*str->begin(); |
| } |
| +template<typename T> |
| +inline void STLAssignToVector(std::vector<T>* vec, const T* ptr, size_t n) { |
| + vec->resize(n); |
| + memcpy(vector_as_array(vec), ptr, n*sizeof(T)); |
| +} |
| + |
| +inline void STLAssignToString(std::string* str, const char* ptr, size_t n) { |
| + str->resize(n); |
| + memcpy(string_as_array(str), ptr, n); |
| +} |
| + |
| // These are methods that test two hash maps/sets for equality. These exist |
| // because the == operator in the STL can return false when the maps/sets |
| // contain identical elements. This is because it compares the internal hash |