Esbasic reusable. Net class library (20) -- Top N ranking container topnorderedcontainer

Source: Internet
Author: User

1.Origin:

Assume that our membership management system has a ranking function, and all Members in the system need to be assigned at intervals (assuming that the number of members has100Tens of thousands of points, and then sort the first100Some awards are given by the user.

This is a typical topn algorithm -sorts a large number of objects, then you only need to retrieve the most top front n name ( n is much smaller than the total number of objects.

To solve this problem, we should note that if we sort all objects in full order every time, it will be very inefficient and cost-effective. Because we only needNName, not the sequence of all objects.

I designedEsbasic. objectmanagement. topnorderedcontainerTo solve the ranking algorithm,TopnorderedcontainerOnly the resources are spent in the places that really need to be calculated. In addition,TopnorderedcontainerYou can add newly generated objects to the ranking list during running.

 

2.Applicable scenarios:

TopnorderedcontainerUsed to perform operations on a large number of objectsTopnSort. Its application scenarios have the following features:

(1)The number of objects to be sorted is very large (for example, millions or even tens of millions ).

(2)The sorting results that are valuable to the system are only the firstNName.

(3)NIt is much smaller than the total number of objects.

 

3Design ideas and implementation

topnorderedcontainer the ranking algorithm is like this, using a length of n array, top n objects, the more top index is smaller. In this way, when an object is added:

(1)First, determine whether the last name of the current ranking is more than the newly added object.TopIf yes, discard it.

(2)Second, check whether the newly added object is more than the first in the current ranking.TopIf yes, the new object should be placed inIndexIs0.

(3)Otherwise, the binary search algorithm is used to locate the appropriate position for the newly added object, and adjust the position of the object behind the insert position in the ranking.

Of course, in the specific implementation source code, we can see that some boundary conditions are not described here.

TopnorderedcontainerThe class diagram is as follows:

we can see topnorderedcontainer there is a generic parameter tobj , which is the type of the object to be sorted. tobj generic constraints indicate tobj required iordered interface. iordered Interface Definition:

 

///   <Summary>
/// The interface required by iordered to participate in ranking.
///   </Summary>
///   <Typeparam name = "torderedobj"> Type of the object involved in ranking </Typeparam>
Public   Interface   Iordered < Torderedobj >
{
Bool Istopthan ( Torderedobj Other );
}

 

Note the following two points for this interface:

First, why is the name of the unique method of this interface not similar? Isgreaterthan , Issmallerthan Wait, Istopthan ? Because different applications have different requirements, some may need to be selected before N Is the largest, some are to be selected before N Minimum, and some may even choose N And so on. While Istopthan All of these situations can be covered. Top Of N Yes.

Second,IorderedThe reason why the interface uses generic parametersTorderedobjTo avoid the implementation of a derived classIstopthanParametersOtherType.

now we are going back to topnorderedcontainer :

(1) ranking containers can be used in multi-threaded environments. topnorderedcontainer use smartrwlocker to Add Method for synchronization, Select a read/write lock instead of a simple lock , this is because the ranking container is able to cope with multiple reads / larger concurrency is supported when less data is written.

(2)The ranking is generated using the insert sorting policy, and the specific sorting algorithm is binary search sorting.AdjustThe implementation of the method is the embodiment of the binary search algorithm.

(3)GettopnReturns a copy of the current ranking. The reason why a copy is returned is that any external operation on the returned array will not affectTopnorderedcontainer.

(4)Why notTopnIs sorting directly implemented as a static method? IfIf it is implemented in a static way, we cannot continue dynamic.AddA new object enters the ranking table. To achieve this goal, only a newList, Call againStatic gettopnThis wastes the previous computing results.

 

4.Precautions for use

If the number of objects to be sorted matchesTopnOfNThe value difference is not big, so useTopnorderedcontainerThis is not necessarily the best choice. In this case, we can use some efficient full sorting algorithms to sort all objects and then extract the previousNName, which may be faster.

Of course, we can also use the maximum and minimum heap algorithm to sort the Top N, which is completely feasible.

 

5.Extension

TopnRanking containerTopnorderedcontainerThere are currently no extensions.

 

Note: The esbasic source code can be foundHttp://esbasic.codeplex.com/Download.
Esbasic discussion QQ group: 37677395
Esbasic open source Preface

 

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.