STIR 6.4.0
IndexRange.inl
Go to the documentation of this file.
1//
2//
3/*
4 Copyright (C) 2000 PARAPET partners
5 Copyright (C) 2000- 2005, Hammersmith Imanet Ltd
6 This file is part of STIR.
7
8 SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
9
10 See STIR/LICENSE.txt for details
11*/
12
24#include <algorithm>
25
26START_NAMESPACE_STIR
27
28/***************************************
29 n-D version
30 ***************************************/
31
32template <int num_dimensions>
34 : base_type(),
35 is_regular_range(regular_true)
36{}
37
38template <int num_dimensions>
40 : base_type(range),
41 is_regular_range(range.is_regular_range)
42{}
43
44template <int num_dimensions>
45IndexRange<num_dimensions>::IndexRange(const base_type& range)
46 : base_type(range),
47 is_regular_range(regular_to_do)
48{}
49
50template <int num_dimensions>
53 : base_type(min_v[1], max_v[1]),
54 is_regular_range(regular_true)
55{
56 const IndexRange<num_dimensions - 1> lower_dims(cut_first_dimension(min_v), cut_first_dimension(max_v));
57 this->fill(lower_dims);
58}
59
60template <int num_dimensions>
62 : base_type(sizes[1]),
63 is_regular_range(regular_true)
64{
65 const IndexRange<num_dimensions - 1> lower_dims(cut_first_dimension(sizes));
66 this->fill(lower_dims);
67}
68
69template <int num_dimensions>
70std::size_t
72{
73 this->check_state();
74 if (this->is_regular_range == regular_true && this->get_length() > 0)
75 return this->get_length() * this->begin()->size_all();
76 // else
77 size_t acc = 0;
78 for (int i = this->get_min_index(); i <= this->get_max_index(); i++)
79 acc += this->num[i].size_all();
80 return acc;
81}
82
83template <int num_dimensions>
84bool
86{
87 return this->get_min_index() == range2.get_min_index() && this->get_length() == range2.get_length()
88 && std::equal(this->begin(), this->end(), range2.begin());
89}
90
91template <int num_dimensions>
92bool
93IndexRange<num_dimensions>::operator!=(const IndexRange<num_dimensions>& range2) const
95 return !(*this == range2);
96}
97
98template <int num_dimensions>
99bool
101{
102 switch (is_regular_range)
103 {
104 case regular_true:
105 return true;
106 case regular_false:
107 return false;
108 case regular_to_do: {
111 return get_regular_range(min, max);
112 }
113 }
114 // although we never get here, VC insists on a return value...
115 // we check anyway
116 assert(false);
117 return true;
118}
119
120/***************************************
121 1D version
122 ***************************************/
123
125 : min(0),
126 max(0)
127{}
128
129IndexRange<1>::IndexRange(const int min_v, const int max_v)
130 : min(min_v),
131 max(max_v)
132{}
133
135 : min(min_v[1]),
136 max(max_v[1])
137{}
138
139IndexRange<1>::IndexRange(const int length)
140 : min(0),
141 max(length - 1)
142{}
143
145 : min(0),
146 max(size[1] - 1)
147{}
148
149int
151{
152 return min;
153}
154
155int
157{
158 return max;
159}
160
161int
163{
164 return max - min + 1;
165}
166
167std::size_t
169{
170 return std::size_t(this->get_length());
171}
172
173bool
174IndexRange<1>::operator==(const IndexRange<1>& range2) const
175{
176 return get_min_index() == range2.get_min_index() && get_length() == range2.get_length();
177}
178
179bool
181{
182 // 1D case: always true
183 return true;
184}
185
186bool
188{
189 // somewhat complicated as we can't assign ints to BasicCoordinate<1,int>
191 tmp[1] = min;
192 min_v = tmp;
193 tmp[1] = max;
194 max_v = tmp;
195 return true;
196}
197
198void
199IndexRange<1>::resize(const int min_index, const int max_index)
200{
201 min = min_index;
202 max = max_index;
203}
204END_NAMESPACE_STIR
class BasicCoordinate<int num_dimensions, typename coordT> defines num_dimensions -dimensional coordi...
Definition BasicCoordinate.h:57
This class defines ranges which can be 'irregular'.
Definition IndexRange.h:69
size_t size_all() const
return the total number of elements in this range
Definition IndexRange.inl:71
bool is_regular() const
checks if the range is 'regular'
Definition IndexRange.inl:100
bool get_regular_range(BasicCoordinate< num_dimensions, int > &min, BasicCoordinate< num_dimensions, int > &max) const
bool operator==(const IndexRange< num_dimensions > &) const
comparison operator
Definition IndexRange.inl:85
IndexRange()
Empty range.
Definition IndexRange.inl:33
virtual void resize(const int min_index, const int max_index)
void fill(const IndexRange< num_dimensions - 1 > &n)
IndexRange< num_dimensions - 1 > * num
Definition VectorWithOffset.h:370
BasicCoordinate< num_dimensions - 1, coordT > cut_first_dimension(const BasicCoordinate< num_dimensions, coordT > &c)
make a shorter BasicCoordinate, by cutting the first element from c
Definition BasicCoordinate.inl:477