michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: // Derived from google3/util/gtl/stl_util.h michael@0: michael@0: #ifndef BASE_STL_UTIL_H_ michael@0: #define BASE_STL_UTIL_H_ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "base/logging.h" michael@0: michael@0: // Clears internal memory of an STL object. michael@0: // STL clear()/reserve(0) does not always free internal memory allocated michael@0: // This function uses swap/destructor to ensure the internal memory is freed. michael@0: template michael@0: void STLClearObject(T* obj) { michael@0: T tmp; michael@0: tmp.swap(*obj); michael@0: // Sometimes "T tmp" allocates objects with memory (arena implementation?). michael@0: // Hence using additional reserve(0) even if it doesn't always work. michael@0: obj->reserve(0); michael@0: } michael@0: michael@0: // For a range within a container of pointers, calls delete (non-array version) michael@0: // on these pointers. michael@0: // NOTE: for these three functions, we could just implement a DeleteObject michael@0: // functor and then call for_each() on the range and functor, but this michael@0: // requires us to pull in all of algorithm.h, which seems expensive. michael@0: // For hash_[multi]set, it is important that this deletes behind the iterator michael@0: // because the hash_set may call the hash function on the iterator when it is michael@0: // advanced, which could result in the hash function trying to deference a michael@0: // stale pointer. michael@0: template michael@0: void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) { michael@0: while (begin != end) { michael@0: ForwardIterator temp = begin; michael@0: ++begin; michael@0: delete *temp; michael@0: } michael@0: } michael@0: michael@0: // For a range within a container of pairs, calls delete (non-array version) on michael@0: // BOTH items in the pairs. michael@0: // NOTE: Like STLDeleteContainerPointers, it is important that this deletes michael@0: // behind the iterator because if both the key and value are deleted, the michael@0: // container may call the hash function on the iterator when it is advanced, michael@0: // which could result in the hash function trying to dereference a stale michael@0: // pointer. michael@0: template michael@0: void STLDeleteContainerPairPointers(ForwardIterator begin, michael@0: ForwardIterator end) { michael@0: while (begin != end) { michael@0: ForwardIterator temp = begin; michael@0: ++begin; michael@0: delete temp->first; michael@0: delete temp->second; michael@0: } michael@0: } michael@0: michael@0: // For a range within a container of pairs, calls delete (non-array version) on michael@0: // the FIRST item in the pairs. michael@0: // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator. michael@0: template michael@0: void STLDeleteContainerPairFirstPointers(ForwardIterator begin, michael@0: ForwardIterator end) { michael@0: while (begin != end) { michael@0: ForwardIterator temp = begin; michael@0: ++begin; michael@0: delete temp->first; michael@0: } michael@0: } michael@0: michael@0: // For a range within a container of pairs, calls delete. michael@0: // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator. michael@0: // Deleting the value does not always invalidate the iterator, but it may michael@0: // do so if the key is a pointer into the value object. michael@0: template michael@0: void STLDeleteContainerPairSecondPointers(ForwardIterator begin, michael@0: ForwardIterator end) { michael@0: while (begin != end) { michael@0: ForwardIterator temp = begin; michael@0: ++begin; michael@0: delete temp->second; michael@0: } michael@0: } michael@0: michael@0: // To treat a possibly-empty vector as an array, use these functions. michael@0: // If you know the array will never be empty, you can use &*v.begin() michael@0: // directly, but that is undefined behaviour if |v| is empty. michael@0: template michael@0: inline T* vector_as_array(std::vector* v) { michael@0: return v->empty() ? NULL : &*v->begin(); michael@0: } michael@0: michael@0: template michael@0: inline const T* vector_as_array(const std::vector* v) { michael@0: return v->empty() ? NULL : &*v->begin(); michael@0: } michael@0: michael@0: // Return a mutable char* pointing to a string's internal buffer, michael@0: // which may not be null-terminated. Writing through this pointer will michael@0: // modify the string. michael@0: // michael@0: // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the michael@0: // next call to a string method that invalidates iterators. michael@0: // michael@0: // As of 2006-04, there is no standard-blessed way of getting a michael@0: // mutable reference to a string's internal buffer. However, issue 530 michael@0: // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530) michael@0: // proposes this as the method. According to Matt Austern, this should michael@0: // already work on all current implementations. michael@0: inline char* string_as_array(std::string* str) { michael@0: // DO NOT USE const_cast(str->data()) michael@0: return str->empty() ? NULL : &*str->begin(); michael@0: } michael@0: michael@0: // The following functions are useful for cleaning up STL containers whose michael@0: // elements point to allocated memory. michael@0: michael@0: // STLDeleteElements() deletes all the elements in an STL container and clears michael@0: // the container. This function is suitable for use with a vector, set, michael@0: // hash_set, or any other STL container which defines sensible begin(), end(), michael@0: // and clear() methods. michael@0: // michael@0: // If container is NULL, this function is a no-op. michael@0: // michael@0: // As an alternative to calling STLDeleteElements() directly, consider michael@0: // STLElementDeleter (defined below), which ensures that your container's michael@0: // elements are deleted when the STLElementDeleter goes out of scope. michael@0: template michael@0: void STLDeleteElements(T* container) { michael@0: if (!container) michael@0: return; michael@0: STLDeleteContainerPointers(container->begin(), container->end()); michael@0: container->clear(); michael@0: } michael@0: michael@0: // Given an STL container consisting of (key, value) pairs, STLDeleteValues michael@0: // deletes all the "value" components and clears the container. Does nothing michael@0: // in the case it's given a NULL pointer. michael@0: template michael@0: void STLDeleteValues(T* container) { michael@0: if (!container) michael@0: return; michael@0: for (typename T::iterator i(container->begin()); i != container->end(); ++i) michael@0: delete i->second; michael@0: container->clear(); michael@0: } michael@0: michael@0: michael@0: // The following classes provide a convenient way to delete all elements or michael@0: // values from STL containers when they goes out of scope. This greatly michael@0: // simplifies code that creates temporary objects and has multiple return michael@0: // statements. Example: michael@0: // michael@0: // vector tmp_proto; michael@0: // STLElementDeleter > d(&tmp_proto); michael@0: // if (...) return false; michael@0: // ... michael@0: // return success; michael@0: michael@0: // Given a pointer to an STL container this class will delete all the element michael@0: // pointers when it goes out of scope. michael@0: template michael@0: class STLElementDeleter { michael@0: public: michael@0: STLElementDeleter(T* container) : container_(container) {} michael@0: ~STLElementDeleter() { STLDeleteElements(container_); } michael@0: michael@0: private: michael@0: T* container_; michael@0: }; michael@0: michael@0: // Given a pointer to an STL container this class will delete all the value michael@0: // pointers when it goes out of scope. michael@0: template michael@0: class STLValueDeleter { michael@0: public: michael@0: STLValueDeleter(T* container) : container_(container) {} michael@0: ~STLValueDeleter() { STLDeleteValues(container_); } michael@0: michael@0: private: michael@0: T* container_; michael@0: }; michael@0: michael@0: // Test to see if a set, map, hash_set or hash_map contains a particular key. michael@0: // Returns true if the key is in the collection. michael@0: template michael@0: bool ContainsKey(const Collection& collection, const Key& key) { michael@0: return collection.find(key) != collection.end(); michael@0: } michael@0: michael@0: namespace base { michael@0: michael@0: // Returns true if the container is sorted. michael@0: template michael@0: bool STLIsSorted(const Container& cont) { michael@0: return std::adjacent_find(cont.begin(), cont.end(), michael@0: std::greater()) michael@0: == cont.end(); michael@0: } michael@0: michael@0: // Returns a new ResultType containing the difference of two sorted containers. michael@0: template michael@0: ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) { michael@0: DCHECK(STLIsSorted(a1)); michael@0: DCHECK(STLIsSorted(a2)); michael@0: ResultType difference; michael@0: std::set_difference(a1.begin(), a1.end(), michael@0: a2.begin(), a2.end(), michael@0: std::inserter(difference, difference.end())); michael@0: return difference; michael@0: } michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_STL_UTIL_H_