创建一个仅指向另一个数组的一部分的数组

本文关键字:数组 另一个 一部分 一个 创建 | 更新日期: 2023-09-27 18:30:23

我有一个包含引用类型元素的巨大数组,我想创建许多其他数组,这些数组基本上只指向该大数组的特定部分。

换句话说,我想创建"索引器"或"带长度的指针"。

在C++使用指针很容易做到这一点,并且为每个指针分配一个长度,例如创建一个包含长度指针的结构。

如何在 C#/.NET 中实现此目的?

重点是避免复制任何东西,我只想要指向内存中已经存在的数组中特定部分的指针。

有什么想法吗?

创建一个仅指向另一个数组的一部分的数组

Jon 建议使用 ArraySegment<T> 可能是你想要的。但是,如果您想以C++的方式表示指向数组内部的指针,这里有一些代码。不作任何明示或暗示的保证,使用风险由您自行承担。

此代码不会以任何方式跟踪内部指针的"长度",但如果需要,添加该功能非常容易。

internal struct ArrayPtr<T>
{
  public static ArrayPtr<T> Null { get { return default(ArrayPtr<T>); } }
  private readonly T[] source;
  private readonly int index;
  private ArrayPtr(ArrayPtr<T> old, int delta)
  {
    this.source = old.source;
    this.index = old.index + delta;
    Debug.Assert(index >= 0);
    Debug.Assert(index == 0 || this.source != null && index < this.source.Length);
  }
  public ArrayPtr(T[] source)
  {
    this.source = source;
    index = 0;
  }
  public bool IsNull()
  {
    return this.source == null;
  }
  public static bool operator <(ArrayPtr<T> a, ArrayPtr<T> b)
  {
    Debug.Assert(Object.ReferenceEquals(a.source, b.source));
    return a.index < b.index;
  }
  public static bool operator >(ArrayPtr<T> a, ArrayPtr<T> b)
  {
    Debug.Assert(Object.ReferenceEquals(a.source, b.source));
    return a.index > b.index;
  }
  public static bool operator <=(ArrayPtr<T> a, ArrayPtr<T> b)
  {
    Debug.Assert(Object.ReferenceEquals(a.source, b.source));
    return a.index <= b.index;
  }
  public static bool operator >=(ArrayPtr<T> a, ArrayPtr<T> b)
  {
    Debug.Assert(Object.ReferenceEquals(a.source, b.source));
    return a.index >= b.index;
  }
  public static int operator -(ArrayPtr<T> a, ArrayPtr<T> b)
  {
    Debug.Assert(Object.ReferenceEquals(a.source, b.source));
    return a.index - b.index;
  }
  public static ArrayPtr<T> operator +(ArrayPtr<T> a, int count)
  {
    return new ArrayPtr<T>(a, +count);
  }
  public static ArrayPtr<T> operator -(ArrayPtr<T> a, int count)
  {
    return new ArrayPtr<T>(a, -count);
  }
  public static ArrayPtr<T> operator ++(ArrayPtr<T> a)
  {
    return a + 1;
  }
  public static ArrayPtr<T> operator --(ArrayPtr<T> a)
  {
    return a - 1;
  }
  public static implicit operator ArrayPtr<T>(T[] x)
  {
    return new ArrayPtr<T>(x);
  }
  public static bool operator ==(ArrayPtr<T> x, ArrayPtr<T> y)
  {
    return x.source == y.source && x.index == y.index;
  }
  public static bool operator !=(ArrayPtr<T> x, ArrayPtr<T> y)
  {
    return !(x == y);
  }
  public override bool Equals(object x)
  {
    if (x == null) return this.source == null;
    var ptr = x as ArrayPtr<T>?;
    if (!ptr.HasValue) return false;
    return this == ptr.Value;
  }
  public override int GetHashCode()
  {
    unchecked
    {
      int hash = this.source == null ? 0 : this.source.GetHashCode();
      return hash + this.index;
    }
  }
  public T this[int index]
  {
    get { return source[index + this.index]; }
    set { source[index + this.index] = value; }
  }
}

现在我们可以做这样的事情:

double[] arr = new double[10];
var p0 = (ArrayPtr<double>)arr;
var p5 = p0 + 5;
p5[0] = 123.4; // sets arr[5] to 123.4
var p7 = p0 + 7;
int diff = p7 - p5; // 2

听起来你正在寻找类似ArraySegment<T>的东西。与我之前的想法相反,它确实有一个索引器和实现IEnumerable<T>等 - 它只是通过显式接口完成的。

示例代码:

using System;
using System.Collections.Generic;
static class Test
{
    static void Main()
    {
        string[] original = { "The", "quick", "brown", "fox", "jumped", "over",
                "the", "lazy", "dog" };
        IList<string> segment = new ArraySegment<string>(original, 3, 4);
        Console.WriteLine(segment[2]); // over
        foreach (var word in segment)
        {
            Console.WriteLine(word); // fox jumped over the
        }
    }
}

编辑:如评论中所述,ArraySegment<T>仅在.NET 4.5中真正"功能齐全"。.NET 4 版本不实现任何接口。

您可以使用

LINQ:

yourArray.Skip(startIndex).Take(numberToTake)

查询是延迟计算的。