简体   繁体   English

一维数组的重载运算符[]

[英]Overloading Operator[] For Single Dimension Array

To solve a problem I already asked here Best/Fastest Way To Change/Access Elements of a Matrix 为了解决问题,我已经在这里问过最佳/最快的方式来更改/访问矩阵元素

I'm using single dimension arrays to store matrixes. 我正在使用一维数组存储矩阵。 But accessing elements of the matrix becomes a cumbersome task. 但是访问矩阵的元素变得繁琐。

I'm currently storing my matrixes in arrays like this 我目前正在将矩阵存储在这样的数组中

type[numberOfRows * numberOfColumns] myArray;

And to access the [n][m] element I have to type this 要访问[n][m]元素,我必须输入

blargh = myArray[(n*numberOfRows)+m];

... I'm wondering if its possible to overload / create new operators [][] that would 'translate' myArray[n][m] to myArray[(n*numberOfRows)+m] . ...我想知道是否有可能重载/创建新的运算符[][] ,从而将myArray[n][m] '转换为myArray[(n*numberOfRows)+m] And IF that IS possible, would that hinder the performance too much. 如果可能的话,那会严重阻碍性能。

edit: turns out a 'forced-inline' method yields a performance gain. 编辑:事实证明,“强制内联”方法可提高性能。

    [MethodImpl(MethodImplOptions.AggressiveInlining)]public void set(int x, int y, T value)
    {
        array[(x * wid) + y] = value;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]public T get(int x, int y)
    {
        return array[(x*wid) + y];
    }

It is not possible to overload the operator for the array type, or change its definition; 无法为数组类型重载运算符或更改其定义; that can only be done from the definition of that type, which you can't control. 只能通过您无法控制的类型定义来完成。

What you can do is create your own type that wraps the array, and that overloads the operators that you want: 可以做的是创建自己的类型,该类型将包装数组,并使所需的运算符超载:

public class Matrix<T>
{
    private T[] array;

    public T this[int row, int column]
    {
        get { return array[row + column]; }
    }
}

Whether or not the performance difference (which should be small, but not nothing) are an issue for your program is something that you're going to need to profile and measure. 性能差异(应该不大,但不算什么)是否是程序的问题,这是您需要分析和衡量的问题。

One way to implement double indexing (ie matrix[r][c] syntax) would be using a Proxy Pattern (also known as the Surrogate Pattern ). 一种实现双重索引(即matrix[r][c]语法)的方法是使用代理模式 (也称为代理模式 )。

The idea is to overload the top-level operator[] to return a special object that "remembers" the first index and the array, and provides its own overload of the operator[] that combines the "remembered" first-level index with the supplied second-level index to produce the actual "flat" index into the data array. 这个想法是重载顶级operator[]以返回一个特殊的对象,该对象“记住”第一个索引和数组,并提供其自己的operator[]重载,该operator[]将“记住的”第一级索引与提供了第二级索引以将实际的“扁平”索引生成到数据数组中。

In terms of the program logic, the proxy object represents the column of your matrix. 就程序逻辑而言,代理对象代表矩阵的列。 Here is how you can implement this: 这是您可以如何实现的方法:

public class Matrix<T> {
    private readonly T[] data;
    private readonly int rowCount;
    public class Column {
        private readonly Matrix<T> m;
        private readonly int r;
        internal Column(Matrix<T> m, int r) {
            this.m = m;
            this.r = r;
        }
        public T this[int c] {
            get {
                return m.data[m.rowCount*r + c];
            }
        }
    }
    public Column this[int r] {
        get {
            return new Column(this, r);
        }
    }
}

The performance implications of this approach is that there would be an object created each time that you access the top level matrix. 这种方法的性能含义是,每次访问顶层矩阵时都会创建一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM