1 /*
2 * Copyright (C) 2018 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 NLP_SAFT_COMPONENTS_COMMON_MOBILE_STL_UTIL_H_
18 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_STL_UTIL_H_
19
20
21 namespace libtextclassifier3 {
22 namespace mobile {
23 namespace utils {
24
25 // Deletes all the elements in an STL container and clears the container. This
26 // function is suitable for use with a vector, set, hash_set, or any other STL
27 // container which defines sensible begin(), end(), and clear() methods. If
28 // container is NULL, this function is a no-op.
29 template <typename T>
STLDeleteElements(T * container)30 void STLDeleteElements(T *container) {
31 if (!container) return;
32 auto it = container->begin();
33 while (it != container->end()) {
34 auto temp = it;
35 ++it;
36 delete *temp;
37 }
38 container->clear();
39 }
40
41 } // namespace utils
42 } // namespace mobile
43 } // namespace nlp_saft
44
45 #endif // NLP_SAFT_COMPONENTS_COMMON_MOBILE_STL_UTIL_H_
46