STIR  6.2.0
copy_fill.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020, 2024 University College London
3  This file is part of STIR.
4 
5  SPDX-License-Identifier: Apache-2.0
6 
7  See STIR/LICENSE.txt for details
8 */
9 #ifndef __stir_copy_fill__H__
10 #define __stir_copy_fill__H__
11 
19 #include "stir/Array.h"
20 #include "stir/ProjDataInMemory.h"
21 
22 START_NAMESPACE_STIR
31 
34 template <typename T>
35 struct CopyFill
36 {
37  template <typename iterT>
38  static iterT copy_to(const T& stir_object, iterT iter)
39  {
40  return std::copy(stir_object.begin_all(), stir_object.end_all(), iter);
41  }
42  template <typename iterT>
43  static void fill_from(T& stir_object, iterT iter, iterT iter_end)
44  {
45  std::copy(iter, iter_end, stir_object.begin_all());
46  }
47 };
48 
50 
52 template <int num_dimensions, class elemT>
53 struct CopyFill<Array<num_dimensions, elemT>>
54 {
56 
57  template <typename iterT>
58  static iterT copy_to(const Array_type& stir_array, iterT iter)
59  {
60  if (stir_array.is_contiguous())
61  {
62  // std::cerr << "Using 1D std::copy for copy_to\n";
63  auto beg = stir_array.get_const_full_data_ptr();
64  auto ret = std::copy(beg, beg + stir_array.size_all(), iter);
65  stir_array.release_const_full_data_ptr();
66  return ret;
67  }
68  else
69  {
70  // std::cerr<<"Using normal std::copy for copy_to\n";
71  return std::copy(stir_array.begin_all(), stir_array.end_all(), iter);
72  }
73  }
74 
75  template <typename iterT>
76  static void fill_from(Array_type& stir_array, iterT iter, iterT iter_end)
77  {
78  if (stir_array.is_contiguous())
79  {
80  // std::cerr << "Using 1D std::copy for fill_from\n";
81  std::copy(iter, iter_end, stir_array.get_full_data_ptr());
82  stir_array.release_full_data_ptr();
83  }
84  else
85  {
86  // std::cerr<<"Using normal std::copy for fill_from\n";
87  std::copy(iter, iter_end, stir_array.begin_all());
88  }
89  }
90 };
91 
93 
95 template <>
97 {
98  template <typename iterT>
99  static iterT copy_to(const ProjData& stir_object, iterT iter)
100  {
101 #if 1
102  if (auto pdm_ptr = dynamic_cast<ProjDataInMemory const*>(&stir_object))
103  {
104  // std::cerr<<"Using stir::copy_to\n";
105  return CopyFill<ProjDataInMemory>::copy_to(*pdm_ptr, iter);
106  }
107  else
108 #endif
109  {
110  // std::cerr<<"Using member copy_to\n";
111  return stir_object.copy_to(iter);
112  }
113  }
114 
115  template <typename iterT>
116  static void fill_from(ProjData& stir_object, iterT iter, iterT iter_end)
117  {
118  if (auto pdm_ptr = dynamic_cast<ProjDataInMemory*>(&stir_object))
119  CopyFill<ProjDataInMemory>::fill_from(*pdm_ptr, iter, iter_end);
120  else
121  stir_object.fill_from(iter);
122  }
123 };
124 
126 
131 template <typename T, typename iterT>
132 inline iterT
133 copy_to(const T& stir_object, iterT iter)
134 {
135  return CopyFill<T>::copy_to(stir_object, iter);
136 }
137 
139 
142 template <typename T, typename iterT>
143 inline void
144 fill_from(T& stir_object, iterT iter, iterT iter_end)
145 {
146  // return
147  CopyFill<T>::fill_from(stir_object, iter, iter_end);
148 }
149 
151 
152 END_NAMESPACE_STIR
153 
154 #endif
iterT fill_from(iterT array_iter)
set all bins from an array iterator
Definition: ProjData.h:281
full_iterator end_all()
end value for iterating through all elements in the array, see full_iterator
Definition: Array.inl:162
bool is_contiguous() const
return if the array is contiguous in memory
Definition: Array.inl:39
iterT copy_to(const T &stir_object, iterT iter)
Copy all bins to a range specified by a iterator.
Definition: copy_fill.h:133
Helper class for stir::copy_to and stir::fill_from.
Definition: copy_fill.h:35
elemT * get_full_data_ptr()
member function for access to the data via a elemT*
Definition: Array.inl:269
Declaration of class stir::ProjDataInMemory.
size_t size_all() const
return the total number of elements in this array
Definition: Array.inl:239
iterT copy_to(iterT array_iter) const
Copy all bins to a range specified by a (forward) iterator.
Definition: ProjData.h:314
defines the Array class for multi-dimensional (numeric) arrays
void fill_from(T &stir_object, iterT iter, iterT iter_end)
set all elements of stir_object from an iterator
Definition: copy_fill.h:144
full_iterator begin_all()
start value for iterating through all elements in the array, see full_iterator
Definition: Array.inl:190
void release_const_full_data_ptr() const
signal end of access to const elemT*
Definition: Array.inl:322
void release_full_data_ptr()
signal end of access to elemT*
Definition: Array.inl:306
This class defines multi-dimensional (numeric) arrays.
Definition: Array.h:73
const elemT * get_const_full_data_ptr() const
member function for access to the data via a const elemT*
Definition: Array.inl:288
The (abstract) base class for the projection data.
Definition: ProjData.h:103