ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
dht.hpp
Go to the documentation of this file.
1/*
2 * $Id: dht.hpp 293 2012-12-17 20:27:36Z jdl3 $
3 * Copyright (C) 2012 John D Lamb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#ifndef CCGSL_DHT_HPP
21#define CCGSL_DHT_HPP
22
23#include<new>
24#include<exception>
25#include<gsl/gsl_dht.h>
26
27namespace gsl {
31 class dht {
32 public:
36 dht(){
37 ccgsl_pointer = 0;
38 count = 0; // initially nullptr will do
39 }
40 // Refines random access container
41 // Refines assignable
46 explicit dht( size_t const size ){
47 ccgsl_pointer = gsl_dht_alloc( size );
48 // just plausibly we could allocate dht but not count
49 try { count = new size_t; } catch( std::bad_alloc& e ){
50 // try to tidy up before rethrowing
51 gsl_dht_free( ccgsl_pointer );
52 throw e;
53 }
54 *count = 1; // initially there is just one reference to ccgsl_pointer
55 }
62 dht( size_t size, double nu, double xmax ){
63 ccgsl_pointer = gsl_dht_new( size, nu, xmax );
64 // just plausibly we could allocate dht but not count
65 try { count = new size_t; } catch( std::bad_alloc& e ){
66 // try to tidy up before rethrowing
67 gsl_dht_free( ccgsl_pointer );
68 throw e;
69 }
70 *count = 1; // initially there is just one reference to ccgsl_pointer
71 }
72
79 explicit dht( gsl_dht* v ){
80 ccgsl_pointer = v;
81 // just plausibly we could fail to allocate count: no further action needed.
82 count = new size_t;
83 *count = 1; // initially there is just one reference to ccgsl_pointer
84 }
85 // copy constructor
91 count = v.count; if( count != 0 ) ++*count; }
92 // assignment operator
97 dht& operator=( dht const& v ){
98 // first, possibly delete anything pointed to by this
99 if( count == 0 or --*count == 0 ){
100 if( ccgsl_pointer != 0 ) gsl_dht_free( ccgsl_pointer );
101 delete count;
102 } // Then copy
103 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
104 }
105 // destructor
110 if( count == 0 or --*count == 0 ){
111 // could have allocated null pointer
112 if( ccgsl_pointer != 0 ) gsl_dht_free( ccgsl_pointer );
113 delete count;
114 }
115 }
116#ifdef __GXX_EXPERIMENTAL_CXX0X__
121 dht( dht&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
122 std::swap( count, v.count );
123 v.ccgsl_pointer = nullptr;
124 }
131 dht( std::move( v ) ).swap( *this );
132 return *this;
133 }
134#endif
135 // Refines equality comparable
136 // == operator
143 bool operator==( dht const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
144 // != operator
151 bool operator!=( dht const& v ) const { return not operator==( v ); }
152 // Refines forward container
153 // Refines less than comparable
154 // operator<
163 bool operator<( dht const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
164 // operator>
173 bool operator>( dht const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
174 // operator<=
183 bool operator<=( dht const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
184 // operator>=
193 bool operator>=( dht const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
198 bool empty() const { return ccgsl_pointer == 0; }
199 // swap() --- should work even if sizes don't match
205 void swap( dht& v ){
206 std::swap( ccgsl_pointer, v.ccgsl_pointer );
207 std::swap( count, v.count );
208 }
209 private:
217 size_t* count;
218 public:
219 // shared reference functions
224 gsl_dht* get() const { return ccgsl_pointer; }
230 bool unique() const { return count != 0 and *count == 1; }
235 size_t use_count() const { return count == 0 ? 0 : *count; }
241#ifdef __GXX_EXPERIMENTAL_CXX0X__
242 explicit
243#endif
244 operator bool() const { return ccgsl_pointer != 0; }
245
252 int init( double nu, double xmax ){ return gsl_dht_init( get(), nu, xmax ); }
253
259 double x_sample( int n ) const { return gsl_dht_x_sample( get(), n ); }
260
266 double k_sample( int n ) const { return gsl_dht_k_sample( get(), n ); }
267
275 int apply( double* f_in, double* f_out ) const {
276 return gsl_dht_apply( get(), f_in, f_out ); }
277
286 template<typename ARRAY>
287 int apply( ARRAY& f_in, ARRAY& f_out ) const {
288 size_t n = get()->size;
289 if( f_in.size() != n )
290 throw std::length_error("gsl::dht::apply(): f_in must have same size as *this.");
291 if( f_out.size() != n )
292 throw std::length_error("gsl::dht::apply(): f_out must have same size as *this.");
293 return gsl_dht_apply( get(), f_in.data(), f_out.data() ); }
294
295 };
296}
297#endif
Discrete Hankel Transforms.
Definition: dht.hpp:31
bool operator>=(dht const &v) const
A container needs to define an ordering for sorting.
Definition: dht.hpp:193
void swap(dht &v)
Swap two dht objects.
Definition: dht.hpp:205
int apply(ARRAY &f_in, ARRAY &f_out) const
C++ version of gsl_dht_apply().
Definition: dht.hpp:287
dht(dht &&v)
Move constructor.
Definition: dht.hpp:121
bool empty() const
Find if the dht is empty.
Definition: dht.hpp:198
double x_sample(int n) const
C++ version of gsl_dht_x_sample().
Definition: dht.hpp:259
int init(double nu, double xmax)
C++ version of gsl_dht_init().
Definition: dht.hpp:252
dht()
The default constructor is only really useful for assigning to.
Definition: dht.hpp:36
dht(size_t size, double nu, double xmax)
C++ version of gsl_dht_new().
Definition: dht.hpp:62
dht(gsl_dht *v)
Could construct from a gsl_dht.
Definition: dht.hpp:79
dht & operator=(dht &&v)
Move operator.
Definition: dht.hpp:130
bool unique() const
Find if this is the only object sharing the gsl_dht.
Definition: dht.hpp:230
bool operator==(dht const &v) const
Two dht are identically equal if their elements are identical.
Definition: dht.hpp:143
dht & operator=(dht const &v)
The assignment operator.
Definition: dht.hpp:97
bool operator>(dht const &v) const
A container needs to define an ordering for sorting.
Definition: dht.hpp:173
bool operator<=(dht const &v) const
A container needs to define an ordering for sorting.
Definition: dht.hpp:183
dht(dht const &v)
The copy constructor.
Definition: dht.hpp:90
int apply(double *f_in, double *f_out) const
C++ version of gsl_dht_apply().
Definition: dht.hpp:275
gsl_dht * get() const
Get the gsl_dht.
Definition: dht.hpp:224
double k_sample(int n) const
C++ version of gsl_dht_k_sample().
Definition: dht.hpp:266
~dht()
The destructor only deletes the pointers if count reaches zero.
Definition: dht.hpp:109
gsl_dht * ccgsl_pointer
The shared pointer.
Definition: dht.hpp:213
size_t * count
The shared reference count.
Definition: dht.hpp:217
dht(size_t const size)
The default constructor creates a new dht with n elements.
Definition: dht.hpp:46
size_t use_count() const
Find how many dht objects share this pointer.
Definition: dht.hpp:235
bool operator<(dht const &v) const
A container needs to define an ordering for sorting.
Definition: dht.hpp:163
bool operator!=(dht const &v) const
Two dht are different if their elements are not identical.
Definition: dht.hpp:151
size_t size(series const &cs)
C++ version of gsl_cheb_size().
Definition: chebyshev.hpp:287
size_t n(workspace const &w)
C++ version of gsl_rstat_n().
Definition: rstat.hpp:299
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34