]>
Commit | Line | Data |
---|---|---|
d424cd7f | 1 | /* |
bde978a6 | 2 | * Copyright (C) 1996-2015 The Squid Software Foundation and contributors |
d424cd7f | 3 | * |
5c193dec AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
d424cd7f | 7 | */ |
8 | ||
9 | #ifndef SQUID_RANGE_H | |
10 | #define SQUID_RANGE_H | |
11 | ||
b8bad68c | 12 | #include <iosfwd> |
25b6a907 | 13 | #include <ostream> |
b8bad68c | 14 | |
d424cd7f | 15 | /* represents [start, end) */ |
528b2c61 | 16 | |
c16f948b | 17 | template <class C, class S = size_t> |
528b2c61 | 18 | class Range |
19 | { | |
20 | ||
21 | public: | |
94f895e4 | 22 | Range (); |
d424cd7f | 23 | Range (C start_, C end_); |
24 | C start; | |
25 | C end; | |
c5dd4956 | 26 | Range intersection (Range const &) const; |
c16f948b | 27 | S size() const; |
d424cd7f | 28 | }; |
29 | ||
c16f948b CT |
30 | template <class C, class S> |
31 | std::ostream& operator << (std::ostream &os, Range<C, S> const &aRange) | |
b8bad68c | 32 | { |
33 | os << "[" << aRange.start << "," << aRange.end << ")"; | |
34 | return os; | |
35 | } | |
36 | ||
c16f948b CT |
37 | template<class C, class S> |
38 | Range<C, S>::Range () : start(), end() {} | |
94f895e4 | 39 | |
c16f948b CT |
40 | template<class C, class S> |
41 | Range<C, S>::Range (C start_, C end_) : start(start_), end(end_) {} | |
d424cd7f | 42 | |
c16f948b CT |
43 | template<class C, class S> |
44 | Range<C, S> | |
45 | Range<C, S>::intersection (Range const &rhs) const | |
d424cd7f | 46 | { |
c16f948b | 47 | Range<C, S> result (max(start, rhs.start), min(end, rhs.end)); |
d424cd7f | 48 | return result; |
49 | } | |
50 | ||
c16f948b CT |
51 | template<class C, class S> |
52 | S | |
53 | Range<C, S>::size() const | |
d424cd7f | 54 | { |
c16f948b | 55 | return (S) (end > start ? end - start : 0); |
d424cd7f | 56 | } |
57 | ||
58 | #endif /* SQUID_RANGE_H */ | |
f53969cc | 59 |