自定义泛型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();
    }
}
时间: 2024-07-30 13:45:15

自定义泛型N维空间数组的相关文章

CI获取自定义配置文件中的二维配置数组

CI获取自定义配置文件中的二维配置数组:如:在admin.php配置文件中有如下配置:$config['admin_menu'][] = array( 'name' => '商品管理', 'c' => 'goods', 'a' => 'lists', 'icon'=>' icon-gift', 'item' => array( //商品 0 => array('name' => '商品列表', 'c' => 'goods', 'a' => 'lists

.NET基础之自定义泛型

在.NET中泛型使用非常频繁,在控制台应用程序中,默认的引入了System.Collection.Generics名称空间,其中就提供了我们经常使用的泛型:List<T>和Dictionary<T>,相信用过它们的都知道它们的强大.还有一种我们经常使用的简单的泛型:System.Nullable<T>,即可空类型.我们可以: System.Nullable<int> nullableInt; 声明一个可空的int类型,由于C#语法对这个做了简化通常我们都不这

【转】CUDA与二维动态数组

1 /* 2 * Copyright 徐洪志(西北农林科技大学.信息工程学院). All rights reserved. 3 * Data: 2012-4-22 4 */ 5 // 6 // 此程序是演示了二维动态数组空间申请和与显存数据相互拷贝的两种方法 7 #include <stdio.h> 8 //#include <cutil_inline.h> 9 #include <iostream> 10 #include <cuda_runtime.h>

一维动态数组和二维动态数组的创建和使用

#include<stdio.h> #include<malloc.h> void main(){ int *a,n=10,i; /* calloc()函数的原型是:(void *)calloc(unsigned n,unsigned size) calloc()函数用于向系统动态申请n个,每个占sizege字节的内存单元,函数返回值为所申请的内存空间首地址 malloc和calloc主要区别在于,当系统的内存只剩下一些非常小的碎片时,用calloc函数设计的动态数组的时间效率优于

java JDK8 学习笔记——第18章 自定义泛型、枚举与注释

第十八章 自定义泛型.枚举与注释 18.1 自定义泛型 泛型定义: (1)仅定义在方法上的泛型语法 (2)用来限制泛型可用类型的extends与super关键字(3)?类型通配字符的使用 18.1.1 使用extends与? 1.若extends之后指定了类与接口,想再指定其他接口,可以使用&连接. 2.如果B是A的子类,而Node< B>可视为一种Node< A>,则称Node具有共变性或有弹性的.Java泛型不具有共变性,可以使用类型通配字符?与extends来声明变量

二维动态数组

之前都是写的小程序,一直用的静态数组,也没出现问题. 可是,最近碰到大型程序和工程,这时就要用动态数组了. 因为静态数组时保存在栈中的,而动态数组保存在堆中. 计算机的栈只有1M大小,而堆可以理论上达到计算机内存大小, 可见当大型工程数据量非常大时,必须使用动态数组了. c++的动态数组的建立和删除要用到new和delete, new用来开辟内存空间,delete用来删除内存空间. 建立二维动态数组test,第一维大小为a,第二维大小为b. 下面时具体实现代码: 1 int **test=new

C++中使用模板,new创建2维动态数组

1 // 使用模板和new创建2维动态数组 2 3 #include<iostream> 4 #include<exception> 5 #include<cstdlib> 6 #include<iomanip> 7 using namespace std; 8 9 template<class Type> 10 bool Make2DArray(Type **&x,int rows,int cols) 11 { 12 int i; 13

结对开发之返回一个二维整数数组中最大联通子数组的和

一.题目要求 输入一个二维整形数组,数组里有正数也有负数.二维数组首尾相接,象个一条首尾相接带子一样.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.要求时间复杂度为O(n)题目:返回一个二维整数数组中最大子数组的和 二.解题思路 先对二维数组进行了重构,形成一个环状二维数组,然后再用求二维数组子矩阵最大和的方法求得最终结果. 三.程序代码 2 #include<iostream.h> 3 int main(int argc, char* argv[]

返回一个二维整数数组中最大联通子数组的和

题目: 输入一个二维整形数组,数组里有正数也有负数. 求所有子数组的和的最大值. 要求: 两人结对完成编程任务. 一人主要负责程序分析,代码编程. 一人负责代码复审和代码测试计划. 发表一篇博客文章发表一篇博客文章讲述设计思想,出现的问题,可能的解决方案(多选).源代码.结果截图.总结. 思想: 在看到本题目后,想了很久也没有想到比较满意的解决方法,觉得题目比较难,超出了我的能力范围.不过,个人认为可能用到了图论的知识,但是学的不好.根据上图给定的二维数组求解最大联通数组也许比较简单.以一个非负