Generated on: Thu Mar 29 07:46:58 PDT 2012 for custom file set | ||
|
||
00001 // Copyright 2010-2012 Google 00002 // Licensed under the Apache License, Version 2.0 (the "License"); 00003 // you may not use this file except in compliance with the License. 00004 // You may obtain a copy of the License at 00005 // 00006 // http://www.apache.org/licenses/LICENSE-2.0 00007 // 00008 // Unless required by applicable law or agreed to in writing, software 00009 // distributed under the License is distributed on an "AS IS" BASIS, 00010 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00011 // See the License for the specific language governing permissions and 00012 // limitations under the License. 00013 00014 #ifndef OR_TOOLS_BASE_STL_UTIL_H_ 00015 #define OR_TOOLS_BASE_STL_UTIL_H_ 00016 00017 #include <string> 00018 00019 namespace operations_research { 00020 00021 // STLDeleteContainerPointers() 00022 // For a range within a container of pointers, calls delete 00023 // (non-array version) on these pointers. 00024 // NOTE: for these three functions, we could just implement a DeleteObject 00025 // functor and then call for_each() on the range and functor, but this 00026 // requires us to pull in all of algorithm.h, which seems expensive. 00027 // For hash_[multi]set, it is important that this deletes behind the iterator 00028 // because the hash_set may call the hash function on the iterator when it is 00029 // advanced, which could result in the hash function trying to deference a 00030 // stale pointer. 00031 template <class ForwardIterator> 00032 void STLDeleteContainerPointers(ForwardIterator begin, 00033 ForwardIterator end) { 00034 while (begin != end) { 00035 ForwardIterator temp = begin; 00036 ++begin; 00037 delete *temp; 00038 } 00039 } 00040 00041 // STLDeleteContainerPairSecondPointers() 00042 // For a range within a container of pairs, calls delete 00043 // (non-array version) on the SECOND item in the pairs. 00044 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator. 00045 // Deleting the value does not always invalidate the iterator, but it may 00046 // do so if the key is a pointer into the value object. 00047 template <class ForwardIterator> 00048 void STLDeleteContainerPairSecondPointers(ForwardIterator begin, 00049 ForwardIterator end) { 00050 while (begin != end) { 00051 ForwardIterator temp = begin; 00052 ++begin; 00053 delete temp->second; 00054 } 00055 } 00056 00057 inline void STLStringResizeUninitialized(std::string* s, size_t new_size) { 00058 s->resize(new_size); 00059 } 00060 00061 // Return a mutable char* pointing to a string's internal buffer, 00062 // which may not be null-terminated. Writing through this pointer will 00063 // modify the string. 00064 // 00065 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the 00066 // next call to a string method that invalidates iterators. 00067 // 00068 // As of 2006-04, there is no standard-blessed way of getting a 00069 // mutable reference to a string's internal buffer. However, issue 530 00070 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530) 00071 // proposes this as the method. According to Matt Austern, this should 00072 // already work on all current implementations. 00073 inline char* string_as_array(std::string* str) { 00074 return str->empty() ? NULL : &*str->begin(); 00075 } 00076 00077 // STLDeleteElements() deletes all the elements in an STL container and clears 00078 // the container. This function is suitable for use with a vector, set, 00079 // hash_set, or any other STL container which defines sensible begin(), end(), 00080 // and clear() methods. 00081 // 00082 // If container is NULL, this function is a no-op. 00083 // 00084 // As an alternative to calling STLDeleteElements() directly, consider 00085 // ElementDeleter (defined below), which ensures that your container's elements 00086 // are deleted when the ElementDeleter goes out of scope. 00087 template <class T> 00088 void STLDeleteElements(T *container) { 00089 if (!container) return; 00090 STLDeleteContainerPointers(container->begin(), container->end()); 00091 container->clear(); 00092 } 00093 00094 // Given an STL container consisting of (key, value) pairs, STLDeleteValues 00095 // deletes all the "value" components and clears the container. Does nothing 00096 // in the case it's given a NULL pointer. 00097 00098 template <class T> 00099 void STLDeleteValues(T *v) { 00100 if (!v) return; 00101 for (typename T::iterator i = v->begin(); i != v->end(); ++i) { 00102 delete i->second; 00103 } 00104 v->clear(); 00105 } 00106 00107 } // namespace operations_research 00108 00109 #endif // OR_TOOLS_BASE_STL_UTIL_H_