Last change
on this file since 3055 was 3020, checked in by Takeshi Nakazato, 10 years ago |
New Development: No
JIRA Issue: Yes CAS-7193
Ready for Test: Yes
Interface Changes: Yes/No
What Interface Changed: Please list interface changes
Test Programs: List test programs
Put in Release Notes: Yes/No
Module(s): Module Names change impacts.
Description: Describe your changes here...
Fix for segmentation fault when invalid Tsys table is applied.
|
File size:
1.8 KB
|
Line | |
---|
1 | //
|
---|
2 | // C++ Implementation: Locator
|
---|
3 | //
|
---|
4 | // Description:
|
---|
5 | //
|
---|
6 | //
|
---|
7 | // Author: Takeshi Nakazato <takeshi.nakazato@nao.ac.jp>, (C) 2012
|
---|
8 | //
|
---|
9 | // Copyright: See COPYING file that comes with this distribution
|
---|
10 | //
|
---|
11 | //
|
---|
12 | #include <assert.h>
|
---|
13 | #include <iostream>
|
---|
14 | #include <stdexcept>
|
---|
15 |
|
---|
16 | #include "Locator.h"
|
---|
17 |
|
---|
18 | namespace asap {
|
---|
19 | template <class T> Locator<T>::Locator()
|
---|
20 | : x_(0),
|
---|
21 | n_(0),
|
---|
22 | ascending_(true),
|
---|
23 | copy_(false)
|
---|
24 | {
|
---|
25 | }
|
---|
26 |
|
---|
27 | template <class T> Locator<T>::Locator(T *v, unsigned int n, bool copystorage)
|
---|
28 | : x_(0),
|
---|
29 | n_(0),
|
---|
30 | ascending_(true),
|
---|
31 | copy_(false)
|
---|
32 | {
|
---|
33 | set(v, n, copystorage);
|
---|
34 | }
|
---|
35 |
|
---|
36 | template <class T> Locator<T>::~Locator()
|
---|
37 | {
|
---|
38 | if (copy_ && x_)
|
---|
39 | delete[] x_;
|
---|
40 | }
|
---|
41 |
|
---|
42 | template <class T> void Locator<T>::set(T *v, unsigned int n, bool copystorage)
|
---|
43 | {
|
---|
44 | if (copy_) {
|
---|
45 | if (!copystorage || n > n_) {
|
---|
46 | delete[] x_;
|
---|
47 | x_ = 0;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | copy_ = copystorage;
|
---|
51 | n_ = n;
|
---|
52 | if (copy_) {
|
---|
53 | if (!x_)
|
---|
54 | x_ = new double[n_];
|
---|
55 | for (unsigned int i = 0; i < n_; i++)
|
---|
56 | x_[i] = v[i];
|
---|
57 | }
|
---|
58 | else {
|
---|
59 | x_ = v;
|
---|
60 | }
|
---|
61 | assert(n_ > 0);
|
---|
62 | if (n_ == 0) {
|
---|
63 | throw std::runtime_error("Input array length is 0.");
|
---|
64 | }
|
---|
65 | ascending_ = (x_[0] <= x_[n_-1]);
|
---|
66 | }
|
---|
67 |
|
---|
68 | template <class T> unsigned int Locator<T>::bisection(T x, unsigned int left, unsigned int right)
|
---|
69 | {
|
---|
70 | unsigned int jl = left;
|
---|
71 | unsigned int ju = right;
|
---|
72 |
|
---|
73 | if (ascending_) {
|
---|
74 | // ascending order
|
---|
75 | if (x <= x_[0])
|
---|
76 | return 0;
|
---|
77 | else if (x > x_[n_-1])
|
---|
78 | return n_;
|
---|
79 |
|
---|
80 | unsigned int jm;
|
---|
81 | while (ju - jl > 1) {
|
---|
82 | jm = (ju + jl) / 2;
|
---|
83 | if (x > x_[jm])
|
---|
84 | jl = jm;
|
---|
85 | else
|
---|
86 | ju = jm;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | else {
|
---|
90 | // descending order
|
---|
91 | if (x >= x_[0])
|
---|
92 | return 0;
|
---|
93 | else if (x < x_[n_-1])
|
---|
94 | return n_;
|
---|
95 |
|
---|
96 | unsigned int jm;
|
---|
97 | while (ju - jl > 1) {
|
---|
98 | jm = (ju + jl) / 2;
|
---|
99 | if (x < x_[jm])
|
---|
100 | jl = jm;
|
---|
101 | else
|
---|
102 | ju = jm;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | return ju;
|
---|
107 | }
|
---|
108 |
|
---|
109 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.