208 {
209 TRACE();
210
211
212
213
214
216
217 if(noElements == 1)
219 AT_,
220 "Apparently this method can not build an adt with just one element. I observed that in 2D but did not "
221 "debug further.");
222
223
226 }
227
228
230
232 from =
new MInt[
mMax(noElements, 0)];
233
234
237
239 MInt nodeCounter = 0;
240 MInt currentNode = 0;
241 MInt currentDepth = 0;
242
244
246
248
249
250 stack<MInt> nodeStack;
251
252
254
255 for(
MInt i = 0; i < 2 * nDim; i++)
257
258
259 m_log <<
" + Building normal tree with " << noElements <<
" elements..." << endl;
260
261
262 vector<MInt>::iterator it, itFrom, itTo;
263 vector<MInt> idArray(noElements);
265 for(it = idArray.begin(); it != idArray.end(); it++)
266 *it = i++;
267
268
269 m_log <<
" - setting root node" << endl;
270
272 currentDir = 0;
273 nodeCounter = 0;
275
276 from[
m_root] = *idArray.begin();
277 to[
m_root] = *(idArray.end() - 1);
280
281 left = 0;
282 right = 0;
283
284 if(noElements == 1) {
287
288
289 currentElement = idArray[to[
m_root]];
293
294 m_log <<
" - done building the tree" << endl;
295 return;
296 }
297
298
299
300 m_log <<
" - inserting other nodes" << endl;
302 currentDepth = 0;
303 while(!nodeStack.empty()) {
304 currentNode = nodeStack.top();
305 nodeStack.pop();
307 currentDir = currentDepth % (nDim * 2);
308
309
310 if(from[currentNode] != to[currentNode]) {
311 left = ++nodeCounter;
315
316
317 if(to[currentNode] - from[currentNode] == 1) {
318 right = 0;
320 } else {
321 right = ++nodeCounter;
325 nodeStack.push(right);
326 }
327 nodeStack.push(left);
328
329
330 itFrom = idArray.begin();
331 itFrom += from[currentNode];
332 itTo = idArray.begin();
333
334 itTo += to[currentNode] + 1;
335
336
338
339
340
341 currentElement = idArray[from[currentNode]++];
343
344
345 partitionId = (
MInt)((to[currentNode] - from[currentNode]) / 2 + from[currentNode]);
347
348
349 m_nodes[currentNode].
m_a = elements[idArray[from[currentNode] - 1]].
m_minMax[currentDir];
351
352
353
354
355 from[left] = from[currentNode];
356 to[left] = partitionId;
357
358
359 if(right) {
360 from[right] = to[left] + 1;
361 to[right] = to[currentNode];
362 }
363
364 } else {
365 right = 0;
366 left = 0;
369
370
371 currentElement = idArray[to[currentNode]];
375 }
376
377
378
379 }
380
381 m_log <<
" - done building the tree" << endl;
382
383 delete[] from;
384 delete[] to;
385}
std::array< MFloat, 2 *nDim > m_minMax
void mTerm(const MInt errorCode, const MString &location, const MString &message)