自定义泛型N维空间数组

Posted 大胡子毛绒老头

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义泛型N维空间数组相关的知识,希望对你有一定的参考价值。

class Space<T> : IEnumerable<Space<T>> {
    public T Filler {
        get {
            if (!ed) {
                ed = true;
                return (filler = Top.create());
            }
            return filler;
        }
    }
    public Space<T> Upper { get; private set; }
    public Space<T> Top => Upper?.Top ?? this;

    private bool ed;
    private Func<T> create;
    private T filler;
    public int[] Chain { get; set; }
    public int[] Dementions { get; }
    Space<T>[] all;

    public Space(Func<T> create, int i, params int[] indexes) : this(new int[0], new[] { i }.Concat(indexes).ToArray()) {
        this.create = create;
    }

    private Space(int[] chain, params int[] indexes) {
        Chain = chain;
        Dementions = indexes;
        var i = indexes[0];
        all = new Space<T>[i];
        var chains = Enumerable.Range(0, i).Select(e => chain.Concat(new int[] { e }).ToArray()).ToArray();
        if (indexes.Length == 1) {
            while (i-- > 0) {
                all[i] = new Space<T>(chains[i]);
                all[i].Upper = this;
            }
        } else {
            var _indexes = indexes.Skip(1).ToArray();
            while (i-- > 0) {
                all[i] = new Space<T>(chains[i], _indexes);
                all[i].Upper = this;
            }
        }
    }

    private Space(int[] chain) {
        Chain = chain;
    }

    public Space<T> this[int i, params int[] indexes] {
        get {
            if (all == null) {
                return (Filler as Space<T>)?[i, indexes];
            }
            if (indexes.Length == 0) {
                return all[i];
            }
            if (indexes.Length == 1) {
                return all[i][indexes[0]];
            }
            return all[i][indexes[0], indexes.Skip(1).ToArray()];
        }
    }

    public override string ToString() {
        return all?.Aggregate(string.Empty, (a, s) => a + s + ,).Trim(,) ?? Filler.ToString();
    }

    public override bool Equals(object obj) {
        if (GetType() != obj?.GetType()) {
            return false;
        }
        return GetHashCode() == obj.GetHashCode();
    }

    public override int GetHashCode() {
        if (Upper == null) {
            return base.GetHashCode();
        }
        return Top.GetHashCode() - Chain.GetHashCode();
    }

    public IEnumerator<Space<T>> GetEnumerator() {
        return ((IEnumerable<Space<T>>)all).GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator() {
        return ((IEnumerable<Space<T>>)all).GetEnumerator();
    }
}

 

以上是关于自定义泛型N维空间数组的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段10—— 数组的响应式方法

C# 使用自定义的泛型函数/方法对泛型数组进行四则运算

泛型动态数组

java自定义泛型 面试题:接收任意数组进行反转

自定义泛型_无多态_通配符无泛型数组_jdk7泛型使用

vs code 自定义代码片段