interval.hpp
1 // Copyright (C) 2018-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 #pragma once
6 
7 #include <algorithm>
8 #include <cstdint>
9 #include <iostream>
10 #include <limits>
11 #include <stdexcept>
12 
13 #include "ngraph/ngraph_visibility.hpp"
14 
15 namespace ngraph
16 {
17  /// \brief Interval arithmetic
18  ///
19  /// An interval is the set of integers from m_min_val through m_max_val.
20  /// The value s_max acts like infinity. The
21  /// addition, subtraction, or multiplication of intervals is the smallest interval
22  /// containing the sums, differences, or products of elements of the two intervals. An empty
23  /// interval is canonicalized to [s_max, s_max].
24  class NGRAPH_API Interval
25  {
26  public:
27  using value_type = std::int64_t;
28  using size_type = std::uint64_t;
29 
30  /// \brief Interval of everything
31  Interval() = default;
32  /// \brief Copy constructor
33  Interval(const Interval& interval) = default;
34 
35  /// \brief Closed interval {x|min_val <= x <= max_val}
36  Interval(value_type min_val, value_type max_val);
37 
38  /// \brief Single-valued interval; just contains val
39  Interval(value_type val);
40 
41  Interval& operator=(const Interval& interval) = default;
42 
43  /// \brief The number of elements in the interval. Zero if max < min.
44  size_type size() const;
45  /// \brief Returns true if the interval has no elements
46  bool empty() const;
47  /// \brief the inclusive lower bound of the interval
48  value_type get_min_val() const { return m_min_val; }
49  /// \brief Set the inclusive lower bound of the interval
50  void set_min_val(value_type val) { m_min_val = val; }
51  /// \brief the inclusive upper bound of the interval
52  value_type get_max_val() const { return m_max_val; }
53  /// \brief Set the inclusive upper bound of the interval
54  void set_max_val(value_type val) { m_max_val = val; }
55  /// \brief True if the upper bound is finite
56  bool has_upper_bound() const { return m_max_val != s_max; }
57  /// \brief True if min and max bounds match
58  bool operator==(const Interval& interval) const;
59  bool operator!=(const Interval& interval) const;
60 
61  /// \brief The interval whose elements are a sum of an element from each interval
62  Interval operator+(const Interval& interval) const;
63 
64  /// \brief Extend this interval to sums of elements in this interval and interval
65  Interval& operator+=(const Interval& interval);
66 
67  /// \brief The interval whose elements are a difference of an element from each interval
68  Interval operator-(const Interval& interval) const;
69 
70  /// \brief Extend this interval to differences of elements in this interval and interval
71  Interval& operator-=(const Interval& interval);
72 
73  /// \brief The smallest interval whose elements are a product of an element from each
74  /// interval
75  Interval operator*(const Interval& interval) const;
76 
77  /// \brief Extend this interval to products of elements in this interval and interval
78  Interval& operator*=(const Interval& interval);
79 
80  /// \brief The interval that is the intersection of this interval and interval
81  Interval operator&(const Interval& interval) const;
82 
83  /// \brief Change this interval to only include elements also in interval
84  Interval& operator&=(const Interval& interval);
85 
86  /// \brief True if this interval includes value
87  bool contains(value_type value) const;
88  /// \brief True if this interval includes all the values in interval
89  bool contains(const Interval& interval) const;
90 
91  /// \brief The value used for no upper bound
92  static constexpr value_type s_max{std::numeric_limits<value_type>::max()};
93 
94  protected:
95  void canonicalize();
96  static value_type clip(value_type value);
97  static value_type clip_times(value_type a, value_type b);
98  static value_type clip_add(value_type a, value_type b);
99  static value_type clip_minus(value_type a, value_type b);
100 
101  value_type m_min_val{0};
102  value_type m_max_val{s_max};
103  };
104 
105  NGRAPH_API
106  std::ostream& operator<<(std::ostream& str, const Interval& interval);
107 } // namespace ngraph
Interval arithmetic.
Definition: interval.hpp:25
Interval operator-(const Interval &interval) const
The interval whose elements are a difference of an element from each interval.
Interval operator+(const Interval &interval) const
The interval whose elements are a sum of an element from each interval.
bool contains(const Interval &interval) const
True if this interval includes all the values in interval.
void set_min_val(value_type val)
Set the inclusive lower bound of the interval.
Definition: interval.hpp:50
Interval operator*(const Interval &interval) const
The smallest interval whose elements are a product of an element from each interval.
value_type get_min_val() const
the inclusive lower bound of the interval
Definition: interval.hpp:48
Interval(value_type min_val, value_type max_val)
Closed interval {x|min_val <= x <= max_val}.
Interval(value_type val)
Single-valued interval; just contains val.
bool empty() const
Returns true if the interval has no elements.
Interval operator&(const Interval &interval) const
The interval that is the intersection of this interval and interval.
Interval & operator-=(const Interval &interval)
Extend this interval to differences of elements in this interval and interval.
bool has_upper_bound() const
True if the upper bound is finite.
Definition: interval.hpp:56
size_type size() const
The number of elements in the interval. Zero if max < min.
bool contains(value_type value) const
True if this interval includes value.
Interval()=default
Interval of everything.
Interval & operator*=(const Interval &interval)
Extend this interval to products of elements in this interval and interval.
Interval & operator&=(const Interval &interval)
Change this interval to only include elements also in interval.
bool operator==(const Interval &interval) const
True if min and max bounds match.
void set_max_val(value_type val)
Set the inclusive upper bound of the interval.
Definition: interval.hpp:54
Interval(const Interval &interval)=default
Copy constructor.
Interval & operator+=(const Interval &interval)
Extend this interval to sums of elements in this interval and interval.
value_type get_max_val() const
the inclusive upper bound of the interval
Definition: interval.hpp:52
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:16