Poplar and PopLibs
Interval.hpp
1// Copyright (c) 2017 Graphcore Ltd. All rights reserved.
2
3#ifndef poplar_Interval_hpp
4#define poplar_Interval_hpp
5
6#include <cstddef>
7#include <ostream>
8#include <poplar/exceptions.hpp>
9#include <tuple>
10
11namespace poplar {
12
18template <class T> struct GenericInterval {
19private:
20 T begin_{};
21 T end_{};
22
23public:
27 GenericInterval() = default;
28
29 GenericInterval(T begin, T end) : begin_(begin), end_(end) {
30 if (end < begin) {
31 throw poplar::index_error("Invalid interval: [" + std::to_string(begin) +
32 "," + std::to_string(end) + ")");
33 }
34 }
35 const T &begin() const { return begin_; }
36 const T &end() const { return end_; }
37
42 const T &lower() const { return begin_; }
43
48 const T &upper() const { return end_; }
49
54 T size() const { return T(end_ - begin_); }
55};
56
57template <class T>
58inline bool operator==(const GenericInterval<T> &a,
59 const GenericInterval<T> &b) {
60 return std::tie(a.begin(), a.end()) == std::tie(b.begin(), b.end());
61}
62
63template <class T>
64inline bool operator<(const GenericInterval<T> &a,
65 const GenericInterval<T> &b) {
66 return std::tie(a.begin(), a.end()) < std::tie(b.begin(), b.end());
67}
68
69template <class T>
70inline bool operator!=(const GenericInterval<T> &a,
71 const GenericInterval<T> &b) {
72 return !(a == b);
73}
74
75template <class T>
76inline bool operator>=(const GenericInterval<T> &a,
77 const GenericInterval<T> &b) {
78 return !(a < b);
79}
80
81template <class T>
82inline bool operator>(const GenericInterval<T> &a,
83 const GenericInterval<T> &b) {
84 return b < a;
85}
86
87template <class T>
88inline bool operator<=(const GenericInterval<T> &a,
89 const GenericInterval<T> &b) {
90 return !(b < a);
91}
92
93template <class T>
94inline std::ostream &operator<<(std::ostream &os, const GenericInterval<T> &b) {
95 os << "[" << b.begin() << ":" << b.end() << ")";
96 return os;
97}
98
99typedef GenericInterval<std::size_t> Interval;
100
101} // namespace poplar
102#endif // poplar_Interval_hpp
Poplar classes and functions.
Definition: ArrayRef.hpp:14
std::ostream & operator<<(std::ostream &os, const DebugNameAndId &dnai)
Display the path name of the DebugNameAndId.
This class represents an interval that is closed at its lower bound and open at its upper bound.
Definition: Interval.hpp:18
T size() const
Get the size of the interval.
Definition: Interval.hpp:54
const T & upper() const
Get the upper bound of the half open interval.
Definition: Interval.hpp:48
GenericInterval()=default
Initialise with begin and end set to their default value of 0.
const T & lower() const
Get the lower bound of the half open interval.
Definition: Interval.hpp:42
This exception is thrown if the index of a subscript is out of the bounds of the field it is accessin...
Definition: exceptions.hpp:111