casacore
Loading...
Searching...
No Matches
Copy.h
Go to the documentation of this file.
1//# Copy.h: Copy objects from one C-style array to another.
2//# Copyright (C) 1994-1997,1999-2002,2005,2015
3//# Associated Universities, Inc. Washington DC, USA.
4//# National Astronomical Observatory of Japan
5//# 2-21-1, Osawa, Mitaka, Tokyo, 181-8588, Japan.
6//#
7//# This library is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU Library General Public License as published by
9//# the Free Software Foundation; either version 2 of the License, or (at your
10//# option) any later version.
11//#
12//# This library is distributed in the hope that it will be useful, but WITHOUT
13//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15//# License for more details.
16//#
17//# You should have received a copy of the GNU Library General Public License
18//# along with this library; if not, write to the Free Software Foundation,
19//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning AIPS++ should be addressed as follows:
22//# Internet email: casa-feedback@nrao.edu.
23//# Postal address: AIPS++ Project Office
24//# National Radio Astronomy Observatory
25//# 520 Edgemont Road
26//# Charlottesville, VA 22903-2475 USA
27
28#ifndef CASA_COPY_H
29#define CASA_COPY_H
30
31//# Includes
32#include <casacore/casa/aips.h>
33#include <casacore/casa/BasicSL/Complex.h>
34#include <algorithm>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38 // <summary>
39 // Copy objects from one C-style array to another.
40 // </summary>
41
42 // <use visibility=export>
43 // <reviewed reviewer="Friso Olnon" date="1995/03/14" tests="tCopy" demos="">
44 // </reviewed>
45
46 // <synopsis>
47 // Objset is used to fill a C-style array of objects.
48 //
49 // Objcopy and objmove are used to copy objects from one place to
50 // another. Optionally a stride can be supplied.
51 //
52 // The functions are equivalent to C's <src>memcpy</src> and
53 // <src>memmove</src>.
54 // When possible C++ standard library functions are used to implement them
55 //
56 // Similar to memcpy and memmove, the difference between objcopy
57 // and objmove is that objmove takes account of an overlap of source and
58 // destination. In general, objcopy is slighty (but only slighty) faster.
59 // </synopsis>
60
61 // <example>
62 // Setting and copying arrays of built-in types:
63 // <srcblock>
64 // // Create int array of 4 elements
65 // size_t size=4;
66 // int* ia = new int[size];
67 // // Initialize all elements to value 99
68 // objset(ia, 99, size);
69 // // Change all odd elements to 66 -> [99 66 99 66]
70 // objset(ia+1, 66, 5, 2);
71 //
72 // // Create another 4-element int array
73 // int* ia2 = new int[size];
74 // // Copy array ia into array ia2 -> [99 66 99 66]
75 // objmove(ia2, ia, size);
76 // // Copy the even elements of ia to the odd elements of ia2
77 // // -> [99 99 99 99]
78 // objcopy(ia2+1, ia, size/2, 2, 2);
79 // </srcblock>
80 //
81 // Setting and copying arrays of a randomly chosen type:
82 // <srcblock>
83 // // Create 4-element array of 3-element Block<int> objects
84 // size_t size=4;
85 // Block<int>* ta = new Block<int>[size];
86 // Block<int> set(3);
87 // // Initialize the array -> [[123][123][123][123]]
88 // set[0] = 1; set[1] = 2; set[2] = 3;
89 // objset(ta, set, size);
90 // // Change odd Blocks to [777]-> [[123][777][123][777]]
91 // set[0] = set[1] = set[2] = 7;
92 // objset(ta + 1, set, size/2, 2);
93 //
94 // // Create another Block<int> array
95 // Block<int>* ta2 = new Block<int>[size];
96 // // Copy the even elements of ta to the first elements of ta2
97 // // -> [[123][123]...]
98 // objcopy(ta2, ta, size/2, 1, 2);
99 // </srcblock>
100 // </example>
101
102 // <group name=throw>
103 // Throw the various AipsErrors when incorrect arguments used
104 void objthrowmv1(const void *to, const void *from, const size_t n);
105 void objthrowmv2(const void *to, const void *from, const size_t n,
106 const size_t toStride, const size_t fromStride);
107 void objthrowcp1(const void *to, const void *from, const size_t n);
108 void objthrowcp2(const void *to, const void *from, const size_t n,
109 const size_t toStride, const size_t fromStride);
110 void objthrowfl1(const void *to, const size_t n);
111 void objthrowfl2(const void *to, const size_t n,
112 const size_t toStride);
113 // </group>
114
115 // <summary> Test routines </summary>
116 // <group name=test>
117 // Test on how to handle the overlap in move
118 void objtestmv(size_t &nLeft, size_t &startLeft, size_t &startRight,
119 const void *to, const void *from, const size_t n,
120 const size_t toStride, const size_t fromStride,
121 const void *toPn, const void *fromPn,
122 const size_t fromMto, const size_t toMfrom);
123 // </group>
124
125 // <summary> Copy methods </summary>
126 // <group name=copy>
127 // The general function to copy <src>n</src> objects from one place
128 // to another if overlap between <src>to</src> and <src>from</src> fields
129 // is possible. Strides may be specified, i.e. you may copy from every
130 // <src>fromStride</src>-th position into every <src>toStride</src>-th
131 // one.
132 //
133 // The function will call <src>std::copy()</src> when possible.
134 // Objmove works correctly if the source and destination overlap in any way.
135 //
136 // An exception will be thrown if the source or the destination does not
137 // exist (and <em>n</em> is non-zero) or if the strides are non-positive.
138 // <thrown>
139 // <li> AipsError
140 // </thrown>
141 //
142 // <group>
143 template<class T> void objmove(T* to, const T* from, size_t n) {
144 objthrowmv1(to,from,n);
145 (to<from || to >= from+n) ? std::copy(from,from+n,to)
146 : std::copy_backward(from,from+n,to+n); }
147 template<class T> void objmove(T* to, const T* from, size_t n,
148 size_t toStride, size_t fromStride) {
149 if (!n) return;
150 objthrowmv2(to,from,n,toStride,fromStride);
151 if (toStride*fromStride == 1) { objmove(to, from, n);
152 return; }
153 size_t nLeft, startLeft, startRight;
154 size_t fromMto=0; size_t toMfrom=0;
155 if (toStride > fromStride && from > to)
156 fromMto = (from-to)/(toStride-fromStride);
157 else if (toStride < fromStride && from < to)
158 toMfrom = (to-from)/(fromStride-toStride);
159 objtestmv(nLeft, startLeft, startRight, to, from, n,
160 toStride, fromStride, to+n*toStride, from+n*fromStride,
161 fromMto, toMfrom);
162 n -= nLeft;
163 if (nLeft) {
164 const T* fromPtr = from + startLeft*fromStride;
165 T* toPtr = to + startLeft*toStride;
166 while (nLeft--) { *toPtr = *fromPtr;
167 fromPtr += fromStride;
168 toPtr += toStride; }; };
169 // Do the moves from the right.
170 if (n) {
171 const T* fromPtr = from + startRight*fromStride;
172 T* toPtr = to + startRight*toStride;
173 while (n--) { fromPtr -= fromStride; toPtr -= toStride;
174 *toPtr = *fromPtr; }; };
175 }
176 // </group>
177
178 // The non-general function to copy <src>n</src> objects from one place
179 // to another. Strides may be specified, i.e. you may copy from every
180 // <src>fromStride</src>-th position into every <src>toStride</src>-th
181 // one.
182 //
183 // Objcopy/objcopyctor does not take an overlap of source and destination into account.
184 // Objmove should be used if that is an issue.
185 //
186 // Objcopyctor copy objects from <src>from</src> by calling copy constructor
187 // on each element in <src>to</src>.
188 //
189 // An exception will be thrown if the source or the destination does not
190 // exist or if the strides are non-positive.
191 // <thrown>
192 // <li> AipsError
193 // </thrown>
194 //
195 // <group>
196 template<class T> void objcopy(T* to, const T* from, size_t n) {
197 objthrowcp1(to,from,n); std::copy(from, from+n, to); }
198 template<class T> void objcopy(T* to, const T* from, size_t n,
199 size_t toStride, size_t fromStride) {
200 objthrowcp2(to,from,n,toStride,fromStride); while (n--) {
201 *to = *from; to += toStride; from += fromStride; } }
202
203 template<class T> void objcopyctor(T* to, const T* from, size_t n) {
204 objthrowcp1(to, from, n);
205 size_t i;
206 try {
207 for (i = 0; i < n; ++i) {
208 ::new (&to[i]) T(from[i]);
209 }
210 } catch (...) {
211 while (i > 0) { // roll back
212 to[--i].~T();
213 }
214 throw;
215 }
216 }
217 template<class T> void objcopyctor(T* to, const T* from, size_t n,
218 size_t toStride, size_t fromStride) {
219 objthrowcp2(to, from, n, toStride, fromStride);
220 size_t i = 0;
221 try {
222 for (i = 0; i < n; ++i) {
223 ::new (to) T(*from);
224 to += toStride;
225 from += fromStride;
226 }
227 } catch (...) {
228 while (i > 0) { // roll back
229 --i;
230 to -= toStride;
231 to->~T();
232 }
233 throw;
234 }
235 }
236 // </group>
237
238 // Fill <src>n</src> elements of an array of objects with the given
239 // value, optionally with a stride. Note that the fillValue is passed
240 // by value.
241 //
242 // An exception will be thrown if the destination array does not exist
243 // or if the stride is non-positive.
244 //
245 // <thrown>
246 // <li> AipsError
247 // </thrown>
248 //
249 // <group>
250 template<class T> void objset(T* to, const T fillValue, size_t n) {
251 objthrowfl1(to,n); std::fill_n(to, n, fillValue); }
252
253 template<class T> void objset(T* to, const T fillValue, size_t n,
254 size_t toStride) {
255 objthrowfl2(to,n,toStride);
256 while (n--){*to = fillValue; to += toStride; }; }
257
258 // </group>
259
260 // </group>
261
262} //# NAMESPACE CASACORE - END
263
264#ifndef CASACORE_NO_AUTO_TEMPLATES
265#include <casacore/casa/Utilities/Copy.tcc>
266#endif //# CASACORE_NO_AUTO_TEMPLATES
267#endif
this file contains all the compiler specific defines
Definition mainpage.dox:28
void objcopyctor(T *to, const T *from, size_t n)
Definition Copy.h:203
void objcopy(T *to, const T *from, size_t n)
The non-general function to copy n objects from one place to another.
Definition Copy.h:196
void objset(T *to, const T fillValue, size_t n, size_t toStride)
Definition Copy.h:253
void objcopyctor(T *to, const T *from, size_t n, size_t toStride, size_t fromStride)
Definition Copy.h:217
void objmove(T *to, const T *from, size_t n)
The general function to copy n objects from one place to another if overlap between to and from field...
Definition Copy.h:143
void objset(T *to, const T fillValue, size_t n)
Fill n elements of an array of objects with the given value, optionally with a stride.
Definition Copy.h:250
void objcopy(T *to, const T *from, size_t n, size_t toStride, size_t fromStride)
Definition Copy.h:198
void objmove(T *to, const T *from, size_t n, size_t toStride, size_t fromStride)
Definition Copy.h:147
void objtestmv(size_t &nLeft, size_t &startLeft, size_t &startRight, const void *to, const void *from, const size_t n, const size_t toStride, const size_t fromStride, const void *toPn, const void *fromPn, const size_t fromMto, const size_t toMfrom)
Test on how to handle the overlap in move.
void objthrowmv1(const void *to, const void *from, const size_t n)
Throw the various AipsErrors when incorrect arguments used.
void objthrowcp1(const void *to, const void *from, const size_t n)
void objthrowfl1(const void *to, const size_t n)
void objthrowmv2(const void *to, const void *from, const size_t n, const size_t toStride, const size_t fromStride)
void objthrowfl2(const void *to, const size_t n, const size_t toStride)
void objthrowcp2(const void *to, const void *from, const size_t n, const size_t toStride, const size_t fromStride)