Re: MinMaxArray

Tech-Archive recommends: Speed Up your PC by fixing your registry



Ondrej Medek wrote:
I would like to have a generic class MinMaxArray<T>, which is a
wrapper around an array of T[] and computes minimum, maximum, and
average value from the array T[]. I need it only for the basic numeric
types (int, float, etc.).

Arrays are immutable. Do you need a mutable collection? The values are easy enough to calculate for any collection after it's created. LINQ offers extension methods to do this directly, actually, so you can write a.Average() for any array of numerical types. Incorporating this in a wrapper to cache the calculated values is easy.

I had problems with operators < > + and /, when I was computing
minimum, maximum and average values. I have errors like "Operator ...
cannot be applied to operans of type 'T' and 'T'." So, I use
IComparer<T> instead of operators < >, but I do not know, how to
compute average value (sum and divide) for a generic numeric type T.
Is there any easy way to do it? Disable the type checking in the
compile time?

Basically, the .NET type system isn't extensive enough to do what you want directly. There is no INumeric interface that all primitive numeric types implement. IComparable and IEquatable are not sufficient for the operations you want. You can use Decimal as a generic integral type, but there's no equivalent for float and double.

In short, you can't really use generics here without resorting to ugly code like this:

if (typeof(T) == typeof(int)) {
T result = (T) (((int) t1) / ((int) t2));
}

This works, but it's not winning any safety or readability prizes. There are various ways of mitigating this, but they're all more or less trying to fit a square peg in a round hole.

--
J.
.



Relevant Pages

  • Wasted Efforts (Parametric polymorphism?)
    ... And even if below solves problems that are solved in generics or templates, does that necessarily mean that we can't come up with a better solution.. ... astrarray = array of string; ... procedure add(const item: AnyType var arr: AnyTypeArray); ...
    (borland.public.delphi.non-technical)
  • Re: I want to parse @ArrayOfDays into @d1 through @d5
    ... condition on the loop indicated that the actual array length could ... -- sending the datasource, number of bins, and bin criteria as input ... My thought was to call a wrapper from the vb.net page, ... The sub-proc would parse the input array to ...
    (comp.databases.ms-sqlserver)
  • RE: Sites web service in C++
    ... I think the generated wrapper code is incorrect. ... > the first problem i run into is that wrapper class fails to compile. ... > array, because in MC++ they're always passed by reference. ... > // get the Site Templates ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: Announcing CodeGear RAD Studio 2007
    ... genuine quest to determine where generics can be useful outside of the ... oft quoted example of type safe containers. ... if generics may be useful. ... How about an array copier? ...
    (borland.public.delphi.non-technical)
  • Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl)
    ... array), although every array may have semantics of container. ... for this reason containers shouldn't be all treated as abstract arrays, ... >> for wrapped type's iterators with wrappers for wrapped type's iterators' ... wrapper for containers here... ...
    (comp.lang.ada)