Simple data movement algorithm (C #)

Source: Internet
Author: User
I have been busy with project version Development recently. Now I am free to write something!
Data Movement Algorithm Is actually a relatively simple Program When the migrated data is one, only the migrated data and the migrated data exchange the sorted values seq. However, when there are more than one migrated data, there are multiple pieces of data, and the data may be continuous or non-consecutive. At this time, although it is not complicated, it is still a bit cumbersome. Some time ago I met such a requirement and wrote a general algorithm.

Algorithm effect demonstration:

1. In the preceding data, move [key0], [key2, key3, key4], and [key9] up simultaneously.

2. Move [key0], [key1, key2, key3], and [key9] in the preceding data at the same time.

AlgorithmCodeAnalysis:


The class diagram above shows the structure of the Implementation. moveseqarithmeticitem is the data element entity in the mobile Sequence Algorithm, where obj is the data object and objkey is the key ID in the data object, in fact, this class is a middle package. It only provides the sorting field seq and aftersetsequenceevent events (events triggered after the sequence number is set)
Moveseqarithmetic is the core of the algorithm. In particular, it must be noted that the sequenceareaentity class is the structure of objects in consecutive regions in a dataset, so as to facilitate operations on multiple consecutive or discontinuous data. The specific implementation of moveseqarithmetic is clearer,

Core code: moveupbykeys Code
///   <Summary>
/// Move all data elements up
///   </Summary>
///   <Param name = "objkeys"> Object keywords </Param>
Public   Void Moveupbykeys ( Object [] Objkeys)
{
# Region Preprocessing

If(Objkeys= Null)
{
Return;
}

# Endregion

hashtable hs = This . getsequenceareasbykeys (objkeys);
foreach (dictionaryentry H in HS)
{< br> sequenceareaentity o = (sequenceareaentity) H. value;

//Mobile Data
Moveseqarithmeticitem<T, K>Moveitem= This. Getpreviewitembyarea (O );

If (Moveitem ! =   Null )
{
Int Moveindex = Moveitem. seq;
Int Findex = O. fitem. seq;
Int Lindex = O. litem. seq;

foreach (moveseqarithmeticitem T, k > entry in O. arrareas)
{< br> entry. SEQ = entry. SEQ - findex + moveindex;
}

moveitem. SEQ = lindex;

O. fitem. previewitem=Moveitem. previewitem;
Moveitem. previewitem=O. litem;
Moveitem. backitem=O. litem. backitem;
O. litem. backitem=Moveitem;
}
}

This. Sort ();
}

Download the following code items for details.

Test Demo: Code
///   <Summary>
/// Move up
///   </Summary>
Private   Static   Void Starttestdatastructmoveup ()
{
Console. writeline ( " ------------- Data to be sorted -------------- " );

List < Moveseqarithmeticitem < Object , String > List =   New List < Moveseqarithmeticitem < Object , String > ();
For ( Int I =   0 ; I <   10 ; I ++ )
{
Moveseqarithmeticitem < Object , String > O =   New Moveseqarithmeticitem < Object , String > (I, String . Format ( " Key {0} " , I ), Null );
List. Add (O );
}

// Construct a Data Structure and add data
Moveseqarithmetic < Object , String > S =   New Moveseqarithmetic < Object , String > (List. toarray < Moveseqarithmeticitem < Object , String > ());

// Move data up
S. moveupbykeys ( New   Object [] { " Key0 " });
S. moveupbykeys ( New   Object [] { " Key2 " , " Key3 " , " Key4 " });
S. moveupbykeys ( New   Object [] { " Key9 " });

// Print
Foreach (Moveseqarithmeticitem < Object , String > Item In S. arrlist)
{
Console. writeline ( String . Format ( " SEQ: {0} --- key: {1} " , Item. seq, item. objkey ));
}
}

The execution result is shown above.

Over!

Download Code: testperformance.rar

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: [email protected] 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.