14#ifndef OR_TOOLS_UTIL_ZVECTOR_H_
15#define OR_TOOLS_UTIL_ZVECTOR_H_
17#if (defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)) && \
19#include <machine/endian.h>
20#elif !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__MINGW64__)
27#include "absl/log/check.h"
46 : base_(nullptr), min_index_(0), max_index_(-1), size_(0), storage_() {}
49 : base_(nullptr), min_index_(0), max_index_(-1), size_(0), storage_() {
51 LOG(DFATAL) <<
"Could not reserve memory for indices ranging from "
62 DCHECK_LE(min_index_,
index);
63 DCHECK_GE(max_index_,
index);
64 DCHECK(base_ !=
nullptr);
71 DCHECK_LE(min_index_,
index);
72 DCHECK_GE(max_index_,
index);
73 DCHECK(base_ !=
nullptr);
78 DCHECK_LE(min_index_,
index);
79 DCHECK_GE(max_index_,
index);
80 DCHECK(base_ !=
nullptr);
87 DCHECK_LE(min_index_,
index);
88 DCHECK_GE(max_index_,
index);
89 DCHECK(base_ !=
nullptr);
96 bool Reserve(int64_t new_min_index, int64_t new_max_index) {
97 if (new_min_index > new_max_index) {
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;
107 }
else if (new_min_index > min_index_ || new_max_index < max_index_) {
111 T* new_storage =
new T[new_size];
112 if (new_storage ==
nullptr) {
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_));
124 min_index_ = new_min_index;
125 max_index_ = new_max_index;
126 storage_.reset(new_storage);
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;
153 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