Home | History | Annotate | Download | only in gtl
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 // This file provides utility functions for use with STL
     17 
     18 #ifndef TENSORFLOW_LIB_GTL_STL_UTIL_H_
     19 #define TENSORFLOW_LIB_GTL_STL_UTIL_H_
     20 
     21 #include <stddef.h>
     22 #include <algorithm>
     23 #include <iterator>
     24 #include <memory>
     25 #include <string>
     26 #include <utility>
     27 #include <vector>
     28 
     29 namespace tensorflow {
     30 namespace gtl {
     31 
     32 // Returns a char* pointing to the beginning of a string's internal buffer.
     33 // The result is a valid "null-terminated byte string", even if *str is empty.
     34 // Up to C++14 it is not valid to *write* to the null terminator; as of C++17,
     35 // it is valid to write zero to the null terminator (but not any other value).
     36 inline char* string_as_array(string* str) { return &*str->begin(); }
     37 
     38 // The following vector_as_array functions return raw pointers to the underlying
     39 // data buffer. The return value is unspecified (but valid) if the input range
     40 // is empty.
     41 template <typename T, typename Allocator>
     42 inline T* vector_as_array(std::vector<T, Allocator>* v) {
     43   return v->data();
     44 }
     45 
     46 template <typename T, typename Allocator>
     47 inline const T* vector_as_array(const std::vector<T, Allocator>* v) {
     48   return v->data();
     49 }
     50 
     51 // Like str->resize(new_size), except any new characters added to "*str" as a
     52 // result of resizing may be left uninitialized, rather than being filled with
     53 // '0' bytes. Typically used when code is then going to overwrite the backing
     54 // store of the string with known data. Uses a Google extension to ::string.
     55 inline void STLStringResizeUninitialized(string* s, size_t new_size) {
     56 #if __google_stl_resize_uninitialized_string
     57   s->resize_uninitialized(new_size);
     58 #else
     59   s->resize(new_size);
     60 #endif
     61 }
     62 
     63 // Calls delete (non-array version) on the SECOND item (pointer) in each pair in
     64 // the range [begin, end).
     65 //
     66 // Note: If you're calling this on an entire container, you probably want to
     67 // call STLDeleteValues(&container) instead, or use ValueDeleter.
     68 template <typename ForwardIterator>
     69 void STLDeleteContainerPairSecondPointers(ForwardIterator begin,
     70                                           ForwardIterator end) {
     71   while (begin != end) {
     72     ForwardIterator temp = begin;
     73     ++begin;
     74     delete temp->second;
     75   }
     76 }
     77 
     78 // Deletes all the elements in an STL container and clears the container. This
     79 // function is suitable for use with a vector, set, hash_set, or any other STL
     80 // container which defines sensible begin(), end(), and clear() methods.
     81 //
     82 // If container is NULL, this function is a no-op.
     83 template <typename T>
     84 void STLDeleteElements(T* container) {
     85   if (!container) return;
     86   auto it = container->begin();
     87   while (it != container->end()) {
     88     auto temp = it;
     89     ++it;
     90     delete *temp;
     91   }
     92   container->clear();
     93 }
     94 
     95 // Given an STL container consisting of (key, value) pairs, STLDeleteValues
     96 // deletes all the "value" components and clears the container. Does nothing in
     97 // the case it's given a NULL pointer.
     98 template <typename T>
     99 void STLDeleteValues(T* container) {
    100   if (!container) return;
    101   auto it = container->begin();
    102   while (it != container->end()) {
    103     auto temp = it;
    104     ++it;
    105     delete temp->second;
    106   }
    107   container->clear();
    108 }
    109 
    110 // Sorts and removes duplicates from a sequence container.
    111 template <typename T>
    112 inline void STLSortAndRemoveDuplicates(T* v) {
    113   std::sort(v->begin(), v->end());
    114   v->erase(std::unique(v->begin(), v->end()), v->end());
    115 }
    116 
    117 }  // namespace gtl
    118 }  // namespace tensorflow
    119 
    120 #endif  // TENSORFLOW_LIB_GTL_STL_UTIL_H_
    121