⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memblk.h

📁 高效的c++科学算法库
💻 H
字号:
#ifndef SL_MATRIX_MEMORY_MEMBLK_H#define SL_MATRIX_MEMORY_MEMBLK_H/* ****************************** * Scientific Library (GNU Public Licence) * * Author: Laurent Deniau, Laurent.Deniau@cern.ch * * $Id: memblk.h,v 1.2 1998/11/12 16:17:36 paubert Exp $ * * Suggestions: sl@mathinsa.insa-lyon.fr * Bugs:   sl-bugs@mathinsa.insa-lyon.fr * * For more information, please see the sl++ Home Page: * http://wwwinfo.cern.ch/~ldeniau/sl.html * ****************************** */#ifndef SL_MATRIX_MEMORY_H#error <sl/matrix/memblk.h> must be included via <sl/matrix/memory.h>#endif#ifdef HAVE_NAMESPACEnamespace sl {#endif    template <typename T_value>        class MemBlock {          public:        typedef T_value value_t;        T_value * base () const { return my_b; }        T_value const* const_base () const { return my_b; }          protected:        Bool isOwner () const { return my_o == this; }          protected:        explicit            MemBlock ()            : my_b ( 0 ), my_o ( this ) {        }        explicit            MemBlock (size_t const n)            : my_b ( new T_value[n] ), my_o ( this ) {#ifdef SL_DEBUG_TRACE_MEMORY_ALLOCATIONS            cerr << "MemBlock: allocation of " << n << " elements "                 << '[' << n * sizeof(T_value) << " bytes] to " << this << endl;#endif        }            explicit            MemBlock (T_value* const memzone)            : my_b ( memzone ), my_o ( 0 ) {        }          // WARNING: Do NOT declare the copy constructor !            public:        ~MemBlock () {            if ( isOwner() ) {                delete [] my_b;#ifdef SL_DEBUG_TRACE_MEMORY_ALLOCATIONS                cerr << "MemBlock: deletion of elements of " << this << endl;#endif            }        }            protected:        template <typename T2_value>            void            set (size_t const n, T2_value const x) {            for(size_t i=0;i<n;i++)                my_b[i] = x;        }        template <typename T2_value>            void            copy (size_t const n, T2_value const* t) {            for(size_t i=0;i<n;i++)                my_b[i] = t[i];        }        void            newBlock (size_t const n) {            assert ( isOwner() );            my_b = n ? new T_value[n] : 0;#ifdef SL_DEBUG_TRACE_MEMORY_ALLOCATIONS            cerr << "MemBlock: allocation of " << n << " elements "                 << '[' << n * sizeof(T_value) << " bytes] to " << this << endl;#endif        }          void            resize (size_t const n) {            delete [] my_b;            newBlock(n);        }        void            reshape (size_t const old_n, size_t const new_n) {            T_value const* const t = my_b;            newBlock(new_n);            copy(min(old_n, new_n), t);            delete [] t;        }          private:        T_value* my_b;          // base pointer to the data        void const* const my_o; // owner of the data (this)    };#ifdef HAVE_NAMESPACE}#endif #endif// SL_MATRIX_MEMORY_MEMBLK_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -