Concise, fast singleton subclasses - c#

I'm writing, in C#, an interpreter for a dynamic language, and implementing primitive functions as an abstract class Primitive with a virtual Apply method, where each actual primitive function will be a subclass that overrides Apply.
(An alternative would be to only have the class Primitive and store a function pointer for Apply. However, making it a virtual method seems likely to be slightly faster, and this code will be run very frequently, so a small speedup is worth having.)
Obviously I could go ahead and create a full-blown class file for each primitive function, but I can't help feeling there ought to be a slightly more concise way of doing things than creating dozens of tiny class files.
In Java I'd use the anonymous subclass syntax to create and instantiate a subclass all in one expression, but I don't think C# has an exact counterpart.
What is the best way of doing this in C#?

Firstly, I wouldn't assume that a virtual method call will be faster than a delegate. Maybe it will, maybe it won't - but if performance is really that important to you, you should measure that. It would be really simple to code this using lambda expressions, particularly if all you're trying to represent is a function:
public static readonly Func<int, int> Addition = (x, y) => x + y;
public static readonly Func<int, int> Subtraction = (x, y) => x - y;
// etc
(I'm just guessing at the sorts of operation here, as we don't know the details.)
There's no particularly tiny syntax for subclasses in C#, but for semi-singletons like this
I find nested classes work well... similar to Java enums:
public abstract class Primitive
{
public static readonly Primitive Addition = new AdditionPrimitive();
public static readonly Primitive Subtraction = new SubtractionPrimitive();
// Prevent outside instantiation
private Primitive()
{
}
public abstract int Apply(int x, int y);
// Anything else you want
private class AdditionPrimitive : Primitive
{
public override int Apply(int x, int y)
{
return x + y;
}
}
private class SubtractionPrimitive : Primitive
{
public override int Apply(int x, int y)
{
return x - y;
}
}
}

Related

Can I store a reference to a local reference?

To be clear, I want the behavior of a pointer-to-a-pointer, and the purpose of this question is to generate clean, readable code.
I have some code that contains conditions checking the result of multiple Dictionary.TryGetValue calls. It would be cleaner if it could retrieve all of the required objects with a single call, so I wanted to write an extension that will allow me to do the following:
Dictionary<string, string> myDictionary; // Initialized somewhere
string x, y, z;
bool foundAllEntries = myDictionary.TryGetValues({"xvalue", out x}, {"yvalue", out y},
{"zvalue", out z});
if (foundAllEntries)
; // Do something with x, y, and z
However, I can't figure out a way to pass the extension method references to the objects that will hold the output. This seems like something that should be very basic.
How can I store a reference to a local reference in an object?
Please note that this question is not asking for alternative approaches to implementing the TryGetValues function. There are many ways I can make this 'work,' but none generate code as clean as the approach I'm trying to take.
This seems like something that should be very basic.
Not only it isn't basic, it's outright impossible: there is no way to decorate a data type with ref or out - these modifiers are applicable exclusively to formal method parameters. In other words, there is no such thing as a "reference variable" or an "output variable"; there are only "reference parameters" and "output parameters" in the language.
Moreover, you cannot pass output or by reference parameters as part of a variable-length argument list (i.e. the params portion) so that approach wouldn't work either.
There are many ways I can make this 'work,' but none generate code as clean as the approach I'm trying to take.
Curiously, the above does not mean that you cannot implement the scheme that you are trying to implement, leaving the code nearly as clean as your original one if you apply the Proxy Design Pattern. The trick is to chain method calls, and provide an implicit conversion operator for the result, like this:
class MyMap {
internal IDictionary<string,string> dict = ...
public ItemGetterResult TryGetValues {
get {
return new ItemGetterResult(this, true);
}
}
}
class ItemGetterResult {
private readonly MyMap map;
private bool IsSuccessful {get;set;}
internal ItemGetterResult(MyMap theMap, bool successFlag) {
map = theMap;
IsSuccessful = successFlag;
}
public static implicit operator bool(ItemGetterResult r) {
return r.IsSuccessful;
}
public ItemGetterResult Get(string key, out string val) {
return new ItemGetterResult(
map
, this.IsSuccessful && map.dict.TryGetValue(key, out val)
);
}
}
Now the call looks like this:
bool foundAllEntries = myDictionary.TryGetValues
.Get("xvalue", out x)
.Get("yvalue", out y)
.Get("zvalue", out z);
You can create a mutable Reference type:
public class Reference<T>
{
public T Value;
}
/* declaration */
bool TryGetValues(
this Dictionary<K,V> dict,
params Tuple<K, Reference<V>>[] requests)
/* call site */
var x = new Reference<string>();
var y = new Reference<string>();
var z = new Reference<string>();
bool foundAllEntries = myDictionary.TryGetValues(
Tuple.Create("xvalue", x),
Tuple.Create("yvalue", y),
Tuple.Create("zvalue", z));

