use sortedset

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace AnotherLog4
  7 {
  8     class Program
  9     {
 10 
 11 
 12         static void Main(string[] args)
 13         {
 14             List<byte[]> list = new List<byte[]>();
 15             Random rd = new Random(); 
 16             for (int i = 0; i < 15; i++)
 17             {
 18                 list.Add(new Byte[] { (Byte)rd.Next(0,200) });
 19                 list.Add(new Byte[] { (Byte)rd.Next(0, 200) });
 20             }
 21 
 22             var compare = new ByteComparetor();
 23             SortedSet<Byte[]> hashset = new SortedSet<Byte[]>(list,compare);        // use binary-tree structure
 24 
 25             Console.WriteLine("My method");
 26             var compare2 = new ByteComparetor();
 27             var result = compare2.GetCompareResult(list);
 28             foreach (var item in result)
 29             {
 30                 Console.WriteLine(item[0]);
 31             }
 32 
 33             Console.WriteLine("LingLing method");
 34             foreach (var item in hashset)
 35             {
 36                 Console.WriteLine(item[0]);
 37                 // Console.WriteLine(item);
 38             }
 39             Console.WriteLine("Total Compare time is :" + compare.count);
 40             Console.WriteLine("Total sencnd Compare time is :" + compare2.count);
 41             Console.WriteLine("Total count  :" + list.Count);
 42             Console.WriteLine("My Filter count  :" + (list.Count - result.Count()));
 43             Console.WriteLine("Filter count  :" + (list.Count - hashset.Count));
 44             Console.Read();
 45         }
 46     }
 47 
 48     public class ByteComparetor : IComparer<byte[]>
 49     {
 50         public  int count = 0;
 51 
 52         public int Compare(byte[] a1, byte[] a2)
 53         {
 54             count++;
 55             int len = a1.Length < a2.Length ? a1.Length : a2.Length;    // get the shorter length
 56             int res = 0;
 57 
 58             for (int i = 0; i < len; i++)
 59             {
 60                 if ((res = (a1[i] - a2[i])) != 0) // if not equal, return the result;
 61                     return res;
 62             }
 63 
 64             if (a1.Length != a2.Length)     // like:"abc" "abc1", we think the longger one is bigger than the shorter.
 65             {
 66                 return (a1.Length - a2.Length);
 67             }
 68             else                                        // when compared strings is totally same.
 69             {
 70                 return 0;
 71             }
 72         }
 73 
 74         bool ByteArrayCompare(byte[] a1, byte[] a2)
 75         {
 76             count++;
 77             if (a1.Length != a2.Length)
 78                 return false;
 79 
 80             for (int i = 0; i < a1.Length; i++)
 81                 if (a1[i] != a2[i])
 82                     return false;
 83 
 84             return true;
 85         }
 86 
 87         public IEnumerable<byte[]> GetCompareResult(List<Byte[]> source)
 88         {
 89             List<Byte[]> result = new List<Byte[]>();
 90             Dictionary<int, List<int>> dic = new Dictionary<int, List<int>>() { };
 91             List<int> sameList = new List<int>();
 92             for (int i = 0; i < source.Count; i++)
 93             {
 94                 if (sameList.Contains(i))
 95                     continue;
 96                 for (int j = i + 1; j < source.Count; j++)
 97                 {
 98                     if (sameList.Contains(j))
 99                         continue;
100                     bool isSame = ByteArrayCompare(source[i], source[j]);
101                     if (isSame)
102                     {
103                         if (!dic.ContainsKey(i))
104                         {
105                             dic.Add(i, new List<int>() { j });
106                         }
107                         else
108                         {
109                             dic[i].Add(j);
110                         }
111 
112                         sameList.Add(j);
113                     }
114                 }
115 
116                 //for data which do not have same
117                 if (!dic.ContainsKey(i))
118                 {
119                     result.Add(source[i]);
120                 }
121             }
122 
123             //For data which 
124             foreach (var key in dic.Keys)
125             {
126                 result.Add(source[key]);
127             }
128 
129             return result;
130         }
131     }
132 }
原文地址:https://www.cnblogs.com/xiaomaogong/p/3251683.html