内存映射+矩阵
来源:互联网 发布: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
- 内存映射+矩阵
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- 内存映射
- three.js环境下的一种飞线动画实现方法
- 网络编程(30)—— IP地址分类和子网掩码的作用
- JAVA中==和equals的区别
- 笑死了,打错一个字,竟然...
- 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来. 提示:帧头和帧尾分别是head和tail 字符串”asdheadhauboisoktail”中headhaubo
- 内存映射+矩阵
- prolog学习_修道士野人问题
- sed的详细用法demo
- 2016年11月
- 利用命令来打开所有程序,这个装逼给满分!
- javaweb项目在linux系统中测试时的乱码问题解决
- View的事件分发机制
- Atitit 图像处理 灰度图片 灰度化的原理与实现
- Atitit Gaussian Blur 高斯模糊 的原理and实现and 用途