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