source: sasview/realSpaceModeling/iqPy/libiqPy/tnt/tnt_fortran_array1d.h @ 8cd029b

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 8cd029b was f2d6445, checked in by Mathieu Doucet <doucetm@…>, 17 years ago

moving realSpaceModeling to trunk

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2*
3* Template Numerical Toolkit (TNT)
4*
5* Mathematical and Computational Sciences Division
6* National Institute of Technology,
7* Gaithersburg, MD USA
8*
9*
10* This software was developed at the National Institute of Standards and
11* Technology (NIST) by employees of the Federal Government in the course
12* of their official duties. Pursuant to title 17 Section 105 of the
13* United States Code, this software is not subject to copyright protection
14* and is in the public domain. NIST assumes no responsibility whatsoever for
15* its use by other parties, and makes no guarantees, expressed or implied,
16* about its quality, reliability, or any other characteristic.
17*
18*/
19
20
21
22#ifndef TNT_FORTRAN_ARRAY1D_H
23#define TNT_FORTRAN_ARRAY1D_H
24
25#include <cstdlib>
26#include <iostream>
27
28#ifdef TNT_BOUNDS_CHECK
29#include <assert.h>
30#endif
31
32
33#include "tnt_i_refvec.h"
34
35namespace TNT
36{
37
38template <class T>
39class Fortran_Array1D
40{
41
42  private:
43
44    i_refvec<T> v_;
45    int n_;
46    T* data_;                           /* this normally points to v_.begin(), but
47                             * could also point to a portion (subvector)
48                                                         * of v_.
49                            */
50
51    void initialize_(int n);
52    void copy_(T* p, const T*  q, int len) const;
53    void set_(T* begin,  T* end, const T& val);
54 
55
56  public:
57
58    typedef         T   value_type;
59
60
61                 Fortran_Array1D();
62        explicit Fortran_Array1D(int n);
63                 Fortran_Array1D(int n, const T &a);
64                 Fortran_Array1D(int n,  T *a);
65    inline   Fortran_Array1D(const Fortran_Array1D &A);
66        inline   Fortran_Array1D & operator=(const T &a);
67        inline   Fortran_Array1D & operator=(const Fortran_Array1D &A);
68        inline   Fortran_Array1D & ref(const Fortran_Array1D &A);
69                 Fortran_Array1D copy() const;
70                     Fortran_Array1D & inject(const Fortran_Array1D & A);
71        inline   T& operator()(int i);
72        inline   const T& operator()(int i) const;
73        inline   int dim1() const;
74        inline   int dim() const;
75              ~Fortran_Array1D();
76
77
78        /* ... extended interface ... */
79
80        inline int ref_count() const;
81        inline Fortran_Array1D<T> subarray(int i0, int i1);
82
83};
84
85
86
87
88template <class T>
89Fortran_Array1D<T>::Fortran_Array1D() : v_(), n_(0), data_(0) {}
90
91template <class T>
92Fortran_Array1D<T>::Fortran_Array1D(const Fortran_Array1D<T> &A) : v_(A.v_),  n_(A.n_), 
93                data_(A.data_)
94{
95#ifdef TNT_DEBUG
96        std::cout << "Created Fortran_Array1D(const Fortran_Array1D<T> &A) \n";
97#endif
98
99}
100
101
102template <class T>
103Fortran_Array1D<T>::Fortran_Array1D(int n) : v_(n), n_(n), data_(v_.begin())
104{
105#ifdef TNT_DEBUG
106        std::cout << "Created Fortran_Array1D(int n) \n";
107#endif
108}
109
110template <class T>
111Fortran_Array1D<T>::Fortran_Array1D(int n, const T &val) : v_(n), n_(n), data_(v_.begin()) 
112{
113#ifdef TNT_DEBUG
114        std::cout << "Created Fortran_Array1D(int n, const T& val) \n";
115#endif
116        set_(data_, data_+ n, val);
117
118}
119
120template <class T>
121Fortran_Array1D<T>::Fortran_Array1D(int n, T *a) : v_(a), n_(n) , data_(v_.begin())
122{
123#ifdef TNT_DEBUG
124        std::cout << "Created Fortran_Array1D(int n, T* a) \n";
125#endif
126}
127
128template <class T>
129inline T& Fortran_Array1D<T>::operator()(int i) 
130{ 
131#ifdef TNT_BOUNDS_CHECK
132        assert(i>= 1);
133        assert(i <= n_);
134#endif
135        return data_[i-1]; 
136}
137
138template <class T>
139inline const T& Fortran_Array1D<T>::operator()(int i) const 
140{ 
141#ifdef TNT_BOUNDS_CHECK
142        assert(i>= 1);
143        assert(i <= n_);
144#endif
145        return data_[i-1]; 
146}
147
148
149       
150
151template <class T>
152Fortran_Array1D<T> & Fortran_Array1D<T>::operator=(const T &a)
153{
154        set_(data_, data_+n_, a);
155        return *this;
156}
157
158template <class T>
159Fortran_Array1D<T> Fortran_Array1D<T>::copy() const
160{
161        Fortran_Array1D A( n_);
162        copy_(A.data_, data_, n_);
163
164        return A;
165}
166
167
168template <class T>
169Fortran_Array1D<T> & Fortran_Array1D<T>::inject(const Fortran_Array1D &A)
170{
171        if (A.n_ == n_)
172                copy_(data_, A.data_, n_);
173
174        return *this;
175}
176
177
178
179
180
181template <class T>
182Fortran_Array1D<T> & Fortran_Array1D<T>::ref(const Fortran_Array1D<T> &A)
183{
184        if (this != &A)
185        {
186                v_ = A.v_;              /* operator= handles the reference counting. */
187                n_ = A.n_;
188                data_ = A.data_; 
189               
190        }
191        return *this;
192}
193
194template <class T>
195Fortran_Array1D<T> & Fortran_Array1D<T>::operator=(const Fortran_Array1D<T> &A)
196{
197        return ref(A);
198}
199
200template <class T>
201inline int Fortran_Array1D<T>::dim1() const { return n_; }
202
203template <class T>
204inline int Fortran_Array1D<T>::dim() const { return n_; }
205
206template <class T>
207Fortran_Array1D<T>::~Fortran_Array1D() {}
208
209
210/* ............................ exented interface ......................*/
211
212template <class T>
213inline int Fortran_Array1D<T>::ref_count() const
214{
215        return v_.ref_count();
216}
217
218template <class T>
219inline Fortran_Array1D<T> Fortran_Array1D<T>::subarray(int i0, int i1)
220{
221#ifdef TNT_DEBUG
222                std::cout << "entered subarray. \n";
223#endif
224        if ((i0 > 0) && (i1 < n_) || (i0 <= i1))
225        {
226                Fortran_Array1D<T> X(*this);  /* create a new instance of this array. */
227                X.n_ = i1-i0+1;
228                X.data_ += i0;
229
230                return X;
231        }
232        else
233        {
234#ifdef TNT_DEBUG
235                std::cout << "subarray:  null return.\n";
236#endif
237                return Fortran_Array1D<T>();
238        }
239}
240
241
242/* private internal functions */
243
244
245template <class T>
246void Fortran_Array1D<T>::set_(T* begin, T* end, const T& a)
247{
248        for (T* p=begin; p<end; p++)
249                *p = a;
250
251}
252
253template <class T>
254void Fortran_Array1D<T>::copy_(T* p, const T* q, int len) const
255{
256        T *end = p + len;
257        while (p<end )
258                *p++ = *q++;
259
260}
261
262
263} /* namespace TNT */
264
265#endif
266/* TNT_FORTRAN_ARRAY1D_H */
267
Note: See TracBrowser for help on using the repository browser.