source: trunk/src/Locator.tcc @ 2732

Last change on this file since 2732 was 2732, checked in by Takeshi Nakazato, 11 years ago

New Development: No

JIRA Issue: Yes CAS-4770

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...

Commit .tcc files for Locator classes.


File size: 1.7 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
14#include "Locator.h"
15
16namespace asap {
17template <class T> Locator<T>::Locator()
18  : x_(0),
19    n_(0),
20    ascending_(true),
21    copy_(false)
22{
23}
24
25template <class T> Locator<T>::Locator(T *v, unsigned int n, bool copystorage)
26  : x_(0),
27    n_(0),
28    ascending_(true),
29    copy_(false)
30{
31  set(v, n, copystorage);
32}
33
34template <class T> Locator<T>::~Locator()
35{
36  if (copy_ && x_)
37    delete[] x_;
38}
39
40template <class T> void Locator<T>::set(T *v, unsigned int n, bool copystorage)
41{
42  if (copy_) {
43    if (!copystorage || n > n_) {
44      delete[] x_;
45      x_ = 0;
46    }
47  }
48  copy_ = copystorage;
49  n_ = n;
50  if (copy_) {
51    if (!x_)
52      x_ = new double[n_];
53    for (unsigned int i = 0; i < n_; i++)
54      x_[i] = v[i];
55  }
56  else {
57    x_ = v;
58  }
59  ascending_ = (x_[0] <= x_[n_-1]);
60}
61
62template <class T> unsigned int Locator<T>::bisection(T x, unsigned int left, unsigned int right)
63{
64  unsigned int jl = left;
65  unsigned int ju = right;
66
67  if (ascending_) {
68    // ascending order
69    if (x <= x_[0])
70      return 0;
71    else if (x > x_[n_-1])
72      return n_;
73
74    unsigned int jm;
75    while (ju - jl > 1) {
76      jm = (ju + jl) / 2;
77      if (x > x_[jm])
78        jl = jm;
79      else
80        ju = jm;
81    }
82  }
83  else {
84    // descending order
85    if (x >= x_[0])
86      return 0;
87    else if (x < x_[n_-1])
88      return n_;
89
90    unsigned int jm;
91    while (ju - jl > 1) {
92      jm = (ju + jl) / 2;
93      if (x < x_[jm])
94        jl = jm;
95      else
96        ju = jm;
97    }
98  }
99
100  return ju;
101}
102
103}
Note: See TracBrowser for help on using the repository browser.