!7

cprint SnippetsDeepWordIndex {
  transient DeepWordIndex<S> wordIndex; // string = snippet ID
  switchable S regexp = "\\w+|\\S"; // a word or a non-space

  start-thread {
    dm_reloadOnFieldChange regexp();
    time "Make double word index" {
      print("Making index");
      new DeepWordIndex<S> wordIndex;
      wordIndex.regexp = regexp;
      wordIndex.useHashMaps = true;
      wordIndex.sortEntries = true;
      for (virtual CSnippet sn : dm_allSnippets()) {
        S snippetID = (S) rcall snippetID(sn);
        S text = cast rcall text(sn);
        wordIndex.add(snippetID, text);
      }
      wordIndex.doneAdding();
      setField(+wordIndex);
    }
    infoBox("Indexed " + nWords(wordIndex.numWords()));
  }
  
  // API
  
  Iterable<Pair<S, Cl<Int>>> snippetPreSearch_withPositions(S query, O... _) {
    long nanos = nanoTime();
    Iterable<Pair<S, Cl<Int>>> l;
    //time "lookup" {
      l = wordIndex == null ? null : wordIndex.lookupString_withPositions(query, _);
    //}
    //print((nanoTime()-nanos) + " nanos in pre");
    ret l;
  }
}