00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef OR_TOOLS_UTIL_ZVECTOR_H_
00015 #define OR_TOOLS_UTIL_ZVECTOR_H_
00016
00017 #if defined(__APPLE__) && defined(__GNUC__)
00018 #include <machine/endian.h>
00019 #elif !defined(_MSC_VER)
00020 #include <endian.h>
00021 #endif
00022 #include <string.h>
00023 #include <climits>
00024 #include <cstdio>
00025 #include <limits>
00026
00027 #include "base/integral_types.h"
00028 #include "base/logging.h"
00029 #include "base/macros.h"
00030 #include "base/scoped_ptr.h"
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 namespace operations_research {
00041
00042 template<class T> class ZVector {
00043 public:
00044 ZVector() : base_(NULL),
00045 min_index_(0),
00046 max_index_(0),
00047 size_(0),
00048 storage_() {}
00049
00050 ZVector(int64 min_index, int64 max_index)
00051 : base_(NULL),
00052 min_index_(0),
00053 max_index_(0),
00054 size_(0),
00055 storage_() {
00056 if (!Reserve(min_index, max_index)) {
00057 LOG(DFATAL) << "Could not reserve memory for indices ranging from "
00058 << min_index << " to " << max_index;
00059 }
00060 }
00061
00062 int64 min_index() const { return min_index_; }
00063
00064 int64 max_index() const { return max_index_; }
00065
00066
00067 T Value(int64 index) const {
00068 DCHECK_LE(min_index_, index);
00069 DCHECK_GE(max_index_, index);
00070 DCHECK(base_ != NULL);
00071 return base_[index];
00072 }
00073
00074 #if !defined(SWIG)
00075
00076 T& operator[](int64 index) {
00077 DCHECK_LE(min_index_, index);
00078 DCHECK_GE(max_index_, index);
00079 DCHECK(base_ != NULL);
00080 return base_[index];
00081 }
00082
00083 const T operator[](int64 index) const {
00084 DCHECK_LE(min_index_, index);
00085 DCHECK_GE(max_index_, index);
00086 DCHECK(base_ != NULL);
00087 return base_[index];
00088 }
00089 #endif
00090
00091
00092 void Set(int64 index, T value) {
00093 DCHECK_LE(min_index_, index);
00094 DCHECK_GE(max_index_, index);
00095 DCHECK(base_ != NULL);
00096 base_[index] = value;
00097 }
00098
00099
00100
00101
00102 bool Reserve(int64 new_min_index, int64 new_max_index) {
00103 if (new_min_index > new_max_index) {
00104 return false;
00105 }
00106 const uint64 new_size = new_max_index - new_min_index + 1;
00107 if (base_ != NULL) {
00108 if (new_min_index >= min_index_ && new_max_index <= max_index_) {
00109 min_index_ = new_min_index;
00110 max_index_ = new_max_index;
00111 size_ = new_size;
00112 return true;
00113 } else if (new_min_index > min_index_ || new_max_index < max_index_) {
00114 return false;
00115 }
00116 }
00117 T* new_storage = new T[new_size];
00118 if (new_storage == NULL) {
00119 return false;
00120 }
00121
00122 T* const new_base = new_storage - new_min_index;
00123 if (base_ != NULL) {
00124 T* const destination = new_base + min_index_;
00125 memcpy(destination, storage_.get(), size_ * sizeof(*base_));
00126 }
00127
00128 base_ = new_base;
00129 size_ = new_size;
00130 min_index_ = new_min_index;
00131 max_index_ = new_max_index;
00132 storage_.reset(new_storage);
00133 return true;
00134 }
00135
00136
00137 void SetAll(T value) {
00138 for (T* current = base_ + min_index_;
00139 current <= base_ + max_index_;
00140 ++current) {
00141 *current = value;
00142 }
00143 }
00144
00145 private:
00146
00147 T* base_;
00148
00149
00150 int64 min_index_;
00151
00152
00153 int64 max_index_;
00154
00155
00156 int64 size_;
00157
00158
00159 scoped_array<T> storage_;
00160 };
00161
00162
00163 typedef ZVector<int8> Int8ZVector;
00164 typedef ZVector<int16> Int16ZVector;
00165 typedef ZVector<int32> Int32ZVector;
00166 typedef ZVector<int64> Int64ZVector;
00167 typedef ZVector<uint8> UInt8ZVector;
00168 typedef ZVector<uint16> UInt16ZVector;
00169 typedef ZVector<uint32> UInt32ZVector;
00170 typedef ZVector<uint64> UInt64ZVector;
00171
00172 }
00173
00174 #endif // OR_TOOLS_UTIL_ZVECTOR_H_