ccgsl 2.7.2
C++wrappersforGnuScientificLibrary
bst.hpp
Go to the documentation of this file.
1/*
2 * $Id$
3 * Copyright (C) 2019 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_BST_HPP
21#define CCGSL_BST_HPP
22
23#include<gsl/gsl_bst.h>
24
25#ifndef DOXYGEN_SKIP
26namespace gsl {
30 namespace bst {
31 class workspace {
32 public:
36 workspace(){
37 ccgsl_pointer = 0;
38 count = 0; // initially nullptr will do
39 }
40 // Refines random access container
41 // Refines assignable
48 explicit workspace( gsl_bst_type const* T, gsl_bst_allocator const* allocator,
49 gsl_bst_cmp_function* compare, void* params ){
50 ccgsl_pointer = gsl_bst_alloc( T, allocator, compare, params );
51 // just plausibly we could allocate bst but not count
52 try { count = new size_t; } catch( std::bad_alloc& e ){
53 // try to tidy up before rethrowing
54 gsl_bst_free( ccgsl_pointer );
55 throw e;
56 }
57 *count = 1; // initially there is just one reference to ccgsl_pointer
58 }
65 explicit workspace( gsl_bst_workspace* v ){
66 ccgsl_pointer = v;
67 // just plausibly we could fail to allocate count: no further action needed.
68 count = new size_t;
69 *count = 1; // initially there is just one reference to ccgsl_pointer
70 }
71 // copy constructor
76 workspace( workspace const& v ){ ccgsl_pointer = v.ccgsl_pointer;
77 count = v.count; if( count != 0 ) ++*count; }
78 // assignment operator
83 workspace& operator=( workspace const& v ){
84 // first, possibly delete anything pointed to by this
85 if( count == 0 or --*count == 0 ){
86 if( ccgsl_pointer != 0 ) gsl_bst_free( ccgsl_pointer );
87 delete count;
88 } // Then copy
89 ccgsl_pointer = v.ccgsl_pointer; count = v.count; if( count != 0 ) ++*count; return *this;
90 }
91 // destructor
95 ~workspace(){
96 if( count == 0 or --*count == 0 ){
97 // could have allocated null pointer
98 if( ccgsl_pointer != 0 ) gsl_bst_free( ccgsl_pointer );
99 delete count;
100 }
101 }
102#ifdef __GXX_EXPERIMENTAL_CXX0X__
107 workspace( workspace&& v ) : ccgsl_pointer( v.ccgsl_pointer ), count( nullptr ){
108 std::swap( count, v.count );
109 v.ccgsl_pointer = nullptr;
110 }
116 workspace& operator=( workspace&& v ){
117 workspace( std::move( v ) ).swap( *this );
118 return *this;
119 }
120#endif
121 // Refines equality comparable
122 // == operator
129 bool operator==( workspace const& v ) const { return ccgsl_pointer == v.ccgsl_pointer; }
130 // != operator
137 bool operator!=( workspace const& v ) const { return not operator==( v ); }
138 // Refines forward container
139 // Refines less than comparable
140 // operator<
149 bool operator<( workspace const& v ) const { return ccgsl_pointer < v.ccgsl_pointer; }
150 // operator>
159 bool operator>( workspace const& v ) const { return ccgsl_pointer > v.ccgsl_pointer; }
160 // operator<=
169 bool operator<=( workspace const& v ) const { return ccgsl_pointer <= v.ccgsl_pointer; }
170 // operator>=
179 bool operator>=( workspace const& v ) const { return ccgsl_pointer >= v.ccgsl_pointer; }
184 bool empty() const { return ccgsl_pointer == 0; }
185 // swap() --- should work even if sizes don't match
191 void swap( workspace& v ){
192 std::swap( ccgsl_pointer, v.ccgsl_pointer );
193 std::swap( count, v.count );
194 }
195 private:
199 gsl_bst_workspace* ccgsl_pointer;
203 size_t* count;
204 public:
205 // shared reference functions
210 gsl_bst_workspace* get() const { return ccgsl_pointer; }
216 bool unique() const { return count != 0 and *count == 1; }
221 size_t use_count() const { return count == 0 ? 0 : *count; }
227#ifdef __GXX_EXPERIMENTAL_CXX0X__
228 explicit
229#endif
230 operator bool() const { return ccgsl_pointer != 0; }
231 };
237 inline int empty( workspace& w ){ return gsl_bst_empty( w.get() ); }
243 inline void* insert( void* item, workspace& w ){ return gsl_bst_insert( item, w.get() ); }
249 inline void* find( void const* item, workspace& w ){ return gsl_bst_find( item, w.get() ); }
255 inline void* remove( void const* item, workspace& w ){
256 return gsl_bst_remove( item, w.get() ); }
262 inline size_t nodes( workspace& w ){ return gsl_bst_nodes( w.get() ); }
268 inline size_t node_size( workspace& w ){ return gsl_bst_node_size( w.get() ); }
274 inline char const* name( workspace& w ){ return gsl_bst_name( w.get() ); }
281 inline int trav_init( gsl_bst_trav* trav, workspace& w ){
282 return gsl_bst_trav_init( trav, w.get() ); }
288 inline void* trav_first( gsl_bst_trav* trav, workspace& w ){
289 return gsl_bst_trav_first( trav, w.get() ); }
295 inline void* trav_last( gsl_bst_trav* trav, workspace& w ){
296 return gsl_bst_trav_last( trav, w.get() ); }
303 inline void* trav_find( void const* item, gsl_bst_trav* trav, workspace& w ){
304 return gsl_bst_trav_find( item, trav, w.get() ); }
311 inline void* trav_insert( void* item, gsl_bst_trav* trav, workspace& w ){
312 return gsl_bst_trav_insert( item, trav, w.get() ); }
318 inline void* trav_copy( gsl_bst_trav* dest, gsl_bst_trav const* src ){
319 return gsl_bst_trav_copy( dest, src ); }
324 inline void* trav_next( gsl_bst_trav* trav ){ return gsl_bst_trav_next( trav ); }
329 inline void* trav_prev( gsl_bst_trav* trav ){ return gsl_bst_trav_prev( trav ); }
334 inline void* trav_cur( gsl_bst_trav const* trav ){ return gsl_bst_trav_cur( trav ); }
340 inline void* trav_replace( gsl_bst_trav* trav, void* new_item ){
341 return gsl_bst_trav_replace( trav, new_item ); }
342 }
343}
344#endif /* DOXYGEN_SKIP */
345#endif
char const * name(workspace const &w)
C++ version of gsl_multifit_nlinear_name().
double get(quantile_workspace &w)
C++ version of gsl_rstat_quantile_get().
Definition: rstat.hpp:626
The gsl package creates an interface to the GNU Scientific Library for C++.
Definition: blas.hpp:34