1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "tools/gn/deps_iterator.h"
6
7#include "tools/gn/target.h"
8
9DepsIterator::DepsIterator(const Target* t) : current_index_(0) {
10  vect_stack_[0] = &t->public_deps();
11  vect_stack_[1] = &t->private_deps();
12  vect_stack_[2] = &t->data_deps();
13
14  if (vect_stack_[0]->empty())
15    Advance();
16}
17
18// Iterate over the public and private linked deps, but not the data deps.
19DepsIterator::DepsIterator(const Target* t, LinkedOnly) : current_index_(0) {
20  vect_stack_[0] = &t->public_deps();
21  vect_stack_[1] = &t->private_deps();
22  vect_stack_[2] = NULL;
23
24  if (vect_stack_[0]->empty())
25    Advance();
26}
27
28// Advance to the next position. This assumes there are more vectors.
29//
30// For internal use, this function tolerates an initial index equal to the
31// length of the current vector. In this case, it will advance to the next
32// one.
33void DepsIterator::Advance() {
34  DCHECK(vect_stack_[0]);
35
36  current_index_++;
37  if (current_index_ >= vect_stack_[0]->size()) {
38    // Advance to next vect. Shift the elements left by one.
39    vect_stack_[0] = vect_stack_[1];
40    vect_stack_[1] = vect_stack_[2];
41    vect_stack_[2] = NULL;
42
43    current_index_ = 0;
44
45    if (vect_stack_[0] && vect_stack_[0]->empty())
46      Advance();
47  }
48}
49