interval.hpp
1 //*****************************************************************************
2 // Copyright 2017-2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16 
17 #pragma once
18 
19 #include <algorithm>
20 #include <cstdint>
21 #include <iostream>
22 #include <limits>
23 #include <stdexcept>
24 
25 #include "ngraph/ngraph_visibility.hpp"
26 
27 namespace ngraph
28 {
29  /// \brief Interval arithmetic
30  ///
31  /// An interval is the set of integers from m_min_val through m_max_val.
32  /// The value s_max acts like infinity. The
33  /// addition, subtraction, or multiplication of intervals is the smallest interval
34  /// containing the sums, differences, or products of elements of the two intervals. An empty
36  class NGRAPH_API Interval
37  {
38  public:
39  using value_type = std::int64_t;
40  using size_type = std::uint64_t;
41 
42  /// \brief Interval of everything
43  Interval() = default;
44  /// \brief Copy constructor
45  Interval(const Interval& interval) = default;
46 
47  /// \brief Closed interval {x|min_val <= x <= max_val}
48  Interval(value_type min_val, value_type max_val);
49 
50  /// \brief Single-valued interval; just contains val
51  Interval(value_type val);
52 
53  Interval& operator=(const Interval& interval) = default;
54 
55  /// \brief The number of elements in the interval. Zero if max < min.
56  size_type size() const;
57  /// \brief Returns true if the interval has no elements
58  bool empty() const;
59  /// \brief the inclusive lower bound of the interval
60  value_type get_min_val() const { return m_min_val; }
61  /// \brief Set the inclusive lower bound of the interval
62  void set_min_val(value_type val) { m_min_val = val; }
63  /// \brief the inclusive upper bound of the interval
64  value_type get_max_val() const { return m_max_val; }
65  /// \brief Set the inclusive upper bound of the interval
66  void set_max_val(value_type val) { m_max_val = val; }
67  /// \brief True if the upper bound is finite
68  bool has_upper_bound() const { return m_max_val != s_max; }
69  /// \brief True if min and max bounds match
70  bool operator==(const Interval& interval) const;
71  bool operator!=(const Interval& interval) const;
72 
73  /// \brief The interval whose elements are a sum of an element from each interval
74  Interval operator+(const Interval& interval) const;
75 
76  /// \brief Extend this interval to sums of elements in this interval and interval
77  Interval& operator+=(const Interval& interval);
78 
79  /// \brief The interval whose elements are a difference of an element from each interval
80  Interval operator-(const Interval& interval) const;
81 
82  /// \brief Extend this interval to differences of elements in this interval and interval
83  Interval& operator-=(const Interval& interval);
84 
85  /// \brief The smallest interval whose elements are a product of an element from each
86  /// interval
87  Interval operator*(const Interval& interval) const;
88 
89  /// \brief Extend this interval to products of elements in this interval and interval
90  Interval& operator*=(const Interval& interval);
91 
92  /// \brief The interval that is the intersection of this interval and interval
93  Interval operator&(const Interval& interval) const;
94 
95  /// \brief Change this interval to only include elements also in interval
96  Interval& operator&=(const Interval& interval);
97 
98  /// \brief True if this interval includes value
99  bool contains(value_type value) const;
100  /// \brief True if this interval includes all the values in interval
101  bool contains(const Interval& interval) const;
102 
103  /// \brief The value used for no upper bound
104  static constexpr value_type s_max{std::numeric_limits<value_type>::max()};
105 
106  protected:
108  static value_type clip(value_type value);
109  static value_type clip_times(value_type a, value_type b);
110  static value_type clip_add(value_type a, value_type b);
111  static value_type clip_minus(value_type a, value_type b);
112 
113  value_type m_min_val{0};
114  value_type m_max_val{s_max};
115  };
116 
117  NGRAPH_API
118  std::ostream& operator<<(std::ostream& str, const Interval& interval);
119 }
ngraph::Interval::operator*=
Interval & operator*=(const Interval &interval)
Extend this interval to products of elements in this interval and interval.
ngraph::Interval::Interval
Interval(value_type val)
Single-valued interval; just contains val.
ngraph::Interval::operator&=
Interval & operator&=(const Interval &interval)
Change this interval to only include elements also in interval.
ngraph::Interval::set_min_val
void set_min_val(value_type val)
Set the inclusive lower bound of the interval.
Definition: interval.hpp:62
ngraph::Interval::size
size_type size() const
The number of elements in the interval. Zero if max < min.
ngraph::Interval::get_min_val
value_type get_min_val() const
the inclusive lower bound of the interval
Definition: interval.hpp:60
ngraph::Interval::get_max_val
value_type get_max_val() const
the inclusive upper bound of the interval
Definition: interval.hpp:64
ngraph::Interval::contains
bool contains(const Interval &interval) const
True if this interval includes all the values in interval.
ngraph::Interval::Interval
Interval(value_type min_val, value_type max_val)
Closed interval {x|min_val <= x <= max_val}.
ngraph::Interval::operator-=
Interval & operator-=(const Interval &interval)
Extend this interval to differences of elements in this interval and interval.
ngraph::Interval::contains
bool contains(value_type value) const
True if this interval includes value.
ngraph::Interval
Interval arithmetic.
Definition: interval.hpp:37
ngraph::Interval::empty
bool empty() const
Returns true if the interval has no elements.
ngraph::Interval::operator-
Interval operator-(const Interval &interval) const
The interval whose elements are a difference of an element from each interval.
ngraph
The Intel nGraph C++ API.
Definition: attribute_adapter.hpp:28
ngraph::Interval::operator+=
Interval & operator+=(const Interval &interval)
Extend this interval to sums of elements in this interval and interval.
ngraph::Interval::set_max_val
void set_max_val(value_type val)
Set the inclusive upper bound of the interval.
Definition: interval.hpp:66
ngraph::Interval::operator&
Interval operator&(const Interval &interval) const
The interval that is the intersection of this interval and interval.
ngraph::Interval::Interval
Interval()=default
Interval of everything.
ngraph::Interval::operator+
Interval operator+(const Interval &interval) const
The interval whose elements are a sum of an element from each interval.
ngraph::Interval::has_upper_bound
bool has_upper_bound() const
True if the upper bound is finite.
Definition: interval.hpp:68
ngraph::Interval::operator==
bool operator==(const Interval &interval) const
True if min and max bounds match.
ngraph::Interval::operator*
Interval operator*(const Interval &interval) const
The smallest interval whose elements are a product of an element from each interval.
ngraph::Interval::Interval
Interval(const Interval &interval)=default
Copy constructor.