I.m使用c#语言。我有一本字典。关键是人们的电子邮件串。每个键的值是一个对象列表。这些对象有2个dateTime变量start和End。
我要做的是找到我所有人都有空的时间。除了找到其中一些可用的时间外。
我设法解决了当所有用户都可用时的第一部分。但是,由于我使用的是线性搜索,因此代码非常慢。
所以我的问题是:
这是负责交叉的方法
private static FreeTime Intersection(FreeTime first, FreeTime second)
{
Console.WriteLine("inside intersection " + first.Start + " "+ first.End + " sescond " + second.Start + " " + second.End);
int result1 = DateTime.Compare(first.Start, second.End);
int result2 = DateTime.Compare(first.End, second.Start);
int result3 = DateTime.Compare(first.Start, second.Start);
int result4 = DateTime.Compare(first.End, second.End);
Boolean equal = (result3 == 0 && result4 == 0);
if (!(result1 >= 0) && !(result2 <= 0) && !equal)
{
Console.WriteLine(new DateTime(Math.Max(first.Start.Ticks, second.Start.Ticks)).TimeOfDay + "min max" + new DateTime(Math.Min(first.End.Ticks, second.End.Ticks)).TimeOfDay);
return new FreeTime(new DateTime(Math.Max(first.Start.Ticks, second.Start.Ticks)), new DateTime(Math.Min(first.End.Ticks, second.End.Ticks)));
}
else if (result3 == 0 && result4 == 0)
return (new FreeTime(first.Start, first.End));
else return null;
这部分用于迭代列表
foreach (var t in allFree)
{
if (allFree != null)///&& list.Count > 1)
{
Console.WriteLine(t.Start + " " + t.End + " in all freeeee");
foreach (var s in allFree)
{
if (!((DateTime.Compare(t.Start.Date, s.Start.Date) == 0) && (DateTime.Compare(t.Start, s.Start) == 0) && (DateTime.Compare(t.End, s.End) == 0)) && (t.Start.Date == s.Start.Date))
{
Console.WriteLine("callling inteersection 2 ");
intersection = Intersection(t, s);
}
// else if ((list.Count == list2.Count) && ((DateTime.Compare(t.Start.Date, s.Start.Date) == 0) && (DateTime.Compare(t.Start, s.Start) == 0) && (DateTime.Compare(t.End, s.End) == 0)) && (t.Start.Date == s.Start.Date))
// intersection = new FreeTime(t.Start, t.End);
if (intersection != null)
{
finalFree.Add(intersection);
Console.WriteLine(intersection.Start + " " + intersection.End + " intersection result");
intersecFound = true;
}
}//end foreach
if (!intersecFound)
{
// allFree.Add(t);
intersecFound = false;
}