diff --git a/ortools/constraint_solver/csharp/IntVarArrayHelper.cs b/ortools/constraint_solver/csharp/IntVarArrayHelper.cs index 22cde01cd4..decf72e365 100644 --- a/ortools/constraint_solver/csharp/IntVarArrayHelper.cs +++ b/ortools/constraint_solver/csharp/IntVarArrayHelper.cs @@ -76,7 +76,7 @@ public static class IntVarArrayHelper // get solver from array of integer variables private static Solver GetSolver(IntVar[] vars) { - if (vars == null || vars.Length <= 0) + if (vars is null || vars.Length <= 0) throw new ArgumentException("Array cannot be null or empty"); return vars[0].solver(); @@ -84,14 +84,14 @@ public static class IntVarArrayHelper // get solver from array of integer expressions private static Solver GetSolver(IntExpr[] expressions) { - if (expressions == null || expressions.Length <= 0) + if (expressions is null || expressions.Length <= 0) throw new ArgumentException("Array cannot be null or empty"); return expressions[0].solver(); } private static Solver GetSolver(IConstraintWithStatus[] cts) { - if (cts == null || cts.Length <= 0) + if (cts is null || cts.Length <= 0) throw new ArgumentException("Array cannot be null or empty"); return cts[0].solver(); diff --git a/ortools/constraint_solver/csharp/IntervalVarArrayHelper.cs b/ortools/constraint_solver/csharp/IntervalVarArrayHelper.cs index 0c39c49092..c83565c99a 100644 --- a/ortools/constraint_solver/csharp/IntervalVarArrayHelper.cs +++ b/ortools/constraint_solver/csharp/IntervalVarArrayHelper.cs @@ -22,7 +22,7 @@ public static class IntervalVarArrayHelper // get solver from array of interval variables private static Solver GetSolver(IntervalVar[] vars) { - if (vars == null || vars.Length <= 0) + if (vars is null || vars.Length <= 0) throw new ArgumentException("Array cannot be null or empty"); return vars[0].solver(); diff --git a/ortools/dotnet/Google.OrTools.csproj.in b/ortools/dotnet/Google.OrTools.csproj.in index c524bdbebf..fc5f735442 100644 --- a/ortools/dotnet/Google.OrTools.csproj.in +++ b/ortools/dotnet/Google.OrTools.csproj.in @@ -1,6 +1,7 @@ Library + 9.0 netcoreapp3.1;net6.0 win-x64;osx-x64;linux-x64 @DOTNET_PROJECT@ diff --git a/ortools/sat/csharp/CpModel.cs b/ortools/sat/csharp/CpModel.cs index 04454866cd..4ddf577748 100644 --- a/ortools/sat/csharp/CpModel.cs +++ b/ortools/sat/csharp/CpModel.cs @@ -71,11 +71,7 @@ public class CpModel public ILiteral TrueLiteral() { - if (true_literal_ == null) - { - true_literal_ = new BoolVar(model_, ConvertConstant(1)); - } - return true_literal_; + return true_literal_ ??= new BoolVar(model_, ConvertConstant(1)); } public ILiteral FalseLiteral() @@ -83,8 +79,9 @@ public class CpModel return TrueLiteral().Not(); } - private long FillLinearConstraint(LinearExpr expr, ref LinearConstraintProto linear) + private long FillLinearConstraint(LinearExpr expr, out LinearConstraintProto linear) { + linear = new LinearConstraintProto(); Dictionary dict = new Dictionary(); long constant = LinearExpr.GetVarValueMap(expr, 1L, dict); foreach (KeyValuePair term in dict) @@ -94,12 +91,12 @@ public class CpModel } return constant; } + public Constraint AddLinearConstraint(LinearExpr expr, long lb, long ub) { - LinearConstraintProto linear = new LinearConstraintProto(); - long constant = FillLinearConstraint(expr, ref linear); - linear.Domain.Add(lb is Int64.MinValue ? lb : lb - constant); - linear.Domain.Add(ub is Int64.MaxValue ? ub : ub - constant); + long constant = FillLinearConstraint(expr, out var linear); + linear.Domain.Add(lb is Int64.MinValue or Int64.MaxValue ? lb : lb - constant); + linear.Domain.Add(ub is Int64.MinValue or Int64.MaxValue ? ub : ub - constant); Constraint ct = new Constraint(model_); ct.Proto.Linear = linear; @@ -108,18 +105,10 @@ public class CpModel public Constraint AddLinearExpressionInDomain(LinearExpr expr, Domain domain) { - LinearConstraintProto linear = new LinearConstraintProto(); - long constant = FillLinearConstraint(expr, ref linear); + long constant = FillLinearConstraint(expr, out var linear); foreach (long value in domain.FlattenedIntervals()) { - if (value == Int64.MinValue || value == Int64.MaxValue) - { - linear.Domain.Add(value); - } - else - { - linear.Domain.Add(value - constant); - } + linear.Domain.Add(value is Int64.MinValue or Int64.MaxValue ? value : value - constant); } Constraint ct = new Constraint(model_); @@ -129,8 +118,7 @@ public class CpModel private Constraint AddLinearExpressionNotEqualCst(LinearExpr expr, long value) { - LinearConstraintProto linear = new LinearConstraintProto(); - long constant = FillLinearConstraint(expr, ref linear); + long constant = FillLinearConstraint(expr, out var linear); linear.Domain.Add(Int64.MinValue); linear.Domain.Add(value - constant - 1); linear.Domain.Add(value - constant + 1); @@ -589,7 +577,7 @@ public class CpModel bool HasObjective() { - return model_.Objective != null; + return model_.Objective is not null; } // Search Decision. @@ -610,10 +598,7 @@ public class CpModel public void AddHint(IntVar var, long value) { - if (model_.SolutionHint == null) - { - model_.SolutionHint = new PartialVariableAssignment(); - } + model_.SolutionHint ??= new PartialVariableAssignment(); model_.SolutionHint.Vars.Add(var.GetIndex()); model_.SolutionHint.Values.Add(value); } @@ -646,7 +631,7 @@ public class CpModel void SetObjective(LinearExpr obj, bool minimize) { CpObjectiveProto objective = new CpObjectiveProto(); - if (obj == null) + if (obj is null) { objective.Offset = 0L; objective.ScalingFactor = minimize ? 1L : -1; @@ -705,34 +690,18 @@ public class CpModel private int ConvertConstant(long value) { - if (constant_map_.ContainsKey(value)) + if (constant_map_.TryGetValue(value, out var index)) { - return constant_map_[value]; - } - else - { - int index = model_.Variables.Count; - IntegerVariableProto var = new IntegerVariableProto(); - var.Domain.Add(value); - var.Domain.Add(value); - constant_map_.Add(value, index); - model_.Variables.Add(var); return index; } - } - private int GetOrCreateIndex(X x) - { - if (typeof(X) == typeof(IntVar)) - { - IntVar vx = (IntVar)(Object)x; - return vx.Index; - } - if (typeof(X) == typeof(long) || typeof(X) == typeof(int)) - { - return ConvertConstant(Convert.ToInt64(x)); - } - throw new ArgumentException("Cannot extract index from argument"); + index = model_.Variables.Count; + IntegerVariableProto var = new IntegerVariableProto(); + var.Domain.Add(value); + var.Domain.Add(value); + constant_map_.Add(value, index); + model_.Variables.Add(var); + return index; } public LinearExpr GetLinearExpr(X x) diff --git a/ortools/sat/csharp/CpSolver.cs b/ortools/sat/csharp/CpSolver.cs index 802d3439f8..5724cccd0c 100644 --- a/ortools/sat/csharp/CpSolver.cs +++ b/ortools/sat/csharp/CpSolver.cs @@ -23,15 +23,15 @@ public class CpSolver { // Setup search. CreateSolveWrapper(); - if (string_parameters_ != null) + if (string_parameters_ is not null) { solve_wrapper_.SetStringParameters(string_parameters_); } - if (log_callback_ != null) + if (log_callback_ is not null) { solve_wrapper_.AddLogCallbackFromClass(log_callback_); } - if (cb != null) + if (cb is not null) { solve_wrapper_.AddSolutionCallback(cb); } @@ -39,7 +39,7 @@ public class CpSolver response_ = solve_wrapper_.Solve(model.Model); // Cleanup search. - if (cb != null) + if (cb is not null) { solve_wrapper_.ClearSolutionCallback(cb); } @@ -66,7 +66,7 @@ public class CpSolver [MethodImpl(MethodImplOptions.Synchronized)] public void StopSearch() { - if (solve_wrapper_ != null) + if (solve_wrapper_ is not null) { solve_wrapper_.StopSearch(); } diff --git a/ortools/sat/csharp/IntegerExpressions.cs b/ortools/sat/csharp/IntegerExpressions.cs index 5107b1d3fe..67a46a228f 100644 --- a/ortools/sat/csharp/IntegerExpressions.cs +++ b/ortools/sat/csharp/IntegerExpressions.cs @@ -281,7 +281,7 @@ public class LinearExpr public static long GetVarValueMap(LinearExpr e, long initial_coeff, Dictionary dict) { List terms = new List(); - if ((Object)e != null) + if (e is not null) { terms.Add(new Term(e, initial_coeff)); } @@ -291,7 +291,7 @@ public class LinearExpr { Term term = terms[0]; terms.RemoveAt(0); - if (term.coefficient == 0 || (Object)term.expr == null) + if (term.coefficient == 0 || term.expr is null) { continue; } @@ -524,7 +524,7 @@ public class LinearExprBuilder : LinearExpr foreach (Term term in terms_) { bool first = String.IsNullOrEmpty(result); - if ((Object)term.expr == null || term.coefficient == 0) + if (term.expr is null || term.coefficient == 0) { continue; } @@ -616,7 +616,6 @@ public class IntVar : LinearExpr { public IntVar(CpModelProto model, Domain domain, string name) { - model_ = model; index_ = model.Variables.Count; var_ = new IntegerVariableProto(); var_.Name = name; @@ -626,7 +625,6 @@ public class IntVar : LinearExpr public IntVar(CpModelProto model, long lb, long ub, string name) { - model_ = model; index_ = model.Variables.Count; var_ = new IntegerVariableProto(); var_.Name = name; @@ -637,7 +635,6 @@ public class IntVar : LinearExpr public IntVar(CpModelProto model, int index) { - model_ = model; index_ = index; var_ = model.Variables[index]; } @@ -673,14 +670,7 @@ public class IntVar : LinearExpr public override string ToString() { - if (var_.Name != null) - { - return var_.Name; - } - else - { - return var_.ToString(); - } + return var_.Name is not null ? var_.Name : var_.ToString(); } public string Name() @@ -688,8 +678,7 @@ public class IntVar : LinearExpr return var_.Name; } - protected CpModelProto model_; - protected int index_; + protected readonly int index_; protected IntegerVariableProto var_; } @@ -706,11 +695,7 @@ public class BoolVar : IntVar, ILiteral public ILiteral Not() { - if (negation_ == null) - { - negation_ = new NotBoolVar(this); - } - return negation_; + return negation_ ??= new NotBoolVar(this); } public LinearExpr NotAsExpr() @@ -755,7 +740,7 @@ public class NotBoolVar : LinearExpr, ILiteral return String.Format("Not({0})", boolvar_.ToString()); } - private BoolVar boolvar_; + private readonly BoolVar boolvar_; } public class BoundedLinearExpression diff --git a/ortools/util/csharp/NestedArrayHelper.cs b/ortools/util/csharp/NestedArrayHelper.cs index 1714bd9e20..9f46001f1b 100644 --- a/ortools/util/csharp/NestedArrayHelper.cs +++ b/ortools/util/csharp/NestedArrayHelper.cs @@ -58,7 +58,7 @@ public static class NestedArrayHelper var result = new int[arr.GetLength(0)]; for (var i = 0; i < arr.GetLength(0); i++) { - if (arr[i] != null) + if (arr[i] is not null) result[i] = arr[i].Length; } return result;