1
我給出了一個包含添加時間,刪除時間,名稱和其他字段的對象列表L。給定一個時間T,我想返回一個只包含L的對象的列表,其中T落在對象的時間加上和時間刪除之間(另外,我想同時搜索特定名稱的所有對象,但它不是如果它使事情複雜化,則是必要的)我將如何使用Boost的ICL來做這件事?使用Boost ICL查找範圍包含點的所有對象
我給出了一個包含添加時間,刪除時間,名稱和其他字段的對象列表L。給定一個時間T,我想返回一個只包含L的對象的列表,其中T落在對象的時間加上和時間刪除之間(另外,我想同時搜索特定名稱的所有對象,但它不是如果它使事情複雜化,則是必要的)我將如何使用Boost的ICL來做這件事?使用Boost ICL查找範圍包含點的所有對象
比方說,你的對象看起來像
struct record {
std::string name;
using ptime = boost::posix_time::ptime;
ptime added, removed;
};
現在,讓我們添加一個便捷的方法,所以我們可以用時間間隔工作更輕鬆:
icl::interval<ptime>::interval_type validity() const {
icl::interval<ptime> x;
return x.right_open(added, removed);
}
一個簡單的測試程序是這樣的:
int main()
{
ptime const start(day_clock::local_day(), { });
std::vector<record> L {
{ "long" , start + hours(77) , start + days(7) } ,
{ "medium" , start + hours(200), start + hours(236) },
{ "short" , start + hours(220), start + hours(226) },
{ "ephemeral", start + hours(100), start + hours(101) },
};
for (ptime T = start; T <= (start + days(10)); T += hours(6))
{
std::cout << "\nT:" << T << "\t";
std::ostream_iterator<record> out(std::cout, ";");
std::copy_if(L.begin(), L.end(), out, [T](record const& r) { return icl::contains(r.validity(), T); });
}
}
它重複了一個時間點T
在10天的範圍s 6小時TEPS,並從L
打印匹配對象,看它Live On Coliru
T:2014-Jun-06 00:00:00
T:2014-Jun-06 06:00:00
T:2014-Jun-06 12:00:00
T:2014-Jun-06 18:00:00
T:2014-Jun-07 00:00:00
T:2014-Jun-07 06:00:00
T:2014-Jun-07 12:00:00
T:2014-Jun-07 18:00:00
T:2014-Jun-08 00:00:00
T:2014-Jun-08 06:00:00
T:2014-Jun-08 12:00:00
T:2014-Jun-08 18:00:00
T:2014-Jun-09 00:00:00
T:2014-Jun-09 06:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-09 12:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-09 18:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-10 00:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-10 06:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-10 12:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-10 18:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-11 00:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-11 06:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-11 12:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-11 18:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-12 00:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-12 06:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-12 12:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-12 18:00:00 {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
T:2014-Jun-13 00:00:00
T:2014-Jun-13 06:00:00
T:2014-Jun-13 12:00:00
T:2014-Jun-13 18:00:00
T:2014-Jun-14 00:00:00
T:2014-Jun-14 06:00:00
T:2014-Jun-14 12:00:00 {medium, 2014-Jun-14 08:00:00, 2014-Jun-15 20:00:00};
T:2014-Jun-14 18:00:00 {medium, 2014-Jun-14 08:00:00, 2014-Jun-15 20:00:00};
T:2014-Jun-15 00:00:00 {medium, 2014-Jun-14 08:00:00, 2014-Jun-15 20:00:00};
T:2014-Jun-15 06:00:00 {medium, 2014-Jun-14 08:00:00, 2014-Jun-15 20:00:00};{short, 2014-Jun-15 04:00:00, 2014-Jun-15 10:00:00};
T:2014-Jun-15 12:00:00 {medium, 2014-Jun-14 08:00:00, 2014-Jun-15 20:00:00};
T:2014-Jun-15 18:00:00 {medium, 2014-Jun-14 08:00:00, 2014-Jun-15 20:00:00};
T:2014-Jun-16 00:00:00
正如你所看到的,"ephemeral"
是如此短暫,它就會被完全使用這種方法錯過!您可能要穿越時間插槽,而不是時間點逐步:
for (auto Slot = icl::interval<ptime>::right_open(start, start + hours(6));
Slot.lower() <= (start + days(10));
Slot = icl::interval<ptime>::right_open(Slot.upper(), Slot.upper() + hours(6)))
{
std::cout << "\nSlot:" << Slot << "\t";
std::ostream_iterator<record> out(std::cout, ";");
std::copy_if(L.begin(), L.end(), out, [Slot](record const& r) { return icl::intersects(Slot, r.validity()); });
}
請參閱Live On Coliru過,並觀察"ephemeral"
如何不再錯過:
...
Slot:[2014-Jun-09 18:00:00,2014-Jun-10 00:00:00) {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
Slot:[2014-Jun-10 00:00:00,2014-Jun-10 06:00:00) {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};{ephemeral, 2014-Jun-10 04:00:00, 2014-Jun-10 05:00:00};
Slot:[2014-Jun-10 06:00:00,2014-Jun-10 12:00:00) {long, 2014-Jun-09 05:00:00, 2014-Jun-13 00:00:00};
...
我是noobie來提升和模板,所以如果可能請給我一個例子,不要試圖立即教我理論(我喜歡瞭解它後,我瞭解的例子),因爲我不明白這是爲什麼我的文檔艾姆在這裏。 – user235236