STIR 6.4.0
array_index_functions.inl
Go to the documentation of this file.
1//
2//
3/*
4 Copyright (C) 2004- 2008, Hammersmith Imanet Ltd
5 This file is part of STIR.
6
7 SPDX-License-Identifier: Apache-2.0
8
9 See STIR/LICENSE.txt for details
10*/
20/*
21 Copyright (C) 2004- 2008, Hammersmith Imanet Ltd
22 See STIR/LICENSE.txt for details
23*/
25#include <algorithm>
26
27START_NAMESPACE_STIR
28
29/* First we define the functions that actually do the work.
30
31 The code is a bit more complicated than need be because we try to accomodate
32 older compilers that have trouble with function overloading of templates.
33 See test_if_1d for some info.
34*/
35namespace detail
36{
37
38template <int num_dimensions, typename T>
39inline BasicCoordinate<num_dimensions, int>
40get_min_indices_help(is_not_1d, const Array<num_dimensions, T>& a)
41{
42 if (a.get_min_index() <= a.get_max_index())
43 return join(a.get_min_index(), get_min_indices(*a.begin()));
44 else
45 {
46 // a is empty. Not clear what to return, so we just return 0
47 // It would be better to throw an exception.
48 BasicCoordinate<num_dimensions, int> tmp(0);
49 return tmp;
50 }
51}
52
53template <typename T>
54inline BasicCoordinate<1, int>
55get_min_indices_help(is_1d, const Array<1, T>& a)
56{
57 BasicCoordinate<1, int> result;
58 result[1] = a.get_min_index();
59 return result;
60}
61
62template <int num_dimensions2, typename T>
63inline bool
64next_help(is_1d, BasicCoordinate<1, int>& index, const Array<num_dimensions2, T>& a)
65{
66 if (a.get_min_index() > a.get_max_index())
67 return false;
68 assert(index[1] >= a.get_min_index());
69 assert(index[1] <= a.get_max_index());
70 index[1]++;
71 return index[1] <= a.get_max_index();
72}
73
74template <typename T, int num_dimensions, int num_dimensions2>
75inline bool
76next_help(is_not_1d, BasicCoordinate<num_dimensions, int>& index, const Array<num_dimensions2, T>& a)
77{
78 if (a.get_min_index() > a.get_max_index())
79 return false;
80 BasicCoordinate<num_dimensions - 1, int> upper_index = cut_last_dimension(index);
81 assert(index[num_dimensions] >= get(a, upper_index).get_min_index());
82 assert(index[num_dimensions] <= get(a, upper_index).get_max_index());
83 index[num_dimensions]++;
84 if (index[num_dimensions] <= get(a, upper_index).get_max_index())
85 return true;
86 if (!next(upper_index, a))
87 return false;
88 index = join(upper_index, get(a, upper_index).get_min_index());
89 return true;
90}
91
92} // end of namespace detail
93
94/* Now define the functions in the stir namespace in terms of the above.
95 Also define get() for which I didn't bother to try the work-arounds,
96 as they don't work for VC 6.0 anyway...
97*/
98template <int num_dimensions, typename T>
99inline BasicCoordinate<num_dimensions, int>
101{
102 return detail::get_min_indices_help(detail::test_if_1d<num_dimensions>(), a);
103}
104
105template <int num_dimensions, typename T, int num_dimensions2>
106inline bool
108{
109 return detail::next_help(detail::test_if_1d<num_dimensions>(), index, a);
110}
111
112template <int num_dimensions, int num_dimensions2, typename elemT>
113inline const Array<num_dimensions - num_dimensions2, elemT>&
118
119template <int num_dimensions, typename elemT>
120inline const elemT&
122{
123 return a[c];
124}
125template <int num_dimensions, typename elemT>
126inline const Array<num_dimensions - 1, elemT>&
127get(const Array<num_dimensions, elemT>& a, const BasicCoordinate<1, int>& c)
128{
129 return a[c[1]];
130}
131
132END_NAMESPACE_STIR
This class defines multi-dimensional (numeric) arrays.
Definition Array.h:78
class BasicCoordinate<int num_dimensions, typename coordT> defines num_dimensions -dimensional coordi...
Definition BasicCoordinate.h:57
BasicCoordinate< num_dimensions+1, coordT > join(const coordT &a, const BasicCoordinate< num_dimensions, coordT > &c)
make a longer BasicCoordinate, by prepending c with the single coordT
Definition BasicCoordinate.inl:447
BasicCoordinate< num_dimensions - 1, coordT > cut_last_dimension(const BasicCoordinate< num_dimensions, coordT > &c)
make a shorter BasicCoordinate, by cutting the last element from c
Definition BasicCoordinate.inl:458
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
BasicCoordinate< num_dimensions, int > get_min_indices(const Array< num_dimensions, T > &a)
Get the first multi-dimensional index of the array.
Definition array_index_functions.inl:100
const Array< num_dimensions - num_dimensions2, elemT > & get(const Array< num_dimensions, elemT > &a, const BasicCoordinate< num_dimensions2, int > &c)
an alternative for array indexing using BasicCoordinate objects
Definition array_index_functions.inl:114
bool next(BasicCoordinate< num_dimensions, int > &indices, const Array< num_dimensions2, T > &a)
Given an index into an array, increment it to the next one.
Definition array_index_functions.inl:107
a templated class used to check if it's a 1D array or not This class only exists to allow a work-arou...
Definition test_if_1d.h:71
Classes for use in implementation of stir::Array, stir::BasicCoordinate etc to test if it's a 1D arra...