2013-03-12 55 views
2

問題描述:C#濾波器雙[]的值的列表

我具有表示向量,其中第0向量元素對應於所述矢量的物理長度和其他雙[]值的列表3 (1到3)對應於x,y和z分量。該列表包含大約1000000個條目。所以perfermonce將是一個問題。我根據矢量的長度對列表進行了排序。現在我需要過濾列表,使得長度不同的矢量保持不變,如果長度相同,則位置1到3中包含不同的條目(不是permuatations)的那些濾波器保持如示例中所示。如果您需要更多信息,請告訴我。 在過濾過程中不應更改矢量。

問題:如何使用C#實現以及如果可能的話使用linq?

實施例:

0, 0,  0;  0,0000 -> select 
0, 1, -1;  8,2883 -> select 
1, 0, -1;  8,2883 -> not select 
0, -1,  1;  8,2883 -> not select 
-1, 0,  1;  8,2883 -> not select 
1, -1,  0;  8,2883 -> not select 
-1, 1,  0;  8,2883 -> not select 
1, 1, -2;  14,3558 -> select 
... 
2,  2, -5; 38,6145 -> select 
-2, -2,  5; 38,6145 -> not select 
1,  4, -4; 38,6145 -> select 
4,  1, -4; 38,6145 -> not select 
-1, -4,  4; 38,6145 -> not select 
-4, -1,  4; 38,6145 -> not select 
-1,  4, -4; 38,6145 -> not select 
4, -1, -4; 38,6145 -> not select 
-4,  1,  4; 38,6145 -> not select 
1, -4,  4; 38,6145 -> not select 
-2,  5, -2; 38,6145 -> not select 
5, -2, -2; 38,6145 -> not select 
2, -5,  2; 38,6145 -> not select 
-5,  2,  2; 38,6145 -> not select 
4, -4, -1; 38,6145 -> not select 
-4,  4, -1; 38,6145 -> not select 
-4,  4,  1; 38,6145 -> not select 
4, -4,  1; 38,6145 -> not select 
... 

CODE:

所有的
private static double absm = 0; 
private static int[] m = new int[3]; 
private static int[] m2 = new int[3]; 
private static List<double[]> ihkl1 = new List<double[]>(); 
private static List<double[]> ihkl2 = new List<double[]>(); 

... 

private static void init_latt() 
{ 
    for (int i = -kmax[2]; i < kmax[2]; i++) 
    { 
     m[2] = i; 
     for (int j = -kmax[1]; j < kmax[1]; j++) 
     { 
      m[1] = j; 
      for (int k = -kmax[0]; k < kmax[0]; k++) 
      {       
       m[0] = k; 
       absm = calcabsm(metten, m);            
       if (absm < gmax) 
       { 
        double[] row1 = new double[4]; 
        row1[0] = absm; 
        row1[1] = (double)m[0]; 
        row1[2] = (double)m[1]; 
        row1[3] = (double)m[2]; 
        ihkl1.Add(row1); 
       } 
      } 
     } 
    }  
    ihkl2 = ihkl1.AsParallel().OrderBy(x => x[0]).ToList(); 
} 
... 
+9

第一個建議:從「列表」更改爲「列表」,其中「Vector」是四個值的適當封裝。你的代碼將會更加清晰。 – 2013-03-12 10:00:46

+1

[你到目前爲止嘗試過什麼](http://whathaveyoutried.com)?你卡在哪裏?請張貼您當前的代碼並解釋它的缺點。 – Oded 2013-03-12 10:00:50

+0

list.distinct(); – 1Mayur 2013-03-12 10:01:24

回答

0

首先,我使用的一類Vector它封裝那些double陣列Jon Skeet's建議一致。之後,你可以這樣做:

public class VectorEqualityComparer : IEqualityComparer<Vector> 
{ 
    public bool Equals(Vector x, Vector y) 
    { 
     //here you implement the equality among vectors you defined in your question 
    } 

    public int GetHashCode(Vector obj) 
    { 
     //you can return something like obj.InnerArray.GetHashCode() 
    } 
} 

現在,如果你有Vector,即yourList列表,您可以撥打:

var result = yourList.Distinct(new VectorEqualityComparer()); 

希望這可以幫助你實現你想要的。祝你好運!!!

+0

非常感謝,我會盡力...如果還有其他問題,我會在這裏發佈 – user2143695 2013-03-12 15:42:23