Skip to content

Commit

Permalink
[gc_heap] Refactor Dict iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Nov 29, 2020
1 parent 835add0 commit 4c38672
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions mycpp/my_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,30 +364,25 @@ class ReverseListIter {
template <class K, class V>
class DictIter {
public:
explicit DictIter(Dict<K, V>* D) : D_(D), i_(0) {
explicit DictIter(Dict<K, V>* D) : D_(D), pos_(ValidPosAfter(0)) {
}
void Next() {
++i_;
pos_ = ValidPosAfter(pos_ + 1);
}
bool Done() {
return ValidPosition() == -1;
return pos_ == -1;
}
K Key() {
int pos = ValidPosition();
assert(pos != -1);
return D_->keys_->items_[pos];
return D_->keys_->items_[pos_];
}
V Value() {
int pos = ValidPosition();
assert(pos != -1);
return D_->values_->items_[pos];
return D_->values_->items_[pos_];
}

private:
int ValidPosition() {
int ValidPosAfter(int pos) {
// Returns the position of a valid entry at or after index i_. Or -1 if
// there isn't one. Advances i_ too.
int pos = i_;
while (true) {
if (pos >= D_->capacity_) {
return -1;
Expand All @@ -402,12 +397,11 @@ class DictIter {
}
break;
}
i_ = pos; // advance
return pos;
}

Dict<K, V>* D_;
int i_;
int pos_;
};

#endif // MY_RUNTIME_H

0 comments on commit 4c38672

Please sign in to comment.