source: issm/branches/trunk-larour-SLPS2022/src/c/classes/Inputs/ArrayInput.cpp@ 27223

Last change on this file since 27223 was 27223, checked in by Eric.Larour, 3 years ago

CHG: fixed large leak!

File size: 3.7 KB
Line 
1/*!\file ArrayInput.c
2 * \brief: implementation of the ArrayInput object
3 */
4
5#ifdef HAVE_CONFIG_H
6 #include <config.h>
7#else
8#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
9#endif
10
11#include "../classes.h"
12#include "../../shared/shared.h"
13#include "./ArrayInput.h"
14
15/*ArrayInput constructors and destructor*/
16ArrayInput::ArrayInput(void){/*{{{*/
17
18 this->numberofelements_local = -1;
19 this->N = NULL;
20 this->values = NULL;
21
22}/*}}}*/
23ArrayInput::ArrayInput(int nbe_in){/*{{{*/
24
25 _assert_(nbe_in>0);
26 _assert_(nbe_in<1e11);
27 this->numberofelements_local = nbe_in;
28 this->N = xNewZeroInit<int>(this->numberofelements_local);
29 this->values = xNewZeroInit<IssmDouble*>(this->numberofelements_local);
30
31}/*}}}*/
32ArrayInput::~ArrayInput(){/*{{{*/
33 if(this->values){
34 for(int i=0;i<this->numberofelements_local;i++) if(this->values[i]) xDelete<IssmDouble>(this->values[i]);
35 xDelete<IssmDouble>(this->values);
36 }
37 if(this->N) xDelete<int>(this->N);
38}
39/*}}}*/
40
41/*Object virtual functions definitions:*/
42Input* ArrayInput::copy() {/*{{{*/
43
44 ArrayInput* output = new ArrayInput(this->numberofelements_local);
45
46 //not needed, already allocated
47 //output->N = xNew<int>(this->numberofelements_local);
48 xMemCpy<int>(output->N,this->N,this->numberofelements_local);
49
50 //not needed, already allocated
51 //output->values = xNew<IssmDouble*>(this->numberofelements_local);
52 for(int i=0;i<this->numberofelements_local;i++){
53 if(this->values[i]){
54 _assert_(this->N[i]>0);
55 output->values[i] = xNew<IssmDouble>(this->N[i]);
56 xMemCpy<IssmDouble>(output->values[i],this->values[i],this->N[i]);
57 }
58 else{
59 output->values[i] = NULL;
60 }
61 }
62
63 return output;
64}
65/*}}}*/
66void ArrayInput::DeepEcho(void){/*{{{*/
67 _printf_("ArrayInput Echo:\n");
68 ///_printf_(" Size: "<<N<<"\n");
69 //printarray(this->values,this->M,this->N);
70 //_printf_(setw(15)<<" ArrayInput "<<setw(25)<<left<<EnumToStringx(this->enum_type)<<" "<<(value?"true":"false") << "\n");
71}
72/*}}}*/
73void ArrayInput::Echo(void){/*{{{*/
74 this->DeepEcho();
75}
76/*}}}*/
77int ArrayInput::Id(void){/*{{{*/
78 return -1;
79}/*}}}*/
80void ArrayInput::Marshall(MarshallHandle* marshallhandle){ /*{{{*/
81
82 int object_enum = ArrayInputEnum;
83 marshallhandle->call(object_enum);
84 marshallhandle->call(this->numberofelements_local);
85 if(this->numberofelements_local){
86 marshallhandle->call(this->N,this->numberofelements_local);
87 for(int i=0;i<this->numberofelements_local;i++){
88 if(this->values[i]){
89 marshallhandle->call(this->values[i],this->N[i]);
90 }
91 }
92 }
93 else{
94 this->N = NULL;
95 this->values = NULL;
96 }
97
98}
99/*}}}*/
100int ArrayInput::ObjectEnum(void){/*{{{*/
101 return ArrayInputEnum;
102}
103/*}}}*/
104
105/*ArrayInput management*/
106void ArrayInput::SetInput(int row,int numindices,IssmDouble* values_in){/*{{{*/
107
108 _assert_(this);
109 _assert_(row>=0 && row<this->numberofelements_local);
110
111 if(this->N[row] != numindices){
112 if(this->values[row]) xDelete<IssmDouble>(this->values[row]);
113 this->values[row] = xNew<IssmDouble>(numindices);
114 }
115
116 IssmDouble *el_values = this->values[row];
117 for(int i=0;i<numindices;i++) el_values[i] = values_in[i];
118
119 this->N[row] = numindices;
120}
121/*}}}*/
122void ArrayInput::GetArray(int row,IssmDouble** pvalues,int* pN){/*{{{*/
123
124 _assert_(this);
125 _assert_(row>=0 && row<this->numberofelements_local);
126 if(pvalues){
127 IssmDouble* outvalues = xNew<IssmDouble>(this->N[row]);
128 xMemCpy<IssmDouble>(outvalues,this->values[row],this->N[row]);
129 *pvalues = outvalues;
130 }
131 if(pN){
132 *pN = this->N[row];
133 }
134}
135/*}}}*/
136void ArrayInput::GetArrayPtr(int row,IssmDouble** pvalues,int* pN){/*{{{*/
137
138 _assert_(this);
139 _assert_(row>=0 && row<this->numberofelements_local);
140 if(pvalues){
141 *pvalues = this->values[row];
142 }
143 if(pN){
144 *pN = this->N[row];
145 }
146}
147/*}}}*/
Note: See TracBrowser for help on using the repository browser.