Why does C# tease with structural typing when it absolutely knows it doesn't have it?

I was surprised to see today that this was possible, but I worry this must be discussed before.
public interface ICanAdd
{
int Add(int x, int y);
}
// Note that MyAdder does NOT implement ICanAdd,
// but it does define an Add method like the one in ICanAdd:
public class MyAdder
{
public int Add(int x, int y)
{
return x + y;
}
}
public class Program
{
void Main()
{
var myAdder = new MyAdder();
var iCanAdd = (ICanAdd)myAdder; //compiles, but for what sake?
int sum = iCanAdd.Add(2, 2); //na, not game for it, cast had already failed
}
}
The compiler will (rightly?) tell me that an explicit cast exists in the above situation. I was all thrilled to sense structural typing in there, but no run time it fails. So when is C# being ever helpful here? Any scenarios such casting would work? Whatever it is, I'm sure compiler beforehand knows myAdder is not ICanAdd, well technically.
C# allows an explicit conversion from a class to an interface (even if the class doesn't implement that interface), because for all the compiler knows, a reference to a certain type might actually (the uncertainty is why it's an explicit rather than implicit conversion) be an instance of a derived type that does implement the interface. Extending your example, suppose you have:
public class DerivedAdder : MyAdder, ICanAdd
{
int ICanAdd.Add(int x, int y)
{
return base.Add(x, y);
}
}
...
MyAdder myAdder = new DerivedAdder();
var iCanAdd = (ICanAdd)myAdder; // Valid in this case
int sum = iCanAdd.Add(2, 2); // sum = 4
If you check section 6.2.4 of the C# Specification, you'll see that if you mark your MyAdder class as sealed, the compiler will actually complain, because then it will know for sure that no conversion is possible, since no derived type could exist. But as long as it can't eliminate every last shred of doubt, it'll allow an explicit conversion.
Casting class to interface is allowed by C# language specification. But for example if ICanAdd was a class - compilation would fail

c# templating integer methods?

