Skip to content

Commit

Permalink
small stylistic improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wizykowski committed Apr 10, 2017
1 parent dbb8a6c commit d108249
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
20 changes: 8 additions & 12 deletions Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static void SetAllValues<T>(this T[,] table, T value, int maxN, int maxM)
for (int i2 = 0; i2 <= maxM; i2++)
table[i, i2] = value;
}

}

public static class DictExtensions
Expand Down Expand Up @@ -62,7 +61,7 @@ public static TValue GetOrNew<TKey, TValue>(this Dictionary<TKey, TValue> dict,

public static bool ContainsAllKeys<TKey, TValue>(this Dictionary<TKey, TValue> dict, IEnumerable<TKey> keys)
{
return keys.All(key => dict.ContainsKey(key));
return keys.All(dict.ContainsKey);
}

public static void Increment<TKey>(this Dictionary<TKey, int> dict, TKey key, int step)
Expand Down Expand Up @@ -100,8 +99,7 @@ public static void AppendItem<TKey, TValue>(this Dictionary<TKey, List<TValue>>
{
if (dict.ContainsKey(key) == false)
{
var newList = new List<TValue>();
newList.Add(item);
var newList = new List<TValue> {item};
dict[key] = newList;
}
else
Expand Down Expand Up @@ -146,8 +144,7 @@ public static class ClientExtensions
{
public static void Swap<T>(ref T left, ref T right)
{
T temp;
temp = left;
T temp = left;
left = right;
right = temp;
}
Expand Down Expand Up @@ -194,22 +191,22 @@ public static string ParseToken<T>(this string line, out T parsed)
{
var all = line.Split(' ');
var head = all.First();
var tail = String.Join(" ", all.Skip(1).ToArray());
var tail = string.Join(" ", all.Skip(1).ToArray());
if (typeof(T) == typeof(string))
{
parsed = (T)(object)head;
}
else if (typeof(T) == typeof(decimal))
{
parsed = (T)(object)Decimal.Parse(head, CultureInfo.InvariantCulture);
parsed = (T)(object)decimal.Parse(head, CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(double))
{
parsed = (T)(object)Double.Parse(head, CultureInfo.InvariantCulture);
parsed = (T)(object)double.Parse(head, CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(int))
{
parsed = (T)(object)Int32.Parse(head, CultureInfo.InvariantCulture);
parsed = (T)(object)int.Parse(head, CultureInfo.InvariantCulture);
}
else if (typeof(T) == typeof(long))
{
Expand All @@ -226,7 +223,7 @@ public static string ParseToken<T>(this string line, out T parsed)

public static string OutputLine(params object[] data)
{
return String.Join(" ", data.Select(d => d.ToString()));
return string.Join(" ", data.Select(d => d.ToString()));
}

public static IEnumerable<Tuple<int, T>> WithIndex<T>(this IEnumerable<T> collection)
Expand Down Expand Up @@ -269,7 +266,6 @@ public static T MaxElement<T>(this IEnumerable<T> collection)
var minimum = collection.Max();
return collection.First(p => p == minimum);
}

}
}

Expand Down
1 change: 0 additions & 1 deletion Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public Tuple<List<Action>, List<Action>> Process(List<Action> actions)
/// Processes actions. Returns list of actions which have been processed and list of ignored actions.
/// Move to the next event
/// </summary>
/// <param name="actions"></param>
public Tuple<List<Action>, List<Action>> NextTurn(List<Action> actions, bool processAllInGivenOrder = false)
{
var actionsIgnored = new List<Action>();
Expand Down
11 changes: 5 additions & 6 deletions SolutionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ public virtual bool Act()
public void RunIterations(Action<RunInfo> go, double timeAvailable, int iterations)
{
Stopwatch timer = new Stopwatch();
double perIteration = 0;
double timeLeft = timeAvailable;
for (int iteration = 0; iteration < iterations; iteration++)
{
timer.Start();
go(new RunInfo(iteration, timeLeft / (iterations - iteration)));
timer.Stop();
timeLeft = timeAvailable - timer.ElapsedMilliseconds;
perIteration = timer.ElapsedMilliseconds / (iteration + 1);
double perIteration = timer.ElapsedMilliseconds / (iteration + 1);

if (1.1 * perIteration >= timeLeft)
break;
Expand Down Expand Up @@ -103,11 +102,11 @@ public void MeasureTime(string name, Action<int> go)

public struct RunInfo
{
public readonly int seed;
public readonly double timeLeft;
public readonly int Seed;
public readonly double TimeLeft;
public RunInfo(int s, double t)
{
seed = s;
timeLeft = t;
Seed = s;
TimeLeft = t;
}
}
21 changes: 13 additions & 8 deletions _Commons/Algorithms/Maps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ public static IEnumerable<Moves4> All4()

public static GridPoint Move(this Moves4 d, GridPoint point)
{
if (d == Moves4.Up)
return new GridPoint(point.X, point.Y - 1);
if (d == Moves4.Down)
return new GridPoint(point.X, point.Y + 1);
if (d == Moves4.Left)
return new GridPoint(point.X - 1, point.Y);

return new GridPoint(point.X + 1, point.Y);
switch (d)
{
case Moves4.Up:
return new GridPoint(point.X, point.Y - 1);
case Moves4.Down:
return new GridPoint(point.X, point.Y + 1);
case Moves4.Left:
return new GridPoint(point.X - 1, point.Y);
case Moves4.Right:
return new GridPoint(point.X + 1, point.Y);
default:
throw new ArgumentException("Unsupported move: " + d);
}
}
}
}

0 comments on commit d108249

Please sign in to comment.