Build a reversible sort of generic dictionary class (2)--Sort direction

Source: Internet
Author: User
Tags bitwise

2. Sort direction

You want the elements in the Reversiblesortedlist class to be stored in the order of the TKey (keys), and it can be sorted from small to large, or from large to smaller. The best way, of course, is to find the right place to insert when adding elements, and the elements are sorted in order. Finding the right insertion point in an ordered array is not difficult, but FCL has done it for us and is using the fastest binary lookup method (called "Binary Search" in MSDN). That's great! It is: Static method Array.BinarySearch. Let's look at the introduction to it in MSDN.

Array.BinarySearch has 8 overloaded versions, and the last one is what we need:

public static int BinarySearch<T> (
  T[] array, //要搜索的从零开始的一维排序 Array
  int index, //要搜索的范围的起始索引。
  int length, //要搜索的范围的长度。
  T value, //要搜索的对象。
  IComparer<T> comparer //比较元素时要使用的 IComparer 实现。
)

where T represents the type of the array element. An introduction to the return value is that if value is found, the index of the specified value in the specified array. If value is not found and the value is less than one or more elements in the array, a negative number that is a bitwise complement of the index of the first element of value. If value is not found and the value is greater than any element in the array, it is a negative number, which is a bitwise complement of the index of the last element plus 1.

Our reversiblesortedlist cannot insert duplicate key values. When the return value is greater than or equal to 0 o'clock, it indicates that it cannot be inserted. When the return value is less than zero, the duplicate key is not found, and the return value also contains information about the insertion position. How thoughtful of you to think!

What is the complement? is to turn the binary number 0 into 1, and 1 to 0. For an int, because it is a signed integer, the complement changes the positive number to a negative number and turns the negative number into a positive number. The original number is obtained by a two-time complement operation on a number. Haha, if the back value is less than 0, it can get the insertion position information. It's not worth the effort!

The problem now is that you need a class that implements the Icomparer<t> interface. You can declare a nested class in reversiblesortedlist to resolve this problem. Of course, in the implementation of the Icomparer<t> interface compare method in the inside do some hands and feet can achieve forward and reverse sort. At this point, you need a positive and reverse sort of thing, FCL is ready-made, it is the System.ComponentModel namespace of the ListSortDirection enumeration. It has two values: ascending represents ascending, descending represents descending order. The following code adds an internal class:sortdirectioncomparer<t> that implements the Icomparer<t> interface on the basis of version 1.0. The code can be copied directly, running it purely to check for errors, with no visible effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.