Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
zvector.h
Go to the documentation of this file.
1// Copyright 2010-2025 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_UTIL_ZVECTOR_H_
15#define OR_TOOLS_UTIL_ZVECTOR_H_
16
17#if (defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
18 defined(__OpenBSD__)) && \
19 defined(__GNUC__)
20#include <machine/endian.h>
21#elif !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__MINGW64__)
22#include <endian.h>
23#endif
24#include <cstdint>
25#include <memory>
26#include <string> // IWYU pragma: keep
27
28#include "absl/log/check.h"
30
31// An array class for storing arrays of integers.
32//
33// The range of indices is specified at the construction of the object.
34// The minimum and maximum indices are inclusive.
35// Think of the Pascal syntax array[min_index..max_index] of ...
36//
37// For example, ZVector<int32_t>(-100000,100000) will store 200001
38// signed integers of 32 bits each, and the possible range of indices
39// will be -100000..100000.
40
41namespace operations_research {
42
43template <class T>
44class ZVector {
45 public:
47 : base_(nullptr), min_index_(0), max_index_(-1), size_(0), storage_() {}
48
49 ZVector(int64_t min_index, int64_t max_index)
50 : base_(nullptr), min_index_(0), max_index_(-1), size_(0), storage_() {
52 LOG(DFATAL) << "Could not reserve memory for indices ranging from "
53 << min_index << " to " << max_index;
54 }
55 }
56
57 int64_t min_index() const { return min_index_; }
58
59 int64_t max_index() const { return max_index_; }
60
61 // Returns the value stored at index.
62 T Value(int64_t index) const {
63 DCHECK_LE(min_index_, index);
64 DCHECK_GE(max_index_, index);
65 DCHECK(base_ != nullptr);
66 return base_[index];
67 }
68
69#if !defined(SWIG)
70 // Shortcut for returning the value stored at index.
71 T& operator[](int64_t index) {
72 DCHECK_LE(min_index_, index);
73 DCHECK_GE(max_index_, index);
74 DCHECK(base_ != nullptr);
75 return base_[index];
76 }
77
78 T operator[](int64_t index) const {
79 DCHECK_LE(min_index_, index);
80 DCHECK_GE(max_index_, index);
81 DCHECK(base_ != nullptr);
82 return base_[index];
83 }
84#endif
85
86 // Sets to value the content of the array at index.
87 void Set(int64_t index, T value) {
88 DCHECK_LE(min_index_, index);
89 DCHECK_GE(max_index_, index);
90 DCHECK(base_ != nullptr);
91 base_[index] = value;
92 }
93
94 // Reserves memory for new minimum and new maximum indices.
95 // Returns true if the memory could be reserved.
96 // Never shrinks the memory allocated.
97 bool Reserve(int64_t new_min_index, int64_t new_max_index) {
98 if (new_min_index > new_max_index) {
99 return false;
100 }
101 const uint64_t new_size = new_max_index - new_min_index + 1;
102 if (base_ != nullptr) {
103 if (new_min_index >= min_index_ && new_max_index <= max_index_) {
104 min_index_ = new_min_index;
105 max_index_ = new_max_index;
106 size_ = new_size;
107 return true;
108 } else if (new_min_index > min_index_ || new_max_index < max_index_) {
109 return false;
110 }
111 }
112 T* new_storage = new T[new_size];
113 if (new_storage == nullptr) {
114 return false;
115 }
116
117 T* const new_base = new_storage - new_min_index;
118 if (base_ != nullptr) {
119 T* const destination = new_base + min_index_;
120 memcpy(destination, storage_.get(), size_ * sizeof(*base_));
121 }
122
123 base_ = new_base;
124 size_ = new_size;
125 min_index_ = new_min_index;
126 max_index_ = new_max_index;
127 storage_.reset(new_storage);
128 return true;
129 }
130
131 // Sets all the elements in the array to value.
132 void SetAll(T value) {
133 DLOG_IF(WARNING, base_ == nullptr || size_ <= 0)
134 << "Trying to set values to uninitialized vector.";
135 for (int64_t i = 0; i < size_; ++i) {
136 base_[min_index_ + i] = value;
137 }
138 }
139
140 private:
141 // Pointer to the element indexed by zero in the array.
142 T* base_;
143
144 // Minimum index for the array.
145 int64_t min_index_;
146
147 // Maximum index for the array.
148 int64_t max_index_;
149
150 // The number of elements in the array.
151 int64_t size_;
152
153 // Storage memory for the array.
154 std::unique_ptr<T[]> storage_;
155};
156
157// Shorthands for all the types of ZVector's.
166
167} // namespace operations_research
168
169#endif // OR_TOOLS_UTIL_ZVECTOR_H_
ZVector(int64_t min_index, int64_t max_index)
Definition zvector.h:49
T operator[](int64_t index) const
Definition zvector.h:78
T & operator[](int64_t index)
Shortcut for returning the value stored at index.
Definition zvector.h:71
T Value(int64_t index) const
Returns the value stored at index.
Definition zvector.h:62
bool Reserve(int64_t new_min_index, int64_t new_max_index)
Definition zvector.h:97
void Set(int64_t index, T value)
Sets to value the content of the array at index.
Definition zvector.h:87
void SetAll(T value)
Sets all the elements in the array to value.
Definition zvector.h:132
In SWIG mode, we don't want anything besides these top-level includes.
ZVector< int32_t > Int32ZVector
Definition zvector.h:160
ZVector< uint16_t > UInt16ZVector
Definition zvector.h:163
ZVector< uint8_t > UInt8ZVector
Definition zvector.h:162
ZVector< int16_t > Int16ZVector
Definition zvector.h:159
ZVector< uint64_t > UInt64ZVector
Definition zvector.h:165
ZVector< int64_t > Int64ZVector
Definition zvector.h:161
ZVector< int8_t > Int8ZVector
Shorthands for all the types of ZVector's.
Definition zvector.h:158
ZVector< uint32_t > UInt32ZVector
Definition zvector.h:164