Is it possible to template methods for any kind of integer size ?
To illustrate, imagine this very trivial example (the body of the method is not important in my question):
public int Mul(int a, int b) {
return a*b;
}
Now, I want the same method that supports any kind of integer (excluding BigInteger of course). I have to write all variants :
public long Mul(long a, long b) {
return a*b;
}
public ulong Mul(ulong a, ulong b) {
return a*b;
}
public short Mul(short a, short b) {
return a*b;
}
public ushort Mul(ushort a, ushort b) {
return a*b;
}
public byte Mul(byte a, byte b) {
return a*b;
}
While this example is very trivial and it's not actually a problem to duplicate, if I have more complex algorithms like this (replicate for all integer kinds):
public static IEnumerable<long> GetPrimesFactors(this long number)
{
for (long i = 2; i <= number / 2; i++)
{
while (number % i == 0)
{
yield return i;
number /= i;
}
}
yield return number;
}
it introduces a maintenance risk as there is duplicated code and logic (coding integrists would say this is the evil to have same logic at multiple place).
Some of you may suggest to implements the long version and cast the result, but having to ask consumer code to cast can be confusing and reduce readability :
void SomeMethod(IEnumerable<int> valuesToProcess)
{
foreach(int value in valuesToProcess) { Console.WriteLine(value); }
}
void Main()
{
int i = 42;
SomeMethod(((long)i).GetPrimesFactors().Select(l=>(int)l));
SomeMethod(GetPrimesFactors(i));
long l = 42L;
SomeMethod(l.GetPrimesFactors().Select(l=>(int)l));
}
When I see the definition of the interface IEnumerable<T>, and especially the definitions of Sum method overloads :
public static decimal? Sum(this IEnumerable<decimal?> source);
public static decimal Sum(this IEnumerable<decimal> source);
public static double? Sum(this IEnumerable<double?> source);
public static double Sum(this IEnumerable<double> source);
public static float? Sum(this IEnumerable<float?> source);
public static float Sum(this IEnumerable<float> source);
public static int? Sum(this IEnumerable<int?> source);
public static int Sum(this IEnumerable<int> source);
public static long? Sum(this IEnumerable<long?> source);
public static long Sum(this IEnumerable<long> source);
I conclude that it's not possible... that's why MS has to implement all overloads.
Does anyone have any tips for designing general purpose integer methods without having to duplicate logic ?
There is no clean high performance solution. The choices I can think of are:
Manually duplicate the code (fast and redundant)
Automatically duplicate the code with a code generator (fast but a bit ugly). One .net numerics library went that way, but I don't remember its name.
Use some form of indirection, such as MiscUtil's Operator class, or the DLR (slow)
The arithmetic helper struct. I'm not sure how good the performance is, but you can try.
Generic methods representing operators:
These were my first idea. The issue is how to implement them. MiscUtil does this by calling a delegate stored in a static field.
static Func<T,T,T> _multiply;
public static T Multiply(T n1,T n2)
{
return _multiply(n1, n2);
}
One point to note here, is that you should avoid a static constructor, since its mere existence slows down static field access.
But that involves an indirect call, and that's expensive. I next tried to improve this by manually specializing for certain known types:
public static T Multiply(T n1,T n2)
{
if(typeof(T)==typeof(int))
return (T)(object)((int)(object)n1*(int)(object)n2);
...
return _multiply(n1, n2);
}
The JIT compiler is smart enough to realize which of those if cases it has to take, and will remove them. While that improved performance, it bloated the IL representation of the methods. And the JIT compiler is not smart enough to inline those method now, since their IL representation is long, and the inline heuristic only looks at the IL length of a method, not its machine code length. I don't remember if these casts cause boxing, or if the JITter was smart enough to optimize that out. Still the lack of inlining is too costly.
How 4) works:
First create an interface that contains the basic operations you need(arithmetic operators,...):
interface IArithmetic<T>
{
T Multiply(T n1,T n2);
}
Implement it for each type you need with a struct:
public struct Int32Arithmetic:IArithmetic<Int32>
{
Int32 Multiply(Int32 n1,Int32 n2)
{
return n1*n2;
}
}
Then make most of your actual code generic, and pass in an arithmetic helper:
internal T MyOperation<T,TArithmetic>(T n1, T n2)
where TArithmetic:struct,IArithmetic<T>
{
return default(TArithmetic).Multiply(n1,n2);
}
And if you want a clean interface for multiple types, create a thin overloaded wrapper forwarding to the generic method:
public Int32 MyOperation(Int32 n1,Int32 n2)
{
return MyOperation<Int32,Int32Arithmetic>(n1, n2);
}
This might be fast, because generics get specialized for each value type. It uses no indirections and the method bodies in IL don't get too long, so inlining is possible. But I haven't tried this myself yet.
Consider the DLR:
static void Main(string[] args)
{
int i = Mul(2, 4);
Console.WriteLine(i);
Console.Read();
}
static dynamic Mul(dynamic x, dynamic y)
{
return x * y;
}
Performance is to be determined (I'd expect it to be slower than straight overloads), but readability is much nicer. Could get a little hairy if you provide types that don't implement the required operators or different types that cause values to truncate.
Updated from comment:
If performance is so critical, then it sounds like you have already chosen the trade-off between duplicating/readability and the performance you seek. Code-gen it and move on. Any maintenance issues from a few pieces of extra code are likely dwarfed by the maintenance of the performance itself.
Well, what you could do is generate the duplicated code during your build process using, for example, T4.
public T Mul<T>(T a, T b){
dynamic x = a;
dynamic y = b;
return (T)x*y;
}
I once tried so implement something similar using CodeDom to generate an assembly during runtime and dynamically load it. This works rather well, but has some limitations. For example, your environment might not allow you to dynamically compile assemblies and there is the big one: performance. Although the "calculator"-class is only generated once, the overhead of calling a virtual method actually doubles the time necessary for the calculation.
You could give it a try to see how it would perform in your environment, I just lay out the classes (since this was a long time ago and I don't have the code anymore).
interface ICalculator<T> {
T Add(T left, T right);
T Multiply(T left, T right);
}
internal static class Calculator<T> {
static ICalculator<T> instance;
static Calculator() {
Type type = typeof(T);
// 1. Use CodeDom to design a class that implements ICalculator<T> using the
// builtin +,-,*,/ operators
// 2. Compile assembly in memory
// 3. Load assembly and create an instance of the ICalculator<T> class
Type concreteType = GetTypeFromDynamicAssembly(); // Get this from the assembly.
instance = Activator.CreateInstance(concreteType) as ICalculator<T>;
}
public static T Add(T left, T right) {
return instance.Add(left, right);
}
}
class MyClassUsingGenericMathType<T> {
T Sum(params T[] values) {
T sum = default(T);
foreach (T value in values) {
sum = Calculator<T>.Add(sum, value);
}
return sum;
}
}
The idea is that you dynamically built the implementation first time it is used (the static constructor is invoked then), after that the Calculator methods directly call the corresponding operator of the numeric type you are using. As I said, I remember that this adds an overhead everytime an operation is performed, but I never analyzed whether there is a potential for speeding up the process using some Compiler-attributes.
Another thing: using a type that doesn't implement the corresponding operators would cause a runtime-exception rather than a compile error. So it's far from perfect.

Inheritance in generic types

Can anyone help me in using Where for generic types?
I was trying to create a function which does ST with a number of type double or int, so I said it should be generic function. But when I try to assign a value to variables of that generic type, I can't because it's not a numerical type. Also, I can't use Where to inherit generic type from int or double data types.
Here is the code:
public static T[,] Unit(int n) where T : PROBLEM
{
T[,] mat = new T[n, n];
for (int i = 0; i < n; i++)
mat[i, i] = (T)1;
return mat;
}
Can anyone help?
Unfortunately one of the shortcomings of C# is that you cannot easily make generic numerical algorithms. You can kind of hack around it, like using this example from MSDN:
public abstract class BaseCalculator<T>
{
public abstract T Add(T arg1,T arg2);
public abstract T Subtract(T arg1,T arg2);
public abstract T Divide(T arg1,T arg2);
public abstract T Multiply(T arg1,T arg2);
}
public class IntCalculator : BaseCalculator<int>
{
public override int Add(int arg1, int arg2)
{
return arg1 + arg2;
}
//Rest of the methods
}
But generally speaking the .Net libraries just have a separate implementation for this sort of thing rather than attempting to use generics.
Not sure what this "Where" is to which you are referring, but you can declare your generic function as
public T Calculate<T>(T x, T y) where T : IComparable<T>
{
// do calculations
}
I believe int and double both implement IComparable<T>.
The constraints system in Generics is still pretty coarse grained. Its still a blunt tool with relatively few options to choose from. A much richer feature set for expressing constraints on type parameters will allow all sorts of sophisticated algos to be designed like pattern based programming etc.
If you only tell me that T is a shirt type that I am looking at ... there aint much I can do with it. But if T is not only a shirt but is actually worn by a hooter with some int[] properties with counts in upper 30's and certain events that I can subscribe to and handle with custom codes then I think it will make programming a little more fun and lively.
You can constrain your generic type parameter to be a struct. Struct represents a value type like int or double. Here's an example from the MSDN article:
public class MyClass<T> where T : struct
{...}

Is there a C# generic constraint for "real number" types? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# generic constraint for only integers
Greets!
I'm attempting to set up a Cartesian coordinate system in C#, but I don't want to restrict myself to any one numerical type for my coordinate values. Sometimes they could be integers, and other times they could be rational numbers, depending on context.
This screams "generic class" to me, but I'm stumped as to how to constrict the type to both integrals and floating points. I can't seem to find a class that covers any concept of real numbers...
public class Point<T> where T : [SomeClassThatIncludesBothIntsandFloats?] {
T myX, myY;
public Point(T x, T y) {
myX = x;
myY = y;
}
}
Point<int> pInt = new Point<int>(5, -10);
Point<float> pFloat = new Point<float>(3.14159, -0.2357);
If I want this level of freedom, am I electing for a "typeof(T)" nightmare when it comes to calculations inside my classes, weeding out bools, strings, objects, etc? Or worse, am I electing to make a class for each type of number I want to work with, each with the same internal math formulae?
Any help would be appreciated. Thanks!
You can't define such a constraint, but you could check the type at runtime. That won't help you for doing calculations though.
If you want to do calculations, something like this would be an option:
class Calculations<T, S> where S: Calculator<T>, new()
{
Calculator<T> _calculator = new S();
public T Square(T a)
{
return _calculator.Multiply(a, a);
}
}
abstract class Calculator<T>
{
public abstract T Multiply(T a, T b);
}
class IntCalculator : Calculator<int>
{
public override int Multiply(int a, int b)
{
return a * b;
}
}
Likewise, define a FloatCalculator and any operations you need. It's not particularly fast, though faster than the C# 4.0 dynamic construct.
var calc = new Calculations<int, IntCalculator>();
var result = calc.Square(10);
A side-effect is that you will only be able to instantiate Calculator if the type you pass to it has a matching Calculator<T> implementation, so you don't have to do runtime type checking.
This is basically what Hejlsberg was referring to in this interview where the issue is discussed. Personally I would still like to see some kind of base type :)
This is a very common question; if you are using .NET 3.5, there is a lot of support for this in MiscUtil, via the Operator class, which supports inbuilt types and any custom types with operators (including "lifted" operators); in particular, this allows use with generics, for example:
public static T Sum<T>(this IEnumerable<T> source) {
T sum = Operator<T>.Zero;
foreach (T value in source) {
if (value != null) {
sum = Operator.Add(sum, value);
}
}
return sum;
}
Or for another example; Complex<T>
This is a known problem, since none of the arithmetic classes arrive from the same class. So you cannot restrict it.
The only thing you could do is
where T : struct
but thats not exactly what you want.
Here is a link to the specific issue.
Arithmetic types like int,double,decimal should implement IArithmetic<T>
You actually can do this, although the solution is tedious to set up, and can be confusing to devs who are not aware of why it was done. (so if you elect to do it document it thououghly!)...
Create two structs, called say, MyInt, and MyDecimal which act as facades to the CTS Int32, and Decimal core types (They contain an internal field of that respective type.) Each should have a ctor that takes an instance of the Core CTS type as input parameter..
Make each one implement an empty interface called INumeric
Then, in your generic methods, make the constraint based upon this interface.
Downside, everywhere you want to use these methods you have to construct an instance of the appropriate custom type instead of the Core CTS type, and pass the custom type to the method.
NOTE: coding the custom structs to properly emulate all the behavior of the core CTS types is the tedious part... You have to implement several built-in CLR interfaces (IComparable, etc.) and overload all the arithmetic, and boolean operators...
You can get closer with implementing few more
public class Point<T> where T : struct, IComparable, IFormattable, IConvertible,
IComparable<T>, IEquatable<T> {
}
The signature conforms to DateTime too. I'm not sure if you will be able to specify more types from the framework. Anyway this only solves part of the problem. To do basic numeric operations you will have to wrap your numeric types and use generic methods instead of standard operators. See this SO question for a few options.
This might be helpful. You have to use a generic class to achieve what you want.
C# doesn't currently allow type constraints on value types. i asked a related question not too long ago.
Enum type constraints in C#
Would this not lend itself to having seperate classes implementing IPoint?
Something like:
public interface IPoint<T>
{
T X { get; set; }
T Y { get; set; }
}
public class IntegerPoint : IPoint<int>
{
public int X { get; set; }
public int Y { get; set; }
}
As the calculations will have to differ in each implementation anyway right?
Dan#

Categories