KADATH
param.cpp
1 /*
2  Copyright 2017 Philippe Grandclement
3 
4  This file is part of Kadath.
5 
6  Kadath is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Kadath is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Kadath. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "param.hpp"
21 // Headers C
22 #include "headcpp.hpp"
23 #include <assert.h>
24 namespace Kadath {
25 
26  //------------------------//
27  // Constructor //
28  //------------------------//
29 
30 Param::Param() : n_int(0),
31  n_double(0)
32  {}
33 
34 
35  //----------------------//
36  // Destructor //
37  //----------------------//
38 
40 
41  if (n_int > 0) delete [] p_int ;
42  if (n_double > 0) delete [] p_double ;
43 }
44 
45 
46  //------------------------------------//
47  // int storage //
48  //------------------------------------//
49 
50 // Total number of stored int
51 // --------------------------
52 
53 int Param::get_n_int() const {
54  return n_int ;
55 }
56 
57 // Addition
58 // --------
59 
60 void Param::add_int(int ti, int index){
61 
62  if (index >= n_int) { // p_int must be rescaled
63  int n_int_nouveau = index + 1 ;
64  int* p_int_nouveau = new int[n_int_nouveau] ;
65 
66  for (int i=0 ; i<n_int ; i++)
67  p_int_nouveau[i] = p_int[i] ;
68  p_int_nouveau[index] = ti ;
69 
70  if (n_int!=0)
71  delete [] p_int ;
72  p_int = p_int_nouveau ;
73  n_int = n_int_nouveau ;
74  }
75  else {
76 
77  if (p_int[index] != 0) {
78  cout << "Param::add_int : the position " << index
79  << " is already occupied !" << endl ;
80  abort() ;
81  }
82  else{
83  p_int[index] = ti ;
84  }
85 
86  }
87 }
88 
89 // Extraction
90 // ----------
91 
92 const int& Param::get_int(int index) const {
93 
94  assert(index >= 0) ;
95  assert(index < n_int) ;
96 
97  return (p_int[index]) ;
98 
99 }
100 
101  //------------------------------------//
102  // double storage //
103  //------------------------------------//
104 
105 // Total number of stored doubles
106 // ------------------------------
107 
108 int Param::get_n_double() const {
109  return n_double ;
110 }
111 
112 // Addition
113 // --------
114 
115 void Param::add_double(double ti, int index){
116 
117  if (index >= n_double) { // p_double must be rescaled
118 
119 
120 
121  int n_double_nouveau = index + 1 ;
122  double* p_double_nouveau = new double[n_double_nouveau] ;
123 
124  for (int i=0 ; i<n_double ; i++)
125  p_double_nouveau[i] = p_double[i] ;
126  p_double_nouveau[index] = ti ;
127 
128  if (n_double!=0)
129  delete [] p_double ;
130  p_double = p_double_nouveau ;
131  n_double = n_double_nouveau ;
132  }
133  else {
134  if (p_double[index] != 0) {
135  cout << "Param::add_double : the position " << index
136  << " is already occupied !" << endl ;
137  abort() ;
138  }
139  else{
140  p_double[index] = ti ;
141  }
142  }
143 }
144 
145 // Extraction
146 // ----------
147 
148 const double& Param::get_double(int index) const {
149 
150  assert(index >= 0) ;
151  assert(index < n_double) ;
152 
153  return (p_double[index]) ;
154 
155 } }
int n_double
Number of double 's (double precis.
Definition: param.hpp:39
~Param()
Destructor.
Definition: param.cpp:39
Param()
Default constructor is the only constructor.
Definition: param.cpp:30
int get_n_double() const
Returns the number of stored double 's addresses.
Definition: param.cpp:108
int * p_int
Array (size n_int ) of the int 's addresses.
Definition: param.hpp:37
int n_int
Number of int 's (integers).
Definition: param.hpp:35
double * p_double
Array (size n_double ) of the double 's addresses.
Definition: param.hpp:41
void add_double(double x, int position=0)
Adds the the address of a new double to the list.
Definition: param.cpp:115
void add_int(int n, int position=0)
Adds the address of a new int to the list.
Definition: param.cpp:60
const double & get_double(int position=0) const
Returns the reference of a double stored in the list.
Definition: param.cpp:148
const int & get_int(int position=0) const
Returns the reference of a int stored in the list.
Definition: param.cpp:92
int get_n_int() const
Returns the number of stored int 's addresses.
Definition: param.cpp:53