• 稀疏数组类


    C++ code

    #include "stdafx.h"
    #include "LinkList.h"
    #include "procs.h"
    #pragma once
    
    template <class T>
    class CSparseMatrix{
    public:
        CSparseMatrix(int row_,int col_)
        {
            row=row_;
            col=col_;
        }
        CSparseMatrix()
        {
            row=5;
            col=5;
        }
        CSparseMatrix(CSparseMatrix &obj)
        {
            row=obj.row;
            col=obj.col;
            for(int i=0;i<obj.ElemList.GetLen();i++)
                ElemList.Add(obj.ElemList[i]);
        }
        void SetElem(int x,int y,T v)
        {
            if(x<0 || y<0 || x>=row || y>=col) return;
            //若存在则覆盖,不存在则按行、列有序插入
            int len=ElemList.GetLen() , i;
            Elem tmp={x,y,v};
            bool flag=0;
            for(i=0;i<len;i++)
            {
                if(ElemList[i].x==x && ElemList[i].y==y)
                {
                    ElemList.SetAt(i,tmp);
                    flag=1;
                    break;
                }
                else if(  (ElemList[i].x==x && ElemList[i].y>y)  ||  (ElemList[i].x>x)  )//行匹配到了,但是列没有匹配到
                {
                    ElemList.Insert(i,tmp);//在这条数据之前插入
                    flag=1;
                    break;
                }
            }
            if(!flag) ElemList.Add(tmp);
        }
        void diaplayList()
        {
            int len=ElemList.GetLen() , i;
            for(i=0;i<len;i++)
                printf("%d,%d,%d
    ",ElemList[i].x,ElemList[i].y,ElemList[i].v);
        }
        void displayMatrix()
        {
            T** show=SpaMat2Mat();
            int i , j;
            for(i=0;i<row;i++)
            {
                for(j=0;j<col;j++)
                    printf("%d ",show[i][j]);
                printf("
    ");
            }
        }
        T** SpaMat2Mat()
        {
            T ** re;
            int i ,j ;
            re=new T*[row];
            for(i=0;i<row;i++) re[i]=new T[col];
            for(i=0;i<row;i++)
                for(j=0;j<col;j++)
                    re[i][j]=NULL;//初始化声明为零
            for(i=0;i<ElemList.GetLen();i++) re[ElemList[i].x][ElemList[i].y]=ElemList[i].v;
            return re;
        }
        CSparseMatrix transpose()
        {
            CSparseMatrix obj;
            obj.col=col;
            obj.row=row;
            Elem empty={0,0,0};
            int i , j , len=ElemList.GetLen();
            for(i=0;i<len;i++) obj.ElemList.Add(empty);//初始化
            int * num=new int[col];
            int * cPot=new int[col];
            zeros(num,col);                //创建好数组后赋值为零
            for(i=0;i<len;i++) num[ElemList[i].y]++;//列数统计
            for(i=0;i<col;i++) cPot[i]=(i ? num[i-1] : 0) + (i ? cPot[i-1] : 0);//对应列下标 标记
            for(i=0;i<len;i++)
            {
                Elem get=ElemList[i];
                Elem set={get.y,get.x,get.v};
                obj.ElemList.SetAt(cPot[get.y]++,set);
            }
            return obj;
        }
        CSparseMatrix Multi(CSparseMatrix right)
        {
            CSparseMatrix<T> obj;
            obj.row=row;
            obj.col=right.col;
            int i , j , k;
            int x , y , v;
            int lastX=ElemList[0].x;
            int * ctemp=new int[right.col];
            zeros(ctemp,right.col);
            for(i=0;i<ElemList.GetLen();i++)
            {
                x=ElemList[i].x;
                lastX=x;
                y=ElemList[i].y;
                v=ElemList[i].v;
                for(j=0;j<right.ElemList.GetLen();j++)//A.y==B.x
                    if(right.ElemList[j].x==y)
                        ctemp[right.ElemList[j].y]+= v * right.ElemList[j].v;
                if( i==ElemList.GetLen()-1  ||  lastX!=ElemList[i+1].x )//检测到循环即将结束 或者 行数据即将更新
                {
                    for(k=0;k<right.col;k++) 
                        if(ctemp[k])
                        {
                            Elem tmp={x,k,ctemp[k]};
                            obj.ElemList.Add(tmp);
                        }
                    zeros(ctemp,right.col);//追加数据完毕,初始化。
                }
            }
            return obj;
        }
    protected:
    private:
        int row;
        int col;
        typedef struct Elem{
            int x;
            int y;
            T v;
        }Elem;
        CLinkList<Elem> ElemList;
    };
  • 相关阅读:
    Weka回归
    R语言知识体系概览
    HDU 1020 Encoding POJ 3438 Look and Say
    FtpClient.storeFile返回false解决方法
    Docker安装ruby2.1
    定制一套属于自己的博客样式(转载)
    Docker常见问题解决
    The method getJspApplicationContext(ServletContext) is undefined for the type
    解决httpServletRequest.getParameter获取不到参数
    淘宝知名工程师(转载)
  • 原文地址:https://www.cnblogs.com/TQCAI/p/7606814.html
Copyright © 2020-2023  润新知