I'm having big trouble with reducing cognitive complexity in given piece of code.
Could you please give some tips on how to fix this issue?
I could reduce it from 24 to 16 using switch, but it is still 16 and I have no options left
protected override bool Compare(object valueToValidate, object valueToCompare)
{
if (RaUtils.IsBlankValue(valueToValidate) || RaUtils.IsBlankValue(valueToCompare))
{
return true;
}
switch (Type.GetTypeCode(valueToCompare.GetType()))
{
case TypeCode.DateTime:
if (DateTime.TryParse(valueToValidate.ToString(), out var valueToValidateDt)
&& DateTime.TryParse(valueToCompare.ToString(), out var valueToCompareDt))
{
return valueToValidateDt >= valueToCompareDt;
}
break;
case TypeCode.Double:
if (double.TryParse(valueToValidate.ToString(), out var valueToValidateDouble)
&& double.TryParse(valueToCompare.ToString(), out var valueToCompareDouble))
{
return valueToValidateDouble >= valueToCompareDouble;
}
break;
case TypeCode.Decimal:
if (decimal.TryParse(valueToValidate.ToString(), out var valueToValidateDecimal)
&& decimal.TryParse(valueToCompare.ToString(), out var valueToCompareDecimal))
{
return valueToValidateDecimal >= valueToCompareDecimal;
}
break;
case TypeCode.Int32:
if (int.TryParse(valueToValidate.ToString(), out var valueToValidateInt32)
&& int.TryParse(valueToCompare.ToString(), out var valueToCompareInt32))
{
return valueToValidateInt32 >= valueToCompareInt32;
}
break;
case TypeCode.Int64:
if (long.TryParse(valueToValidate.ToString(), out var valueToValidateInt64)
&& long.TryParse(valueToCompare.ToString(), out var valueToCompareInt64))
{
return valueToValidateInt64 >= valueToCompareInt64;
}
break;
default: throw new NotImplementedException();
}
return false;
}
You could try to move the code inside the case blocks into their own methods:
private bool CompareDateTime(string value1, string value2)
{
if (DateTime.TryParse(value1, out var valueToValidateDt)
&& DateTime.TryParse(value2, out var valueToCompareDt))
{
return valueToValidateDt >= valueToCompareDt;
}
return false;
}
Your Comapre would simplify to this:
protected override bool Compare(object valueToValidate, object valueToCompare)
{
if (RaUtils.IsBlankValue(valueToValidate) || RaUtils.IsBlankValue(valueToCompare))
{
return true;
}
switch (Type.GetTypeCode(valueToCompare.GetType()))
{
case TypeCode.DateTime:
return CompareDateTime(valueToValidate.ToString(), valueToCompare.ToString());
case TypeCode.Double:
return CompareDouble(valueToValidate.ToString(), valueToCompare.ToString());
case TypeCode.Decimal:
return CompareDecimal(valueToValidate.ToString(), valueToCompare.ToString());
case TypeCode.Int32:
return CompareInt32(valueToValidate.ToString(), valueToCompare.ToString());
case TypeCode.Int64:
return CompareInt64(valueToValidate.ToString(), valueToCompare.ToString());
default: throw new NotImplementedException();
}
}
Because all your methods have the same signature, you could also use a Dictionary<TypeCode,Func<string,string,bool>> instead of a switch block:
protected override bool Compare(object valueToValidate, object valueToCompare)
{
if (RaUtils.IsBlankValue(valueToValidate) || RaUtils.IsBlankValue(valueToCompare))
{
return true;
}
var methods = new Dictionary<TypeCode,Func<string,string,bool>>
{
{ TypeCode.DateTime, CompareDateTime },
{ TypeCode.Double, CompareDouble },
{ TypeCode.Decimal, CompareDecimal },
{ TypeCode.Int32, CompareInt32 },
{ TypeCode.Int64, CompareInt64 }
};
if(methods.TryGetValue(Type.GetTypeCode(valueToCompare.GetType()), out var method))
{
return method.Invoke(valueToValidate.ToString(), valueToCompare.ToString());
}
else
{
throw new NotImplementedException();
}
}
You may be able to entirely eliminate the switch and the associated redundancy if you can pass concrete types to a generic function like the following. The class contains a perfunctory test in Main.
In general, switches over types should raise suspicions because they try to manually re-invent polymorphism, either compile-time (as here) or runtime polymorphism. It's often better to use the language mechanisms for that.
public class VariableCompare
{
public static bool Compare<N>(N valueToValidate, N valueToCompare) where N: IComparable<N>, IParsable<N>
{
N lhs, rhs;
if ( N.TryParse( valueToValidate.ToString(),
null,
out lhs )
&& N.TryParse( valueToCompare.ToString(),
null,
out rhs) )
{
return lhs.CompareTo(rhs) >= 0;
}
return false;
}
static int Main()
{
int i1 = 1, i2 = 2;
Console.WriteLine(Compare(i1, i2));
float f1 = 2, f2 = 1;
Console.WriteLine(Compare(f1, f2));
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Today;
Console.WriteLine(Compare(d1, d2));
return 0;
}
}
I think for this method formatting and short names can make a world of difference. Here are two versions that I think are very easy to read and to maintain:
Version 1
bool Compare(object valueToValidate, object valueToCompare)
{
if (RaUtils.IsBlankValue(valueToValidate) || RaUtils.IsBlankValue(valueToCompare))
{
return true;
}
var a = valueToValidate.ToString();
var b = valueToCompare.ToString();
return Type.GetTypeCode(valueToCompare.GetType()) switch
{
TypeCode.DateTime =>
DateTime.TryParse(a, out var x)
&& DateTime.TryParse(b, out var y)
&& x >= y,
TypeCode.Double =>
double.TryParse(a, out var x)
&& double.TryParse(b, out var y)
&& x >= y,
TypeCode.Decimal =>
decimal.TryParse(a, out var x)
&& decimal.TryParse(b, out var y)
&& x >= y,
TypeCode.Int32 =>
int.TryParse(a, out var x)
&& int.TryParse(b, out var y)
&& x >= y,
TypeCode.Int64 =>
long.TryParse(a, out var x)
&& long.TryParse(b, out var y)
&& x >= y,
_ => throw new NotImplementedException();
};
}
Version 2
bool Compare(object valueToValidate, object valueToCompare)
{
if (RaUtils.IsBlankValue(valueToValidate) || RaUtils.IsBlankValue(valueToCompare))
{
return true;
}
var a = valueToValidate.ToString();
var b = valueToCompare.ToString();
switch (Type.GetTypeCode(valueToCompare.GetType()))
{
case TypeCode.DateTime:
{
return DateTime.TryParse(a, out var x)
&& DateTime.TryParse(b, out var y)
&& x >= y;
}
case TypeCode.Double:
{
return double.TryParse(a, out var x)
&& double.TryParse(b, out var y)
&& x >= y;
}
case TypeCode.Decimal:
{
return decimal.TryParse(a, out var x)
&& decimal.TryParse(b, out var y)
&& x >= y;
}
case TypeCode.Int32:
{
return int.TryParse(a, out var x)
&& int.TryParse(b, out var y)
&& x >= y;
}
case TypeCode.Int64:
{
return long.TryParse(a, out var x)
&& long.TryParse(b, out var y)
&& x >= y;
}
default: throw new NotImplementedException();
}
}
Could you use IComparable?
For example:
bool Compare(object valueToValidate, object valueToCompare)
{
if (RaUtils.IsBlankValue(valueToValidate) || RaUtils.IsBlankValue(valueToCompare))
{
return true;
}
if( valueToValidate is IComparable x && valueToCompare is IComparable y)
return x.CompareTo(y) > 0;
throw new NotImplementedException();
}
If you want/need keep the switch checking the types can be made simpler (which also removes the ToString/Parse):
bool Compare(object valueToValidate, object valueToCompare)
{
...
return (valueToValidate, valueToCompare) switch
{
(DateTime a, DateTime b) => a >= b,
(double a, double b) => a >= b,
//...
_ => throw new NotImplementedException()
};
}
If you really really really have to round trip of ToString->Parse then in .NET7 you could use the IParseable...er...I mean IParsable interface, part of the Generic Math feature. Could something like this work?
using System.Numerics;
bool CustomCompare<T>(object a, object b) where T: IComparable<T>, IParsable<T>
{
return T.TryParse(a.ToString(),System.Globalization.CultureInfo.CurrentCulture, out var x)
&& T.TryParse(b.ToString(), System.Globalization.CultureInfo.CurrentCulture, out var y)
&& x.CompareTo(y) > 0;
}
This could then be used in you switch.
Related
I have to find parser for solving equation with unknows. User enters two numbers and the parser parses equation to solve. For example:
Equation:
x^3 + y^2 + sin60°
User input:
x=4, y=2
It have to be done using AForge.NET framework. Console app/WinForms
I have been looking for it in AForge .NET documentation but can't find something matches my problem.
I don't believe there is an evaluator for regular mathematical expressions in AForge, but you could use the PolishExpression class to evaluate an expression written in reverse polish notation:
using AForge;
string expression = "$0 $0 $0 * * $1 $1 * + " + (Math.PI / 3) + " sin +";
// variables for the expression
double x = 4;
double y = 2;
double[] vars = new double[] { x, y };
// expression evaluation
double result = PolishExpression.Evaluate(expression, vars);
Console.WriteLine("Polish expression evaluation: " + result);
This yields the value 68.86602540378443. There's also no direct exponential operator, so I just multiplied x by itself three times. But you could use logaritms instead:
string expression = "$0 ln 3 * exp $1 ln 2 * exp + " + (Math.PI / 3) + " sin +";
You can also confirm that this is the same as you'd get in C# (note that you must convert 60 degrees to radians in order to use Math.sin):
double TestEvaluate(double x, double y)
{
// x^3 + y^2 + sin60°
// Note that Math.sin accepts radians, so we must convert 60°
return Math.Pow(x, 3) + Math.Pow(y, 2) + Math.Sin((Math.PI / 180) * 60);
}
Console.WriteLine("C# evalution: " + TestEvaluate(x, y));
But yeah, I'd recommend using another library if you don't want to use polish notation in your expressions.
I also tried the code example above with "AForge.Math.Expression", but I don't believe this class exists in AForge.Math 2.2.5.
Solution
It seems you need an expression parser. I decided to write one, that uses System.Linq.Expression for building expression trees. I started with the simple expression parser from https://github.com/toptensoftware/SimpleExpressionEngine and build from there.
Example Code
First the fun stuff. Look at this test code
public static void TestSOExpr()
{
string expression = "x^3 + y^2 + sin(60°)";
Parser parser = new Parser(expression);
Console.WriteLine("Expression: ");
Console.WriteLine(parser.Expression);
Console.WriteLine("Arguments: ");
foreach (var item in parser.Arguments)
{
Console.WriteLine(item);
}
Func<double,double,double> f = parser.CompileBinary();
double x = 4, y = 2;
Console.WriteLine($"f({x},{y}) = {f(x, y)}");
}
with output
Expression:
(((x ^ 3) + (y * y)) + sin((60 * 0.0174532925199433)))
Arguments:
x
y
f(4,2) = 68.8660254037844
The result is numerically correct. A lot of time was spent in trying to deal with ° as a constant value with implied multiplication. I am happy with the results.
Class Structure
To get there there are three classes needed
Token
This information class holds the data extracted from the string such as names, values, operators, functions and parenthesis.
public enum TokenType
{
EOF,
Operator,
StartBlock,
EndBlock,
Delimiter,
Identifier,
Number,
}
public readonly struct Token
{
public static readonly Token Empty = new Token(TokenType.EOF, string.Empty, 0, 0);
public Token(Token token, int level) : this()
{
this = token;
Level = level;
}
Token(TokenType type, string symbol, double number, int level) : this()
{
Type = type;
Symbol = symbol;
Number = number;
Level = level;
}
public static Token Operator(char symbol, int level = 0) => new Token(TokenType.Operator, symbol.ToString(), 0, level);
public static Token StartBlock(char symbol, int level = 0) => new Token(TokenType.StartBlock, symbol.ToString(), 0, level);
public static Token EndBlock(char symbol, int level = 0) => new Token(TokenType.EndBlock, symbol.ToString(), 0, level);
public static Token Delimiter(char symbol, int level = 0) => new Token(TokenType.Delimiter, symbol.ToString(), 0, level);
public static Token Identifier(string symbol, int level = 0) => new Token(TokenType.Identifier, symbol, 0, level);
public static Token Value(double number, int level = 0) => new Token(TokenType.Number, string.Empty, number, level);
public static Token operator +(Token token, int delta) => new Token(token, token.Level + delta);
public static Token operator -(Token token, int delta) => new Token(token, token.Level - delta);
public TokenType Type { get; }
public string Symbol { get; }
public double Number { get; }
public int Level { get; }
public bool IsEOF() => Type == TokenType.EOF;
public bool IsOperator(char #operator) => IsOperator(out char op) && op == #operator;
public bool IsOperator(out char #operator)
{
if (Type == TokenType.Operator)
{
#operator = Symbol[0];
return true;
}
#operator = '\0';
return false;
}
public bool IsIdentifier(string symbol) => IsIdentifier(out string x) && x.Equals(symbol);
public bool IsIdentifier(out string symbol)
{
if (Type == TokenType.Identifier)
{
symbol = Symbol;
return true;
}
symbol = string.Empty;
return false;
}
public bool IsNumber(double value) => IsNumber(out double x) && x.Equals(value);
public bool IsNumber(out double value)
{
if (Type == TokenType.Number)
{
value = Number;
return true;
}
value = 0;
return false;
}
public bool IsStartBlock(char delimiter) => IsStartBlock(out char sym) && sym == delimiter;
public bool IsStartBlock(out char delimiter)
{
if (Type == TokenType.StartBlock)
{
delimiter = Symbol[0];
return true;
}
delimiter = '\0';
return false;
}
public bool IsEndBlock(char delimiter) => IsEndBlock(out char sym) && sym == delimiter;
public bool IsEndBlock(out char delimiter)
{
if (Type == TokenType.EndBlock)
{
delimiter = Symbol[0];
return true;
}
delimiter = '\0';
return false;
}
public bool IsDelimiter(char delimiter) => IsDelimiter(out char sym) && sym == delimiter;
public bool IsDelimiter(out char delimiter)
{
if (Type == TokenType.Delimiter)
{
delimiter = Symbol[0];
return true;
}
delimiter = '\0';
return false;
}
public override string ToString()
{
var tab = new string('\t', Level);
switch (Type)
{
case TokenType.EOF:
return $"{tab}{Type}";
case TokenType.Operator:
case TokenType.StartBlock:
case TokenType.EndBlock:
case TokenType.Delimiter:
case TokenType.Identifier:
return $"{tab}{Type}: {Symbol}";
case TokenType.Number:
return $"{tab}{Type}: {Number}";
default:
throw new NotSupportedException();
}
}
}
Tokenizer
The job of the tokenizer is to extract the Token from a string. This is done one step at a time with the .MoveNext() and .Reset() functions. At each step the .Current property holds the token information.
/// <summary>
/// Parses strings into Tokens for further processing.
/// </summary>
/// <remarks>Code taken from https://github.com/toptensoftware/SimpleExpressionEngine </remarks>
public class Tokenizer : IEnumerator<Token>, IEnumerable<Token>
{
readonly string _expression;
TextReader _reader;
char _currentChar;
public Tokenizer(string expression)
{
_expression = expression;
Reset();
}
public Token Current { get; private set; }
object System.Collections.IEnumerator.Current { get => Current; }
public IEnumerator<Token> GetEnumerator() => this;
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
public void Reset()
{
_reader = new StringReader(_expression);
NextChar();
//MoveNext();
}
// Read the next character from the input strem
// and store it in _currentChar, or load '\0' if EOF
void NextChar()
{
int ch = _reader.Read();
_currentChar = ch < 0 ? '\0' : (char)ch;
}
public bool MoveNext()
{
// Skip whitespace
while (char.IsWhiteSpace(_currentChar))
{
NextChar();
}
switch (_currentChar)
{
case '\0':
{
if (Current.Level > 0)
{
throw new InvalidOperationException();
}
Current = Token.Empty;
return false;
}
case '+':
case '-':
case '*':
case '/':
case '^':
case '=':
{
Current = Token.Operator(_currentChar, Current.Level);
NextChar();
return true;
}
case '(':
{
Current = Token.StartBlock(_currentChar, Current.Level + 1);
NextChar();
return true;
}
case ')':
{
Current = Token.EndBlock(_currentChar, Current.Level - 1);
NextChar();
return true;
}
case ';':
case ',':
{
Current = Token.Delimiter(_currentChar, Current.Level);
NextChar();
return true;
}
}
if (char.IsDigit(_currentChar) || _currentChar == '.')
{
// Capture digits/decimal point
StringBuilder sb = new StringBuilder();
bool haveDecimalPoint = false;
while (char.IsDigit(_currentChar) || (!haveDecimalPoint && _currentChar == '.'))
{
sb.Append(_currentChar);
haveDecimalPoint = _currentChar == '.';
NextChar();
}
// Parse it
if (double.TryParse(sb.ToString(), NumberStyles.Float, CultureInfo.InvariantCulture, out double x))
{
Current = Token.Value(x, Current.Level);
//Token = Token.Number;
return true;
}
}
// Identifier - starts with letter or underscore
if (char.IsLetter(_currentChar) || _currentChar == '_' || _currentChar == '°')
{
var sb = new StringBuilder();
// Accept letter, digit or underscore
while (char.IsLetterOrDigit(_currentChar) || _currentChar == '_' || _currentChar == '°')
{
sb.Append(_currentChar);
NextChar();
}
// Setup token
Current = Token.Identifier(sb.ToString(), Current.Level);
return true;
}
Current = Token.Empty;
return false;
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposed)
{
if (disposed)
{
_reader.Dispose();
}
}
}
Parser
This is the main class that recursively builds the expression tree. When the constructor is called, the resulting expression tree is stored in the .Expression property, as well as any required arguments in .Arguments.
/// <summary>
/// Parses a string into an <see cref="Expression"/> tree.
/// </summary>
public class Parser
{
readonly Tokenizer _tokenizer;
readonly Dictionary<string, ParameterExpression> _arguments;
public Parser(string expression) : this(new Tokenizer(expression)) { }
public Parser(Tokenizer tokenizer)
{
this._tokenizer = tokenizer;
this._arguments = new Dictionary<string, ParameterExpression>();
Expression = ParseTokens();
}
public Expression Expression { get; }
public IReadOnlyList<ParameterExpression> Arguments => _arguments.Values.ToList();
public Func<double> CompileNonary()
{
if (Arguments.Count == 0)
{
return Compile<Func<double>>();
}
throw new InvalidOperationException("Expression has too many arguments.");
}
public Func<double, double> CompileUnary()
{
if (Arguments.Count == 1)
{
return Compile<Func<double, double>>();
}
if (Arguments.Count > 1)
{
throw new InvalidOperationException("Expression has too many arguments.");
}
else
{
throw new InvalidOperationException("Expression has too few arguments.");
}
}
public Func<double, double, double> CompileBinary()
{
if (Arguments.Count == 2)
{
return Compile<Func<double, double, double>>();
}
if (Arguments.Count > 2)
{
throw new InvalidOperationException("Expression has too many arguments.");
}
else
{
throw new InvalidOperationException("Expression has too few arguments.");
}
}
TFunc Compile<TFunc>() where TFunc : Delegate
{
ParameterExpression[] arguments = _arguments.Values.ToArray();
return Expression.Lambda<TFunc>(Expression, arguments).Compile() as TFunc;
}
Expression ParseTokens()
{
_tokenizer.Reset();
if (_tokenizer.MoveNext())
{
Expression expr = ParseEquals();
// Check everything was consumed
if (_tokenizer.Current.Type != TokenType.EOF)
{
throw new InvalidOperationException("Unexpected characters at end of expression");
}
return expr;
}
throw new InvalidOperationException("Invalid Expression");
}
Expression ParseEquals()
{
Expression lhs = ParseAddSubtract();
while (true)
{
if (_tokenizer.Current.IsOperator('='))
{
_tokenizer.MoveNext();
Expression rhs = ParseAddSubtract();
lhs = Expression.Equal(lhs, rhs);
}
else
{
return lhs;
}
}
}
Expression ParseAddSubtract()
{
Expression lhs = ParseMulDivide();
while (true)
{
if (_tokenizer.Current.IsOperator('+'))
{
_tokenizer.MoveNext();
Expression rhs = ParseMulDivide();
lhs = Expression.Add(lhs, rhs);
}
else if (_tokenizer.Current.IsOperator('-'))
{
_tokenizer.MoveNext();
Expression rhs = ParseMulDivide();
lhs = Expression.Subtract(lhs, rhs);
}
else
{
return lhs;
}
}
}
Expression ParseMulDivide()
{
Expression lhs = ParsePower();
bool negate = false;
if (lhs is ConstantExpression lex)
{
double x = Convert.ToDouble(lex.Value);
if (x == 0)
{
return Expression.Constant(0.0);
}
else if (x == -1)
{
negate = true;
}
if (_tokenizer.Current.IsIdentifier(out string name))
{
// 60π - or example
Expression rhs = ParseLeaf();
return Expression.Multiply(lhs, rhs);
}
}
while (true)
{
if (_tokenizer.Current.IsOperator('*'))
{
_tokenizer.MoveNext();
Expression rhs = ParsePower();
if (rhs is ConstantExpression rex)
{
double y = Convert.ToDouble(rex.Value);
if (y == 0)
{
lhs = Expression.Constant(0.0);
}
else if (y == 1)
{
// do nothing
}
else if (y == -1)
{
negate = !negate;
}
else
{
lhs = Expression.Multiply(lhs, rhs);
}
}
else
{
lhs = Expression.Multiply(lhs, rhs);
}
}
else if (_tokenizer.Current.IsOperator('/'))
{
_tokenizer.MoveNext();
Expression rhs = ParsePower();
if (rhs is ConstantExpression rex)
{
double y = Convert.ToDouble(rex.Value);
if (y == 0)
{
lhs = Expression.Constant(double.PositiveInfinity);
}
else if (y == 1)
{
// do nothing
}
else if (y == -1)
{
negate = !negate;
}
else
{
lhs = Expression.Divide(lhs, rhs);
}
}
else
{
lhs = Expression.Divide(lhs, rhs);
}
}
else
{
return negate ? Expression.Negate(lhs) : lhs;
}
}
}
Expression ParsePower()
{
Expression lhs = ParseUnary();
while (true)
{
if (_tokenizer.Current.IsOperator('^'))
{
_tokenizer.MoveNext();
Expression rhs = ParseUnary();
if (rhs is ConstantExpression cex)
{
double x = Convert.ToDouble(cex.Value);
if (x == 0)
{
return Expression.Constant(1.0);
}
if (x == 1)
{
return lhs;
}
if (x == -1)
{
return Expression.Divide(Expression.Constant(1.0), lhs);
}
if (x == 2)
{
return Expression.Multiply(lhs, lhs);
}
}
lhs = Expression.Power(lhs, rhs);
}
else
{
return lhs;
}
}
}
Expression ParseUnary()
{
while (true)
{
if (_tokenizer.Current.IsOperator('+'))
{
// ignore unary +
_tokenizer.MoveNext();
continue;
}
if (_tokenizer.Current.IsOperator('-'))
{
_tokenizer.MoveNext();
Expression rhs = ParseUnary();
if (rhs is UnaryExpression uex)
{
if (uex.NodeType == ExpressionType.Negate)
{
return uex.Operand;
}
}
return Expression.Negate(rhs);
}
return ParseLeaf();
}
}
private Expression ParseLeaf()
{
if (_tokenizer.Current.IsNumber(out double x))
{
_tokenizer.MoveNext();
return Expression.Constant(x);
}
if (_tokenizer.Current.IsStartBlock('('))
{
_tokenizer.MoveNext();
Expression node = ParseAddSubtract();
if (!_tokenizer.Current.IsEndBlock(')'))
{
throw new InvalidOperationException("Mismatched Parenthesis.");
}
_tokenizer.MoveNext();
return node;
}
if (_tokenizer.Current.IsIdentifier(out string name))
{
_tokenizer.MoveNext();
if (_tokenizer.Current.IsStartBlock('('))
{
// function call
_tokenizer.MoveNext();
// Parse arguments
List<Expression> arguments = new List<Expression>();
while (true)
{
Expression node = ParseAddSubtract();
arguments.Add(node);
if (_tokenizer.Current.IsDelimiter(','))
{
_tokenizer.MoveNext();
continue;
}
// end of arguments
break;
}
if (!_tokenizer.Current.IsEndBlock(')'))
{
throw new InvalidOperationException("Mismatched Parenthesis.");
}
_tokenizer.MoveNext();
MethodInfo f = typeof(MathFunctions).GetMethod(name, BindingFlags.Static | BindingFlags.Public);
if (f != null)
{
switch (arguments.Count)
{
case 0:
return Expression.Call(f);
case 1:
return Expression.Call(f, arguments[0]);
case 2:
return Expression.Call(f, arguments[0], arguments[1]);
default:
throw new InvalidOperationException($"Too many arguments for function {name}.");
}
}
else
{
throw new InvalidOperationException($"Unknown function {name}.");
}
}
else
{
if (MathFunctions.knownConstants.ContainsKey(name))
{
// named constant
return Expression.Constant(MathFunctions.knownConstants[name]);
}
// variable
if (!_arguments.ContainsKey(name))
{
// add to list of arguments
_arguments.Add(name, Expression.Parameter(typeof(double), name));
}
return _arguments[name];
}
}
if (!_tokenizer.Current.IsEOF())
{
throw new InvalidOperationException($"Unexpected token {_tokenizer.Current}");
}
throw new NotImplementedException();
}
}
MathFunctions
The parser depends on a utility function to provide method bodies for math functions. Most are wrappers around the Math class, but some are new. The names are lowercase to match with any functions called in the parsed string.
public static class MathFunctions
{
internal static readonly Random rng = new Random();
internal static readonly Dictionary<string, double> knownConstants =
new Dictionary<string, double>()
{
["pi"] = Math.PI,
["π"] = Math.PI,
["e"] = Math.E,
["Φ"] = (1 + Math.Sqrt(5)) / 2,
["°"] = Math.PI / 180,
["deg"] = Math.PI / 180,
["rad"] = 180 / Math.PI,
["rpm"] = 2 * Math.PI / 60,
};
public static double rand() => rng.NextDouble();
public static double abs(double x) => Math.Abs(x);
public static double sqr(double x) => x * x;
public static double sqrt(double x) => Math.Sqrt(x);
public static double sign(double x) => (double)Math.Sign(x);
public static double floor(double x) => Math.Floor(x);
public static double round(double x) => Math.Round(x);
public static double exp(double x) => Math.Exp(x);
public static double log(double x) => Math.Log(x);
public static double sin(double x) => Math.Sin(x);
public static double cos(double x) => Math.Cos(x);
public static double tan(double x) => Math.Tan(x);
public static double asin(double x) => Math.Asin(x);
public static double acos(double x) => Math.Acos(x);
public static double atan(double x) => Math.Atan(x);
public static double sinh(double x) => Math.Sinh(x);
public static double cosh(double x) => Math.Cosh(x);
public static double tanh(double x) => Math.Tanh(x);
public static double asinh(double x) => Math.Log(Math.Sqrt(x * x + 1) + x);
public static double acosh(double x) => Math.Log(Math.Sqrt(x * x - 1) + x);
public static double atanh(double x) => -Math.Log((1 - x) / (1 + x)) / 2;
public static double pow(double x, double y) => Math.Pow(x, y);
public static double atan2(double dy, double dx) => Math.Atan2(dy, dx);
public static double min(double x, double y) => Math.Min(x, y);
public static double max(double x, double y) => Math.Max(x, y);
}
As a final note the parser also supports some named constants
"pi" = Math.PI,
"π" = Math.PI,
"e" = Math.E,
"Φ" = (1 + Math.Sqrt(5)) / 2,
"°" = Math.PI / 180,
"deg" = Math.PI / 180,
"rad" = 180 / Math.PI,
"rpm" = 2 * Math.PI / 60,
Such that inputs such as 6π and 60° and 1000rpm are supported.
Compiling
The .NET Expression class has a .Compile() method that can be used to turn the expression into a function. Wrappers around this method exists in the parser such that CompileUnary() and CompileBinary() produce functions of the form f(x) and f(x,y) respectively with arguments and return types that of double numbers.
From the example code above this is demonstrated with
var f = parser.CompileBinary();
double x = ...
double y = ...
double z = f(x, y);
I would like to compare two arbitrary JTokens of the same type and structure (Json.Net from NewtonSoft).
static int CompareTokens(JToken x, JToken y);
// possible output: 0 / 1 / -1
The main goal is to be able use this method to sort two Json strings, so that even if in the beginning they had the same data, but in the different order, in the end these are two exactly same strings. So the sort criterion doesn't really matter, it just matters that this criterion is always the same. And each small element of data should be taken into account.
JToken can be of one of next several types: Array, Boolean, Date, Float, Guid, Integer, Null, Object, Property, String, TimeSpan, Uri. I don't take into account comparing Bytes, Comment, Constructor, None, Undefined, Raw.
It would be great to gain some idea about comparing JArrays and JObjects. That should be some recursive comparison, because JArrays may consist of other JArrays and JObjects and vice versa. Any idea would be appreciated.
But knowing about comparing simpler types would also be very helpful. I wonder rather about knowing how to convert from JToken to actual type (than about knowing how to do it logically).
JValue has IComparable implemented, but I didn't figure out how to convert simple typed JToken to JValue. Knowing about this would also be helpful.
In Linq-to-JSON, JValue represents a primitive value (string, number, boolean, and so on). It implements IComparable<JValue>, so Json.NET takes care of sorting primitive values for you.
Building off of that, you're going to need to recursively descend the two JToken object hierarchies in parallel. When you encounter the first token with a different .Net type, or different properties (if not a JValue), or with a different value (if a JValue), you need to return back the comparison value.
Keep in mind the following:
A comparison method should be reflexive, antisymmetric and transitive.
Container tokens of different .Net type need to be ordered by type in some consistent manner.
the child tokens of JArray and JConstructor are ordered.
the child tokens of JObject are not, so they need to be compared in some stable, symmetric manner. Walking both in order of property name would seem to work.
There is no obvious way to compare JRaw, so don't try, and let an exception get thrown.
The following is a prototype implementation:
public class JTokenComparer : IComparer<JToken>
{
public static JTokenComparer Instance { get { return instance; } }
static JTokenComparer instance;
static JTokenComparer()
{
instance = new JTokenComparer();
}
readonly Dictionary<Type, KeyValuePair<int, IComparer<JToken>>> dict;
JTokenComparer()
{
dict = new Dictionary<Type, KeyValuePair<int, IComparer<JToken>>>
{
// Order chosen semi-arbitrarily. Putting values first seems reasonable though.
{typeof(JValue), new KeyValuePair<int, IComparer<JToken>>(0, new JValueComparer()) },
{typeof(JProperty), new KeyValuePair<int, IComparer<JToken>>(1, new JPropertyComparer()) },
{typeof(JArray), new KeyValuePair<int, IComparer<JToken>>(2, new JArrayComparer()) },
{typeof(JObject), new KeyValuePair<int, IComparer<JToken>>(3, new JObjectComparer()) },
{typeof(JConstructor), new KeyValuePair<int, IComparer<JToken>>(4, new JConstructorComparer()) },
};
}
#region IComparer<JToken> Members
public int Compare(JToken x, JToken y)
{
if (x is JRaw || y is JRaw)
throw new InvalidOperationException("Tokens of type JRaw cannot be sorted");
if (object.ReferenceEquals(x, y))
return 0;
else if (x == null)
return -1;
else if (y == null)
return 1;
var typeData1 = dict[x.GetType()];
var typeData2 = dict[y.GetType()];
int comp;
if ((comp = typeData1.Key.CompareTo(typeData2.Key)) != 0)
return comp;
if (typeData1.Value != typeData2.Value)
throw new InvalidOperationException("inconsistent dictionary values"); // Internal error
return typeData2.Value.Compare(x, y);
}
#endregion
}
abstract class JTokenComparerBase<TJToken> : IComparer<JToken> where TJToken : JToken
{
protected TJToken CheckType(JToken item)
{
if (item != null && item.GetType() != typeof(TJToken))
throw new ArgumentException(string.Format("Actual type {0} of token \"{1}\" does not match expected type {2}", item.GetType(), item, typeof(TJToken)));
return (TJToken)item;
}
protected bool TryBaseCompare(TJToken x, TJToken y, out int comparison)
{
CheckType(x);
CheckType(y);
if (object.ReferenceEquals(x, y))
{
comparison = 0;
return true;
}
else if (x == null)
{
comparison = -1;
return true;
}
else if (y == null)
{
comparison = 1;
return true;
}
comparison = 0;
return false;
}
protected abstract int CompareDerived(TJToken x, TJToken y);
protected int TokenCompare(JToken x, JToken y)
{
var tx = CheckType(x);
var ty = CheckType(y);
int comp;
if (TryBaseCompare(tx, ty, out comp))
return comp;
return CompareDerived(tx, ty);
}
#region IComparer<JToken> Members
int IComparer<JToken>.Compare(JToken x, JToken y)
{
return TokenCompare(x, y);
}
#endregion
}
abstract class JContainerOrderedComparerBase<TJToken> : JTokenComparerBase<TJToken> where TJToken : JContainer
{
protected int CompareItemsInOrder(TJToken x, TJToken y)
{
int comp;
// Dictionary order: sort on items before number of items.
for (int i = 0, n = Math.Min(x.Count, y.Count); i < n; i++)
if ((comp = JTokenComparer.Instance.Compare(x[i], y[i])) != 0)
return comp;
if ((comp = x.Count.CompareTo(y.Count)) != 0)
return comp;
return 0;
}
}
class JPropertyComparer : JTokenComparerBase<JProperty>
{
protected override int CompareDerived(JProperty x, JProperty y)
{
int comp;
if ((comp = x.Name.CompareTo(y.Name)) != 0)
return comp;
return JTokenComparer.Instance.Compare(x.Value, y.Value);
}
}
class JObjectComparer : JTokenComparerBase<JObject>
{
protected override int CompareDerived(JObject x, JObject y)
{
int comp;
// Dictionary order: sort on items before number of items.
// Order both property sequences to preserve reflexivity.
foreach (var propertyComp in x.Properties().OrderBy(p => p.Name).Zip(y.Properties().OrderBy(p => p.Name), (xp, yp) => JTokenComparer.Instance.Compare(xp, yp)))
if (propertyComp != 0)
return propertyComp;
if ((comp = x.Count.CompareTo(y.Count)) != 0)
return comp;
return 0;
}
}
class JArrayComparer : JContainerOrderedComparerBase<JArray>
{
protected override int CompareDerived(JArray x, JArray y)
{
int comp;
if ((comp = CompareItemsInOrder(x, y)) != 0)
return comp;
return 0;
}
}
class JConstructorComparer : JContainerOrderedComparerBase<JConstructor>
{
protected override int CompareDerived(JConstructor x, JConstructor y)
{
int comp;
if ((comp = x.Name.CompareTo(y.Name)) != 0)
return comp;
if ((comp = CompareItemsInOrder(x, y)) != 0)
return comp;
return 0;
}
}
class JValueComparer : JTokenComparerBase<JValue>
{
protected override int CompareDerived(JValue x, JValue y)
{
return Comparer<JToken>.Default.Compare(x, y); // JValue implements IComparable<JValue>
}
}
Lightly tested prototype fiddle.
This could, actually, be done with less code. Not as nice, because using string comparison instead of JValue comparison.
Following is not an exact answer to my own question, but the goal is achieved.
public static JToken Normalize(this JToken token)
{
var result = token;
switch (token.Type)
{
case JTokenType.Object:
var jObject = (JObject)token;
if (jObject != null && jObject.HasValues)
{
var newObject = new JObject();
foreach (var property in jObject.Properties().OrderBy(x => x.Name).ToList())
{
var value = property.Value as JToken;
if (value != null)
{
value = Normalize(value);
}
newObject.Add(property.Name, value);
}
return newObject;
}
break;
case JTokenType.Array:
var jArray = (JArray)token;
if (jArray != null && jArray.Count > 0)
{
var normalizedArrayItems = jArray
.Select(x => Normalize(x))
.OrderBy(x => x.ToString(), StringComparer.Ordinal);
result = new JArray(normalizedArrayItems);
}
break;
default:
break;
}
return result;
}
I need a way to represent an integer number that can be infinite. I'd prefer not to use a floating point type (double.PositiveInfinity) since the number can never be fractional and this might make the API confusing. What is the best way to do this?
Edit: One idea I haven't seen yet is using int? with null representing infinity. Are there any good reasons not to do this?
If you don't need the full range of integer values, you can use the int.MaxValue and int.MinValue constants to represent infinities.
However, if the full range of values is required, I'd suggest either creating a wrapper class or simply going for doubles.
An example partial implementation along the lines of the comments of SLaks and others (feedback welcome):
Usage:
int x = 4;
iint pi = iint.PositiveInfinity;
iint ni = iint.NegativeInfinity;
Assert.IsTrue(x + pi == iint.PositiveInfinity);
Assert.IsTrue(pi + 1 == iint.PositiveInfinity);
Assert.IsTrue(pi + (-ni) == iint.PositiveInfinity);
Assert.IsTrue((int)((iint)5) == 5);
Implementation:
public struct iint
{
private readonly int _int;
public iint(int value)
{
if(value == int.MaxValue || value == int.MinValue)
throw new InvalidOperationException("min/max value reserved in iint");
_int = value;
}
public static explicit operator int(iint #this)
{
if(#this._int == int.MaxValue || #this._int == int.MinValue)
throw new InvalidOperationException("cannot implicit convert infinite iint to int");
return #this._int;
}
public static implicit operator iint(int other)
{
if(other == int.MaxValue || other == int.MinValue)
throw new InvalidOperationException("cannot implicit convert max-value into to iint");
return new iint(other);
}
public bool IsPositiveInfinity {get { return _int == int.MaxValue; } }
public bool IsNegativeInfinity { get { return _int == int.MinValue; } }
private iint(bool positive)
{
if (positive)
_int = int.MaxValue;
else
_int = int.MinValue;
}
public static readonly iint PositiveInfinity = new iint(true);
public static readonly iint NegativeInfinity = new iint(false);
public static bool operator ==(iint a, iint b)
{
return a._int == b._int;
}
public static bool operator !=(iint a, iint b)
{
return a._int != b._int;
}
public static iint operator +(iint a, iint b)
{
if (a.IsPositiveInfinity && b.IsNegativeInfinity)
throw new InvalidOperationException();
if (b.IsPositiveInfinity && a.IsNegativeInfinity)
throw new InvalidOperationException();
if (a.IsPositiveInfinity)
return PositiveInfinity;
if (a.IsNegativeInfinity)
return NegativeInfinity;
if (b.IsPositiveInfinity)
return PositiveInfinity;
if (b.IsNegativeInfinity)
return NegativeInfinity;
return a._int + b._int;
}
public static iint operator -(iint a, iint b)
{
if (a.IsPositiveInfinity && b.IsPositiveInfinity)
throw new InvalidOperationException();
if (a.IsNegativeInfinity && b.IsNegativeInfinity)
throw new InvalidOperationException();
if (a.IsPositiveInfinity)
return PositiveInfinity;
if (a.IsNegativeInfinity)
return NegativeInfinity;
if (b.IsPositiveInfinity)
return NegativeInfinity;
if (b.IsNegativeInfinity)
return PositiveInfinity;
return a._int - b._int;
}
public static iint operator -(iint a)
{
if (a.IsNegativeInfinity)
return PositiveInfinity;
if (a.IsPositiveInfinity)
return NegativeInfinity;
return -a;
}
/* etc... */
/* other operators here */
}
Your API can use a convention that int.MaxValue represents positive infinity value and int.MinValue - negative infinity.
But you still need to document it somewhere and, may be you will need some operations with your infinite integer:
/// <summary>
/// Making int infinity
/// ...
/// </summary>
public static class IntExtension
{
public const int PositiveInfinity = int.MaxValue;
public const int NegativeInfinity = int.MinValue;
public static bool IsPositiveInfinity(this int x)
{
return x == PositiveInfinity;
}
public static bool IsNegativeInfinity(this int x)
{
return x == NegativeInfinity;
}
public static int Operation(this int x, int y)
{
// ...
return PositiveInfinity;
}
}
Another partial implementation (I see Jack was faster):
struct InfinityInt
{
readonly int Value;
InfinityInt(int value, bool allowInfinities)
{
if (!allowInfinities && (value == int.MinValue || value == int.MaxValue))
throw new ArgumentOutOfRangeException("value");
Value = value;
}
public InfinityInt(int value)
: this(value, false)
{
}
public static InfinityInt PositiveInfinity = new InfinityInt(int.MaxValue, true);
public static InfinityInt NegativeInfinity = new InfinityInt(int.MinValue, true);
public bool IsAnInfinity
{
get { return Value == int.MaxValue || Value == int.MinValue; }
}
public override string ToString()
{
if (Value == int.MinValue)
return double.NegativeInfinity.ToString();
if (Value == int.MaxValue)
return double.PositiveInfinity.ToString();
return Value.ToString();
}
public static explicit operator int(InfinityInt ii)
{
if (ii.IsAnInfinity)
throw new OverflowException();
return ii.Value;
}
public static explicit operator double(InfinityInt ii)
{
if (ii.Value == int.MinValue)
return double.NegativeInfinity;
if (ii.Value == int.MaxValue)
return double.PositiveInfinity;
return ii.Value;
}
public static explicit operator InfinityInt(int i)
{
return new InfinityInt(i); // can throw
}
public static explicit operator InfinityInt(double d)
{
if (double.IsNaN(d))
throw new ArgumentException("NaN not supported", "d");
if (d >= int.MaxValue)
return PositiveInfinity;
if (d <= int.MinValue)
return NegativeInfinity;
return new InfinityInt((int)d);
}
static InfinityInt FromLongSafely(long x)
{
if (x >= int.MaxValue)
return PositiveInfinity;
if (x <= int.MinValue)
return NegativeInfinity;
return new InfinityInt((int)x);
}
public static InfinityInt operator +(InfinityInt a, InfinityInt b)
{
if (a.IsAnInfinity || b.IsAnInfinity)
{
if (!b.IsAnInfinity)
return a;
if (!a.IsAnInfinity)
return b;
if (a.Value == b.Value)
return a;
throw new ArithmeticException("Undefined");
}
return FromLongSafely((long)a.Value + (long)b.Value);
}
public static InfinityInt operator *(InfinityInt a, InfinityInt b)
{
if (a.IsAnInfinity || b.IsAnInfinity)
{
if (a.Value == 0 || b.Value == 0)
throw new ArithmeticException("Undefined");
return (a.Value > 0) == (b.Value > 0) ? PositiveInfinity : NegativeInfinity;
}
return FromLongSafely((long)a.Value * (long)b.Value);
}
// and so on, and so on
}
C# has a type for this the BigInteger class is unlimited size
http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx
If you want the class to have a representation of infinity -- then wrap BigInteger in a class that gives it an infinity flag.
You will have to redefine all standard operators and conversions to get this to work.
How exactly to have operations on infinity work depends on your domain.
(For example in some forms of math you would like 2 x infinity = infinity and in some you don't).
How the details are implemented really depend on your domain problem and are not clear from your question.
I want to write a custom comparer for a SortedDictionary, where keys are sorted based on their type. Is this possible?
public class StateBase
{
// This is a base class I have to inherit from
}
SortedDictionary<StateBase, int> _stateDictionary =
new SortedDictionary<StateBase, int>(new StateComparer());
class StateComparer : IComparer<StateBase>
{
public int Compare(StateBase a, StateBase b)
{
// I'd like to sort these based on their type
// I don't particularly care what order they are in, I just want them
// to be sorted.
}
}
Sure, why not? Note that we must be talking about reference-types for this to apply, so something like:
public class TypeComparer<T> : IComparer<T>, IEqualityComparer<T> where T : class
{
public static readonly TypeComparer<T> Singleton= new TypeComparer<T>();
private TypeComparer(){}
bool IEqualityComparer<T>.Equals(T x, T y)
{
if (ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
Type xType = x.GetType(), yType = y.GetType();
return xType == yType && EqualityComparer<T>.Default.Equals(x, y);
}
int IEqualityComparer<T>.GetHashCode(T x)
{
if (x == null) return 0;
return -17*x.GetType().GetHashCode() + x.GetHashCode();
}
int IComparer<T>.Compare(T x, T y)
{
if(x==null) return y == null ? 0 : -1;
if (y == null) return 1;
Type xType = x.GetType(), yType = y.GetType();
int delta = xType == yType ? 0 : string.Compare(
xType.FullName, yType.FullName);
if (delta == 0) delta = Comparer<T>.Default.Compare(x, y);
return delta;
}
}
You can. If your comparer implements IComparer<T>, it can be passed to a new SortedDictionary instance by the corresponding constructor overload.
The Compare method then somehow decides what item is greater or lower. It is the place where you can implement your compare-by-type logic.
Here is an example to compare Type instances based on their name:
public class TypeComparer : IComparer<Type>
{
public int Compare(Type x, Type y)
{
if(x != null && y != null)
return x.FullName.CompareTo(y.FullName);
else if(x != null)
return x.FullName.CompareTo(null);
else if(y != null)
return y.FullName.CompareTo(null);
else
return 0;
}
}
I've made myself a rss reader that keeps me up to date and informs me on new shows, or atleast thats the thought behind.
I've made a struct "SeasonEpisode" that hold two ints (season+episode) and a override ToString function.
I store the latest watched locally and i then read whats the newest is from the rss. But how could I compare SeasonEpisodes? right now I take each of the ints and compare them
if( se1.Season >= se2.Season )
if( se1.Episode > se2.Episode || se1.Season > se2.Season )
// new episode!
What i really want is
if( se1 > se2 )
// new episode
Could i get any help please?
There are two ways:
Implement IComparable<T> and use CompareTo
Overload the greater and less than operators
I suggest, you use both ways:
public class SeasonEpisode : IComparable<SeasonEpisode>
{
public int CompareTo(SeasonEpisode other)
{
if(other == null)
return 1;
if(Season == other.Season)
{
if(Episode == other.Episode)
return 0;
else if(Episode < other.Episode)
return -1;
else
return 1;
}
else if(Season < other.Season)
return -1;
else
return 1;
}
public static bool operator <(SeasonEpisode e1, SeasonEpisode e2)
{
return e1.CompareTo(e2) < 0;
}
public static bool operator >(SeasonEpisode e1, SeasonEpisode e2)
{
return e1.CompareTo(e2) > 0;
}
}
As i tripped over a NullReferenceException, here's an improvement (well this may be subjective ;-)) to Daniel Hilgarth's answer.
The only change is that it handles nulls in case the first argument to the > or < operator is null:
public class SeasonEpisode : IComparable<SeasonEpisode>
{
private static int Compare(SeasonEpisode e1, SeasonEpisode e2)
{
if (e1 == null && e2 == null)
return 0;
else if (e1 == null)
return -1;
else if (e2 == null)
return 1;
if(e1.Season == e2.Season)
{
if(e1.Episode == e2.Episode)
return 0;
else if(e1.Episode < e2.Episode)
return -1;
else
return 1;
}
else if(e1.Season < e2.Season)
return -1;
else
return 1;
}
public int CompareTo(SeasonEpisode other)
{
return Compare(this, other);
}
public static bool operator <(SeasonEpisode e1, SeasonEpisode e2)
{
return Compare(e1, e2) < 0;
}
public static bool operator >(SeasonEpisode e1, SeasonEpisode e2)
{
return Compare(e1, e2) > 0;
}
}
You can implement the IComparer<T> interface
Defines a method that a type implements to compare two objects.
You can implement IComparable if you want a class to be comparable to another instance of that class. Which is probably what you want, in this case.
Implement IComparer if you need a class that compares two objects.