private IEnumerable <int> PerformSearch(IList <SearchField> fields, Func <bool> isSearchCanceled) { var results = new HashSet <int>(); lock (m_syncRoot) { foreach (SearchField field in fields) { if (isSearchCanceled()) { return(null); } Tuple <int, int> key = IsFieldMultiString(field) ? Tuple.Create(field.Flid, field.String.get_WritingSystemAt(0)) : Tuple.Create(field.Flid, 0); int pos; if (!m_indexObjPos.TryGetValue(key, out pos)) { pos = 0; } if (m_searchableObjs == null || pos < m_searchableObjs.Count) { // only use the IWorkerThreadReadHandler if we are executing on the worker thread if (SynchronizationContext.Current == m_synchronizationContext) { pos = BuildIndex(pos, field, isSearchCanceled); } else { using (new WorkerThreadReadHelper(m_cache.ServiceLocator.GetInstance <IWorkerThreadReadHandler>())) pos = BuildIndex(pos, field, isSearchCanceled); } m_indexObjPos[key] = pos; } } foreach (SearchField field in fields) { if (isSearchCanceled()) { return(null); } results.UnionWith(m_searcher.Search(field.Flid, field.String)); } } if (isSearchCanceled()) { return(null); } return(results); }
public void KMP_Search_String() { string stringW = "put"; string stringS = "computer"; char[] W = stringW.ToCharArray(); char[] S = stringS.ToCharArray(); StringSearcher ss = new StringSearcher(W); int ix = ss.Search(S); Console.WriteLine("Location of W in S is " + ix); }
private bool PerformUpdate(object param) { CreateSearcher(); try { m_changingSelection = true; try { m_listBox.BeginUpdate(); m_listBox.Items.Clear(); // TODO: sort the results foreach (ICmPossibility poss in m_searcher.Search(0, (ITsString)param)) { // Every so often see whether the user has typed something that makes our search irrelevant. if (ShouldAbort()) { return(false); } var autoCompleteItem = ObjectLabel.CreateObjectLabel(m_cache, poss, m_displayNameProperty, m_displayWs); if (m_listBox.Items.OfType <ObjectLabel>().All(item => !ReferenceEquals(item.Object, autoCompleteItem.Object))) { m_listBox.Items.Add(autoCompleteItem); } } } finally { m_listBox.EndUpdate(); } if (m_listBox.Items.Count > 0) { m_listBox.AdjustSize(500, 400); m_listBox.SelectedIndex = 0; m_listBox.Launch(m_control.RectangleToScreen(m_control.Bounds), Screen.GetWorkingArea(m_control)); } else { m_listBox.HideForm(); } } finally { m_changingSelection = false; } return(true); }
private static void CheckNoResultsSearch(StringSearcher<int> searcher, ITsString tss) { Assert.AreEqual(0, searcher.Search(0, tss).Count()); }
private static void CheckSearch(StringSearcher<int> searcher, ITsString tss, int[] expectedResults) { Assert.AreEqual(expectedResults.Length, searcher.Search(0, tss).Intersect(expectedResults).Count()); }
/// <summary> /// Searches the specified fields. /// </summary> /// <param name="fields">The fields.</param> /// <param name="filters">The filters.</param> public void Search(IEnumerable <SearchField> fields, IEnumerable <ICmObject> filters) { // If we abort this search (because the user typed), but somehow the system becomes idle before we complete a new // search, go ahead and complete the original one. m_pendingFields = fields; m_pendingFilters = filters; m_mediator.IdleQueue.Add(IdleQueuePriority.High, DoPendingSearchWhenIdle); CreateSearchers(); var results = new HashSet <ICmObject>(); ITsString firstSearchStr = null; foreach (SearchField field in fields) { if (ShouldAbort()) { return; } if (firstSearchStr == null) { firstSearchStr = field.String; } results.UnionWith(m_searcher.Search(field.Flid, field.String)); } if (filters != null) { results.ExceptWith(filters); } if (ShouldAbort()) { return; } // The following fixes LT-10293. RecordSorter sorter = null; if (firstSearchStr != null) { int ws = firstSearchStr.get_WritingSystemAt(0); bool isVern = m_cache.ServiceLocator.WritingSystems.VernacularWritingSystems.Contains(ws); sorter = m_bvMatches.CreateSorterForFirstColumn(isVern, ws); } if (sorter != null) { // Convert each ICmObject in results to a IManyOnePathSortItem, and sort // using the sorter. var records = new ArrayList(results.Count); foreach (ICmObject obj in results) { records.Add(new ManyOnePathSortItem(obj)); } sorter.Sort(records); var hvos = new int[records.Count]; for (int i = 0; i < records.Count; ++i) { hvos[i] = (((IManyOnePathSortItem)records[i]).KeyObject); } UpdateResults(hvos); } else { UpdateResults(results.Select(obj => obj.Hvo).ToArray()); } // Completed successfully, don't want to do again. m_pendingFields = null; m_mediator.IdleQueue.Remove(DoPendingSearchWhenIdle); }
public static void Main() { //string word = "xx"; //string text = "xxx"; var word = Console.ReadLine(); var text = Console.ReadLine(); char[] S = text.ToCharArray(); var prefix = ""; BigInteger result = 0; for (int i = 0; i < word.Length; i++) { prefix += word[i]; var suffix = ""; for (int j = i; j < word.Length - 1; j++) { suffix += word[j + 1]; } char[] prefixCharArr = prefix.ToCharArray(); char[] suffixCharArr = suffix.ToCharArray(); StringSearcher prefixSearcher = new StringSearcher(prefixCharArr); int prefixIndex = 0; BigInteger prefixCount = 0; do { prefixIndex = prefixSearcher.Search(S, prefixIndex); if (prefixIndex != -1) { prefixCount++; //prefixIndex += prefix.Length; prefixIndex++; } } while (prefixIndex != -1); int suffixIndex = 0; BigInteger suffixCount = 0; if (suffix != "") { StringSearcher suffixSearcher = new StringSearcher(suffixCharArr); do { suffixIndex = suffixSearcher.Search(S, suffixIndex); if (suffixIndex != -1) { suffixCount++; //suffixIndex += suffix.Length; suffixIndex++; } } while (suffixIndex != -1); } else { suffixCount = 1; } result += prefixCount * suffixCount; } Console.WriteLine(result); }