内存映射+矩阵

接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MemoryMatrix
{
    public interface IMemoryMapMatrix
    {
        /// <summary>
        /// 矩阵值读取
        /// </summary>
        /// <param name="x">横坐标</param>
        /// <param name="y">纵坐标</param>
        /// <returns>矩阵(x,y)存储的值</returns>
        double? Read(int x, int y);

        /// <summary>
        /// 矩阵值写入
        /// </summary>
        /// <param name="x">横坐标</param>
        /// <param name="y">纵坐标</param>
        /// <param name="val">矩阵(x,y)写入的值</param>
        void Write(int x, int y, double val);
    }
}

实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.MemoryMappedFiles;

namespace MemoryMatrix
{
    public class MemoryMapMatrix:IMemoryMapMatrix
    {
        private readonly MatrixBound m_MatrixBound;
        private MemoryMappedViewAccessor m_ViewAccessor;
        private int Size;

        public MemoryMapMatrix(int top, int bottom, int left, int right)
        {
            m_MatrixBound = new MatrixBound()
            {
                top = top,
                bottom = bottom,
                left = left,
                right = right
            };
            Size=sizeof(double);
            //矩阵容量
            long capacity=(m_MatrixBound.top-m_MatrixBound.bottom+1)*(m_MatrixBound.right-m_MatrixBound.left+1)*Size;
            MemoryMappedFile m_MemoryMappedFile = MemoryMappedFile.CreateFromFile(@"E://TempData//Test.mp",System.IO.FileMode.OpenOrCreate,"TestMp",capacity);
            m_ViewAccessor = m_MemoryMappedFile.CreateViewAccessor();
        }

        private long GetIndex(int x, int y)
        {
            if (x < m_MatrixBound.left || x > m_MatrixBound.right || y < m_MatrixBound.bottom || y > m_MatrixBound.top) return -1;
            return (x - m_MatrixBound.left) + (m_MatrixBound.top - y) * (m_MatrixBound.right - m_MatrixBound.left + 1);
        }

        public double? Read(int x, int y)
        {
            long index = GetIndex(x, y);
            if (index == -1) return null;
            double val;
            m_ViewAccessor.Read(index * Size, out val);
            return val;
        }

        public void Write(int x, int y, double val)
        {
            long index = GetIndex(x, y);
            if (index == -1) return;
            m_ViewAccessor.Write(index * Size, val);
        }

        
    }

    public struct MatrixBound
    {
        /// <summary>
        /// 上
        /// </summary>
        public int top { get; set; }
        /// <summary>
        /// 下
        /// </summary>
        public int bottom { get; set; }
        /// <summary>
        /// 左
        /// </summary>
        public int left { get; set; }
        /// <summary>
        /// 右
        /// </summary>
        public int right { get; set; }
    }
}

测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MemoryMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            MemoryMapMatrix mp = new MemoryMapMatrix(10, 8, 3, 6);

            mp.Write(3, 10, 10);
            mp.Write(4, 10, 12.3);
            mp.Write(5, 10, 187);
            mp.Write(6, 10, 109);

            mp.Write(3, 9, 3);
            mp.Write(4, 9, 8);
            mp.Write(5, 9, 10000);
            mp.Write(6, 9, 98);

            mp.Write(3, 8, 22);
            mp.Write(4, 8, 2222);
            mp.Write(5, 8, 222222);
            mp.Write(6, 8, 2);

            for (int y = 10; y >= 8; y--)
            {
                for (int x = 3; x <= 6; x++)
                {
                    Console.Write(mp.Read(x, y).ToString() + " ");
                }

                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}

测试结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值