OR-Tools  9.6
vector_map.h
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 // Vector with map from element to index in the vector.
15 
16 #ifndef OR_TOOLS_UTIL_VECTOR_MAP_H_
17 #define OR_TOOLS_UTIL_VECTOR_MAP_H_
18 
19 #include <vector>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "ortools/base/logging.h"
23 
24 namespace operations_research {
25 
26 // This class stores a vector of distinct elements, as well as a map
27 // from elements to index to find the index in the vector.
28 // This is useful to store mapping between objects and indices.
29 template <class T>
30 class VectorMap {
31  public:
32  // Adds an element if not already present, and returns its index in
33  // the vector-map.
34  int Add(const T& element) {
35  int current_index = Index(element);
36  if (current_index != -1) {
37  return current_index;
38  }
39  const int index = list_.size();
40  CHECK_EQ(index, map_.size());
41  list_.push_back(element);
42  map_[element] = index;
43  return index;
44  }
45  // TODO(user): Use ArraySlice.
46 
47  // Adds all elements of the vector.
48  void Add(const std::vector<T>& elements) {
49  for (int i = 0; i < elements.size(); ++i) {
50  Add(elements[i]);
51  }
52  }
53 
54  // Will return the index of the element if present, or die otherwise.
55  int IndexOrDie(const T& element) const { return map_.at(element); }
56 
57  // Returns -1 if the element is not in the vector, or its unique
58  // index if it is.
59  int Index(const T& element) const {
60  const auto& it = map_.find(element);
61  return it != map_.end() ? it->second : -1;
62  }
63  // TODO(user): explore a int-type version.
64 
65  // Returns whether the element has already been added to the vector-map.
66  bool Contains(const T& element) const { return map_.contains(element); }
67 
68  // Returns the element at position index.
69  const T& Element(int index) const {
70  CHECK_GE(index, 0);
71  CHECK_LT(index, list_.size());
72  return list_[index];
73  }
74 
75  const T& operator[](int index) const { return Element(index); }
76 
77  // Returns the number of distinct elements added to the vector-map.
78  int size() const { return list_.size(); }
79 
80  // Clears all the elements added to the vector-map.
81  void clear() {
82  list_.clear();
83  map_.clear();
84  }
85 
86  // Returns a read-only access to the vector of elements.
87  const std::vector<T>& list() const { return list_; }
88 
89  // Standard STL container boilerplate.
90  typedef T value_type;
91  typedef const T* pointer;
92  typedef const T& reference;
93  typedef const T& const_reference;
94  typedef size_t size_type;
95  typedef ptrdiff_t difference_type;
96  static const size_type npos;
97  typedef const T* const_iterator;
98  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
99  const_iterator begin() const { return list_.data(); }
100  const_iterator end() const { return list_.data() + list_.size(); }
102  return const_reverse_iterator(list_.data() + list_.size());
103  }
105  return const_reverse_iterator(list_.data());
106  }
107 
108  private:
109  std::vector<T> list_;
110  absl::flat_hash_map<T, int> map_;
111 };
112 
113 } // namespace operations_research
114 #endif // OR_TOOLS_UTIL_VECTOR_MAP_H_
const_reverse_iterator rend() const
Definition: vector_map.h:104
int Add(const T &element)
Definition: vector_map.h:34
const_iterator begin() const
Definition: vector_map.h:99
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: vector_map.h:98
bool Contains(const T &element) const
Definition: vector_map.h:66
const std::vector< T > & list() const
Definition: vector_map.h:87
const T & Element(int index) const
Definition: vector_map.h:69
int IndexOrDie(const T &element) const
Definition: vector_map.h:55
void Add(const std::vector< T > &elements)
Definition: vector_map.h:48
const T & operator[](int index) const
Definition: vector_map.h:75
static const size_type npos
Definition: vector_map.h:96
const_iterator end() const
Definition: vector_map.h:100
const_reverse_iterator rbegin() const
Definition: vector_map.h:101
int Index(const T &element) const
Definition: vector_map.h:59
int index
Collection of objects used to extend the Constraint Solver library.