Coder Social home page Coder Social logo

loafey / dimarray Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 1.0 27 KB

A simple library abstracting 1D arrays to work like multiple dimension arrays.

License: MIT License

C# 100.00%
csharp csharp-library csharp-core dotnet dotnetcore dot-net-core dotnet-core array arrays multidimensional-arrays

dimarray's Introduction

DimArray

A simple library abstracting 1D arrays to work like multiple dimension arrays.

Why?

Since multidimensional arrays are generally not performant this library kind of works around the problem by making a 1D array work like a 2D or 3D array.

Installation

Either download a prebuilt binary from the release section. Otherwise clone the repository and use msbuild /p:TargetFrameworks=net472\ to compile against Mono 4.7.2 or dotnet build /p:TargetFrameworks=netcoreapp3.1\ to target Net Core 3.1.

Drop the DimArray.dll into the project directory and add this to the .csproj in the right ItemGroup.

<Reference Include="DimArray">
    <HintPath>DimArray.dll</HintPath>
</Reference>

Usage

class Program
{
    static void Main(string[] args)
    {
        // This creates a "2D" array of the specifed size.
        var twoArray = new DimArray.TwoDim<int>(5, 5);

        // Use it like you would use a normal C# 2D jagged array.
        for (int x = 0; x < twoArray.X; x++)
        {
            for (int y = 0; y < twoArray.Y; y++)
            {
                twoArray[x, y] = Math.Pow(x + y, 2);
            }
        }
        
        // This creates a "3D" array of the specifed size.
        var threeArray = new DimArray.ThreeDim<int>(5, 5, 5);

        // Use it like you would use a normal C# 3D jagged array.
        for (int x = 0; x < threeArray.X; x++)
        {
            for (int y = 0; y < threeArray.Y; y++)
            {
                for (int z = 0; z < threeArray.Z; z++)
                {
                    threeArray[x, y, z] = Math.Pow(x + y + z, 2);
                }
            }
        } 

        // This creates a "4D" array of the specifed size using an NDim.
        // NDims can be any size, and size and index are passed in as int arrays.
        var nDim = new DimArray.NDim<int>(new int[]{5, 5, 5, 5});

        // Use it like you would use a normal C# jagged array.
        for (int x = 0; x < nDim.Sizes[0]; x++)
        {
            for (int y = 0; y < nDim.Sizes[1]; y++)
            {
                for (int z = 0; z < nDim.Sizes[2]; z++)
                {
                    for (int w = 0; w < nDim.Sizes[3]; w++)
                    {
                        nDim[new int[]{x, y, z, w}] = Math.Pow(x + y + z + w, 2);
                    }
                }
            }
        } 
        
        // You can even print it!
        Console.WriteLine(threeArray);
        Console.WriteLine(twoArray.ToString1D());
        Console.WriteLine(nDim); // nDims are currently printed as 1D.
    }
}

Other implementations

A CPP version can be found here made by AnzoDK.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

dimarray's People

Contributors

loafey avatar

Watchers

James Cloos avatar  avatar Anzo avatar

Forkers

anzodk

dimarray's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.