其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
matrix(c++实现,初版)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

namespace mylib {

    template<typename Ty,

             const size_t line_sz,

             const size_t row_sz>

    class matrix { 

    public:

        //typedef

        typedef Ty                   value_type;

        typedef size_t               size_type;

        typedef ptrdiff_t            diffecence_type;

        typedef Ty*                  pointer;

        typedef const Ty*            const_pointer;

        typedef Ty&                  reference;

        typedef const Ty&            const_reference;

        typedef Ty(*cmat_ptr)[row_sz];

        typedef const Ty(*const_cmat_ptr)[row_sz];

               

        //constructors

        matrix(void)

        : mat_ptr(NULL)

        {

            get_memory();

            for(size_type i = 0; i < line_sz; ++i)

                for(size_type j = 0; j < row_sz; ++j)

                    mat_ptr[i][j] = value_type();

        }

        matrix(const_cmat_ptr cmat)

        : mat_ptr(NULL)

        {

            get_memory();

            for(size_type i = 0; i < line_sz; ++i)

                for(size_type j = 0; j < row_sz; ++j)

                    mat_ptr[i][j] = cmat[i][j];

        }

   

        matrix(const matrix<Ty,line_sz,row_sz>& mat)

        : mat_ptr(NULL)

        {

            get_memory();

            for(size_type i = 0; i < line_sz; ++i)

                for(size_type j = 0; j < row_sz; ++j)

                    mat_ptr[i][j] = mat[i][j];

        }

       

        //destructors

        ~matrix(void)

        {

            destroy_memory();

        }

       

        //public member methods

        const size_type line_size(void) const

        {

            return line_sz;

        }

        const size_type row_size(void) const

        {

            return row_sz;

        }

        const size_type size(void) const

        {

            return size_type(line_sz * row_sz);

        }

       

        //operator member

        matrix<Ty,line_sz,row_sz>&

            operator= (const matrix<Ty,line_sz,row_sz>& mat)

        {

            if(this == &mat)

                return *this;

            if(mat_ptr != BULL)

                destroy_memory();

            get_memory();

            for(size_type i = 0; i < line_sz; ++i)

                for(size_type j = 0; j < row_sz; ++j)

                    mat_ptr[i][j] = mat[i][j];

        }

        matrix<Ty,line_sz,row_sz>&

            operator= (const_cmat_ptr cmat)

        {

            if(this == cmat)

                return *this;

            if(mat_ptr != BUL L)

                destroy_memory();

            get_memory();

            for(size_type i = 0; i < line_sz; ++i)

                for(size_type j = 0; j < row_sz; ++j)

                    mat_ptr[i][j] = cmat[i][j];

        }

        pointer operator[] (const size_type lidx)

        {

            if(lidx >= line_sz)

            {

                std::cerr << "lidx >= line_sz" << std::endl;

                std::exit(EXIT_FAILURE);

            }

            return mat_ptr[lidx];

        }

        const_pointer operator[] (const size_type lidx) const

        {

            if(lidx >= line_sz)

            {

                std::cerr << "lidx >= line_sz" << std::endl;

                std::exit(EXIT_FAILURE);

            }

           

            return mat_ptr[lidx];

        }

        operator const_cmat_ptr() const

        {

            return mat_ptr;

        }

       

    private:

        //private methods

        void get_memory(void)

        {

            mat_ptr = new value_type[line_sz][row_sz];

        }

        void destroy_memory(void)

        {

            delete [] mat_ptr;

        }

 

        //date member

        cmat_ptr mat_ptr;

    };

}




相关文章

相关软件