C# Generics With Comparisons and Math Ops

ObscureCaucasian

Diamond Member
Jul 23, 2006
3,934
0
0
I'm porting some C++ code into C# and I'm having an issue with a matrix class I have. This program reads in data from a 4096x4096x2Byte matrix of data. In C++ I made this a template class so I could in the future make it take a full 4Byte Integer, however implementing that now would make a single matrix double it's size (to 64 MB). In C++ I wrote some of the code under the assumption that the datatype of the template would only be numeric, so I freely used comparison (<,>,==) and mathematical operations (*,/,+,-). This is fine with C++, however C# seems to be typed strong enough to forbid such an assumption.

I managed to do the comparisons by forcing the datatype to implement IComparable, however I'm still stuck when it comes to the mathematical operators. Is there an easy way to accomplish this (or is there an interface that would facilitate this)?
 

ObscureCaucasian

Diamond Member
Jul 23, 2006
3,934
0
0
I guess I'll just make classes like FloatMatrix, ShortMatrix, and IntMatrix for now. I was just trying to avoid the redundancy thing.
 

JasonCoder

Golden Member
Feb 23, 2005
1,893
1
81
Am I missing something or can you simply implement some .GreaterThan(), etc. methods? Object already gives you .Equals().
 

ObscureCaucasian

Diamond Member
Jul 23, 2006
3,934
0
0
Originally posted by: JasonCoder
Am I missing something or can you simply implement some .GreaterThan(), etc. methods? Object already gives you .Equals().

Would this work on value types like Int, short, float, etc? Am I able to add to those classes? I'm not terribly familiar with the rules of .Net Generics (C# in general is also moderately new to me).
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
Originally posted by: ObscureCaucasian
Originally posted by: JasonCoder
Am I missing something or can you simply implement some .GreaterThan(), etc. methods? Object already gives you .Equals().

Would this work on value types like Int, short, float, etc? Am I able to add to those classes? I'm not terribly familiar with the rules of .Net Generics (C# in general is also moderately new to me).

In order to be typesafe you need to assert that any type for which your generic might be instantiated supports all the operations called on it in the body of the method (or the scope of the class if we're talking about generic classes rather than methods). The compiler refuses to make that assertion about unconstrained generic type parameters because the only thing it can know about them is that they derive from object, and object doesn't support those operators.

There are a couple of ways you can attack this. You can roll your own base class and declare virtual methods on it for the operations you need, and then derive specific numeric types from that and have them implement the methods. A generic constraint can then be used to enforce that only types derived from that base can be used to instantiate the generic.

You could define extension methods for object, but that's not a very good solution since you don't know that all objects you might be working with can handle the semantics of your operations. Again you wind up with the need for a base class, and unfortunately it has to be a reference type since structs don't have inheritance in C#.

You might be able to hack up something using an array of bytes and some converters, but still you're not going to get to the point where you can write expressions with these types using the standard operators.

C# simply lacks the expressiveness of C++ when it comes to creating new types and defining their behavior within expressions using the standard operators. Some probably consider that a good thing, but I miss being able to do that.
 

ObscureCaucasian

Diamond Member
Jul 23, 2006
3,934
0
0
Thanks for the tips, I ended up just doing it the sloppy way and made classes like MatrixShort and MatrixFloat. Pretty much 95% duplicate code, but it's a small class.

Too bad C# 4.0 isn't out yet though, seems like the dynamic typing is just what I need.
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
You shouldn't need dynamic typing to solve this, not to mention that will impose a terrible performance penalty.

If you were willing to go that far, why not simply define an interface containing the operations you want? Then you can constrain the generic type parameter to that interface, and implement it for each type you want the matrix to be parameterized by.

This will still require boxing, unfortunately, due to the poor design of C#. But it should be somewhat better than dynamic typing, which requires boxing and tag-checking. And you won't get to use operators, but, that's something you should be able to deal with as a programmer.
 

ObscureCaucasian

Diamond Member
Jul 23, 2006
3,934
0
0
Originally posted by: dinkumthinkum
You shouldn't need dynamic typing to solve this, not to mention that will impose a terrible performance penalty.

If you were willing to go that far, why not simply define an interface containing the operations you want? Then you can constrain the generic type parameter to that interface, and implement it for each type you want the matrix to be parameterized by.

This will still require boxing, unfortunately, due to the poor design of C#. But it should be somewhat better than dynamic typing, which requires boxing and tag-checking. And you won't get to use operators, but, that's something you should be able to deal with as a programmer.

Yea I had thought of a similar idea after the fact, but oh well, maybe I'll do some refactoring later.
 

imported_Dhaval00

Senior member
Jul 23, 2004
573
0
0
Originally posted by: ObscureCaucasian
Thanks for the tips, I ended up just doing it the sloppy way and made classes like MatrixShort and MatrixFloat. Pretty much 95% duplicate code, but it's a small class.

Too bad C# 4.0 isn't out yet though, seems like the dynamic typing is just what I need.

You don't need dynamic typing for this. Also, if it is 95% duplicate code, and if generics can't help, then revert to good old inheritance and polymorphism - create a base class with virtual methods? Others can weigh in on this thought.


Originally posted by: dinkumthinkum
This will still require boxing, unfortunately, due to the poor design of C#.

?? I lost you here...
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |