//# ArrayUtil.cc: Utility functions for arrays (templated) //# Copyright (C) 1995,2001 //# Associated Universities, Inc. Washington DC, USA. //# //# This library is free software; you can redistribute it and/or modify it //# under the terms of the GNU Library General Public License as published by //# the Free Software Foundation; either version 2 of the License, or (at your //# option) any later version. //# //# This library is distributed in the hope that it will be useful, but WITHOUT //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public //# License for more details. //# //# You should have received a copy of the GNU Library General Public License //# along with this library; if not, write to the Free Software Foundation, //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning AIPS++ should be addressed as follows: //# Internet email: aips2-request@nrao.edu. //# Postal address: AIPS++ Project Office //# National Radio Astronomy Observatory //# 520 Edgemont Road //# Charlottesville, VA 22903-2475 USA //# //# $Id$ #ifndef CASA_ARRAYUTIL_2_TCC #define CASA_ARRAYUTIL_2_TCC #include "ArrayUtil.h" #include "ArrayError.h" #include namespace casacore { //# NAMESPACE CASACORE - BEGIN template Array concatenateArray (const Array& left, const Array& right) { if (left.nelements() == 0) { return right.copy(); } if (right.nelements() == 0) { return left.copy(); } IPosition shape = right.shape(); IPosition leftShape = left.shape(); std::size_t ndim = shape.nelements(); if (! shape.isEqual (leftShape, ndim-1)) { throw (ArrayConformanceError ("concatenateArray(left,right)")); } shape(ndim-1) += leftShape(ndim-1); Array result (shape); IPosition start(ndim, 0); result (start, leftShape-1).assign_conforming( left ); start(ndim-1) = leftShape(ndim-1); result (start, shape-1).assign_conforming( right ); return result; } template Array reorderArray (const Array& array, const IPosition& newAxisOrder, bool alwaysCopy) { const IPosition& shape = array.shape(); IPosition newShape, incr; size_t contAxes = reorderArrayHelper (newShape, incr, shape, newAxisOrder); // If not reordered, we can simply return the array (or a copy if needed). size_t ndim = shape.nelements(); if (contAxes == ndim) { if (alwaysCopy) { return array.copy(); } return array; } Array result(newShape); bool deleteData, deleteRes; const T* arrData = array.getStorage (deleteData); const T* data = arrData; T* resData = result.getStorage (deleteRes); T* res = resData; // Find out the nr of contiguous elements. size_t nrcont = 1; if (contAxes == 0) { contAxes = 1; } else { for (size_t i=0; i 1) { std::copy_n(data, nrcont, res); data += nrcont; res += nrcont; } else { for (size_t i=0; i Array reverseArray (const Array& array, size_t axis, bool alwaysCopy) { const IPosition& shape = array.shape(); if (axis >= shape.size()) { throw ArrayError( std::string(__FUNCTION__) + ": axis number is higher than number of axes in the array" ); } bool nothingToDo = shape[axis] == 1; if (nothingToDo) { if (alwaysCopy) { return array.copy(); } return array; } bool deletein, deleteout; const T *indata = array.getStorage(deletein); Array result(shape); T *outdata = result.getStorage(deleteout); size_t outerProduct = 1; size_t innerProduct = 1; for (size_t i=0; iaxis) { outerProduct *= shape[i]; } } for (size_t j=0; j Array reverseArray (const Array& array, const IPosition& reversedAxes, bool alwaysCopy) { const IPosition& shape = array.shape(); bool nothingToDo = true; for (size_t i=0; i= int(shape.size())) { throw ArrayError(std::string(__FUNCTION__) + ": axis number " + std::to_string(reversedAxes[i]) + " is higher than number of axes in the array" ); } if (shape[reversedAxes[i]] > 1) { nothingToDo = false; break; } } if (nothingToDo) { if (alwaysCopy) { return array.copy(); } return array; } Array result = array.copy(); for (size_t i=0; i