合并两个列表以覆盖整个工作时间 [英] Merge Two List in a way to cover full hours

查看:62
本文介绍了合并两个列表以覆盖整个工作时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表Schedule(有总时数)和Registered(有时间间隔).我想创建一个列表,该列表的注册时间已包含在计划列表"的总小时数中.

请参见上图.如果未在剩余小时内找到任何注册,则结果列表将涵盖注册时间和ENd.它将根据剩余的小时数来创建更多时间.

解决方案

使用以下类表示您的源数据:

public class Schedule {
    public int From;
    public int To;
    public int TotalHours => To - From;
    public string Type;

    public Schedule(int from, int to, string type) {
        From = from;
        To = to;
        Type = type;
    }
}

public class Register {
    public int From;
    public int To;

    public Register(int from, int to) {
        From = from;
        To = to;
    }
}

这是一个for循环实现,它在注册的时间间隔内分散每个Schedule成员,然后输出其余的成员:

var ans = new List<Schedule>();

int currentRegistered = 0;
var rp = registered[currentRegistered];
var currentFrom = rp.From;

for (int curScheduled = 0; curScheduled < scheduled.Count; ++curScheduled) {
    var s = scheduled[curScheduled];
    for (var hours = s.TotalHours; hours > 0;) {
        if (currentFrom >= rp.To)
            rp = (++currentRegistered < registered.Count)
                    ? registered[currentRegistered]
                    : new Register(currentFrom, int.MaxValue);
        if (rp.From > currentFrom)
            currentFrom = rp.From;

        var newTo = (rp.To - currentFrom > hours) ? currentFrom + hours : rp.To;
        ans.Add(new Schedule(currentFrom, newTo, s.Type));
        hours -= newTo - currentFrom;
        currentFrom = newTo;
    }
}

I have two lists Schedule(Having Total Hours) And Registered (Having Time Intervals). I want to create a list that is having registered time covered with Schedule list total hours finished.

See the Upper image. The resultant list is covering Registered Times and ENd if it doesn't find any registration for left hours. It will just create further times according to hours left with their types.

解决方案

Using the following classes to represent your source data:

public class Schedule {
    public int From;
    public int To;
    public int TotalHours => To - From;
    public string Type;

    public Schedule(int from, int to, string type) {
        From = from;
        To = to;
        Type = type;
    }
}

public class Register {
    public int From;
    public int To;

    public Register(int from, int to) {
        From = from;
        To = to;
    }
}

Here is a for loop implementation that spreads each Schedule member across the Registered time intervals and then outputs the rest:

var ans = new List<Schedule>();

int currentRegistered = 0;
var rp = registered[currentRegistered];
var currentFrom = rp.From;

for (int curScheduled = 0; curScheduled < scheduled.Count; ++curScheduled) {
    var s = scheduled[curScheduled];
    for (var hours = s.TotalHours; hours > 0;) {
        if (currentFrom >= rp.To)
            rp = (++currentRegistered < registered.Count)
                    ? registered[currentRegistered]
                    : new Register(currentFrom, int.MaxValue);
        if (rp.From > currentFrom)
            currentFrom = rp.From;

        var newTo = (rp.To - currentFrom > hours) ? currentFrom + hours : rp.To;
        ans.Add(new Schedule(currentFrom, newTo, s.Type));
        hours -= newTo - currentFrom;
        currentFrom = newTo;
    }
}

这篇关于合并两个列表以覆盖整个工作时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