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