簡單的資料移動演算法(C#)

來源:互聯網
上載者:User
      最近一直都在忙於項目的版本開發,現在終於有時間閑下來寫點東西了!
      資料移動演算法,其實是一個比較簡單的程式,當移動的資料是一個的時候,只是移動的資料和被移動的資料相互交換下排序值Seq就可以了,不過當移動的資料不止一個的時候,有多個資料,而且這些資料可能是連續的,也可能是不連續的,這個時候雖然也不複雜,但是還是有點煩瑣的。前段時間我正好碰到了個這樣的需求,就寫了個比較通用的演算法。

演算法效果示範:

1、在上述的資料中,同時向上移動[Key0]、[Key2,Key3,Key4]、[Key9]這三部分資料

2、在上述的資料中,同時向下移動[Key0]、[Key1,Key2,Key3]、[Key9]這三部分資料

演算法程式碼分析:


      通過上面的類圖可以基本瞭解實現的結構,MoveSeqArithmeticItem是移動順序演算法中的資料元素實體,其中Obj是資料對象,ObjKey是資料對象中的關鍵ID,其實該類就是中間的一個封裝,只是提供了排序欄位Seq和AfterSetSequenceEvent事件(設定序號後觸發的事件)
      MoveSeqArithmetic是演算法的核心,需要特別說明下的是SequenceAreaEntity類,該類是資料集合中連續地區對象的結構,目的是方便對於多個連續或不連續的資料進行操作。MoveSeqArithmetic中移動的具體實現還是看下面的圖更加清晰,

核心代碼:MoveUpByKeysCode
/// <summary>
/// 將資料元素全部向上移動
/// </summary>
/// <param name="objKeys">對象關鍵字</param>
public void MoveUpByKeys(object[] objKeys)
{
    #region 預先處理

    if (objKeys == null)
    {
        return;
    }

    #endregion

    Hashtable hs = this.GetSequenceAreasByKeys(objKeys);
    foreach (DictionaryEntry h in hs)
    {
        SequenceAreaEntity o = (SequenceAreaEntity)h.Value;

        //移動的資料
        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)
            {
                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();
}

詳細的代碼下載下面的代碼項目進行瞭解。

測試DEMO:Code
/// <summary>
/// 向上移動
/// </summary>
private static void StartTestDataStructMoveUp()
{
    Console.WriteLine("-------------待排序的資料--------------");

    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);
    }

    //構造資料結構,並加入資料
    MoveSeqArithmetic<object, string> s = new MoveSeqArithmetic<object, string>(list.ToArray<MoveSeqArithmeticItem<object, string>>());

    //向上移動資料
    s.MoveUpByKeys(new object[] { "key0" });
    s.MoveUpByKeys(new object[] { "key2", "key3", "key4" });
    s.MoveUpByKeys(new object[] { "key9" });

    //列印
    foreach (MoveSeqArithmeticItem<object, string> item in s.ArrList)
    {
        Console.WriteLine(string.Format("Seq: {0} --- Key: {1}", item.Seq, item.ObjKey));
    }
}

執行結果上面已經展示。

      OVER!

下載代碼:TestPerformance.rar

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.