Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
protobuf_util.h
Go to the documentation of this file.
1// Copyright 2010-2024 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// RepeatedField version.
35template <typename T>
36inline void Truncate(RepeatedField<T>* array, int new_size) {
37 // RepeatedField::Truncate performs size validity checks.
38 array->Truncate(new_size);
39}
40
41// Removes the elements at the indices specified by 'indices' from 'array' in
42// time linear in the size of 'array' (on average, even when 'indices' is a
43// singleton) while preserving the relative order of the remaining elements.
44// The indices must be a container of ints in strictly increasing order, such
45// as vector<int>, set<int> or initializer_list<int>, and in the range [0, N -
46// 1] where N is the number of elements in 'array', and RepeatedType must be
47// RepeatedField or RepeatedPtrField.
48// Returns number of elements erased.
49template <typename RepeatedType, typename IndexContainer = std::vector<int>>
50int RemoveAt(RepeatedType* array, const IndexContainer& indices) {
51 if (indices.size() == 0) {
52 return 0;
53 }
54 const int num_indices = indices.size();
55 const int num_elements = array->size();
56 DCHECK_LE(num_indices, num_elements);
57 if (num_indices == num_elements) {
58 // Assumes that 'indices' consists of [0 ... N-1].
59 array->Clear();
60 return num_indices;
61 }
62 typename IndexContainer::const_iterator remove_iter = indices.begin();
63 int write_index = *(remove_iter++);
64 for (int scan = write_index + 1; scan < num_elements; ++scan) {
65 if (remove_iter != indices.end() && *remove_iter == scan) {
66 ++remove_iter;
67 } else {
68 array->SwapElements(scan, write_index++);
69 }
70 }
71 DCHECK_EQ(write_index, num_elements - num_indices);
72 Truncate(array, write_index);
73 return num_indices;
74}
75
76template <typename T>
77T ParseTextOrDie(const std::string& input) {
78 T result;
79 CHECK(TextFormat::MergeFromString(input, &result));
80 return result;
81}
82
83} // namespace util
84} // namespace protobuf
85} // namespace google
86
87#endif // OR_TOOLS_BASE_PROTOBUF_UTIL_H_
IntegerValue size
int RemoveAt(RepeatedType *array, const IndexContainer &indices)
T ParseTextOrDie(const std::string &input)
void Truncate(RepeatedPtrField< T > *array, int new_size)
RepeatedPtrField version.
A collections of i/o utilities for the Graph classes in ./graph.h.
static int input(yyscan_t yyscanner)