HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
multiInterval.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_GF_MULTI_INTERVAL_H
8 #define PXR_BASE_GF_MULTI_INTERVAL_H
9 
10 /// \file gf/multiInterval.h
11 /// \ingroup group_gf_BasicMath
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/interval.h"
15 #include "pxr/base/gf/api.h"
16 
17 #include <iosfwd>
18 #include <set>
19 #include <vector>
20 
22 
23 /// \class GfMultiInterval
24 /// \ingroup group_gf_BasicMath
25 ///
26 /// GfMultiInterval represents a subset of the real number line as an
27 /// ordered set of non-intersecting GfIntervals.
28 ///
30 {
31 public:
32  typedef std::set<GfInterval> Set;
33  typedef Set::const_iterator const_iterator;
34  typedef Set::const_iterator iterator;
35 
36  /// \name Constructors
37  /// @{
38  /// Constructs an empty multi-interval.
39  GfMultiInterval() = default;
40  /// Constructs an multi-interval with the single given interval.
41  GF_API explicit GfMultiInterval(const GfInterval &i);
42  /// Constructs an multi-interval containing the given input intervals.
43  GF_API explicit GfMultiInterval(const std::vector<GfInterval> &intervals);
44  /// @}
45 
46  GF_API bool operator==(const GfMultiInterval &that) const { return _set == that._set; }
47  GF_API bool operator!=(const GfMultiInterval &that) const { return !(*this == that); }
48  GF_API bool operator<(const GfMultiInterval &that) const { return _set < that._set; }
49  GF_API bool operator>=(const GfMultiInterval &that) const { return !(*this < that); }
50  GF_API bool operator>(const GfMultiInterval &that) const { return (that < *this); }
51  GF_API bool operator<=(const GfMultiInterval &that) const { return !(that < *this); }
52 
53 
54  /// Hash value.
55  /// Just a basic hash function, not particularly high quality.
56  GF_API size_t Hash() const;
57 
58  friend inline size_t hash_value(const GfMultiInterval &mi) {
59  return mi.Hash();
60  }
61 
62  /// \name Accessors
63  /// @{
64 
65  /// Returns true if the multi-interval is empty.
66  GF_API bool IsEmpty() const { return _set.empty(); }
67 
68  /// Returns the number of intervals in the set.
69  GF_API size_t GetSize() const { return _set.size(); }
70 
71  /// Returns an interval bounding the entire multi-interval.
72  /// Returns an empty interval if the multi-interval is empty.
73  GF_API GfInterval GetBounds() const;
74 
75  /// Returns true if the multi-interval contains the given value.
76  GF_API bool Contains(double d) const;
77 
78  /// Returns true if the multi-interval contains the given interval.
79  GF_API bool Contains(const GfInterval & i) const;
80 
81  /// Returns true if the multi-interval contains all the intervals in the
82  /// given multi-interval.
83  GF_API bool Contains(const GfMultiInterval & s) const;
84 
85  /// @}
86 
87  /// \name Mutation
88  /// @{
89 
90  /// Clear the multi-interval.
91  GF_API void Clear() { _set.clear(); }
92 
93  /// Add the given interval to the multi-interval.
94  GF_API void Add( const GfInterval & i );
95  /// Add the given multi-interval to the multi-interval.
96  /// Sets this object to the union of the two sets.
97  GF_API void Add( const GfMultiInterval &s );
98 
99  /// Uses the given interval to extend the multi-interval in
100  /// the interval arithmetic sense.
101  GF_API void ArithmeticAdd( const GfInterval &i );
102 
103  /// Remove the given interval from this multi-interval.
104  GF_API void Remove( const GfInterval & i );
105  /// Remove the given multi-interval from this multi-interval.
106  GF_API void Remove( const GfMultiInterval &s );
107 
108  GF_API void Intersect( const GfInterval & i );
109  GF_API void Intersect( const GfMultiInterval &s );
110 
111  /// Return the complement of this set.
113 
114  /// @}
115 
116  /// \name Iteration
117  /// Only const iterators are returned. To maintain the invariants of
118  /// the multi-interval, changes must be made via the public mutation API.
119  /// @{
120 
121  GF_API const_iterator begin() const { return _set.begin(); }
122  GF_API const_iterator end() const { return _set.end(); }
123 
124  /// Returns an iterator identifying the first (lowest) interval whose
125  /// minimum value is >= x. If no such interval exists, returns end().
126  GF_API const_iterator lower_bound( double x ) const;
127 
128  /// Returns an iterator identifying the first (lowest) interval whose
129  /// minimum value is > x. If no such interval exists, returns end().
130  GF_API const_iterator upper_bound( double x ) const;
131 
132  /// Returns an iterator identifying the first (lowest) interval whose
133  /// minimum value is > x. If no such interval exists, returns end().
135 
136  /// Returns an iterator identifying the last (highest) interval whose
137  /// maximum value is < x. If no such interval exists, returns end().
139 
140  /// Returns an iterator identifying the interval that contains x. If
141  /// no interval contains x, then it returns end()
143 
144  /// @}
145 
146  /// Returns the full interval (-inf, inf).
149  }
150 
151  /// Swap two multi-intervals
152  void swap(GfMultiInterval &other) { _set.swap(other._set); }
153 
154 private:
155  void _AssertInvariants() const;
156 
157  Set _set;
158 };
159 
160 /// Output a GfMultiInterval
161 /// \ingroup group_gf_DebuggingOutput
162 GF_API std::ostream & operator<<(std::ostream &out, const GfMultiInterval &s);
163 
165 
166 #endif // PXR_BASE_GF_MULTI_INTERVAL_H
GF_API bool IsEmpty() const
Returns true if the multi-interval is empty.
Definition: multiInterval.h:66
GF_API bool Contains(double d) const
Returns true if the multi-interval contains the given value.
GF_API const_iterator lower_bound(double x) const
void swap(GfMultiInterval &other)
Swap two multi-intervals.
GF_API bool operator==(const GfMultiInterval &that) const
Definition: multiInterval.h:46
GF_API bool operator<(const GfMultiInterval &that) const
Definition: multiInterval.h:48
GF_API const_iterator GetPriorNonContainingInterval(double x) const
GLdouble s
Definition: glad.h:3009
static GfInterval GetFullInterval()
Returns the full interval (-inf, inf).
Definition: interval.h:325
GF_API bool operator<=(const GfMultiInterval &that) const
Definition: multiInterval.h:51
GF_API void Add(const GfInterval &i)
Add the given interval to the multi-interval.
GF_API bool operator>=(const GfMultiInterval &that) const
Definition: multiInterval.h:49
GF_API const_iterator upper_bound(double x) const
GF_API GfMultiInterval GetComplement() const
Return the complement of this set.
GF_API bool operator!=(const GfMultiInterval &that) const
Definition: multiInterval.h:47
static GfMultiInterval GetFullInterval()
Returns the full interval (-inf, inf).
GF_API const_iterator GetNextNonContainingInterval(double x) const
GF_API size_t GetSize() const
Returns the number of intervals in the set.
Definition: multiInterval.h:69
GfMultiInterval()=default
Constructs an multi-interval with the single given interval.
Set::const_iterator const_iterator
Definition: multiInterval.h:33
GF_API void ArithmeticAdd(const GfInterval &i)
GF_API GfInterval GetBounds() const
GF_API std::ostream & operator<<(std::ostream &out, const GfMultiInterval &s)
GF_API const_iterator end() const
GLint GLenum GLint x
Definition: glcorearb.h:409
friend size_t hash_value(const GfMultiInterval &mi)
Definition: multiInterval.h:58
Set::const_iterator iterator
Definition: multiInterval.h:34
GF_API bool operator>(const GfMultiInterval &that) const
Definition: multiInterval.h:50
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
GF_API const_iterator GetContainingInterval(double x) const
GF_API void Clear()
Clear the multi-interval.
Definition: multiInterval.h:91
std::set< GfInterval > Set
Definition: multiInterval.h:32
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GF_API void Intersect(const GfInterval &i)
Clear the multi-interval.
GF_API void Remove(const GfInterval &i)
Remove the given interval from this multi-interval.
GF_API size_t Hash() const
#define GF_API
Definition: api.h:23
GF_API const_iterator begin() const