blob: 8d96f77f6a24c398ac8d5a781877ac8b5b08c41b [file] [log] [blame]
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07004 *
5 * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
6 * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
7 *
8 * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070019 * @author Zhiyi Zhang <zhiyi@cs.ucla.edu>
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070020 */
21
22#include "interval.hpp"
23
24namespace ndn {
25namespace gep {
26
27static const TimeStamp DEFAULT_TIME = boost::posix_time::from_iso_string("14000101T000000");
28
29Interval::Interval(bool isValid)
30 : m_startTime(DEFAULT_TIME)
31 , m_endTime(DEFAULT_TIME)
32 , m_isValid(isValid)
33{
34}
35
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070036Interval::Interval(const TimeStamp& startTime, const TimeStamp& endTime)
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070037 : m_startTime(startTime)
38 , m_endTime(endTime)
39 , m_isValid(true)
40{
41 BOOST_ASSERT(startTime < endTime);
42}
43
44bool
45Interval::covers(const TimeStamp& tp) const
46{
47 BOOST_ASSERT(isValid());
48
49 if (isEmpty())
50 return false;
51 return (m_startTime <= tp && tp < m_endTime);
52}
53
54Interval&
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070055Interval::operator&&(const Interval& interval)
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070056{
57 BOOST_ASSERT(isValid() && interval.isValid());
58
59 // if one is empty, result is empty
60 if (isEmpty() || interval.isEmpty()) {
61 m_startTime = m_endTime;
62 return *this;
63 }
64 // two intervals do not have intersection
65 if (m_startTime >= interval.getEndTime() || m_endTime <= interval.getStartTime()) {
66 m_startTime = m_endTime;
67 return *this;
68 }
69
70 // get the start time
71 if (m_startTime <= interval.getStartTime())
72 m_startTime = interval.getStartTime();
73
74 // get the end time
75 if (m_endTime > interval.getEndTime())
76 m_endTime = interval.getEndTime();
77
78 return *this;
79}
80
81Interval&
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070082Interval::operator||(const Interval& interval)
Zhiyi Zhangcea58d52015-08-26 10:19:56 -070083{
84 BOOST_ASSERT(this->isValid() && interval.isValid());
85
86 if (isEmpty()) {
87 // left interval is empty, return left one
88 m_startTime = interval.getStartTime();
89 m_endTime = interval.getEndTime();
90 return *this;
91 }
92 if (interval.isEmpty()) {
93 // right interval is empty, return right one
94 return *this;
95 }
96 if (m_startTime >= interval.getEndTime() || m_endTime <= interval.getStartTime()) {
97 // two intervals do not have intersection
98 BOOST_THROW_EXCEPTION(Error("cannot generate a union interval when there's no intersection"));
99 }
100
101 // get the start time
102 if (m_startTime > interval.getStartTime())
103 m_startTime = interval.getStartTime();
104
105 // get the end time
106 if (m_endTime <= interval.getEndTime())
107 m_endTime = interval.getEndTime();
108
109 return *this;
110}
111
112} // namespace gep
113} // namespace ndn