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

时间:2014-04-01 15:29:59

标签: .net vb.net recursion generic-list

我会尽量清楚,因为这是我的第一个问题。我有一个我正在处理约会的项目。在过滤掉人们要求预约的可用时段时,我遇到了麻烦。我按如下方式定义了一段时间的课程:

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

现在,如果我有一个TimePeriod,它表示可以作为StartTime = 9:00和EndTime = 5:00进行约会的时间段,并且我有各种当前预定约会的通用列表(例如9:30) -10:00,10:00-11:00),我将如何编写一个递归函数来返回仍然可用时间段的通用列表(如果它不清除,则从可用时间段中删除块) ?

1 个答案:

答案 0 :(得分:1)

在TimePeriod类中设置一个名为IsBusy的布尔属性。然后,您可以编写一个lambda来查询列表,其中IsBusy = False。

另外作为旁注,如果有一个预先建立的时间段列表,比如说15分钟的增量,那就更有意义了,如果有人想要阻止,请说上午9:00 - 9:45你只需循环遍历列表就可以找到那些(也可以用lambda完成)来找到三个TimePeriod并将它们的IsBusy更新为True。如果每个开始和结束时间都是不同的时间增量,则会遇到测试列表中每个项目的问题,以查看是否存在与计划事件冲突的问题。