根据时间将一个列表附加到另一个列表的代码优化

问题描述 投票:0回答:1

我有一个包含日期范围的列表,我们将其称为“shiftRange”,如下所示:

start  to  end
9:00am to 10:00 am
10:00am to 11:am
11:00am to 12:00 pm

我有另一个列表,其中包含员工登录和注销,我们将其称为 applyRecords,它可能会重叠:

    signIn to signOut
E1: 9:10am to 9:30am
E2: 9:20am to 10:10am
E3: 10:10am to 11:00am

现在我使用 applyRecords 来分割 shiftRange 以获得如下块:

start  to   end
9:00am to 9:10am
9:10am to 9:20am E1
9:20am to 9:30am E1, E2
9:30am to 10:00am E2
10:00am to 10:10am E2
10:10am to 11:00am E3
11:00am to 12:pm 

这是我实现此目的的代码,是我能得到的最优化的代码,但问题是我的 ShiftRange 中有数百万行,并且当前的执行时间是不可接受的,有没有办法进一步优化它:

    protected override IEnumerable<TSplit> Join(Query query, IEnumerable<TSplit> shiftRange, IEnumerable<TApply> applyRecords)
    {
        var state = this.GetState(query);
        var applyRecordsCached = applyRecords.ToArray();

        foreach (var split in splitRecords)
        {
            var activeList = new LinkedList<TSplit>();
            activeList.AddLast(split);

            foreach (var apply in applyRecordsCached)
            {
                foreach (var current in activeList.ToList())
                {
                    // Apply split logic and replace nodes in-place
                    activeList.Remove(current);
                    var parts = current.Split(apply); //the Split function returns before, during and after for the event
                    if (parts.Item1 != null) activeList.AddLast(parts.Item1);
                    if (parts.Item2 != null) activeList.AddLast(this.ApplyRecord(apply, parts.Item2, state)); //the ApplyRecord function attaches the apply record to shift range
                    if (parts.Item3 != null) activeList.AddLast(parts.Item3);
                }
            }

            // Emit all remaining parts in order
            foreach (var remainingSplit in activeList)
            {
                yield return remainingSplit;
            }
        }
    }
c# list algorithm data-structures split
1个回答
0
投票

创建一个新的结构,称为事件。 他们有时间和描述。

从每个事件中创建一个事件

  • 换档开始
  • 班次结束
  • 签到
  • 结账

按时间顺序对事件进行排序

从每对排序事件创建块

© www.soinside.com 2019 - 2024. All rights reserved.