casa
$Rev:20696$
|
00001 //# Copy.h: Copy objects from one C-style array to another. 00002 //# Copyright (C) 1994-1997,1999-2002,2005 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: Copy.h 20739 2009-09-29 01:15:15Z Malte.Marquarding $ 00027 00028 #ifndef CASA_COPY_H 00029 #define CASA_COPY_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 #include <casa/BasicSL/Complex.h> 00034 #include <algorithm> 00035 00036 namespace casa { //# NAMESPACE CASA - BEGIN 00037 00038 // <summary> 00039 // Copy objects from one C-style array to another. 00040 // </summary> 00041 00042 // <use visibility=export> 00043 // <reviewed reviewer="Friso Olnon" date="1995/03/14" tests="tCopy" demos=""> 00044 // </reviewed> 00045 00046 // <synopsis> 00047 // Objset is used to fill a C-style array of objects. 00048 // 00049 // Objcopy and objmove are used to copy objects from one place to 00050 // another. Optionally a stride can be supplied. 00051 // 00052 // The functions are equivalent to C's <src>memcpy</src> and 00053 // <src>memmove</src>. 00054 // When possible C++ standard library functions are used to implement them 00055 // 00056 // Similar to memcpy and memmove, the difference between objcopy 00057 // and objmove is that objmove takes account of an overlap of source and 00058 // destination. In general, objcopy is slighty (but only slighty) faster. 00059 // </synopsis> 00060 00061 // <example> 00062 // Setting and copying arrays of built-in types: 00063 // <srcblock> 00064 // // Create int array of 4 elements 00065 // size_t size=4; 00066 // int* ia = new int[size]; 00067 // // Initialize all elements to value 99 00068 // objset(ia, 99, size); 00069 // // Change all odd elements to 66 -> [99 66 99 66] 00070 // objset(ia+1, 66, 5, 2); 00071 // 00072 // // Create another 4-element int array 00073 // int* ia2 = new int[size]; 00074 // // Copy array ia into array ia2 -> [99 66 99 66] 00075 // objmove(ia2, ia, size); 00076 // // Copy the even elements of ia to the odd elements of ia2 00077 // // -> [99 99 99 99] 00078 // objcopy(ia2+1, ia, size/2, 2, 2); 00079 // </srcblock> 00080 // 00081 // Setting and copying arrays of a randomly chosen type: 00082 // <srcblock> 00083 // // Create 4-element array of 3-element Block<int> objects 00084 // size_t size=4; 00085 // Block<int>* ta = new Block<int>[size]; 00086 // Block<int> set(3); 00087 // // Initialize the array -> [[123][123][123][123]] 00088 // set[0] = 1; set[1] = 2; set[2] = 3; 00089 // objset(ta, set, size); 00090 // // Change odd Blocks to [777]-> [[123][777][123][777]] 00091 // set[0] = set[1] = set[2] = 7; 00092 // objset(ta + 1, set, size/2, 2); 00093 // 00094 // // Create another Block<int> array 00095 // Block<int>* ta2 = new Block<int>[size]; 00096 // // Copy the even elements of ta to the first elements of ta2 00097 // // -> [[123][123]...] 00098 // objcopy(ta2, ta, size/2, 1, 2); 00099 // </srcblock> 00100 // </example> 00101 00102 // <group name=throw> 00103 // Throw the various AipsErrors when incorrect arguments used 00104 void objthrowmv1(const void *to, const void *from, const size_t n); 00105 void objthrowmv2(const void *to, const void *from, const size_t n, 00106 const size_t toStride, const size_t fromStride); 00107 void objthrowcp1(const void *to, const void *from, const size_t n); 00108 void objthrowcp2(const void *to, const void *from, const size_t n, 00109 const size_t toStride, const size_t fromStride); 00110 void objthrowfl1(const void *to, const size_t n); 00111 void objthrowfl2(const void *to, const size_t n, 00112 const size_t toStride); 00113 // </group> 00114 00115 // <summary> Test routines </summary> 00116 // <group name=test> 00117 // Test on how to handle the overlap in move 00118 void objtestmv(size_t &nLeft, size_t &startLeft, size_t &startRight, 00119 const void *to, const void *from, const size_t n, 00120 const size_t toStride, const size_t fromStride, 00121 const void *toPn, const void *fromPn, 00122 const size_t fromMto, const size_t toMfrom); 00123 // </group> 00124 00125 // <summary> Copy methods </summary> 00126 // <group name=copy> 00127 // The general function to copy <src>n</src> objects from one place 00128 // to another if overlap between <src>to</src> and <src>from</src> fields 00129 // is possible. Strides may be specified, i.e. you may copy from every 00130 // <src>fromStride</src>-th position into every <src>toStride</src>-th 00131 // one. 00132 // 00133 // The function will call <src>std::copy()</src> when possible. 00134 // Objmove works correctly if the source and destination overlap in any way. 00135 // 00136 // An exception will be thrown if the source or the destination does not 00137 // exist (and <em>n</em> is non-zero) or if the strides are non-positive. 00138 // <thrown> 00139 // <li> AipsError 00140 // </thrown> 00141 // 00142 // <group> 00143 template<class T> void objmove(T* to, const T* from, size_t n) { 00144 objthrowmv1(to,from,n); 00145 (to<from || to >= from+n) ? std::copy(from,from+n,to) 00146 : std::copy_backward(from,from+n,to+n); } 00147 template<class T> void objmove(T* to, const T* from, size_t n, 00148 size_t toStride, size_t fromStride) { 00149 if (!n) return; 00150 objthrowmv2(to,from,n,toStride,fromStride); 00151 if (toStride*fromStride == 1) { objmove(to, from, n); 00152 return; } 00153 size_t nLeft, startLeft, startRight; 00154 size_t fromMto=0; size_t toMfrom=0; 00155 if (toStride > fromStride && from > to) 00156 fromMto = (from-to)/(toStride-fromStride); 00157 else if (toStride < fromStride && from < to) 00158 toMfrom = (to-from)/(fromStride-toStride); 00159 objtestmv(nLeft, startLeft, startRight, to, from, n, 00160 toStride, fromStride, to+n*toStride, from+n*fromStride, 00161 fromMto, toMfrom); 00162 n -= nLeft; 00163 if (nLeft) { 00164 const T* fromPtr = from + startLeft*fromStride; 00165 T* toPtr = to + startLeft*toStride; 00166 while (nLeft--) { *toPtr = *fromPtr; 00167 fromPtr += fromStride; 00168 toPtr += toStride; }; }; 00169 // Do the moves from the right. 00170 if (n) { 00171 const T* fromPtr = from + startRight*fromStride; 00172 T* toPtr = to + startRight*toStride; 00173 while (n--) { fromPtr -= fromStride; toPtr -= toStride; 00174 *toPtr = *fromPtr; }; }; 00175 } 00176 // </group> 00177 00178 //# To support a container of const void*. 00179 //# I think that nowadays handled ok 00184 00185 //# To support a container of void*. 00190 00191 // The non-general function to copy <src>n</src> objects from one place 00192 // to another. Strides may be specified, i.e. you may copy from every 00193 // <src>fromStride</src>-th position into every <src>toStride</src>-th 00194 // one. 00195 // 00196 // Objcopy does not take an overlap of source and destination into account. 00197 // Objmove should be used if that is an issue. 00198 // 00199 // An exception will be thrown if the source or the destination does not 00200 // exist or if the strides are non-positive. 00201 // <thrown> 00202 // <li> AipsError 00203 // </thrown> 00204 // 00205 // <group> 00206 template<class T> void objcopy(T* to, const T* from, size_t n) { 00207 objthrowcp1(to,from,n); std::copy(from, from+n, to); } 00208 template<class T> void objcopy(T* to, const T* from, size_t n, 00209 size_t toStride, size_t fromStride) { 00210 objthrowcp2(to,from,n,toStride,fromStride); while (n--) { 00211 *to = *from; to += toStride; from += fromStride; } } 00212 // </group> 00213 00214 //# To support a container of const void*. 00215 //# Not necessary I think wnb 00220 00221 //# To support a container of void*. 00226 00227 // Fill <src>n</src> elements of an array of objects with the given 00228 // value, optionally with a stride. Note that the fillValue is passed 00229 // by value. 00230 // 00231 // An exception will be thrown if the destination array does not exist 00232 // or if the stride is non-positive. 00233 // 00234 // <thrown> 00235 // <li> AipsError 00236 // </thrown> 00237 // 00238 // <group> 00239 template<class T> void objset(T* to, const T fillValue, size_t n) { 00240 objthrowfl1(to,n); std::fill_n(to, n, fillValue); } 00241 00242 template<class T> void objset(T* to, const T fillValue, size_t n, 00243 size_t toStride) { 00244 objthrowfl2(to,n,toStride); 00245 while (n--){*to = fillValue; to += toStride; }; } 00246 00247 // </group> 00248 00249 // </group> 00250 00251 } //# NAMESPACE CASA - END 00252 00253 #ifndef CASACORE_NO_AUTO_TEMPLATES 00254 #include <casa/Utilities/Copy.tcc> 00255 #endif //# CASACORE_NO_AUTO_TEMPLATES 00256 #endif