Skip to content

Commit

Permalink
NextCombination V2, Parallel.For, namespace sync
Browse files Browse the repository at this point in the history
- Remade NextCombination method to actually support skipping, instead of running in a loop. A bit complicated code if you consider it uses `goto`.
- Added toggle to use Parallel.For instead of task pools. Testing shows a slight improvement on AMD CPUs.
- Synced all namespaces to organize codebase
  • Loading branch information
Prevter committed Apr 25, 2023
1 parent b638bef commit 4496c5f
Show file tree
Hide file tree
Showing 20 changed files with 2,316 additions and 2,232 deletions.
3 changes: 2 additions & 1 deletion FloatTool/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

using DiscordRPC;
using FloatTool.Common;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -31,7 +32,7 @@

namespace FloatTool
{
public partial class App : Application
public partial class App : Application
{
public static ResourceDictionary ThemeDictionary { get; set; }

Expand Down
3 changes: 2 additions & 1 deletion FloatTool/AppHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
*/

using DiscordRPC;
using FloatTool.Common;
using System.Collections.Generic;
using System.IO;

namespace FloatTool
{
internal static class AppHelpers
internal static class AppHelpers
{
public static FileSystemWatcher Watcher;
public static DiscordRpcClient DiscordClient;
Expand Down
117 changes: 71 additions & 46 deletions FloatTool/Common/Calculations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,81 @@
- along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;
using System.Numerics;

namespace FloatTool
namespace FloatTool.Common
{
static public class Calculations
{
public static double Craft(InputSkin[] ingridients, double minFloat, double floatRange)
{
return floatRange * (ingridients[0].WearValue
+ ingridients[1].WearValue
+ ingridients[2].WearValue
+ ingridients[3].WearValue
+ ingridients[4].WearValue
+ ingridients[5].WearValue
+ ingridients[6].WearValue
+ ingridients[7].WearValue
+ ingridients[8].WearValue
+ ingridients[9].WearValue) + minFloat;
}
static public class Calculations
{
public static double Craft(InputSkin[] ingridients, double minFloat, double floatRange)
{
return floatRange * (ingridients[0].WearValue
+ ingridients[1].WearValue
+ ingridients[2].WearValue
+ ingridients[3].WearValue
+ ingridients[4].WearValue
+ ingridients[5].WearValue
+ ingridients[6].WearValue
+ ingridients[7].WearValue
+ ingridients[8].WearValue
+ ingridients[9].WearValue) + minFloat;
}

public static bool NextCombination(int[] num, int n)
{
bool finished = false;
for (int i = 9; !finished; --i)
{
if (num[i] < n + i)
{
++num[i];
if (i < 9)
for (int j = i + 1; j < 10; ++j)
num[j] = num[j - 1] + 1;
return true;
}
finished = i == 0;
}
return false;
}

public static long GetCombinationsCount(int poolSize)
{
BigInteger fact1 = poolSize;
for (int i = poolSize - 1; i > 10; i--)
fact1 *= i;
public static bool NextCombination(int[] num, int n)
{
bool finished = false;
for (int i = 9; !finished; --i)
{
if (num[i] < n + i)
{
++num[i];
if (i < 9)
for (int j = i + 1; j < 10; ++j)
num[j] = num[j - 1] + 1;
return true;
}
finished = i == 0;
}
return false;
}

BigInteger fact2 = poolSize - 10;
for (int i = poolSize - 11; i > 1; i--)
fact2 *= i;
public static bool NextCombination(int[] arr, int n, int step)
{
arr[9] += step;
if (arr[9] < n + 10) return true;
else
{
fix_loop:
int overflow = arr[9] - (n + 10);
for (int i = 9; i >= 0; --i)
{
if (arr[i] < n + i)
{
++arr[i];
for (int j = i + 1; j < 10; ++j)
arr[j] = arr[j - 1] + 1;

return (long)(fact1 / fact2);
}
}
arr[9] += overflow;

if (arr[9] < n + 10) return true;
else goto fix_loop;
}
}
return false;
}
}

public static long GetCombinationsCount(int poolSize)
{
BigInteger fact1 = poolSize;
for (int i = poolSize - 1; i > 10; i--)
fact1 *= i;

BigInteger fact2 = poolSize - 10;
for (int i = poolSize - 11; i > 1; i--)
fact2 *= i;

return (long)(fact1 / fact2);
}
}
}
72 changes: 36 additions & 36 deletions FloatTool/Common/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,47 @@
using log4net.Repository.Hierarchy;
using System.Reflection;

namespace FloatTool
namespace FloatTool.Common
{
public sealed class Logger
{
public static ILog Log { get; } = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public sealed class Logger
{
public static ILog Log { get; } = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

public static void Initialize()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
public static void Initialize()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

PatternLayout patternLayout = new()
{
ConversionPattern = "%date [%thread] %-5level - %message%newline"
};
patternLayout.ActivateOptions();
PatternLayout patternLayout = new()
{
ConversionPattern = "%date [%thread] %-5level - %message%newline"
};
patternLayout.ActivateOptions();

RollingFileAppender roller = new()
{
AppendToFile = false,
File = @"logs/log.txt",
Layout = patternLayout,
MaxSizeRollBackups = 5,
MaximumFileSize = "250KB",
RollingStyle = RollingFileAppender.RollingMode.Size,
StaticLogFileName = true
};
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);
RollingFileAppender roller = new()
{
AppendToFile = false,
File = @"logs/log.txt",
Layout = patternLayout,
MaxSizeRollBackups = 5,
MaximumFileSize = "250KB",
RollingStyle = RollingFileAppender.RollingMode.Size,
StaticLogFileName = true
};
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);

MemoryAppender memory = new();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
MemoryAppender memory = new();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);

hierarchy.Root.Level = Level.Info;
hierarchy.Configured = true;
}
hierarchy.Root.Level = Level.Info;
hierarchy.Configured = true;
}

public static void Debug(object message) => Log.Debug(message);
public static void Info(object message) => Log.Info(message);
public static void Warn(object message) => Log.Warn(message);
public static void Error(object message) => Log.Error(message);
public static void Fatal(object message) => Log.Fatal(message);
}
public static void Debug(object message) => Log.Debug(message);
public static void Info(object message) => Log.Info(message);
public static void Warn(object message) => Log.Warn(message);
public static void Error(object message) => Log.Error(message);
public static void Fatal(object message) => Log.Fatal(message);
}
}
56 changes: 28 additions & 28 deletions FloatTool/Common/RelayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,38 @@
using System;
using System.Windows.Input;

namespace FloatTool
namespace FloatTool.Common
{
public sealed class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public sealed class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;

public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}

public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}

public bool CanExecute(object parameters)
{
return _canExecute == null || _canExecute(parameters);
}
public bool CanExecute(object parameters)
{
return _canExecute == null || _canExecute(parameters);
}

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(object parameters)
{
_execute(parameters);
}
}
public void Execute(object parameters)
{
_execute(parameters);
}
}
}
Loading

0 comments on commit 4496c5f

Please sign in to comment.