内存映射+矩阵

来源:互联网 发布:isp图像处理编程 编辑:程序博客网 时间:2024/05/01 14:46

接口:

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
原创粉丝点击