forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
zvector.h
168 lines (143 loc) · 4.92 KB
/
zvector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef OR_TOOLS_UTIL_ZVECTOR_H_
#define OR_TOOLS_UTIL_ZVECTOR_H_
#if (defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)) && \
defined(__GNUC__)
#include <machine/endian.h>
#elif !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__MINGW64__)
#include <endian.h>
#endif
#include <cstdint>
#include <memory>
#include <string> // IWYU pragma: keep
#include "absl/log/check.h"
#include "ortools/base/logging.h"
// An array class for storing arrays of integers.
//
// The range of indices is specified at the construction of the object.
// The minimum and maximum indices are inclusive.
// Think of the Pascal syntax array[min_index..max_index] of ...
//
// For example, ZVector<int32_t>(-100000,100000) will store 200001
// signed integers of 32 bits each, and the possible range of indices
// will be -100000..100000.
namespace operations_research {
template <class T>
class ZVector {
public:
ZVector()
: base_(nullptr), min_index_(0), max_index_(-1), size_(0), storage_() {}
ZVector(int64_t min_index, int64_t max_index)
: base_(nullptr), min_index_(0), max_index_(-1), size_(0), storage_() {
if (!Reserve(min_index, max_index)) {
LOG(DFATAL) << "Could not reserve memory for indices ranging from "
<< min_index << " to " << max_index;
}
}
int64_t min_index() const { return min_index_; }
int64_t max_index() const { return max_index_; }
// Returns the value stored at index.
T Value(int64_t index) const {
DCHECK_LE(min_index_, index);
DCHECK_GE(max_index_, index);
DCHECK(base_ != nullptr);
return base_[index];
}
#if !defined(SWIG)
// Shortcut for returning the value stored at index.
T& operator[](int64_t index) {
DCHECK_LE(min_index_, index);
DCHECK_GE(max_index_, index);
DCHECK(base_ != nullptr);
return base_[index];
}
const T operator[](int64_t index) const {
DCHECK_LE(min_index_, index);
DCHECK_GE(max_index_, index);
DCHECK(base_ != nullptr);
return base_[index];
}
#endif
// Sets to value the content of the array at index.
void Set(int64_t index, T value) {
DCHECK_LE(min_index_, index);
DCHECK_GE(max_index_, index);
DCHECK(base_ != nullptr);
base_[index] = value;
}
// Reserves memory for new minimum and new maximum indices.
// Returns true if the memory could be reserved.
// Never shrinks the memory allocated.
bool Reserve(int64_t new_min_index, int64_t new_max_index) {
if (new_min_index > new_max_index) {
return false;
}
const uint64_t new_size = new_max_index - new_min_index + 1;
if (base_ != nullptr) {
if (new_min_index >= min_index_ && new_max_index <= max_index_) {
min_index_ = new_min_index;
max_index_ = new_max_index;
size_ = new_size;
return true;
} else if (new_min_index > min_index_ || new_max_index < max_index_) {
return false;
}
}
T* new_storage = new T[new_size];
if (new_storage == nullptr) {
return false;
}
T* const new_base = new_storage - new_min_index;
if (base_ != nullptr) {
T* const destination = new_base + min_index_;
memcpy(destination, storage_.get(), size_ * sizeof(*base_));
}
base_ = new_base;
size_ = new_size;
min_index_ = new_min_index;
max_index_ = new_max_index;
storage_.reset(new_storage);
return true;
}
// Sets all the elements in the array to value.
void SetAll(T value) {
DLOG_IF(WARNING, base_ == nullptr || size_ <= 0)
<< "Trying to set values to uninitialized vector.";
for (int64_t i = 0; i < size_; ++i) {
base_[min_index_ + i] = value;
}
}
private:
// Pointer to the element indexed by zero in the array.
T* base_;
// Minimum index for the array.
int64_t min_index_;
// Maximum index for the array.
int64_t max_index_;
// The number of elements in the array.
int64_t size_;
// Storage memory for the array.
std::unique_ptr<T[]> storage_;
};
// Shorthands for all the types of ZVector's.
typedef ZVector<int8_t> Int8ZVector;
typedef ZVector<int16_t> Int16ZVector;
typedef ZVector<int32_t> Int32ZVector;
typedef ZVector<int64_t> Int64ZVector;
typedef ZVector<uint8_t> UInt8ZVector;
typedef ZVector<uint16_t> UInt16ZVector;
typedef ZVector<uint32_t> UInt32ZVector;
typedef ZVector<uint64_t> UInt64ZVector;
} // namespace operations_research
#endif // OR_TOOLS_UTIL_ZVECTOR_H_