Show / Hide Table of Contents

MultidimLib - Quick start

Introduction

MultidimLib is a .NET Core 3 class library that provides a way to work with n-dimensional spaces, formally known as multidimensional arrays, that store objects which can self determine their location inside these spaces. Moreover, it allows for linearly mapping a location from one space into the corresponding one in another space. To expose this functionality, the library implements the following main components:

  • Point: A collection of coordinates that represent a location. It is implemented by the Point class.
  • Container: An object that wraps or represents data. It is implemented by the Container<T> and the Container classes.
  • Space: An n-dimensional array that stores containers at locations specified by points. It is implemented by the Space<C> class.

Additional information on these and other -important- components can be found in the API reference.

Usage

Initialization

The above mentioned components can be initialized either by directly calling their constructors or, in some cases, through inheritance.

Initializing points

Library's functions and methods that expect a point as an argument can implicitly initialize it by simply passing to them the point's coordinates. However, a point can be explicitly initialized in the following ways:

  1. By calling the constructor with as many coordinates as needed:
// Initialize a point with one single coordinate equals to 0.
var p1 = new Point();

// Initialize a one coordinate point.
var p2 = new Point(890);

// Initialize a two coordinate point.
var p3 = new Point(2, -4);

// Initialize a two coordinate point (trailing 0s are ignored).
var p4 = new Point(1, 45, 0, 0);

// Initialize a five coordinate point.
var p5 = new Point(0, 0, 0, 0, 8);
  1. By calling the constructor with an IEnumerable<int> as argument:
var coordinateList = new List<int> { 3, 4, 7 };
var p6 = new Point(coordinateList);
  1. By transforming an IEnumerable<int>:
int[] coordinateArr = { 1, -2, 35, 6, 0, -4 };
var p7 = coordinateArr.P();
  1. By parsing a point's string:
var p8 = Point.FromString("(0, -5, +3, 2, 1)"); // White space is ignored.

Initializing containers

A container can be initialized in the following ways:

  1. By calling the constructor with the data to be wrapped:
var c1 = new Container<int>(-5);
var c2 = new Container<double>(1.99);

// A container may be initialized empty and wrap some data at a later stage.
var c3 = new Container<string>(); 
c3.Value = "lorem";
  1. By transforming the data to be wrapped:
// Initialize a new Container<string>.
var c4 = "I'm wrapped inside a container!".C();

// Initialize a new Container<int>.
var c5 = 4.C();

// Initialize a new Container<double>.
var c6 = 3.14159.C();
  1. Through inheritance:
class ChessPiece : Container {    
    public string Color { get; }    
    public ChessPiece(string color) {
        Color = color;
    }    
}

class Bishop : ChessPiece {    
    public Bishop(color string) : base(color) { }    
}
var blackBishop1 = new Bishop("black");
var blackBishop2 = new Bishop("black");
var whiteBishop1 = new Bishop("white");
var whiteBishop2 = new Bishop("white");

Initializing spaces

A space can be initialized in the following ways:

  1. By calling the constructor with as many dimensions as needed that will shape the space:
// Initialize a three dimensional space of size 3x3x3.
var rubik = new Space<Container<string>>(
    // First dimension from coordinates 0 to 2 through the factory method.
    Dimension.FromLength(3),
    // Second dimension from coordinates 0 to 2 through the constructor.
    new Dimension(0, 2),
    // Third dimension from coordinates 0 to 2.
    Dimension.FromLength(3)
);

// Initialize a two dimensional space of size 6x5.
var wordle = new Space<Container<char>>(
    // First dimension from coordinates 1 to 6.
    new Dimension(1, 6),
    // Second dimension from coordinates 1 to 5.
    new Dimension(1, 5)
);

// Initialize a one dimensional space of size 10000.
var line = new Space<Container<int>>(
    // First dimension from coordinates -5000 to 5000.
    Dimension.FromLength(-5000, 5000)
);
  1. Through inheritance:
class ChessBoard : Space<ChessPiece> {    
    public ChessBoard() : base(Dimension.FromLength(8), Dimension.FromLength(8)) { }
}
var chess = new ChessBoard();

Storing data

Use bracket notation to store containers inside spaces at specific locations:

// Implicit initialization of a point.
chess[0, 2] = blackBishop1;
chess[0, 5] = blackBishop2;

// Explicit initialization of a point.
chess[new Point(7, 2, 0)] = whiteBishop1;

// Initialization of a point through its string representation.
var p = Point.FromString("(7, 5)");
chess[p] = whiteBishop2; 

Retrieving data

Use bracket notation to retrieve the container stored at a specific location inside a space:

var piece = board[0, 2];

Call a container's Locate() method to retrieve the location, represented by points, of the container inside the specified spaces:

// Retrieve the location of a container inside a particular space. It is possible to specify in a single method call as many spaces as needed from where to retrieve the container's location.
var locationCollection = blackBishop1.Locate(chess);

// Retrieve the location of a container from every space that stores it by not specifying any particular space in the method call.
var locationDictionary = blackBishop1.Locate();

Call a space's Points() method to retrieve and iterate over all possible points that fit inside the space:

// Iterate over the space's points sorted according to the original dimension order.
foreach (Point point in rubik.Points())
    Console.WriteLine(rubik[point]);

// Iterate over the space's points sorted according to a specific dimension order.
foreach (Point point in rubik.Points(2, 0, 1))
    Console.WriteLine(rubik[point]);

Removing data

Use bracket notation to remove a container from a space at a specific location by setting null thereat:

chess[0, 2] = null;

Call a container's Unlink() method to remove it from everywhere inside the specified spaces:

// Remove a container from everywhere inside a space. It is possible to specify in a single method call as many spaces as needed from where to remove the container.
blackBishop1.Unlink(chess);

// Remove a container from every space that stores it by not specifying any particular space in the method call.
blackBishop1.Unlink();

Call a space's Clear() method to remove from it every stored container:

chess.Clear();

Call a space's IsEmptyAt() method to check whether a specific location is empty:

var isEmpty = chess.IsEmptyAt(0, 4);

Location mapping

A point that represents a location in one space can be mapped into the corresponding location in another space by calling the ToR1Point(), FromR1Point(), ToRnPoint(), and FromRnPoint()methods of a space.

var space1 = new Space<Container<int>>(
    new Dimension(5, 15),
    new Dimension(-4, 2)
);
var space2 = new Space<Container<string>>(
    new Dimension(24, 30),
    new Dimension(-11, -1)
);

var pointInSpace1 = new Point(6, -3);
var pointInSpace2 = space2.FromRnPoint(pointInSpace1, space1);

API reference

Find details on MultidimLib's public members in the following links.

Namespace Link
MultidimLib Visit
MultidimLib.Exceptions Visit
MultidimLib.Extensions Visit
Back to top MultidimLib from Ecuador.