| 1 |
/* vector/swap_source.c |
| 2 |
* |
| 3 |
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
int |
| 21 |
FUNCTION (gsl_vector, swap) (TYPE (gsl_vector) * v, TYPE (gsl_vector) * w) |
| 22 |
{ |
| 23 |
ATOMIC * d1 = v->data ; |
| 24 |
ATOMIC * d2 = w->data ; |
| 25 |
const size_t size = v->size ; |
| 26 |
const size_t s1 = MULTIPLICITY * v->stride ; |
| 27 |
const size_t s2 = MULTIPLICITY * w->stride ; |
| 28 |
size_t i, k ; |
| 29 |
|
| 30 |
if (v->size != w->size) |
| 31 |
{ |
| 32 |
GSL_ERROR("vector lengths must be equal", GSL_EINVAL); |
| 33 |
} |
| 34 |
|
| 35 |
for (i = 0; i < size; i++) |
| 36 |
{ |
| 37 |
for (k = 0; k < MULTIPLICITY; k++) |
| 38 |
{ |
| 39 |
ATOMIC tmp = d1[i*s1 + k]; |
| 40 |
d1[i*s1+k] = d2[i*s2 + k]; |
| 41 |
d2[i*s2+k] = tmp; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return GSL_SUCCESS; |
| 46 |
} |
| 47 |
|
| 48 |
int |
| 49 |
FUNCTION (gsl_vector, swap_elements) (TYPE (gsl_vector) * v, const size_t i, const size_t j) |
| 50 |
{ |
| 51 |
ATOMIC * data = v->data ; |
| 52 |
const size_t size = v->size ; |
| 53 |
const size_t stride = v->stride ; |
| 54 |
|
| 55 |
if (i >= size) |
| 56 |
{ |
| 57 |
GSL_ERROR("first index is out of range", GSL_EINVAL); |
| 58 |
} |
| 59 |
|
| 60 |
if (j >= size) |
| 61 |
{ |
| 62 |
GSL_ERROR("second index is out of range", GSL_EINVAL); |
| 63 |
} |
| 64 |
|
| 65 |
if (i != j) |
| 66 |
{ |
| 67 |
const size_t s = MULTIPLICITY * stride ; |
| 68 |
size_t k ; |
| 69 |
|
| 70 |
for (k = 0; k < MULTIPLICITY; k++) |
| 71 |
{ |
| 72 |
ATOMIC tmp = data[j*s + k]; |
| 73 |
data[j*s+k] = data[i*s + k]; |
| 74 |
data[i*s+k] = tmp; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return GSL_SUCCESS; |
| 79 |
} |
| 80 |
|
| 81 |
int |
| 82 |
FUNCTION (gsl_vector, reverse) (TYPE (gsl_vector) * v) |
| 83 |
{ |
| 84 |
ATOMIC * data = v->data ; |
| 85 |
const size_t size = v->size ; |
| 86 |
const size_t stride = v->stride ; |
| 87 |
|
| 88 |
const size_t s = MULTIPLICITY * stride ; |
| 89 |
|
| 90 |
size_t i ; |
| 91 |
|
| 92 |
for (i = 0 ; i < (size / 2) ; i++) |
| 93 |
{ |
| 94 |
size_t j = size - i - 1 ; |
| 95 |
size_t k; |
| 96 |
|
| 97 |
for (k = 0; k < MULTIPLICITY; k++) |
| 98 |
{ |
| 99 |
ATOMIC tmp = data[j*s + k]; |
| 100 |
data[j*s+k] = data[i*s + k]; |
| 101 |
data[i*s+k] = tmp; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return GSL_SUCCESS; |
| 106 |
} |
| 107 |
|