| 1 | #ifndef DATE_TIME_PERIOD_HPP___ |
| 2 | #define DATE_TIME_PERIOD_HPP___ |
| 3 | |
| 4 | /* Copyright (c) 2002,2003 CrystalClear Software, Inc. |
| 5 | * Use, modification and distribution is subject to the |
| 6 | * Boost Software License, Version 1.0. (See accompanying |
| 7 | * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
| 8 | * Author: Jeff Garland, Bart Garst |
| 9 | * $Date$ |
| 10 | */ |
| 11 | |
| 12 | /*! \file period.hpp |
| 13 | This file contain the implementation of the period abstraction. This is |
| 14 | basically the same idea as a range. Although this class is intended for |
| 15 | use in the time library, it is pretty close to general enough for other |
| 16 | numeric uses. |
| 17 | |
| 18 | */ |
| 19 | |
| 20 | #include <boost/operators.hpp> |
| 21 | #include <boost/date_time/compiler_config.hpp> |
| 22 | |
| 23 | |
| 24 | namespace boost { |
| 25 | namespace date_time { |
| 26 | //!Provides generalized period type useful in date-time systems |
| 27 | /*!This template uses a class to represent a time point within the period |
| 28 | and another class to represent a duration. As a result, this class is |
| 29 | not appropriate for use when the number and duration representation |
| 30 | are the same (eg: in the regular number domain). |
| 31 | |
| 32 | A period can be specified by providing either the begining point and |
| 33 | a duration or the begining point and the end point( end is NOT part |
| 34 | of the period but 1 unit past it. A period will be "invalid" if either |
| 35 | end_point <= begin_point or the given duration is <= 0. Any valid period |
| 36 | will return false for is_null(). |
| 37 | |
| 38 | Zero length periods are also considered invalid. Zero length periods are |
| 39 | periods where the begining and end points are the same, or, the given |
| 40 | duration is zero. For a zero length period, the last point will be one |
| 41 | unit less than the begining point. |
| 42 | |
| 43 | In the case that the begin and last are the same, the period has a |
| 44 | length of one unit. |
| 45 | |
| 46 | The best way to handle periods is usually to provide a begining point and |
| 47 | a duration. So, day1 + 7 days is a week period which includes all of the |
| 48 | first day and 6 more days (eg: Sun to Sat). |
| 49 | |
| 50 | */ |
| 51 | template<class point_rep, class duration_rep> |
| 52 | class BOOST_SYMBOL_VISIBLE period : private |
| 53 | boost::less_than_comparable<period<point_rep, duration_rep> |
| 54 | , boost::equality_comparable< period<point_rep, duration_rep> |
| 55 | > > |
| 56 | { |
| 57 | public: |
| 58 | typedef point_rep point_type; |
| 59 | typedef duration_rep duration_type; |
| 60 | |
| 61 | BOOST_CXX14_CONSTEXPR period(point_rep first_point, point_rep end_point); |
| 62 | BOOST_CXX14_CONSTEXPR period(point_rep first_point, duration_rep len); |
| 63 | BOOST_CXX14_CONSTEXPR point_rep begin() const; |
| 64 | BOOST_CXX14_CONSTEXPR point_rep end() const; |
| 65 | BOOST_CXX14_CONSTEXPR point_rep last() const; |
| 66 | BOOST_CXX14_CONSTEXPR duration_rep length() const; |
| 67 | BOOST_CXX14_CONSTEXPR bool is_null() const; |
| 68 | BOOST_CXX14_CONSTEXPR bool operator==(const period& rhs) const; |
| 69 | BOOST_CXX14_CONSTEXPR bool operator<(const period& rhs) const; |
| 70 | BOOST_CXX14_CONSTEXPR void shift(const duration_rep& d); |
| 71 | BOOST_CXX14_CONSTEXPR void expand(const duration_rep& d); |
| 72 | BOOST_CXX14_CONSTEXPR bool contains(const point_rep& point) const; |
| 73 | BOOST_CXX14_CONSTEXPR bool contains(const period& other) const; |
| 74 | BOOST_CXX14_CONSTEXPR bool intersects(const period& other) const; |
| 75 | BOOST_CXX14_CONSTEXPR bool is_adjacent(const period& other) const; |
| 76 | BOOST_CXX14_CONSTEXPR bool is_before(const point_rep& point) const; |
| 77 | BOOST_CXX14_CONSTEXPR bool is_after(const point_rep& point) const; |
| 78 | BOOST_CXX14_CONSTEXPR period intersection(const period& other) const; |
| 79 | BOOST_CXX14_CONSTEXPR period merge(const period& other) const; |
| 80 | BOOST_CXX14_CONSTEXPR period span(const period& other) const; |
| 81 | private: |
| 82 | point_rep begin_; |
| 83 | point_rep last_; |
| 84 | }; |
| 85 | |
| 86 | //! create a period from begin to last eg: [begin,end) |
| 87 | /*! If end <= begin then the period will be invalid |
| 88 | */ |
| 89 | template<class point_rep, class duration_rep> |
| 90 | inline BOOST_CXX14_CONSTEXPR |
| 91 | period<point_rep,duration_rep>::period(point_rep first_point, |
| 92 | point_rep end_point) : |
| 93 | begin_(first_point), |
| 94 | last_(end_point - duration_rep::unit()) |
| 95 | {} |
| 96 | |
| 97 | //! create a period as [begin, begin+len) |
| 98 | /*! If len is <= 0 then the period will be invalid |
| 99 | */ |
| 100 | template<class point_rep, class duration_rep> |
| 101 | inline BOOST_CXX14_CONSTEXPR |
| 102 | period<point_rep,duration_rep>::period(point_rep first_point, duration_rep len) : |
| 103 | begin_(first_point), |
| 104 | last_(first_point + len-duration_rep::unit()) |
| 105 | { } |
| 106 | |
| 107 | |
| 108 | //! Return the first element in the period |
| 109 | template<class point_rep, class duration_rep> |
| 110 | inline BOOST_CXX14_CONSTEXPR |
| 111 | point_rep period<point_rep,duration_rep>::begin() const |
| 112 | { |
| 113 | return begin_; |
| 114 | } |
| 115 | |
| 116 | //! Return one past the last element |
| 117 | template<class point_rep, class duration_rep> |
| 118 | inline BOOST_CXX14_CONSTEXPR |
| 119 | point_rep period<point_rep,duration_rep>::end() const |
| 120 | { |
| 121 | return last_ + duration_rep::unit(); |
| 122 | } |
| 123 | |
| 124 | //! Return the last item in the period |
| 125 | template<class point_rep, class duration_rep> |
| 126 | inline BOOST_CXX14_CONSTEXPR |
| 127 | point_rep period<point_rep,duration_rep>::last() const |
| 128 | { |
| 129 | return last_; |
| 130 | } |
| 131 | |
| 132 | //! True if period is ill formed (length is zero or less) |
| 133 | template<class point_rep, class duration_rep> |
| 134 | inline BOOST_CXX14_CONSTEXPR |
| 135 | bool period<point_rep,duration_rep>::is_null() const |
| 136 | { |
| 137 | return end() <= begin_; |
| 138 | } |
| 139 | |
| 140 | //! Return the length of the period |
| 141 | template<class point_rep, class duration_rep> |
| 142 | inline BOOST_CXX14_CONSTEXPR |
| 143 | duration_rep period<point_rep,duration_rep>::length() const |
| 144 | { |
| 145 | if(last_ < begin_){ // invalid period |
| 146 | return last_+duration_rep::unit() - begin_; |
| 147 | } |
| 148 | else{ |
| 149 | return end() - begin_; // normal case |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | //! Equality operator |
| 154 | template<class point_rep, class duration_rep> |
| 155 | inline BOOST_CXX14_CONSTEXPR |
| 156 | bool period<point_rep,duration_rep>::operator==(const period& rhs) const |
| 157 | { |
| 158 | return ((begin_ == rhs.begin_) && |
| 159 | (last_ == rhs.last_)); |
| 160 | } |
| 161 | |
| 162 | //! Strict as defined by rhs.last <= lhs.last |
| 163 | template<class point_rep, class duration_rep> |
| 164 | inline BOOST_CXX14_CONSTEXPR |
| 165 | bool period<point_rep,duration_rep>::operator<(const period& rhs) const |
| 166 | { |
| 167 | return (last_ < rhs.begin_); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | //! Shift the start and end by the specified amount |
| 172 | template<class point_rep, class duration_rep> |
| 173 | inline BOOST_CXX14_CONSTEXPR |
| 174 | void period<point_rep,duration_rep>::shift(const duration_rep& d) |
| 175 | { |
| 176 | begin_ = begin_ + d; |
| 177 | last_ = last_ + d; |
| 178 | } |
| 179 | |
| 180 | /** Expands the size of the period by the duration on both ends. |
| 181 | * |
| 182 | *So before expand |
| 183 | *@code |
| 184 | * |
| 185 | * [-------] |
| 186 | * ^ ^ ^ ^ ^ ^ ^ |
| 187 | * 1 2 3 4 5 6 7 |
| 188 | * |
| 189 | *@endcode |
| 190 | * After expand(2) |
| 191 | *@code |
| 192 | * |
| 193 | * [----------------------] |
| 194 | * ^ ^ ^ ^ ^ ^ ^ |
| 195 | * 1 2 3 4 5 6 7 |
| 196 | * |
| 197 | *@endcode |
| 198 | */ |
| 199 | template<class point_rep, class duration_rep> |
| 200 | inline BOOST_CXX14_CONSTEXPR |
| 201 | void period<point_rep,duration_rep>::expand(const duration_rep& d) |
| 202 | { |
| 203 | begin_ = begin_ - d; |
| 204 | last_ = last_ + d; |
| 205 | } |
| 206 | |
| 207 | //! True if the point is inside the period, zero length periods contain no points |
| 208 | template<class point_rep, class duration_rep> |
| 209 | inline BOOST_CXX14_CONSTEXPR |
| 210 | bool period<point_rep,duration_rep>::contains(const point_rep& point) const |
| 211 | { |
| 212 | return ((point >= begin_) && |
| 213 | (point <= last_)); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | //! True if this period fully contains (or equals) the other period |
| 218 | template<class point_rep, class duration_rep> |
| 219 | inline BOOST_CXX14_CONSTEXPR |
| 220 | bool period<point_rep,duration_rep>::contains(const period<point_rep,duration_rep>& other) const |
| 221 | { |
| 222 | return ((begin_ <= other.begin_) && (last_ >= other.last_)); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | //! True if periods are next to each other without a gap. |
| 227 | /* In the example below, p1 and p2 are adjacent, but p3 is not adjacent |
| 228 | * with either of p1 or p2. |
| 229 | *@code |
| 230 | * [-p1-) |
| 231 | * [-p2-) |
| 232 | * [-p3-) |
| 233 | *@endcode |
| 234 | */ |
| 235 | template<class point_rep, class duration_rep> |
| 236 | inline BOOST_CXX14_CONSTEXPR |
| 237 | bool period<point_rep,duration_rep>::is_adjacent(const period<point_rep,duration_rep>& other) const |
| 238 | { |
| 239 | return (other.begin() == end() || |
| 240 | begin_ == other.end()); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | //! True if all of the period is prior or t < start |
| 245 | /* In the example below only point 1 would evaluate to true. |
| 246 | *@code |
| 247 | * [---------]) |
| 248 | * ^ ^ ^ ^ ^ |
| 249 | * 1 2 3 4 5 |
| 250 | * |
| 251 | *@endcode |
| 252 | */ |
| 253 | template<class point_rep, class duration_rep> |
| 254 | inline BOOST_CXX14_CONSTEXPR |
| 255 | bool period<point_rep,duration_rep>::is_after(const point_rep& t) const |
| 256 | { |
| 257 | if (is_null()) |
| 258 | { |
| 259 | return false; //null period isn't after |
| 260 | } |
| 261 | |
| 262 | return t < begin_; |
| 263 | } |
| 264 | |
| 265 | //! True if all of the period is prior to the passed point or end <= t |
| 266 | /* In the example below points 4 and 5 return true. |
| 267 | *@code |
| 268 | * [---------]) |
| 269 | * ^ ^ ^ ^ ^ |
| 270 | * 1 2 3 4 5 |
| 271 | * |
| 272 | *@endcode |
| 273 | */ |
| 274 | template<class point_rep, class duration_rep> |
| 275 | inline BOOST_CXX14_CONSTEXPR |
| 276 | bool period<point_rep,duration_rep>::is_before(const point_rep& t) const |
| 277 | { |
| 278 | if (is_null()) |
| 279 | { |
| 280 | return false; //null period isn't before anything |
| 281 | } |
| 282 | |
| 283 | return last_ < t; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | //! True if the periods overlap in any way |
| 288 | /* In the example below p1 intersects with p2, p4, and p6. |
| 289 | *@code |
| 290 | * [---p1---) |
| 291 | * [---p2---) |
| 292 | * [---p3---) |
| 293 | * [---p4---) |
| 294 | * [-p5-) |
| 295 | * [-p6-) |
| 296 | *@endcode |
| 297 | */ |
| 298 | template<class point_rep, class duration_rep> |
| 299 | inline BOOST_CXX14_CONSTEXPR |
| 300 | bool period<point_rep,duration_rep>::intersects(const period<point_rep,duration_rep>& other) const |
| 301 | { |
| 302 | return ( contains(other.begin_) || |
| 303 | other.contains(begin_) || |
| 304 | ((other.begin_ < begin_) && (other.last_ >= begin_))); |
| 305 | } |
| 306 | |
| 307 | //! Returns the period of intersection or invalid range no intersection |
| 308 | template<class point_rep, class duration_rep> |
| 309 | inline BOOST_CXX14_CONSTEXPR |
| 310 | period<point_rep,duration_rep> |
| 311 | period<point_rep,duration_rep>::intersection(const period<point_rep,duration_rep>& other) const |
| 312 | { |
| 313 | if (begin_ > other.begin_) { |
| 314 | if (last_ <= other.last_) { //case2 |
| 315 | return *this; |
| 316 | } |
| 317 | //case 1 |
| 318 | return period<point_rep,duration_rep>(begin_, other.end()); |
| 319 | } |
| 320 | else { |
| 321 | if (last_ <= other.last_) { //case3 |
| 322 | return period<point_rep,duration_rep>(other.begin_, this->end()); |
| 323 | } |
| 324 | //case4 |
| 325 | return other; |
| 326 | } |
| 327 | //unreachable |
| 328 | } |
| 329 | |
| 330 | //! Returns the union of intersecting periods -- or null period |
| 331 | /*! |
| 332 | */ |
| 333 | template<class point_rep, class duration_rep> |
| 334 | inline BOOST_CXX14_CONSTEXPR |
| 335 | period<point_rep,duration_rep> |
| 336 | period<point_rep,duration_rep>::merge(const period<point_rep,duration_rep>& other) const |
| 337 | { |
| 338 | if (this->intersects(other)) { |
| 339 | if (begin_ < other.begin_) { |
| 340 | return period<point_rep,duration_rep>(begin_, last_ > other.last_ ? this->end() : other.end()); |
| 341 | } |
| 342 | |
| 343 | return period<point_rep,duration_rep>(other.begin_, last_ > other.last_ ? this->end() : other.end()); |
| 344 | |
| 345 | } |
| 346 | return period<point_rep,duration_rep>(begin_,begin_); // no intersect return null |
| 347 | } |
| 348 | |
| 349 | //! Combine two periods with earliest start and latest end. |
| 350 | /*! Combines two periods and any gap between them such that |
| 351 | * start = min(p1.start, p2.start) |
| 352 | * end = max(p1.end , p2.end) |
| 353 | *@code |
| 354 | * [---p1---) |
| 355 | * [---p2---) |
| 356 | * result: |
| 357 | * [-----------p3----------) |
| 358 | *@endcode |
| 359 | */ |
| 360 | template<class point_rep, class duration_rep> |
| 361 | inline BOOST_CXX14_CONSTEXPR |
| 362 | period<point_rep,duration_rep> |
| 363 | period<point_rep,duration_rep>::span(const period<point_rep,duration_rep>& other) const |
| 364 | { |
| 365 | point_rep start((begin_ < other.begin_) ? begin() : other.begin()); |
| 366 | point_rep newend((last_ < other.last_) ? other.end() : this->end()); |
| 367 | return period<point_rep,duration_rep>(start, newend); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | } } //namespace date_time |
| 372 | |
| 373 | |
| 374 | |
| 375 | #endif |
| 376 | |