【发布时间】:2017-09-17 13:33:54
【问题描述】:
我目前正在开发一个带有多个(重复发生的)闹钟的闹钟。
我正在使用安装了 Microsoft IoT 和 UWP (C#) 的树莓派进行布局和底层算法。
我遇到的问题是检索下一个闹钟时间。
伪代码:
Select nextAlarm()
For all alarms a
if (((a.time >= now.time AND a.repeatDay == now.DayOfWeek)
OR a.repeatDay > now.DayOfWeek) AND a.dateTime < currentAlarm.dateTime)
currentAlarm = a;
但是,每个闹钟都需要 O(n) 时间,并且函数 a.repeatDay > now.DayOfWeek 不是一个微不足道的函数(如果当天是星期三而下一个闹钟是星期一,则该函数不会' t 工作)。
我要问的是如何以上述函数工作的方式存储警报(并且最好比 O(n) 更快),或者如何存储所述问题已解决的重复天数。
目前使用 SQLite.net-pcl 包
Alarm 和 RepeatDay 类:
public class Alarm
{
[PrimaryKey, AutoIncrement]
public long Id { get; set; }
[NotNull]
public string Name { get; set; }
[NotNull]
public DateTime Time { get; set; }
[NotNull]
public int Repeat { get; set; }
public Alarm(string name, DateTime time, RepeatWeek repeat)
{
this.Name = name;
this.Time = time;
this.Repeat = repeat;
}
}
public class RepeatWeek
{
int repeat = 0;
public static implicit operator int(RepeatWeek w)
{
return w.repeat;
}
public void setDay(DayOfWeek w)
{
repeat |= 1 << (int)w;
}
public void removeDay(DayOfWeek w)
{
repeat &= ~(1 << (int)w);
}
public DayOfWeek getNext(DayOfWeek d, bool inclToday = false)
{
throw new NotImplementedException();
return DayOfWeek.Monday; //Needs work
}
}
【问题讨论】:
-
您只想要下一个预定的闹钟吗?
-
是的,我目前被困在重复的日子里
-
为什么不只检查报价并抓住价值最低的报价?
-
我知道这种方法适用于单一发生的警报,但它如何处理重复警报
-
如果您发布警报类/对象的外观。我可以给你更多的建议。
标签: c# datetime time uwp data-management