14#ifndef OR_TOOLS_SET_COVER_BASE_TYPES_H_
15#define OR_TOOLS_SET_COVER_BASE_TYPES_H_
22#include "absl/log/check.h"
23#include "absl/time/time.h"
24#include "absl/types/span.h"
78template <
typename EntryIndex,
typename Index>
102template <
typename EntryIndex,
typename Index>
117 : memorized_value_(0), byte_stream_() {
118 for (
const Index x : strong_vector) {
124 : memorized_value_(0), byte_stream_() {
125 for (
const Index x : vec) {
134 const std::vector<uint8_t>&
byte_stream()
const {
return byte_stream_; }
139 bool empty()
const {
return byte_stream_.empty(); }
142 void Reserve(
size_t n) { byte_stream_.reserve(n); }
153 void EncodeInteger(
BaseInt x) {
154 BaseInt delta = x - memorized_value_;
158 while (delta >= 128) {
161 byte_stream_.push_back(
static_cast<uint8_t
>(delta | 0x80));
167 byte_stream_.push_back(
static_cast<uint8_t
>(delta));
170 memorized_value_ = x + kMinDelta;
177 static constexpr int64_t kMinDelta = 1;
180 std::vector<uint8_t> byte_stream_;
187template <
typename EntryIndex,
typename Index>
192 : compressed_vector_(compressed_vector),
200 : compressed_vector_(compressed_vector),
203 last_pos_(
at_end ? compressed_vector.size_in_bytes() : 0) {}
206 DCHECK_LE(last_pos_, compressed_vector_.size_in_bytes());
207 return last_pos_ == compressed_vector_.size_in_bytes();
213 DCHECK_EQ(&compressed_vector_, &(other.compressed_vector_));
214 return last_pos_ == other.last_pos_;
218 return !(*
this == other);
227 Index DecodeInteger() {
229 const std::vector<uint8_t>& encoded = compressed_vector_.byte_stream();
232 for (pos_ = last_pos_, shift = 0;
233 encoded[pos_] >= 128 && pos_ < compressed_vector_.size_in_bytes();
234 shift += 7, ++pos_) {
235 delta |=
static_cast<BaseInt>(encoded[pos_] & 0x7F) << shift;
237 delta |=
static_cast<BaseInt>(encoded[pos_]) << shift;
239 memorized_value_ += Index(delta + kMinDelta);
240 return memorized_value_ - Index(kMinDelta);
244 const CompressedStrongVector<EntryIndex, Index>& compressed_vector_;
248 Index memorized_value_;
256 static constexpr int64_t kMinDelta = 1;
272template <
typename Index>
277template <
typename Index>
305template <
class Index>
309template <
typename Index>
313 : range_(range), current_(range.get_start()) {}
316 : range_(range), current_(
at_end ? range.get_end() : range.get_start()) {}
318 bool at_end()
const {
return current_ == range_.get_end(); }
323 DCHECK_EQ(range_.get_start(), other.range_.get_start());
324 DCHECK_EQ(range_.get_end(), other.range_.get_end());
325 return current_ == other.current_;
329 return !(*
this == other);
349template <
typename T,
typename Derived>
355 auto begin() {
return static_cast<Derived*
>(
this)->begin_impl(); }
356 auto end() {
return static_cast<Derived*
>(
this)->end_impl(); }
357 auto begin()
const {
return static_cast<const Derived*
>(
this)->begin_impl(); }
358 auto end()
const {
return static_cast<const Derived*
>(
this)->end_impl(); }
386 explicit StopWatch(absl::Duration* duration) : duration_(duration), timer_() {
391 *duration_ = timer_.GetDuration();
400 absl::Duration* duration_;
bool operator!=(const CompressedStrongVectorIterator &other) const
CompressedStrongVectorIterator(const CompressedStrongVector< EntryIndex, Index > &compressed_vector)
bool operator==(const CompressedStrongVectorIterator &other) const
CompressedStrongVectorIterator & operator++()
CompressedStrongVectorIterator(const CompressedStrongVector< EntryIndex, Index > &compressed_vector, bool at_end)
void push_back(Index x)
Appends an x to the vector in a compressed form.
size_t size_in_bytes() const
Returns the number of bytes needed to store the vector.
CompressedStrongVectorIterator< EntryIndex, Index > iterator
CompressedStrongVector(const util_intops::StrongVector< EntryIndex, Index > &strong_vector)
void Reserve(size_t n)
Reserves space for n bytes.
CompressedStrongVectorIterator< EntryIndex, Index > const_iterator
const_iterator begin() const
For const range-for loops.
iterator begin()
For range-for loops.
const_iterator end() const
CompressedStrongVector(absl::Span< const Index > vec)
const std::vector< uint8_t > & byte_stream() const
Returns a reference to the underlying byte stream.
The iterator for an IndexRange.
IndexRangeIterator(const IndexRange< Index > &range, bool at_end)
bool operator!=(const IndexRangeIterator &other) const
bool operator==(const IndexRangeIterator &other) const
IndexRangeIterator & operator++()
IndexRangeIterator(const IndexRange< Index > &range)
typename std::decay_t< T >::value_type value_type
decltype(std::begin(std::declval< T >())) iterator_type
IterableContainer(const T &data_source)
double elapsed_time_in_seconds() const
absl::Duration GetElapsedDuration() const
StopWatch(absl::Duration *duration)
In SWIG mode, we don't want anything besides these top-level includes.
IterableContainer(const T &data_source) -> IterableContainer< T >
Additional deduction guide.
CompressedStrongVectorIterator< ColumnEntryIndex, ElementIndex > CompressedColumnIterator
util_intops::StrongVector< ElementIndex, CompressedRow > CompressedRowView
util_intops::StrongVector< RowEntryIndex, SubsetIndex > SparseRow
util_intops::StrongVector< SubsetIndex, Cost > SubsetCostVector
CompressedStrongVector< RowEntryIndex, SubsetIndex > CompressedRow
util_intops::StrongVector< SubsetIndex, BaseInt > SubsetToIntVector
util_intops::StrongVector< ElementIndex, SubsetIndex > ElementToSubsetVector
Maps from element to subset. Useful to compress the sparse row view.
util_intops::StrongIntRange< SubsetIndex > SubsetRange
util_intops::StrongVector< ElementIndex, Cost > ElementCostVector
CompressedStrongVector< ColumnEntryIndex, ElementIndex > CompressedColumn
util_intops::StrongIntRange< ColumnEntryIndex > ColumnEntryRange
IndexRange(Index a, Index b) -> IndexRange< Index >
Additional deduction guide.
util_intops::StrongVector< ElementIndex, BaseInt > ElementToIntVector
util_intops::StrongVector< SubsetIndex, bool > SubsetBoolVector
util_intops::StrongVector< SubsetIndex, SparseColumn > SparseColumnView
Views of the sparse vectors.
util_intops::StrongVector< ElementIndex, bool > ElementBoolVector
CompressedStrongVectorIterator< RowEntryIndex, SubsetIndex > CompressedRowIterator
util_intops::StrongVector< SubsetIndex, CompressedColumn > CompressedColumnView
double Cost
Basic non-strict type for cost. The speed penalty for using double is ~2%.
util_intops::StrongIntRange< ElementIndex > ElementRange
util_intops::StrongVector< ColumnEntryIndex, ElementIndex > SparseColumn
util_intops::StrongVector< ElementIndex, SparseRow > SparseRowView
#define DEFINE_STRONG_INT_TYPE(type_name, value_type)
IndexRangeIterator< Index > const_iterator
IndexRangeIterator< Index > iterator
IndexRange(Index start, Index end)
iterator begin()
For range-for loops.
const_iterator begin() const
For const range-for loops.
const_iterator end() const