Range.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 #ifndef SQUID_SRC_BASE_RANGE_H
10 #define SQUID_SRC_BASE_RANGE_H
11 
12 #include <iosfwd>
13 #include <ostream>
14 
15 /* represents [start, end) */
16 
17 template <class C, class S = size_t>
18 class Range
19 {
20 
21 public:
22  Range ();
23  Range (C start_, C end_);
25  C end;
26  Range intersection (Range const &) const;
27  bool contains(C const &) const;
28  S size() const;
29 };
30 
31 template <class C, class S>
32 std::ostream& operator << (std::ostream &os, Range<C, S> const &aRange)
33 {
34  os << "[" << aRange.start << "," << aRange.end << ")";
35  return os;
36 }
37 
38 template<class C, class S>
39 Range<C, S>::Range () : start(), end() {}
40 
41 template<class C, class S>
42 Range<C, S>::Range (C start_, C end_) : start(start_), end(end_) {}
43 
44 template<class C, class S>
46 Range<C, S>::intersection (Range const &rhs) const
47 {
48  Range<C, S> result (max(start, rhs.start), min(end, rhs.end));
49  return result;
50 }
51 
52 template<class C, class S>
53 bool
54 Range<C, S>::contains(C const &value) const {
55  assert(start <= end);
56  return (start <= value && value <= end);
57 }
58 
59 template<class C, class S>
60 S
62 {
63  return (S) (end > start ? end - start : 0);
64 }
65 
66 #endif /* SQUID_SRC_BASE_RANGE_H */
67 
C end
Definition: Range.h:25
const A & max(A const &lhs, A const &rhs)
S size() const
Definition: Range.h:61
Definition: Range.h:18
Range()
Definition: Range.h:39
#define assert(EX)
Definition: assert.h:17
static uint32 C
Definition: md4.c:43
bool contains(C const &) const
Definition: Range.h:54
Range intersection(Range const &) const
Definition: Range.h:46
const A & min(A const &lhs, A const &rhs)
std::ostream & operator<<(std::ostream &os, Range< C, S > const &aRange)
Definition: Range.h:32
C start
Definition: Range.h:24

 

Introduction

Documentation

Support

Miscellaneous