14#ifndef OR_TOOLS_UTIL_ZVECTOR_H_
15#define OR_TOOLS_UTIL_ZVECTOR_H_
17#if (defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
18 defined(__OpenBSD__)) && \
20#include <machine/endian.h>
21#elif !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__MINGW64__)
28#include "absl/log/check.h"
47 : base_(nullptr), min_index_(0), max_index_(-1), size_(0), storage_() {}
50 : base_(nullptr), min_index_(0), max_index_(-1), size_(0), storage_() {
52 LOG(DFATAL) <<
"Could not reserve memory for indices ranging from "
63 DCHECK_LE(min_index_, index);
64 DCHECK_GE(max_index_, index);
65 DCHECK(base_ !=
nullptr);
72 DCHECK_LE(min_index_, index);
73 DCHECK_GE(max_index_, index);
74 DCHECK(base_ !=
nullptr);
79 DCHECK_LE(min_index_, index);
80 DCHECK_GE(max_index_, index);
81 DCHECK(base_ !=
nullptr);
87 void Set(int64_t index, T value) {
88 DCHECK_LE(min_index_, index);
89 DCHECK_GE(max_index_, index);
90 DCHECK(base_ !=
nullptr);
97 bool Reserve(int64_t new_min_index, int64_t new_max_index) {
98 if (new_min_index > new_max_index) {
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;
108 }
else if (new_min_index > min_index_ || new_max_index < max_index_) {
112 T* new_storage =
new T[new_size];
113 if (new_storage ==
nullptr) {
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_));
125 min_index_ = new_min_index;
126 max_index_ = new_max_index;
127 storage_.reset(new_storage);
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;
154 std::unique_ptr<T[]> storage_;
ZVector(int64_t min_index, int64_t max_index)
T operator[](int64_t index) const
T & operator[](int64_t index)
Shortcut for returning the value stored at index.
int64_t min_index() const
T Value(int64_t index) const
Returns the value stored at index.
bool Reserve(int64_t new_min_index, int64_t new_max_index)
void Set(int64_t index, T value)
Sets to value the content of the array at index.
int64_t max_index() const
void SetAll(T value)
Sets all the elements in the array to value.
In SWIG mode, we don't want anything besides these top-level includes.
ZVector< int32_t > Int32ZVector
ZVector< uint16_t > UInt16ZVector
ZVector< uint8_t > UInt8ZVector
ZVector< int16_t > Int16ZVector
ZVector< uint64_t > UInt64ZVector
ZVector< int64_t > Int64ZVector
ZVector< int8_t > Int8ZVector
Shorthands for all the types of ZVector's.
ZVector< uint32_t > UInt32ZVector