2013-03-16 160 views
-4

我列出了填充文件名。我需要用搜索結果填充另一個列表。這個搜索結果必須通過二進制搜索來完成,下一個規範:關鍵字可以匹配文件名或它的子串(0,key.Length)。 例如關鍵字= 「車」 搜索結果= 「car.gif」, 「carffur.pdf」 等,但不是 「mobilecar.jar」用二進制搜索填充列表

我的二進制搜索:

class Search 
{ 
    private const int KEY_NOT_FOUND = -1; 

    public static int BinarySearch<T>(List<T> A, T key, IComparer<T> comparer, int imin, int imax) 
    { 
     // test if array is empty 
     if (imax < imin) 
      // set is empty, so return value showing not found 
      return KEY_NOT_FOUND; 
     else 
     { 
      // calculate midpoint to cut set in half 
      int imid = imin + ((imax - imin)/2); 
      int compareResult = comparer.Compare(A[imid], key); 
      // three-way comparison 
      if (compareResult > 0) 
       // key is in lower subset 
       return BinarySearch<T>(A, key, comparer,imin, imid - 1); 
      else if (compareResult < 0) 
       // key is in upper subset 
       return BinarySearch<T>(A, key, comparer, imid + 1, imax); 
      else 
       // key has been found 
       return imid; 
     } 
    } 
} 

我的比較器類:

class SubStringComparison : IComparer<string> 
{ 
    public int Compare(string x, string y) 
    { 
     if (x.Length > y.Length && x.Substring(0, y.Length).Equals(y)) 
      return 0; 

     return String.Compare(x, y, StringComparison.OrdinalIgnoreCase); 
    } 
} 

用法:

private void backgroundWorkerSearch_DoWork(object sender, DoWorkEventArgs e) 
    { 
     SearchForFiles(); 
    } 

    private void SearchForFiles() 
    { 
     List<string> files = listBoxFiles.Items.OfType<string>().ToList(); 
     searchResults = new List<string>(); 
     listBoxFiles.Dispatcher.Invoke(new Action(delegate() 
      { 
       while (true) 
       { 
        int index = Search.BinarySearch<string>(files, textBoxFileToSearch.Text, new SubStringComparison(), 0, files.Count - 1); 
        if (index == -1) 
         break; 
        else 
         searchResults.Add(files[index]); 
       } 
      })); 
    } 
+0

請出示的努力和解釋什麼是「搜索結果必須由二進制搜索來完成」。通常這是通過'fileNames.OrderBy(....)完成的,但是你的要求很不明確。 – 2013-03-16 05:58:07

+0

我不應該使用任何內置的.net方法。我必須自己寫。 問題是,當我使用自己寫的二進制搜索時,它每次都會返回一個結果,但我需要它返回另一個結果。 – 2013-03-16 06:00:57

+0

你在看什麼 - 這樣做的權限?請說清楚你的問題是「自己寫」,這樣可以提供幫助。 – 2013-03-16 06:05:37

回答

0

-1,作業的一部分,你有麻煩? 它是二進制搜索嗎? 是否選擇文件名? BackgroundWorker是不是按預期工作?

我看到你有一些BackgroundWorker的問題,好像你在BackgroundWorker線程中從UI中選擇了一個文件列表,然後創建一個將在主線程中運行的Action,這是一種非常奇怪的方式做到這一點。

正確的方法是:

public void button1_Click(object sender) 
{ 
    List<string> files = listBoxFiles.Items.OfType<string>().ToList(); 
    string key = textBoxFileToSearch.Text; 
    backgroundWorkerSearch.RunWorkerAsync(new Tupple<List<string>,string>(files, key)); 
} 

void backgroundWorkerSearch_DoWork(object sender, DoWorkEventArgs e) 
{ 
    var state = e.Argument as Tupple<List<string>,string>; 
    List<string> files = state.Item1; 
    string key = state.Item2; 
    // You can now access the needed data. 
    List<string> searchResult = new List<string>(); 
    // ... 
    e.Result = searchResult; 
} 

void backgroundWorkerSearch_RunWorkerCompleted(RunWorkerCompletedEventArgs e) 
{ 
    List<string> searchResult = e.Result; 
    // Show result in the UI thread. 
}