private void TrustSignaturesSort(string propertyName, ListSortDirection direction) { this.TrustSignaturesView.IsLiveSorting = true; this.TrustSignaturesView.LiveSortingProperties.Clear(); this.TrustSignaturesView.SortDescriptions.Clear(); if (propertyName == "Signature") { this.TrustSignaturesView.LiveSortingProperties.Add(propertyName); this.TrustSignaturesView.CustomSort = new CustomSortComparer(direction, (x, y) => { if (x is Signature tx && y is Signature ty) { int c = tx.Name.CompareTo(ty.Name); if (c != 0) { return(c); } c = Unsafe.Compare(tx.Id, ty.Id); if (c != 0) { return(c); } } return(0); });
public int Compare(Key x, Key y) { int c = x.GetHashCode().CompareTo(y.GetHashCode()); if (c != 0) { return(c); } c = x.HashAlgorithm.CompareTo(y.HashAlgorithm); if (c != 0) { return(c); } c = ((x.Hash == null) ? 0 : 1) - ((y.Hash == null) ? 0 : 1); if (c != 0) { return(c); } if (x.Hash != null && y.Hash != null) { c = Unsafe.Compare(x.Hash, y.Hash); if (c != 0) { return(c); } } return(0); }
private void Sort(string propertyName, ListSortDirection direction) { this.ContentsView.IsLiveSorting = true; this.ContentsView.LiveSortingProperties.Clear(); this.ContentsView.SortDescriptions.Clear(); if (propertyName == "Value") { this.ContentsView.LiveSortingProperties.Add(propertyName); this.ContentsView.CustomSort = new CustomSortComparer(direction, (x, y) => { if (x is SearchCondition <Signature> tx && y is SearchCondition <Signature> ty) { int c = tx.Value.Name.CompareTo(ty.Value.Name); if (c != 0) { return(c); } c = Unsafe.Compare(tx.Value.Id, ty.Value.Id); if (c != 0) { return(c); } } return(0); }); } else { this.ContentsView.LiveSortingProperties.Add(propertyName); this.ContentsView.SortDescriptions.Add(new SortDescription(propertyName, direction)); } }
public void SearchTest() { var tempList = new List <NetworkManager.Node <int> >(); for (int i = 0; i < 256; i++) { var node = new NetworkManager.Node <int>(_random.GetBytes(32), i); tempList.Add(node); } var sortedList = NetworkManager.RouteTableMethods.Search(null, new byte[32], tempList.Randomize(), 32).ToList(); tempList.Sort((x, y) => Unsafe.Compare(x.Id, y.Id)); Assert.True(CollectionUtils.Equals(sortedList.Select(n => n.Value), tempList.Select(n => n.Value).Take(32))); }
public static IEnumerable <Node <T> > Search <T>(byte[] baseId, byte[] targetId, IEnumerable <Node <T> > nodeList, int count) { if (baseId == null) { throw new ArgumentNullException(nameof(baseId)); } if (targetId == null) { throw new ArgumentNullException(nameof(targetId)); } if (nodeList == null) { throw new ArgumentNullException(nameof(nodeList)); } if (count == 0) { return(Array.Empty <Node <T> >()); } var targetList = new List <SortInfo <T> >(); if (baseId != null) { var xor = new byte[targetId.Length]; Unsafe.Xor(targetId, baseId, xor); targetList.Add(new SortInfo <T>(null, xor)); } foreach (var node in nodeList) { var xor = new byte[targetId.Length]; Unsafe.Xor(targetId, node.Id, xor); targetList.Add(new SortInfo <T>(node, xor)); } for (int i = 1; i < targetList.Count; i++) { var temp = targetList[i]; int left = 0; int right = Math.Min(i, count); while (left < right) { int middle = (left + right) / 2; if (Unsafe.Compare(targetList[middle].Xor, temp.Xor) <= 0) { left = middle + 1; } else { right = middle; } } for (int j = Math.Min(i, count); left < j; --j) { targetList[j] = targetList[j - 1]; } targetList[left] = temp; } return(targetList.Take(count).TakeWhile(n => n.Node.HasValue).Select(n => n.Node.Value).ToList()); }
private void Sort(string propertyName, ListSortDirection direction) { this.ContentsView.IsLiveSorting = true; this.ContentsView.LiveSortingProperties.Clear(); this.ContentsView.SortDescriptions.Clear(); if (propertyName == "Id") { this.ContentsView.LiveSortingProperties.Add(propertyName); this.ContentsView.CustomSort = new CustomSortComparer(direction, (x, y) => Unsafe.Compare(((Tag)x).Id, ((Tag)y).Id)); } else { this.ContentsView.LiveSortingProperties.Add(propertyName); this.ContentsView.SortDescriptions.Add(new SortDescription(propertyName, direction)); } }
public void Compare() { Random random = new Random(); Stopwatch totalStopwatch = new Stopwatch(); totalStopwatch.Start(); Stopwatch sw1 = new Stopwatch(); Stopwatch sw2 = new Stopwatch(); Stopwatch sw3 = new Stopwatch(); Stopwatch sw4 = new Stopwatch(); var flags = new int[] { 0, 1, 2, 3 }; for (int i = 0; i < 1024 * 256 * 32; i++) { byte[] x; byte[] y; if (random.Next(0, 2) == 0) { var length = random.Next(0, 1024); x = new byte[length]; y = new byte[length]; } else { x = new byte[random.Next(0, 1024)]; y = new byte[random.Next(0, 1024)]; } if (random.Next(0, 2) == 0) { random.NextBytes(x); random.NextBytes(y); } int result1 = 0; int result2 = 0; int result3 = 0; int result4 = 0; var maxLength = Math.Min(x.Length, y.Length); random.Shuffle(flags); foreach (var index in flags) { if (index == 0) { sw1.Start(); result1 = Unsafe.Compare(x, y); sw1.Stop(); } else if (index == 1) { sw2.Start(); result2 = Unsafe.Compare2(x, y); sw2.Stop(); } else if (index == 2) { sw3.Start(); result3 = Unsafe.Compare(x, 0, y, 0, maxLength); sw3.Stop(); } else if (index == 3) { sw4.Start(); result4 = Unsafe.Compare2(x, 0, y, 0, maxLength); sw4.Stop(); } } Assert.IsTrue(result1 == result2); Assert.IsTrue(result3 == result4); if (totalStopwatch.ElapsedMilliseconds > 1000 * 60) { break; } } StringBuilder sb = new StringBuilder(); sb.AppendLine("Native Compare: " + sw1.Elapsed.ToString()); sb.AppendLine("Native Compare2: " + sw2.Elapsed.ToString()); sb.AppendLine("Native Compare: " + sw3.Elapsed.ToString()); sb.AppendLine("Native Compare2: " + sw4.Elapsed.ToString()); Console.WriteLine(sb.ToString()); }
public int Compare(byte[] x, byte[] y) { return(Unsafe.Compare(x, y)); }
public static IEnumerable <T> Search(byte[] targetId, IEnumerable <T> nodeList, int count) { if (targetId == null) { throw new ArgumentNullException("targetId"); } if (nodeList == null) { throw new ArgumentNullException("nodeList"); } if (count == 0) { yield break; } int InfoIndex = 0; var infoManager = _threadLocalInfoManager.Value; infoManager.SetBufferSize(targetId.Length); int linkIndex = 0; Info firstItem = null; Info lastItem = null; // 挿入ソート(countが小さい場合、countで比較範囲を狭めた挿入ソートが高速。) foreach (var node in nodeList) { var targetItem = infoManager.GetInfo(InfoIndex++); Unsafe.Xor(targetId, node.Id, targetItem.Xor); targetItem.Node = node; var currentItem = lastItem; while (currentItem != null) { if (Unsafe.Compare(currentItem.Xor, targetItem.Xor) <= 0) { break; } currentItem = currentItem.Previous; } // 初期化。 if (firstItem == null && lastItem == null) { firstItem = targetItem; lastItem = targetItem; } // 最前列に挿入。 else if (currentItem == null) { firstItem.Previous = targetItem; targetItem.Next = firstItem; firstItem = targetItem; } // 最後尾に挿入。 else if (lastItem == currentItem) { currentItem.Next = targetItem; targetItem.Previous = currentItem; lastItem = targetItem; } // 中間に挿入。 else { var swapItem = currentItem.Next; currentItem.Next = targetItem; targetItem.Previous = currentItem; targetItem.Next = swapItem; swapItem.Previous = targetItem; } linkIndex++; // count数を超えている場合はlastItemを削除する。 if (linkIndex > count) { var previousItem = lastItem.Previous; previousItem.Next = null; lastItem = previousItem; linkIndex--; } } for (var currentItem = firstItem; currentItem != null; currentItem = currentItem.Next) { yield return(currentItem.Node); } }