/// <summary> /// 增加一个,如已有,则覆盖。 /// </summary> /// <param name="key"></param> public void Add(MatrixItem item) { if (!this.data.ContainsKey(item.Row)) { this.data[item.Row] = new Dictionary <int, MatrixItem>(); } this.data[item.Row][item.Col] = item; }
/// <summary> /// 从二进制文件中读取。 /// 格式定义:1. 前3个为int,分别指行、列和集合数量;2.后面子项集合,分别为行(int)列(int)标号和双精度值。 /// </summary> /// <param name="path"></param> /// <returns></returns> public static SparseMatrix FromBinary(string path) { SparseMatrix matr = null; using (BinaryReader br = new BinaryReader(new FileStream(path, FileMode.Open, FileAccess.Read))) { int RowCount = br.ReadInt32(); int ColCount = br.ReadInt32(); matr = new SparseMatrix(RowCount, ColCount); int itemCount = br.ReadInt32(); for (int i = 0; i < itemCount; i++) { //各个节点位置无关,可以考虑并行存取,但是由于内存共享,还得序列化读取。 MatrixItem item = new MatrixItem() { Row = br.ReadInt32(), Col = br.ReadInt32(), Value = br.ReadDouble() }; matr.MatrixItems.Add(item); } return(matr); } }