16 #ifndef OR_TOOLS_UTIL_VECTOR_MAP_H_
17 #define OR_TOOLS_UTIL_VECTOR_MAP_H_
21 #include "absl/container/flat_hash_map.h"
34 int Add(
const T& element) {
35 int current_index =
Index(element);
36 if (current_index != -1) {
39 const int index = list_.size();
40 CHECK_EQ(
index, map_.size());
41 list_.push_back(element);
42 map_[element] =
index;
48 void Add(
const std::vector<T>& elements) {
49 for (
int i = 0; i < elements.size(); ++i) {
55 int IndexOrDie(
const T& element)
const {
return map_.at(element); }
59 int Index(
const T& element)
const {
60 const auto& it = map_.find(element);
61 return it != map_.end() ? it->second : -1;
66 bool Contains(
const T& element)
const {
return map_.contains(element); }
71 CHECK_LT(
index, list_.size());
78 int size()
const {
return list_.size(); }
87 const std::vector<T>&
list()
const {
return list_; }
109 std::vector<T> list_;
110 absl::flat_hash_map<T, int> map_;
const_reverse_iterator rend() const
int Add(const T &element)
const_iterator begin() const
std::reverse_iterator< const_iterator > const_reverse_iterator
bool Contains(const T &element) const
const std::vector< T > & list() const
const T & Element(int index) const
int IndexOrDie(const T &element) const
void Add(const std::vector< T > &elements)
const T & operator[](int index) const
static const size_type npos
const_iterator end() const
ptrdiff_t difference_type
const_reverse_iterator rbegin() const
int Index(const T &element) const
const T & const_reference
Collection of objects used to extend the Constraint Solver library.