1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include "../sys/platform.h"
7#include "../math/math.h"
8
9namespace embree
10{
11 template<typename Ty>
12 struct range
13 {
14 __forceinline range() {}
15
16 __forceinline range(const Ty& begin)
17 : _begin(begin), _end(begin+1) {}
18
19 __forceinline range(const Ty& begin, const Ty& end)
20 : _begin(begin), _end(end) {}
21
22 __forceinline range(const range& other)
23 : _begin(other._begin), _end(other._end) {}
24
25 template<typename T1>
26 __forceinline range(const range<T1>& other)
27 : _begin(Ty(other._begin)), _end(Ty(other._end)) {}
28
29 template<typename T1>
30 __forceinline range& operator =(const range<T1>& other) {
31 _begin = other._begin;
32 _end = other._end;
33 return *this;
34 }
35
36 __forceinline Ty begin() const {
37 return _begin;
38 }
39
40 __forceinline Ty end() const {
41 return _end;
42 }
43
44 __forceinline range intersect(const range& r) const {
45 return range (max(_begin,r._begin),min(_end,r._end));
46 }
47
48 __forceinline Ty size() const {
49 return _end - _begin;
50 }
51
52 __forceinline bool empty() const {
53 return _end <= _begin;
54 }
55
56 __forceinline Ty center() const {
57 return (_begin + _end)/2;
58 }
59
60 __forceinline std::pair<range,range> split() const
61 {
62 const Ty _center = center();
63 return std::make_pair(range(_begin,_center),range(_center,_end));
64 }
65
66 __forceinline void split(range& left_o, range& right_o) const
67 {
68 const Ty _center = center();
69 left_o = range(_begin,_center);
70 right_o = range(_center,_end);
71 }
72
73 __forceinline friend bool operator< (const range& r0, const range& r1) {
74 return r0.size() < r1.size();
75 }
76
77 friend embree_ostream operator<<(embree_ostream cout, const range& r) {
78 return cout << "range [" << r.begin() << ", " << r.end() << "]";
79 }
80
81 Ty _begin, _end;
82 };
83
84 template<typename Ty>
85 range<Ty> make_range(const Ty& begin, const Ty& end) {
86 return range<Ty>(begin,end);
87 }
88
89 template<typename Ty>
90 struct extended_range : public range<Ty>
91 {
92 __forceinline extended_range () {}
93
94 __forceinline extended_range (const Ty& begin)
95 : range<Ty>(begin), _ext_end(begin+1) {}
96
97 __forceinline extended_range (const Ty& begin, const Ty& end)
98 : range<Ty>(begin,end), _ext_end(end) {}
99
100 __forceinline extended_range (const Ty& begin, const Ty& end, const Ty& ext_end)
101 : range<Ty>(begin,end), _ext_end(ext_end) {}
102
103 __forceinline Ty ext_end() const {
104 return _ext_end;
105 }
106
107 __forceinline Ty ext_size() const {
108 return _ext_end - range<Ty>::_begin;
109 }
110
111 __forceinline Ty ext_range_size() const {
112 return _ext_end - range<Ty>::_end;
113 }
114
115 __forceinline bool has_ext_range() const {
116 assert(_ext_end >= range<Ty>::_end);
117 return (_ext_end - range<Ty>::_end) > 0;
118 }
119
120 __forceinline void set_ext_range(const size_t ext_end){
121 assert(ext_end >= range<Ty>::_end);
122 _ext_end = ext_end;
123 }
124
125 __forceinline void move_right(const size_t plus){
126 range<Ty>::_begin += plus;
127 range<Ty>::_end += plus;
128 _ext_end += plus;
129 }
130
131 friend embree_ostream operator<<(embree_ostream cout, const extended_range& r) {
132 return cout << "extended_range [" << r.begin() << ", " << r.end() << " (" << r.ext_end() << ")]";
133 }
134
135 Ty _ext_end;
136 };
137}
138