<commit_before>package org.smoothbuild.fs.base;

import static com.google.common.base.Preconditions.checkState;

import java.util.ArrayDeque;
import java.util.Iterator;

import org.smoothbuild.plugin.api.Path;

public class RecursiveFilesIterator implements Iterator<Path> {
  private final FileSystem fileSystem;

  private final ArrayDeque<Path> directoryStack;
  private final ArrayDeque<Path> fileStack;
  private Path nextFile;

  public RecursiveFilesIterator(FileSystem fileSystem) {
    this.fileSystem = fileSystem;
    this.directoryStack = new ArrayDeque<Path>();
    this.fileStack = new ArrayDeque<Path>();
    this.directoryStack.push(Path.rootPath());

    nextFile = fetchNextFile();
  }

  @Override
  public boolean hasNext() {
    return nextFile != null;
  }

  @Override
  public Path next() {
    checkState(hasNext());

    Path result = nextFile;
    nextFile = fetchNextFile();
    return result;
  }

  private Path fetchNextFile() {
    while (!fileStack.isEmpty() || !directoryStack.isEmpty()) {
      if (fileStack.isEmpty()) {
        Path dir = directoryStack.remove();
        for (String name : fileSystem.childNames(dir)) {
          fileStack.add(dir.append(Path.path(name)));
        }
      } else {
        Path file = fileStack.remove();
        if (fileSystem.pathExistsAndIsDirectory(file)) {
          directoryStack.add(file);
        } else {<commit_msg>fixed RecursiveFilesIterator to keep directories to visit on stack so traversing algorithm uses less memory on average<commit_after>package org.smoothbuild.fs.base;

import static com.google.common.base.Preconditions.checkState;

import java.util.ArrayDeque;
import java.util.Iterator;

import org.smoothbuild.plugin.api.Path;

public class RecursiveFilesIterator implements Iterator<Path> {
  private final FileSystem fileSystem;

  private final ArrayDeque<Path> directoryStack;
  private final ArrayDeque<Path> fileStack;
  private Path nextFile;

  public RecursiveFilesIterator(FileSystem fileSystem) {
    this.fileSystem = fileSystem;
    this.directoryStack = new ArrayDeque<Path>();
    this.fileStack = new ArrayDeque<Path>();
    this.directoryStack.push(Path.rootPath());

    nextFile = fetchNextFile();
  }

  @Override
  public boolean hasNext() {
    return nextFile != null;
  }

  @Override
  public Path next() {
    checkState(hasNext());

    Path result = nextFile;
    nextFile = fetchNextFile();
    return result;
  }

  private Path fetchNextFile() {
    while (!fileStack.isEmpty() || !directoryStack.isEmpty()) {
      if (fileStack.isEmpty()) {
        Path dir = directoryStack.remove();
        for (String name : fileSystem.childNames(dir)) {
          fileStack.push(dir.append(Path.path(name)));
        }
      } else {
        Path file = fileStack.remove();
        if (fileSystem.pathExistsAndIsDirectory(file)) {
          directoryStack.push(file);
        } else {
{'fraction_non_alphanumeric': 0.10816396830864623, 'fraction_numerical': 0.0, 'mean_word_length': 3.573228346456693, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3520715', 'n_tokens_mistral': 851, 'n_tokens_neox': 821, 'n_words': 192}