1 /* gsl_histogram2d_copy.c
2  * Copyright (C) 2000  Simone Piccardi
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 /***************************************************************
19  *
20  * File gsl_histogram2d_copy.c:
21  * Routine to copy a 2D histogram.
22  * Need GSL library and header.
23  *
24  * Author: S. Piccardi
25  * Jan. 2000
26  *
27  ***************************************************************/
28 #include <config.h>
29 #include <stdlib.h>
30 #include <gsl/gsl_errno.h>
31 #include <gsl/gsl_histogram2d.h>
32 
33 /*
34  * gsl_histogram2d_copy:
35  * copy the contents of an histogram into another
36  */
37 int
gsl_histogram2d_memcpy(gsl_histogram2d * dest,const gsl_histogram2d * src)38 gsl_histogram2d_memcpy (gsl_histogram2d * dest, const gsl_histogram2d * src)
39 {
40   size_t nx = src->nx;
41   size_t ny = src->ny;
42   size_t i;
43   if (dest->nx != src->nx || dest->ny != src->ny)
44     {
45       GSL_ERROR ("histograms have different sizes, cannot copy",
46                  GSL_EINVAL);
47     }
48 
49   for (i = 0; i <= nx; i++)
50     {
51       dest->xrange[i] = src->xrange[i];
52     }
53 
54   for (i = 0; i <= ny; i++)
55     {
56       dest->yrange[i] = src->yrange[i];
57     }
58 
59   for (i = 0; i < nx * ny; i++)
60     {
61       dest->bin[i] = src->bin[i];
62     }
63 
64   return GSL_SUCCESS;
65 }
66 
67 /*
68  * gsl_histogram2d_duplicate:
69  * duplicate an histogram creating
70  * an identical new one
71  */
72 
73 gsl_histogram2d *
gsl_histogram2d_clone(const gsl_histogram2d * src)74 gsl_histogram2d_clone (const gsl_histogram2d * src)
75 {
76   size_t nx = src->nx;
77   size_t ny = src->ny;
78   size_t i;
79   gsl_histogram2d *h;
80 
81   h = gsl_histogram2d_calloc_range (nx, ny, src->xrange, src->yrange);
82 
83   if (h == 0)
84     {
85       GSL_ERROR_VAL ("failed to allocate space for histogram struct",
86                         GSL_ENOMEM, 0);
87     }
88 
89   for (i = 0; i < nx * ny; i++)
90     {
91       h->bin[i] = src->bin[i];
92     }
93 
94   return h;
95 }
96