• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBTEXTCLASSIFIER_UTIL_GTL_STL_UTIL_H_
18 #define LIBTEXTCLASSIFIER_UTIL_GTL_STL_UTIL_H_
19 
20 namespace libtextclassifier {
21 
22 // Deletes all the elements in an STL container and clears the container. This
23 // function is suitable for use with a vector, set, hash_set, or any other STL
24 // container which defines sensible begin(), end(), and clear() methods.
25 // If container is NULL, this function is a no-op.
26 template <typename T>
STLDeleteElements(T * container)27 void STLDeleteElements(T *container) {
28   if (!container) return;
29   auto it = container->begin();
30   while (it != container->end()) {
31     auto temp = it;
32     ++it;
33     delete *temp;
34   }
35   container->clear();
36 }
37 
38 // Given an STL container consisting of (key, value) pairs, STLDeleteValues
39 // deletes all the "value" components and clears the container. Does nothing in
40 // the case it's given a nullptr.
41 template <typename T>
STLDeleteValues(T * container)42 void STLDeleteValues(T *container) {
43   if (!container) return;
44   auto it = container->begin();
45   while (it != container->end()) {
46     auto temp = it;
47     ++it;
48     delete temp->second;
49   }
50   container->clear();
51 }
52 
53 }  // namespace libtextclassifier
54 
55 #endif  // LIBTEXTCLASSIFIER_UTIL_GTL_STL_UTIL_H_
56