Welcome to Dream.In.Code
Become a C# Expert!

Join 136,912 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,735 people online right now. Registration is fast and FREE... Join Now!




The ideal data structure for a list of parameters

 
Reply to this topicStart new topic

The ideal data structure for a list of parameters

killnine
2 Apr, 2008 - 10:26 AM
Post #1

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 107



Thanked: 3 times
My Contributions
Hey guys,

Hope you are enjoying the day =). It's another wonderful day at work coding.

I just had a question to pose to the members here. I think I have a possible solution, but I am wondering if there may be more eloquent solutions out there (there typically are).

I have a list of parameters. Each parameter can be a scalar, or it can be a matrix of values from 2x2 all the way up to 15x21. I was thinking about how to store each of these parameters (I am displaying them on a grid) and had a solution, but I don't know if its confusing and if there is a better way:

CODE

//Note that parameterDimensions[] is an array that has the number of ROWS in [0] and number of COLUMNS in [1]

            double[,] formattedParameterArray = new double[parameterDimensions[0],parameterDimensions[1]];
            for (int c = 0; c < parameterDimensions[1]; c++)
            {

                for (int r = 0; r < parameterDimensions[0]; r++)
                {
                    formattedParameterArray[r, c] = parameterValues[r + c];//TEST
                }

            }



Thoughts? Improvements? Better structures? I think this is a fairly expensive method to use, but don't really have anything better that is still readable.

This post has been edited by killnine: 2 Apr, 2008 - 10:27 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: The Ideal Data Structure For A List Of Parameters
2 Apr, 2008 - 11:12 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,042



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
I'd honestly make a class for the parameter type. It helps enforce the rules you've stated and any others that come along.

e.g.
csharp

class FormattedParameter {
double singleValue;
int rows, cols;
double[] array = null;

public FormattedParameter(double singleValue) {
this.singleValue = singleValue;
this.rows = -1;
this.cols = -1;
}

public FormattedParameter(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.array = new double[this.Size];
}

public bool IsScalar { get { return array == null; } }
public int Size { get { return (IsScalar) ? 1 : rows * cols; } }
public int Rows { get { return rows; } }
public int Cols { get { return cols; } }
private int GetIndex(int row, int col) {
if (IsScalar) { throw new ArgumentOutOfRangeException(); }
int index = (row * rows) + col;
if (index >= Size) { throw new ArgumentOutOfRangeException(); }
return index;
}
public double SingleValue {
get {
if (!IsScalar) { throw new ArgumentOutOfRangeException(); }
return this.singleValue;
}
}
public double this[int row, int col] {
get { return this.array[GetIndex(row, col)]; }
set { this.array[GetIndex(row, col)] = value; }
}
}


FormattedParameter param = new FormattedParameter(parameterDimensions[0], parameterDimensions[1]);
for (int c = 0; c < param.Cols; c++) {
for (int r = 0; r < param.Rows; r++) {
param[r, c] = parameterValues[r + c];//TEST
}
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: The Ideal Data Structure For A List Of Parameters
2 Apr, 2008 - 11:25 AM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well to simplify everything you could consider creating a parameter object class which would take a matrix or scalar as a value. The beauty of this setup would be that it would create an object in which case you could create an array of objects, sort them, move them around as a collection, iterate through the parameters and apply interfaces to them.

It would also get out of the situation where you would have to start thinking about all these array indexes and dimensions etc. You could just have the class take in matrices and think of scalars as a matrix of dimensions 1 x 1.

A slight alteration of the idea is storying the matrices in a structure like arraylist. Then when you get at the values cast them back out as Double[,], use your dimension subscripts on it to get the values. It could be a little messy but it would work as well.

Lastly if you want to go with the array subscript route you can try an array of 2d arrays or a jagged array of multidimensional arrays. Whoa, what a mouthful. Example below...

csharp

// Our matrix example of doubles
Double[,] matrix = new Double[1,1];
matrix[0, 0] = 3.5;

// Our jagged array of 100 2d matrices
Double [][,] lstMartices = new Double[100][,];

// Store the matrix in the matrices list
lstMartices[0] = matrix;

// Return the matrix, access element 0,0 and show it in a messagebox
MessageBox.Show(lstMartices[0][0, 0].ToString());


Crazy stuff I tell you but those are some options you could go with. Whatever you wish to do will be based on your overall design. smile.gif

Enjoy!

"At DIC we be jagged array loving code ninjas, because if it is jagged it means it can cut you!" decap.gif
User is offlineProfile CardPM
+Quote Post

killnine
RE: The Ideal Data Structure For A List Of Parameters
3 Apr, 2008 - 06:33 AM
Post #4

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 107



Thanked: 3 times
My Contributions
Thanks for the replies guys,

baavgai: why is the line
CODE

int index = (row * rows) + col;


should it be...

CODE

int index = (row * cols) + col;


Also...no matter what only a Array of size 1 is created... I believe this is because in the constructor for a matrix, this.Size is called before array is actually created so it always returns a scalar:

CODE

this.array = new double[rows * cols /*this.Size*/]


Thanks for the prototype code, though, it really helps. I also learned what a ternary operator is!

I think I may go with this new type for simplicity of modification later.

This may lead me to go back and change a few other items in code to behave similarly.

I feel that in the year that I have been programming C#, I first learned how to create solutions to problems. Now I am learning how to make more EFFICIENT solutions to problems.

Thanks again.

This post has been edited by killnine: 3 Apr, 2008 - 07:52 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: The Ideal Data Structure For A List Of Parameters
3 Apr, 2008 - 09:28 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,042



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(killnine @ 3 Apr, 2008 - 10:33 AM) *

baavgai: why is the line
CODE

int index = (row * rows) + col;


should it be...

CODE

int index = (row * cols) + col;



You are correct! Sorry about that, some days right is left...

QUOTE(killnine @ 3 Apr, 2008 - 10:33 AM) *

Also...no matter what only a Array of size 1 is created... I believe this is because in the constructor for a matrix, this.Size is called before array is actually created so it always returns a scalar:

CODE

this.array = new double[rows * cols /*this.Size*/]



Pity, I hate redundant code. If you have to do figure it out in the constructor, I'd store it. e.g.

csharp

class FormattedParameter {
private double singleValue;
private int rows, cols, size;
private double[] array = null;

public FormattedParameter(double singleValue) {
this.singleValue = singleValue;
this.rows = -1;
this.cols = -1;
this.size = 1;
}

public FormattedParameter(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.size = rows * cols
this.array = new double[this.size];
}

public int Size { get { return this.size; } }
//...


QUOTE(killnine @ 3 Apr, 2008 - 10:33 AM) *

I feel that in the year that I have been programming C#, I first learned how to create solutions to problems.
Now I am learning how to make more EFFICIENT solutions to problems.


No worries, happy to help.

You always learn stuff, that's the fun. You'll look at code you did a few years ago and yell at yourself, but it's nice to know you can do better now. There's always something to discover, like bloody late binding properties in the constructor. tongue.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:02PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month