OR-Tools  9.2
protobuf_util.h
Go to the documentation of this file.
1// Copyright 2010-2021 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_BASE_PROTOBUF_UTIL_H_
15#define OR_TOOLS_BASE_PROTOBUF_UTIL_H_
16
17#include <string>
18
19#include "google/protobuf/repeated_field.h"
20#include "google/protobuf/text_format.h"
22
23namespace google {
24namespace protobuf {
25namespace util {
26// RepeatedPtrField version.
27template <typename T>
28inline void Truncate(RepeatedPtrField<T>* array, int new_size) {
29 const int size = array->size();
30 DCHECK_GE(size, new_size);
31 array->DeleteSubrange(new_size, size - new_size);
32}
33
34// Removes the elements at the indices specified by 'indices' from 'array' in
35// time linear in the size of 'array' (on average, even when 'indices' is a
36// singleton) while preserving the relative order of the remaining elements.
37// The indices must be a container of ints in strictly increasing order, such
38// as vector<int>, set<int> or initializer_list<int>, and in the range [0, N -
39// 1] where N is the number of elements in 'array', and RepeatedType must be
40// RepeatedField or RepeatedPtrField.
41// Returns number of elements erased.
42template <typename RepeatedType, typename IndexContainer = std::vector<int>>
43int RemoveAt(RepeatedType* array, const IndexContainer& indices) {
44 if (indices.size() == 0) {
45 return 0;
46 }
47 const int num_indices = indices.size();
48 const int num_elements = array->size();
49 DCHECK_LE(num_indices, num_elements);
50 if (num_indices == num_elements) {
51 // Assumes that 'indices' consists of [0 ... N-1].
52 array->Clear();
53 return num_indices;
54 }
55 typename IndexContainer::const_iterator remove_iter = indices.begin();
56 int write_index = *(remove_iter++);
57 for (int scan = write_index + 1; scan < num_elements; ++scan) {
58 if (remove_iter != indices.end() && *remove_iter == scan) {
59 ++remove_iter;
60 } else {
61 array->SwapElements(scan, write_index++);
62 }
63 }
64 DCHECK_EQ(write_index, num_elements - num_indices);
65 Truncate(array, write_index);
66 return num_indices;
67}
68
69template <typename T>
70T ParseTextOrDie(const std::string& input) {
71 T result;
72 CHECK(TextFormat::MergeFromString(input, &result));
73 return result;
74}
75
76} // namespace util
77} // namespace protobuf
78} // namespace google
79
80#endif // OR_TOOLS_BASE_PROTOBUF_UTIL_H_
#define CHECK(condition)
Definition: base/logging.h:492
#define DCHECK_LE(val1, val2)
Definition: base/logging.h:889
#define DCHECK_GE(val1, val2)
Definition: base/logging.h:891
#define DCHECK_EQ(val1, val2)
Definition: base/logging.h:887
int RemoveAt(RepeatedType *array, const IndexContainer &indices)
Definition: protobuf_util.h:43
T ParseTextOrDie(const std::string &input)
Definition: protobuf_util.h:70
void Truncate(RepeatedPtrField< T > *array, int new_size)
Definition: protobuf_util.h:28
static int input(yyscan_t yyscanner)