#include "sorted_linear_finder.h"

#include <algorithm>

using std::string, std::string_view;

SortedLinearFinder::SortedLinearFinder(const WordList &word_list)
    : word_list_(word_list) {
  std::sort(
      word_list_.begin(), word_list_.end(),
      [](const string *left, const string *right) { return *left < *right; });
}

WordRefList SortedLinearFinder::find_prefix(string_view search_term) const {
  WordRefList matching_words;

  bool in_range = false;
  for (const auto *word : word_list_) {
    if (word->starts_with(search_term)) {
      matching_words.push_back(word);
      in_range = true;
    } else if (in_range) {
      break;
    }
  }

  return matching_words;
}
