简体   繁体   English

我需要一个递归函数从其他时间段中删除时间段

[英]I need a recursive function to remove periods of time from other periods of time

I will try to be as clear as possible as this is my first question. 我将尝试尽可能明确,因为这是我的第一个问题。 I have a project I am working on that handles appointments. 我有一个正在处理的项目可以处理约会。 I have hit a snag when it comes to filtering out available time slots for people to request an appointment. 在筛选出可供人们请求约会的可用时隙时,我遇到了麻烦。 I have defined classes for a period of time as follows: 我定义了一段时间的类,如下所示:

Public Class TimePeriod
    #Region "Private Variables"
        Private _startTime As TimeSpan
        Private _endTime As TimeSpan
    #End Region

    #Region "Public Properties"
        Public Property StartTime As TimeSpan
            Get
                Return Me._startTime
            End Get
            Set(value As TimeSpan)
                Me._startTime = value
            End Set
        End Property
        Public Property EndTime As TimeSpan
            Get
                Return Me._endTime
            End Get
            Set(value As TimeSpan)
                Me._endTime = value
            End Set
        End Property
    #End Region

    #Region "Constructors"
        Public Sub New()
            Me.New(New TimeSpan(0, 0, 0), New TimeSpan(0, 0, 0))
        End Sub
        Public Sub New(StartTime As TimeSpan, EndTime As TimeSpan)
            Me._startTime = StartTime
            Me._endTime = EndTime
        End Sub
    #End Region
End Class

Public Class TimePeriodFunctions
    Public Shared Function RemoveTimePeriod(MasterTimePeriod As TimePeriod, TimePeriodToRemove As TimePeriod) As System.Collections.Generic.List(Of TimePeriod)
        'SEE FIRST IF THERE IS ANY OVERLAP
        If (TimePeriodToRemove.StartTime >= MasterTimePeriod.StartTime And TimePeriodToRemove.StartTime <= MasterTimePeriod.EndTime) Or _
           (TimePeriodToRemove.EndTime >= MasterTimePeriod.StartTime And TimePeriodToRemove.EndTime <= MasterTimePeriod.EndTime) Then
            Dim TimePeriods As New System.Collections.Generic.List(Of TimePeriod)

            If TimePeriodToRemove.StartTime >= MasterTimePeriod.StartTime And TimePeriodToRemove.StartTime <= MasterTimePeriod.EndTime And _
               MasterTimePeriod.StartTime <> TimePeriodToRemove.StartTime Then
                'TIME TO RETURN IS FROM MASTERTIMEPERIOD.STARTTIME TO TIMEPERIODTOREMOVE.STARTTIME
                TimePeriods.Add(New TimePeriod(MasterTimePeriod.StartTime, TimePeriodToRemove.StartTime))
            End If
            If TimePeriodToRemove.EndTime >= MasterTimePeriod.StartTime And TimePeriodToRemove.EndTime <= MasterTimePeriod.EndTime And _
               TimePeriodToRemove.EndTime <> MasterTimePeriod.EndTime Then
                'TIME TO RETURN IS FROM TIMEPERIODTOREMOVE.ENDTIME TO MASTERTIMEPERIOD.ENDTIME
                TimePeriods.Add(New TimePeriod(TimePeriodToRemove.EndTime, MasterTimePeriod.EndTime))
            End If

            Return TimePeriods
        Else
            'IF THERE IS NO OVERLAP, THEN RETURN AN EMPTY COLLECTION
            Return New System.Collections.Generic.List(Of TimePeriod)
        End If
    End Function
End Class

Now, if I have a TimePeriod that represents the period of time available to make appointments as StartTime = 9:00 and EndTime = 5:00, and I have a generic list of various currently scheduled appointments (eg. 9:30-10:00, 10:00-11:00), how would I write a recursive function to return a generic list of the still available time periods (like removing chunks from the available time period if it wasn't clear)? 现在,如果我有一个TimePeriod,它表示可用于进行约会的时间段,例如StartTime = 9:00和EndTime = 5:00,并且我有一个各种当前预定约会的通用列表(例如9:30-10: 00,10:00-11:00),我将如何编写一个递归函数以返回尚可用时间段的通用列表(例如,如果不清楚,则从可用时间段中删除块)?

Set a boolean property in your TimePeriod class maybe named IsBusy. 在您的TimePeriod类中设置一个布尔属性(可能名为IsBusy)。 You can then write a lambda to query your list where IsBusy = False. 然后,您可以编写一个lambda来查询IsBusy = False的列表。

Also as a side note, wouldn't it make more sense to have a pre-built list of time blocks, say 15 minute increments, then if someone wants to block out say 9:00AM - 9:45AM you would only need to loop through the list to find those (which can be done with a lambda as well) to find three TimePeriods and update their IsBusy to True. 另外请注意,预先建立时间段列表(例如以15分钟为增量)更有意义,那么如果有人要说出9:00 AM-9:45 AM,您只需要循环在列表中找到这些对象(也可以使用lambda来完成)以找到三个TimePeriods并将其IsBusy更新为True。 If each begin and end times are different increments of time you run into the issue of testing every item in the list to see if something conflicts with the scheduled event. 如果每个开始时间和结束时间都是不同的时间增量,则会遇到测试列表中每个项目的问题,以查看是否与计划的事件发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM