| 65 | class HashSet { |
| 66 | template <class Elem, class HashSetType> |
| 67 | class BaseIterator : std::iterator<std::forward_iterator_tag, Elem> { |
| 68 | public: |
| 69 | BaseIterator(const BaseIterator&) = default; |
| 70 | BaseIterator(BaseIterator&&) = default; |
| 71 | BaseIterator(HashSetType* hash_set, size_t index) : index_(index), hash_set_(hash_set) { |
| 72 | } |
| 73 | BaseIterator& operator=(const BaseIterator&) = default; |
| 74 | BaseIterator& operator=(BaseIterator&&) = default; |
| 75 | |
| 76 | bool operator==(const BaseIterator& other) const { |
| 77 | return hash_set_ == other.hash_set_ && this->index_ == other.index_; |
| 78 | } |
| 79 | |
| 80 | bool operator!=(const BaseIterator& other) const { |
| 81 | return !(*this == other); |
| 82 | } |
| 83 | |
| 84 | BaseIterator operator++() { // Value after modification. |
| 85 | this->index_ = this->NextNonEmptySlot(this->index_, hash_set_); |
| 86 | return *this; |
| 87 | } |
| 88 | |
| 89 | BaseIterator operator++(int) { |
| 90 | BaseIterator temp = *this; |
| 91 | this->index_ = this->NextNonEmptySlot(this->index_, hash_set_); |
| 92 | return temp; |
| 93 | } |
| 94 | |
| 95 | Elem& operator*() const { |
| 96 | DCHECK(!hash_set_->IsFreeSlot(this->index_)); |
| 97 | return hash_set_->ElementForIndex(this->index_); |
| 98 | } |
| 99 | |
| 100 | Elem* operator->() const { |
| 101 | return &**this; |
| 102 | } |
| 103 | |
| 104 | // TODO: Operator -- --(int) (and use std::bidirectional_iterator_tag) |
| 105 | |
| 106 | private: |
| 107 | size_t index_; |
| 108 | HashSetType* hash_set_; |
| 109 | |
| 110 | size_t NextNonEmptySlot(size_t index, const HashSet* hash_set) const { |
| 111 | const size_t num_buckets = hash_set->NumBuckets(); |
| 112 | DCHECK_LT(index, num_buckets); |
| 113 | do { |
| 114 | ++index; |
| 115 | } while (index < num_buckets && hash_set->IsFreeSlot(index)); |
| 116 | return index; |
| 117 | } |
| 118 | |
| 119 | friend class HashSet; |
| 120 | }; |
| 121 | |
| 122 | public: |
| 123 | using value_type = T; |
nothing calls this directly
no test coverage detected