array_impl.hpp

Go to the documentation of this file.
00001 /*
00002  * 
00003  * Copyright (c) Kresimir Fresl 2003
00004  *
00005  * Distributed under the Boost Software License, Version 1.0.
00006  * (See accompanying file LICENSE_1_0.txt or copy at
00007  * http://www.boost.org/LICENSE_1_0.txt)
00008  *
00009  * Author acknowledges the support of the Faculty of Civil Engineering, 
00010  * University of Zagreb, Croatia.
00011  *
00012  */
00013 
00014 #ifndef BOOST_NUMERIC_BINDINGS_TRAITS_DETAIL_ARRAY_IMPL_HPP
00015 #define BOOST_NUMERIC_BINDINGS_TRAITS_DETAIL_ARRAY_IMPL_HPP
00016 
00017 /*
00018  very simple dynamic array class which is used in `higher level' 
00019  bindings functions for pivot and work arrays 
00020 
00021  Namely, there are (at least) two versions of all bindings functions 
00022  where called LAPACK function expects work and/or pivot array, e.g.
00023  
00024       `lower' level (user should provide work and pivot arrays):
00025            int sysv (SymmA& a, IVec& i, MatrB& b, Work& w);
00026  
00027       `higher' level (with `internal' work and pivot arrays):
00028            int sysv (SymmA& a, MatrB& b);
00029  
00030  Probably you ask why I didn't use std::vector. There are two reasons. 
00031  First is efficiency -- std::vector's constructor initialises vector 
00032  elements. Second is consistency. LAPACK functions use `info' parameter 
00033  as an error indicator. On the other hand, std::vector's allocator can
00034  throw an exception if memory allocation fails. detail::array's 
00035  constructor uses `new (nothrow)' which returns 0 if allocation fails. 
00036  So I can check whether array::storage == 0 and return appropriate error 
00037  in `info'.
00038  */
00039 
00040 #include <new>
00041 #include <boost/noncopyable.hpp> 
00042 
00043 namespace boost { namespace numeric { namespace bindings { 
00044 
00045   namespace traits { namespace detail {
00046 
00047     template <typename T> 
00048     class array : private noncopyable {
00049     public:
00050 
00051       array (int n) {
00052         stg = new (std::nothrow) T[n]; 
00053         sz = (stg != 0) ? n : 0; 
00054       }
00055       ~array() { delete[] stg; }
00056 
00057       int size() const { return sz; }
00058       bool valid() const { return stg != 0; } 
00059       void resize (int n) {
00060         delete[] stg; 
00061         stg = new (std::nothrow) T[n]; 
00062         sz = (stg != 0) ? n : 0; 
00063       }
00064 
00065       T* storage() { return stg; }
00066       T const* storage() const { return stg; }
00067 
00068       T& operator[] (int i) { return stg[i]; }
00069       T const& operator[] (int i) const { return stg[i]; }
00070 
00071     private:
00072       int sz; 
00073       T* stg; 
00074     };
00075 
00076   }}
00077 
00078 }}}
00079 
00080 #endif 

Generated on Wed Nov 23 18:59:57 2011 for FreeCAD by  doxygen 1.6.1