1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file |
3 | // distributed with this work for additional information |
4 | // regarding copyright ownership. The ASF licenses this file |
5 | // to you under the Apache License, Version 2.0 (the |
6 | // "License"); you may not use this file except in compliance |
7 | // with the License. You may obtain a copy of the License at |
8 | // |
9 | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | // |
11 | // Unless required by applicable law or agreed to in writing, |
12 | // software distributed under the License is distributed on an |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | // KIND, either express or implied. See the License for the |
15 | // specific language governing permissions and limitations |
16 | // under the License. |
17 | |
18 | #ifndef ARROW_UTIL_STL_H |
19 | #define ARROW_UTIL_STL_H |
20 | |
21 | #include <vector> |
22 | |
23 | #include "arrow/util/logging.h" |
24 | |
25 | namespace arrow { |
26 | namespace internal { |
27 | |
28 | template <typename T> |
29 | inline std::vector<T> DeleteVectorElement(const std::vector<T>& values, size_t index) { |
30 | DCHECK(!values.empty()); |
31 | DCHECK_LT(index, values.size()); |
32 | std::vector<T> out; |
33 | out.reserve(values.size() - 1); |
34 | for (size_t i = 0; i < index; ++i) { |
35 | out.push_back(values[i]); |
36 | } |
37 | for (size_t i = index + 1; i < values.size(); ++i) { |
38 | out.push_back(values[i]); |
39 | } |
40 | return out; |
41 | } |
42 | |
43 | template <typename T> |
44 | inline std::vector<T> AddVectorElement(const std::vector<T>& values, size_t index, |
45 | const T& new_element) { |
46 | DCHECK_LE(index, values.size()); |
47 | std::vector<T> out; |
48 | out.reserve(values.size() + 1); |
49 | for (size_t i = 0; i < index; ++i) { |
50 | out.push_back(values[i]); |
51 | } |
52 | out.push_back(new_element); |
53 | for (size_t i = index; i < values.size(); ++i) { |
54 | out.push_back(values[i]); |
55 | } |
56 | return out; |
57 | } |
58 | |
59 | template <typename T> |
60 | inline std::vector<T> ReplaceVectorElement(const std::vector<T>& values, size_t index, |
61 | const T& new_element) { |
62 | DCHECK_LE(index, values.size()); |
63 | std::vector<T> out; |
64 | out.reserve(values.size()); |
65 | for (size_t i = 0; i < index; ++i) { |
66 | out.push_back(values[i]); |
67 | } |
68 | out.push_back(new_element); |
69 | for (size_t i = index + 1; i < values.size(); ++i) { |
70 | out.push_back(values[i]); |
71 | } |
72 | return out; |
73 | } |
74 | |
75 | } // namespace internal |
76 | } // namespace arrow |
77 | |
78 | #endif // ARROW_UTIL_STL_H |
79 | |