Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
indexmap.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_INDEXMAP_H_
19 #define ION_BASE_INDEXMAP_H_
20 
21 #include "ion/base/logging.h"
22 
23 namespace ion {
24 namespace base {
25 
39 template <typename OrderedIndexType, typename UnorderedIndexType>
40 class IndexMap {
41  public:
47  IndexMap(const UnorderedIndexType unordered_indices[], size_t count)
48  : unordered_indices_(unordered_indices),
49  count_(count) {}
50 
52  size_t GetCount() const { return count_; }
53 
56  UnorderedIndexType GetUnorderedIndex(OrderedIndexType ordered_index) const {
57  DCHECK_LE(0, static_cast<int>(ordered_index));
58  DCHECK_GT(count_, static_cast<size_t>(ordered_index));
59  return unordered_indices_[ordered_index];
60  }
61 
65  OrderedIndexType GetOrderedIndex(UnorderedIndexType unordered_index) const {
66  int ordered_index = -1;
67  for (size_t i = 0; i < count_; ++i) {
68  if (unordered_indices_[i] == unordered_index) {
69  ordered_index = static_cast<int>(i);
70  break;
71  }
72  }
73  DCHECK_GE(ordered_index, 0) << "IndexMap: Invalid unordered index "
74  << unordered_index << " does not match any "
75  << "ordered index.";
76  return static_cast<OrderedIndexType>(ordered_index);
77  }
78 
79  private:
80  const UnorderedIndexType* unordered_indices_;
81  const size_t count_;
82 };
83 
84 } // namespace base
85 } // namespace ion
86 
87 #endif // ION_BASE_INDEXMAP_H_
size_t GetCount() const
Returns the count passed to the constructor.
Definition: indexmap.h:52
OrderedIndexType GetOrderedIndex(UnorderedIndexType unordered_index) const
Returns the OrderedIndexType corresponding to the given UnorderedIndexType value. ...
Definition: indexmap.h:65
#define DCHECK_GT(val1, val2)
Definition: logging.h:337
UnorderedIndexType GetUnorderedIndex(OrderedIndexType ordered_index) const
Returns the UnorderedIndexType corresponding to the given OrderedIndexType value. ...
Definition: indexmap.h:56
This template class can be used to map between two kinds of indices when the following assumptions ap...
Definition: indexmap.h:40
#define DCHECK_GE(val1, val2)
Definition: logging.h:336
Copyright 2016 Google Inc.
IndexMap(const UnorderedIndexType unordered_indices[], size_t count)
The map is initialized with an array of N UnorderedIndexType values that directly correspond to a 0-b...
Definition: indexmap.h:47
#define DCHECK_LE(val1, val2)
Definition: logging.h:334