user613326
user613326

Reputation: 2180

jagged arrays of multiple types

i'm not really sure if it is possible but can a jagged array contain multiple types

i need a data structure layered of which the first 2D layer is of the type bytes Then the next 2D layers can be integer type or float type and finally 2D layer will be again byte, the total number of layers can vary.

is that possible if yes, how would i declare it in C# and how in c++ ?

Upvotes: 2

Views: 1257

Answers (5)

user7116
user7116

Reputation: 64098

An approach in C# could be one of the following:

// Base class for each layer
abstract class Layer
{
    public abstract int Rank { get; }
    public abstract int GetLength(int dimension);
}

// Individual layers would inherit from the base class and handle
// the actual input/output
class ByteLayer : Layer
{
    private byte[,] bytes;

    public override int Rank { get { return 2; } }

    public override int GetLength(int dimension)
    {
        return this.bytes.GetLength(dimension);
    }

    public ByteLayer(byte[,] bytes)
    {
        this.bytes = bytes;
    }

    // You would then expose this.bytes as you choose
}
// Also make IntLayer and FloatLayer

You could then create an abstraction to hold each of these layer types:

class Layers : IEnumerable<Layer>
{
    private ByteLayer top, bottom;
    private List<Layer> layers = new List<Layer>();

    public ByteLayer Top { get { return top; } }
    public IEnumerable<Layer> Middle { get { return this.layers.AsEnumerable(); } }
    public ByteLayer Bottom { get { return bottom; } }

    public Layers(byte[,] top, byte[,] bottom)
    {
        this.top = top;
        this.bottom = bottom;
    }

    public void Add(int[,] layer)
    {
        this.layers.Add(new IntLayer(layer));
    }

    public void Add(float[,] layer)
    {
        this.layers.Add(new FloatLayer(layer));
    }

    public IEnumerator<Layer> GetEnumerator()
    {
        yield return bottom;
        foreach (var layer in this.layers) yield return layer;
        yield return top;
    }
}

Upvotes: 2

returneax
returneax

Reputation: 709

A vector of Layer objects would work. A Layer object would have a pointer to a 2D array and a "hint" value which would denote the type of the 2D array. When adding a new Layer you would push to the vector a Layer, which upon construction is told what type it is to be.

Upvotes: 1

aib
aib

Reputation: 46981

You've already divided the data structure up into layers, why not do that in code as well? Use two 2D byte arrays for the first and the last layers and a 3D array for the set of layers in between?

For the data type in the intermediate layers, you can use an union if you take care to remember which type is stored in what element. If not, you can use a struct or even a class.

Also, you can wrap the whole thing in a class, too.

Though this is a suspiciously specific question. You've already decided that you need to store a single jagged array with a jumble of types. Why?

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272667

I don't know about C#, but you cannot do this in C++ (at least not in a way that is simultaneously useful, meaningful, and well-defined).

As a first-cut workaround, I would suggest a struct of arrays:1

struct Stuff {
    byte *bytes;
    int  *ints;
    byte *bytesAgain;
};

This doesn't deal with your desire to have the number of layers vary. It's not clear how that could work; how would you know what each layer is supposed to contain?

[Perhaps if you edit your question to explain the problem you're trying to solve, I can give a more focused answer.]


1. And as this is C++, not C, consider using a container like std::vector rather than raw C-style arrays and pointers.

Upvotes: 3

Colin D
Colin D

Reputation: 5661

Sure its possible. Just use some objects and some abstraction.

Have you jagged arrays be some some Object type. The object can then have a getType() and a getValue(). GetType() would return some enum and getValue() would return the actual object.

I see this as non-intuitive and not easily maintainable. Is there a better way you could store your data? How about having each 'layer' as a seperate array/object?

Upvotes: 1

Related Questions