public ISearchResult Find(SearchOptions options, bool reverse)
        {
            // insanity check
            Debug.Assert(searchStrategy      != null);
            Debug.Assert(documentIterator    != null);
            Debug.Assert(options             != null);

            while (!cancelled)
            {
                if (info != null && textIterator != null && documentIterator.CurrentFileName != null) {
                    if (info.FileName != documentIterator.CurrentFileName || lastWasReverse != reverse) {
                        // create new iterator, if document changed or search direction has changed.
                        info = documentIterator.Current;
                        textIterator = info.GetTextIterator ();
                        reverseSearchMap = null;
                        lastResultPos = -1;
                        if (reverse)
                            textIterator.MoveToEnd ();
                    }

                    ISearchResult result;
                    if (!reverse)
                        result = searchStrategy.FindNext (textIterator, options, false);
                    else {
                        if (searchStrategy.SupportsReverseSearch (textIterator, options)) {
                            result = searchStrategy.FindNext (textIterator, options, true);
                        }
                        else {
                            if (reverseSearchMap == null) {
                                reverseSearchMap = new SearchMap ();
                                reverseSearchMap.Build (searchStrategy, textIterator, options);
                            }
                            if (lastResultPos == -1)
                                lastResultPos = textIterator.Position;
                            result = reverseSearchMap.GetPreviousMatch (lastResultPos);
                            if (result != null)
                                textIterator.Position = result.Position;
                        }
                    }

                    if (result != null) {
                        matches++;
                        lastResultPos = result.Position;
                        lastWasReverse = reverse;
                        return result;
                    }
                }

                if (textIterator != null) textIterator.Close ();

                // not found or first start -> move forward to the next document
                bool more = !reverse ? documentIterator.MoveForward () : documentIterator.MoveBackward ();
                if (more) {
                    searchedFiles++;
                    info = documentIterator.Current;
                    textIterator = info.GetTextIterator ();
                    reverseSearchMap = null;
                    lastResultPos = -1;
                    if (reverse)
                        textIterator.MoveToEnd ();
                }
                else
                    cancelled = true;

                lastWasReverse = reverse;
            }

            cancelled = false;
            return null;
        }
 public void Reset()
 {
     documentIterator.Reset();
     textIterator = null;
     reverseSearchMap = null;
     cancelled = false;
     searchedFiles = 0;
     matches = 0;
     lastResultPos = -1;
 }