1 
2 
3 /*
4  *  Math2
5  *  Copyright (c) 2003-2006 by Mattias Hultgren <mattias_hultgren@tele2.se>
6  *
7  *  See math2.h
8  */
9 
10 
11 #include "math2.h"
12 #include "keyfile.h"
13 
14 namespace math
15 {
16 
Array()17 Array::Array()
18 {
19 	nrofvariables = 0;
20 	array = 0;
21 }
Array(const Array & src)22 Array::Array(const Array &src) throw(error_obj)
23 {
24 	nrofvariables = 0;
25 	array = 0;
26 	*this = src;
27 }
~Array()28 Array::~Array()
29 {
30 	clear();
31 }
32 
operator ==(const Array & arr) const33 bool Array::operator==(const Array &arr) const
34 {
35 	if( nrofvariables != arr.nrofvariables )
36 		return false;
37 
38 	for( uint32 i=0; i<nrofvariables; i++ )
39 	{
40 		if( *array[i] != *arr.array[i] )
41 			return false;
42 	}
43 	return true;
44 }
45 
clear(void)46 void Array::clear(void)
47 {
48 	for(uint32 i=0;i<nrofvariables;i++)
49 		delete array[i];
50 	delete [] array;
51 
52 	array = 0;
53 	nrofvariables = 0;
54 }
55 
operator =(const Array & src)56 void Array::operator=(const Array &src) throw(error_obj)
57 {
58 	clear();
59 
60 	try
61 	{
62 		for(uint32 i=0;i<src.nrofvariables;i++)
63 			add_variable(src.array[i]);
64 	}
65 	catch(...)
66 	{
67 		clear();
68 		throw;
69 	}
70 }
71 
add_this_variable(Variable * var)72 void Array::add_this_variable( Variable *var ) throw(error_obj)
73 {
74 	Variable  **newarray = 0;
75 	if(var == 0)
76 		return;
77 
78 	try
79 	{
80 		newarray = new Variable * [nrofvariables + 1];
81 		newarray[nrofvariables] = var;
82 	}
83 	catch(...)
84 	{
85 		if(newarray != 0)
86 			delete newarray[nrofvariables];
87 		delete [] newarray;
88 		THROW_ERROR( ErrorType_Memory, _("Couldn't get memory.") );
89 	}
90 
91 	for(uint32 i=0;i<nrofvariables;i++)
92 		newarray[i] = array[i];
93 
94 	delete [] array;
95 
96 	nrofvariables++;
97 
98 	array = newarray;
99 }
100 
add_variable(const Variable * var)101 void Array::add_variable(const Variable *var) throw(error_obj)
102 {
103 	Variable *new_var;
104 	if(var == 0)
105 		return;
106 
107 	try
108 	{
109 		new_var = 0;
110 		new_var = new Variable;
111 		*new_var = *var;
112 		new_var->set_name_from( *var );
113 
114 		add_this_variable( new_var );
115 	}
116 	catch(...)
117 	{
118 		delete new_var;
119 		THROW_ERROR( ErrorType_Memory, _("Couldn't get memory.") );
120 	}
121 }
122 
get_variable(uint32 nr) const123 const Variable * Array::get_variable(uint32 nr) const throw(error_obj)
124 {
125 	if( nr >= nrofvariables )
126 		THROW_ERROR( ErrorType_General, _("Array-index out of range") );
127 	return array[nr];
128 }
129 
set_variable(uint32 nr,const Variable & new_var)130 void Array::set_variable( uint32 nr, const Variable &new_var) throw(error_obj)
131 {
132 	if( nr >= nrofvariables )
133 		THROW_ERROR( ErrorType_General, _("Array-index out of range") );
134 
135 	*array[nr] = new_var;
136 	array[nr]->set_name_from( new_var );
137 }
138 
set_size(uint32 new_size)139 void Array::set_size( uint32 new_size ) throw(error_obj)
140 {
141 	clear();
142 
143 	try
144 	{
145 		array = new Variable * [new_size];
146 		nrofvariables = new_size;
147 
148 		for( uint32 i=0; i<new_size; i++ )
149 			array[i] = 0;
150 
151 		for( uint32 i=0; i<new_size; i++ )
152 			array[i] = new Variable;
153 	}
154 	catch(...)
155 	{
156 		clear();
157 		THROW_ERROR( ErrorType_Memory, _("Couldn't get memory.") );
158 	}
159 }
160 
increase_size(void)161 void Array::increase_size(void) throw(error_obj)
162 {
163 	Variable **new_array = 0;
164 	uint32 i;
165 
166 	try{  new_array = new Variable * [nrofvariables + 1];  }
167 	catch(...) {  THROW_ERROR( ErrorType_Memory, _("Couldn't get memory.") );  }
168 
169 	for( i=0; i<nrofvariables; i++ )
170 		new_array[i] = array[i];
171 
172 	new_array[nrofvariables] = 0;
173 
174 	delete [] array;
175 	nrofvariables++;
176 	array = new_array;
177 }
178 
179 
insert(const Variable * var,uint32 at_pos)180 void Array::insert( const Variable *var, uint32 at_pos ) throw(error_obj)
181 {
182 	Variable *new_var;
183 	if(var == 0)
184 		return;
185 
186 	try
187 	{
188 		new_var = 0;
189 		new_var = new Variable;
190 		*new_var = *var;
191 		new_var->set_name_from( *var );
192 
193 		insert_this( new_var, at_pos );
194 	}
195 	catch(...)
196 	{
197 		delete new_var;
198 		THROW_ERROR( ErrorType_Memory, _("Couldn't get memory.") );
199 	}
200 }
201 
insert_this(Variable * var,uint32 at_pos)202 void Array::insert_this( Variable *var, uint32 at_pos ) throw(error_obj)
203 {
204 	if( at_pos > nrofvariables )
205 		at_pos = nrofvariables;
206 
207 	increase_size();
208 
209 	for( uint32 i=nrofvariables-1; i>at_pos; i-- )
210 		array[i] = array[i-1];
211 
212 	array[at_pos] = var;
213 }
214 
remove_range(uint32 from_nr,uint32 this_many)215 void Array::remove_range( uint32 from_nr, uint32 this_many )
216 {
217 	if( this_many == 0  ||  from_nr >= nrofvariables )
218 		return;
219 
220 	if( (from_nr + this_many) >= nrofvariables )
221 	{
222 		for( uint32 i=from_nr; i<nrofvariables; i++ )
223 			delete array[i];
224 		nrofvariables = from_nr;
225 	}
226 	else
227 	{
228 		uint32 removed=0;
229 		nrofvariables -= this_many;
230 		for( uint32 i=from_nr; i<nrofvariables; i++ )
231 		{
232 			if( removed != this_many )
233 			{
234 				delete array[i];
235 				removed++;
236 			}
237 			array[i] = array[i+this_many];
238 		}
239 	}
240 }
241 
242 
243 
244 } // namespace math
245