Powercollection Study: removeall Algorithm

Source: Internet
Author: User

Previous Article: many problems have been found in the 1st bullet

The removeall () method of biglist is targeted.AlgorithmIt is consistent with the removeall Implementation of list.
The algorithm is very simple and highly efficient, but it is not always known to new users. I want to go over and share it here. Just repeat it.
We know that objects cannot be directly deleted during list traversal. in this case, the index of the item must be recorded after being retrieved and put into a temporary removelist. after traversing ,"Reverse"Traverse removelist and delete it one by one.
Today, we compared this method with list removeall and tested it. We can see that the result is like hitting the wall. In the case of a large number of elements in the Set, there is a world of difference.

 
When the Count value is 10 times larger, the result is not displayed in 1 or 2 minutes, mainly because the removelist is too large and the capacity is 500000. The system protested. Wow, how many garbage objects will be generated in the middle.
Fortunately, when Count = 1000, the performance is similar, but it is not cost-effective to increase the garbage collection burden.

OK. Let's see the removeall algorithm:

The algorithm is very simple. traverse the items that do not need to be removed one by one and put them forward. the initial low value is-1, and 1 is added each time an item that does not need to be removed is found. After traversal, all items that do not need to be removed are placed before low, therefore, the items after the removal of low will end the process. no temporary containers such as removelist are required throughout the process, and no garbage objects are generated.

The previous concise graph: (remove all prime numbers)

Although biglist is consistent with the Remove Algorithm of list, let's look at the efficiency:

The difference is not very big. = biglist returns a set of all items to be removed. list only returns the number of removed items, which is unfair. so let biglist also return only the number, what is the result of another comparison:

There is still a small gap, because the underlying objects used by the two are different. One is Tree and the other is array. with the inherent advantage of array for memory operations, list still wins in the remove operation.

Below is the reflected list <t>Code:

Public   Int Removeall (Predicate < T > Match)
{
If (Match =   Null )
{
Throwhelper. throwargumentnullexception (exceptionargument. Match );
}
Int Index =   0 ;
While (Index <   This . _ SIZE) &&   ! Match ( This . _ Items [Index])
{
Index++;
}
If (Index > =   This . _ SIZE)
{
Return 0;
}
Int Num2 = Index +   1 ;
While (Num2 <   This . _ SIZE)
{
While (Num2 <   This . _ SIZE) && Match ( This . _ Items [num2])
{
Num2++;
}
If (Num2 <   This . _ SIZE)
{
This. _ Items [Index++]= This. _ Items [num2++];
}
}
Array. Clear ( This . _ Items, index, This . _ Size - Index );
Int Num3 =   This . _ Size - Index;
This . _ Size = Index;
This . _ Version ++ ;
Return Num3;
}

 

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.