Skip to content

Commit

Permalink
Refactor Kompression to 2.1.0;
Browse files Browse the repository at this point in the history
  • Loading branch information
onepiecefreak3 committed Jan 6, 2025
1 parent 14c3a68 commit 0b25e24
Show file tree
Hide file tree
Showing 255 changed files with 4,008 additions and 3,703 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Kompression.Contract.Configuration
{
public interface ICompressionConfigurationBuilder
{
IEncoderConfigurationBuilder Encode { get; }

IDecoderConfigurationBuilder Decode { get; }

/// <summary>
/// Builds the current configuration to an <see cref="ICompression"/>.
/// </summary>
/// <returns>The <see cref="ICompression"/> for this configuration.</returns>
ICompression Build();

ICompressionConfigurationBuilder Clone();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Kompression.Contract.Decoder;

namespace Kompression.Contract.Configuration
{
public delegate IDecoder CreateDecoderDelegate();

public interface IDecoderConfigurationBuilder
{
/// <summary>
/// Sets the factory to create an <see cref="IDecoder"/>.
/// </summary>
/// <param name="decoderDelegate">The factory to create an <see cref="IDecoder"/>.</param>
/// <returns>The configuration object.</returns>
ICompressionConfigurationBuilder With(CreateDecoderDelegate decoderDelegate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Kompression.Contract.Encoder;

namespace Kompression.Contract.Configuration
{
public delegate IEncoder CreateEncoderDelegate();
public delegate ILempelZivEncoder CreateLempelZivEncoderDelegate();
public delegate IHuffmanEncoder CreateHuffmanEncoderDelegate();
public delegate ILempelZivHuffmanEncoder CreateLempelZivHuffmanEncoderDelegate();

public interface IEncoderConfigurationBuilder
{
/// <summary>
/// Sets the factory to create an <see cref="IEncoder"/>.
/// </summary>
/// <param name="encoderDelegate">The factory to create an <see cref="IEncoder"/>.</param>
/// <returns>The configuration object.</returns>
ICompressionConfigurationBuilder With(CreateEncoderDelegate encoderDelegate);

/// <summary>
/// Sets the factory to create an <see cref="ILempelZivEncoder"/>.
/// </summary>
/// <param name="encoderDelegate">The factory to create an <see cref="ILempelZivEncoder"/>.</param>
/// <returns>The configuration object.</returns>
ILempelZivConfigurationBuilder With(CreateLempelZivEncoderDelegate encoderDelegate);

/// <summary>
/// Sets the factory to create an <see cref="IHuffmanEncoder"/>.
/// </summary>
/// <param name="encoderDelegate">The factory to create an <see cref="IHuffmanEncoder"/>.</param>
/// <returns>The configuration object.</returns>
IHuffmanConfigurationBuilder With(CreateHuffmanEncoderDelegate encoderDelegate);

/// <summary>
/// Sets the factory to create an <see cref="ILempelZivHuffmanEncoder"/>.
/// </summary>
/// <param name="encoderDelegate">The factory to create an <see cref="ILempelZivHuffmanEncoder"/>.</param>
/// <returns>The configuration object.</returns>
ILempelZivHuffmanConfigurationBuilder With(CreateLempelZivHuffmanEncoderDelegate encoderDelegate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Kompression.Contract.Configuration
{
public delegate void ConfigureHuffmanOptions(IHuffmanOptionsBuilder options);

public interface IHuffmanConfigurationBuilder : ICompressionConfigurationBuilder
{
/// <summary>
/// Sets and modifies the configuration for huffman encodings.
/// </summary>
/// <param name="configure">The action to configure huffman encoding operations.</param>
/// <returns>The configuration object.</returns>
ICompressionConfigurationBuilder ConfigureHuffman(ConfigureHuffmanOptions configure);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Kompression.Contract.Configuration
{
public interface IHuffmanEncoderOptionsBuilder
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Kompression.Contract.Encoder.Huffman;

namespace Kompression.Contract.Configuration
{
public delegate IHuffmanTreeBuilder CreateHuffmanTreeBuilder();

public interface IHuffmanOptionsBuilder
{
IHuffmanOptionsBuilder BuildTreeWith(CreateHuffmanTreeBuilder treeDelegate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Kompression.Contract.Configuration
{
public delegate void ConfigureLempelZivOptions(ILempelZivOptionsBuilder options);

public interface ILempelZivConfigurationBuilder : ICompressionConfigurationBuilder
{
/// <summary>
/// Sets and modifies the configuration to search and find pattern matches.
/// </summary>
/// <param name="configure">The action to configure pattern match operations.</param>
/// <returns>The configuration object.</returns>
ICompressionConfigurationBuilder ConfigureLempelZiv(ConfigureLempelZivOptions configure);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Kompression.Contract.Configuration
{
public interface ILempelZivEncoderAdditionalOptionsBuilder : ILempelZivEncoderOptionsBuilder
{
ILempelZivEncoderLimitationsOptionsBuilder AndFindWith(CreateMatchFinderDelegate finderDelegate);
ILempelZivEncoderLimitationsOptionsBuilder AndFindPatternMatches();
ILempelZivEncoderLimitationsOptionsBuilder AndFindRunLength();
ILempelZivEncoderLimitationsOptionsBuilder AndFindConstantRunLength(int value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Kompression.Contract.Configuration
{
public interface ILempelZivEncoderLimitationsOptionsBuilder
{
ILempelZivEncoderAdditionalOptionsBuilder WithinLimitations(int minLength, int maxLength);
ILempelZivEncoderAdditionalOptionsBuilder WithinLimitations(int minLength, int maxLength, int minDisplacement, int maxDisplacement);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Kompression.Contract.DataClasses.Encoder.LempelZiv.MatchFinder;
using Kompression.Contract.Encoder.LempelZiv.MatchFinder;
using Kompression.Contract.Encoder.LempelZiv.MatchParser;
using Kompression.Contract.Encoder.LempelZiv.PriceCalculator;
using Kompression.Contract.Enums.Encoder.LempelZiv;

namespace Kompression.Contract.Configuration
{
public delegate ILempelZivMatchFinder CreateMatchFinderDelegate(LempelZivMatchFinderOptions options);
public delegate ILempelZivPriceCalculator CreatePriceCalculatorDelegate();
public delegate void AdjustInputDelegate(ILempelZivInputAdjustmentOptionsBuilder inputBuilder);

public interface ILempelZivEncoderOptionsBuilder
{
ILempelZivEncoderLimitationsOptionsBuilder FindWith(CreateMatchFinderDelegate finderDelegate);
ILempelZivEncoderLimitationsOptionsBuilder FindPatternMatches();
ILempelZivEncoderLimitationsOptionsBuilder FindRunLength();
ILempelZivEncoderLimitationsOptionsBuilder FindConstantRunLength(int value);

ILempelZivEncoderOptionsBuilder CalculatePricesWith(CreatePriceCalculatorDelegate calculatorDelegate);

ILempelZivEncoderOptionsBuilder SkipUnitsAfterMatch(int skipUnits);
ILempelZivEncoderOptionsBuilder HasUnitSize(UnitSize unitSize);

ILempelZivEncoderOptionsBuilder AdjustInput(AdjustInputDelegate inputDelegate);

ILempelZivMatchParser Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Kompression.Contract.Configuration
{
public interface ILempelZivHuffmanConfigurationBuilder : ICompressionConfigurationBuilder
{
/// <summary>
/// Sets and modifies the configuration to search and find pattern matches.
/// </summary>
/// <param name="configure">The action to configure pattern match operations.</param>
/// <returns>The configuration object.</returns>
IHuffmanConfigurationBuilder ConfigureLempelZiv(ConfigureLempelZivOptions configure);

/// <summary>
/// Sets and modifies the configuration for huffman encodings.
/// </summary>
/// <param name="configure">The action to configure huffman encoding operations.</param>
/// <returns>The configuration object.</returns>
ILempelZivConfigurationBuilder ConfigureHuffman(ConfigureHuffmanOptions configure);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Kompression.Contract.Configuration
{
public interface ILempelZivInputAdjustmentOptionsBuilder
{
ILempelZivInputAdjustmentOptionsBuilder Skip(int skip);
ILempelZivInputAdjustmentOptionsBuilder Reverse();
ILempelZivInputAdjustmentOptionsBuilder Prepend(int byteCount, byte value = 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Kompression.Contract.DataClasses.Encoder.LempelZiv.MatchParser;
using Kompression.Contract.Encoder.LempelZiv.MatchParser;

namespace Kompression.Contract.Configuration
{
public delegate ILempelZivMatchParser CreateMatchParserDelegate(LempelZivMatchParserOptions options);

public interface ILempelZivOptionsBuilder
{
ILempelZivOptionsBuilder WithDegreeOfParallelism(int taskCount);

ILempelZivOptionsBuilder ParseMatchesWith(CreateMatchParserDelegate parserDelegateDelegate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace Kompression.Contract.DataClasses.Encoder.Huffman
{
/// <summary>
/// Represents a node in a huffman tree.
/// </summary>
public class HuffmanTreeNode
{
/// <summary>
/// Gets or sets the value this node represents.
/// </summary>
public int Code { get; set; }

/// <summary>
/// Gets or sets the frequency the value.
/// </summary>
public int Frequency { get; set; }

/// <summary>
/// Gets or sets the two children elements.
/// </summary>
public HuffmanTreeNode[]? Children { get; set; }

/// <summary>
/// Indicates whether this node branches to children or contains a value.
/// </summary>
public bool IsLeaf => Children == null;

/// <summary>
/// Gets the huffman codes for all values under this node.
/// </summary>
/// <returns>The huffman codes for all values under this node.</returns>
public IEnumerable<(int, string)> GetHuffCodes() =>
GetHuffCodes("");

/// <summary>
/// Calculates the depth of the tree from this node onwards.
/// </summary>
/// <returns></returns>
public int GetDepth() => GetDepth(0);

private IEnumerable<(int, string)> GetHuffCodes(string seed) =>
Children?.SelectMany((child, i) => child.GetHuffCodes(seed + i)) ?? new[] { (Code, seed) };

private int GetDepth(int seed)
{
if (IsLeaf || Children == null)
return seed;

var depth1 = Children[0].GetDepth(seed + 1);
var depth2 = Children[1].GetDepth(seed + 1);
return Math.Max(depth1, depth2);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Kompression.Contract.DataClasses.Encoder.LempelZiv
{
public class LempelZivAggregateMatch
{
private readonly (int displacement, int length)[] _matches;

public int MaxLength => _matches.Last().length;

public bool HasMatches => _matches.Any();

public LempelZivAggregateMatch(IList<(int displacement, int length)> matches)
{
_matches = matches.Select(x => (x.displacement, x.length)).ToArray();
}

public LempelZivAggregateMatch(int displacement, int length)
{
_matches = new[] { (displacement, length) };
}

public int GetDisplacement(int length)
{
var minLength = 1;
for (var i = 0; i < _matches.Length; i++)
{
if (length >= minLength && length <= _matches[i].length)
return _matches[i].displacement;

minLength = _matches[i].length;
}

return -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Kompression.Contract.DataClasses.Encoder.LempelZiv
{
/// <summary>
/// The pattern match containing all its information.
/// </summary>
public class LempelZivMatch
{
/// <summary>
/// The position at which the match was found.
/// </summary>
public int Position { get; private set; }

/// <summary>
/// Gets the length the pattern match has.
/// </summary>
public int Length { get; }

/// <summary>
/// Gets the displacement from the position at which the match begins.
/// </summary>
public int Displacement { get; }

/// <summary>
/// Creates a new instance of <see cref="LempelZivMatch"/>.
/// </summary>
/// <param name="position">The position at which the match was found.</param>
/// <param name="displacement">The length the pattern match has.</param>
/// <param name="length">The displacement from the position at which the match begins.</param>
public LempelZivMatch(int position, int displacement, int length)
{
Position = position;
Displacement = displacement;
Length = length;
}

/// <summary>
/// Resets the position to a bew value.
/// </summary>
/// <param name="newPosition">The new position value.</param>
public void SetPosition(int newPosition)
{
Position = newPosition;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Kompression.Contract.Enums.Encoder.LempelZiv;

namespace Kompression.Contract.DataClasses.Encoder.LempelZiv.MatchFinder
{
public class LempelZivMatchFinderOptions
{
public required LempelZivMatchLimitations Limitations { get; init; }
public required UnitSize UnitSize { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Kompression.Contract.DataClasses.Encoder.LempelZiv.MatchFinder
{
public class LempelZivMatchLimitations
{
public required int MinLength { get; init; }
public required int MaxLength { get; init; }
public int MinDisplacement { get; init; }
public int MaxDisplacement { get; init; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Kompression.Contract.Encoder.LempelZiv.InputManipulation;
using Kompression.Contract.Encoder.LempelZiv.MatchFinder;
using Kompression.Contract.Encoder.LempelZiv.PriceCalculator;
using Kompression.Contract.Enums.Encoder.LempelZiv;

namespace Kompression.Contract.DataClasses.Encoder.LempelZiv.MatchParser
{
public class LempelZivMatchParserOptions
{
public required ILempelZivMatchFinder[] MatchFinders { get; init; }
public required ILempelZivPriceCalculator PriceCalculator { get; init; }
public required IInputManipulator InputManipulation { get; init; }
public required UnitSize UnitSize { get; init; }
public required int TaskCount { get; init; }
}
}
Loading

0 comments on commit 0b25e24

Please sign in to comment.