public static byte[] ToBinary(SudokuGrid grid, SudokuConvertionAlgorithm algorithm) { var sb = new StringBuilder(grid.Metrics.CellsTotal); grid.IterateLinesXY((x, y) => sb.Append(grid.Cells[y, x].Number)); return(ToBinary(sb.ToString(), algorithm, grid.Constraints, grid.Metrics)); }
public static byte[] ToBinary(FileStream textFile, SudokuGrid grid, SudokuConvertionAlgorithm algorithm) { string content; textFile.Position = 0; using (var sr = new StreamReader(textFile)) { content = sr.ReadToEnd(); } textFile.Position = 0; return(ToBinary(content, algorithm, grid.Constraints, grid.Metrics)); }
/// <summary> /// Creates binary file with compressed data. /// </summary> /// <param name="textFile">File with text representation of a grid.</param> /// <param name="outputFilePath">Fully qualified name of output file.</param> /// <param name="alg">Preferable compression algorithm.</param> /// <returns>Seeked to the beginning binary file with read-write acessibilty /// (existing file will be overwritten).</returns> public static FileStream CreateBinaryFromText(FileStream textFile, string outputFilePath, SudokuConvertionAlgorithm alg) { // TODO: ? duplicate functionality -- use ToBinary(FileStream, ...) instead textFile.Position = 0; string content = new StreamReader(textFile).ReadToEnd(); textFile.Position = 0; var restrictions = SudokuGridConstraints.Traditional; var metrics = new SudokuGridMetrics(9, 3, 3); byte[] binary = SudokuConverter.ToBinary(content, alg, restrictions, metrics); var outputFile = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite); outputFile.Write(binary, 0, binary.Length); outputFile.Position = 0; return outputFile; }
/// <summary> /// Creates binary file with compressed data. /// </summary> /// <param name="textFile">File with text representation of a grid.</param> /// <param name="outputFilePath">Fully qualified name of output file.</param> /// <param name="alg">Preferable compression algorithm.</param> /// <returns>Seeked to the beginning binary file with read-write acessibilty /// (existing file will be overwritten).</returns> public static FileStream CreateBinaryFromText(FileStream textFile, string outputFilePath, SudokuConvertionAlgorithm alg) { // TODO: ? duplicate functionality -- use ToBinary(FileStream, ...) instead textFile.Position = 0; string content = new StreamReader(textFile).ReadToEnd(); textFile.Position = 0; var restrictions = SudokuGridConstraints.Traditional; var metrics = new SudokuGridMetrics(9, 3, 3); byte[] binary = SudokuConverter.ToBinary(content, alg, restrictions, metrics); var outputFile = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite); outputFile.Write(binary, 0, binary.Length); outputFile.Position = 0; return(outputFile); }
public static byte[] ToBinary(string content, SudokuConvertionAlgorithm algorithm, SudokuGridConstraints constraints, SudokuGridMetrics metrics) { byte[] compressedContent; byte[] result; switch (algorithm) { case SudokuConvertionAlgorithm.Uniform: compressedContent = TextToBinaryUniform(content, metrics.MaximumNumber); result = new byte[(constraints == SudokuGridConstraints.Traditional) ? HEADER_UNIFORM_LENGTH : HEADER_UNIFORM_RESTRICTED_LENGTH + compressedContent.Length]; //if (constraints == SudokuGridConstraints.Traditional) //{ // result = new byte[HEADER_UNIFORM_LENGTH + compressedContent.Length]; //} //else //{ // result = new byte[HEADER_UNIFORM_RESTRICTED_LENGTH + compressedContent.Length]; //} result[0] = (byte)SudokuConvertionAlgorithm.Uniform; result[1] = (byte)constraints; result[2] = metrics.MaximumNumber; result[3] = metrics.BlockWidth; result[4] = metrics.BlockHeight; if (constraints != SudokuGridConstraints.Traditional) { throw new NotImplementedException(); //var restrictionsPos = (ushort)(); //result[5] = (byte)(); } Array.Copy(compressedContent, 0, result, (constraints == SudokuGridConstraints.Traditional) ? HEADER_UNIFORM_LENGTH : HEADER_UNIFORM_RESTRICTED_LENGTH, compressedContent.Length); return(result); case SudokuConvertionAlgorithm.NonUniform: ushort zerosPairsPos; byte bitsPerPos; byte bitsPerZerosQuantity; compressedContent = TextToBinaryNonUniform(content, metrics.MaximumNumber, out zerosPairsPos, out bitsPerPos, out bitsPerZerosQuantity); result = new byte[(constraints == SudokuGridConstraints.Traditional) ? HEADER_NONUNIFORM_LENGTH : HEADER_NONUNIFORM_RESTRICTED_LENGTH + compressedContent.Length]; result[0] = (byte)SudokuConvertionAlgorithm.NonUniform; result[1] = (byte)(zerosPairsPos >> 8); result[2] = (byte)zerosPairsPos; result[3] = (byte)(((bitsPerPos - 1) << 4) | (bitsPerZerosQuantity - 1)); result[4] = (byte)constraints; result[5] = metrics.MaximumNumber; result[6] = metrics.BlockWidth; result[7] = metrics.BlockHeight; Array.Copy(compressedContent, 0, result, (constraints == SudokuGridConstraints.Traditional) ? HEADER_NONUNIFORM_LENGTH : HEADER_NONUNIFORM_RESTRICTED_LENGTH, compressedContent.Length); //Array.Copy(compressedContent, 0, result, NONLINEAR_HEADER_LENGTH, compressedContent.Length); return(result); default: return(null); } }
public static byte[] ToBinary(FileStream textFile, SudokuGrid grid, SudokuConvertionAlgorithm algorithm) { string content; textFile.Position = 0; using (var sr = new StreamReader(textFile)) { content = sr.ReadToEnd(); } textFile.Position = 0; return ToBinary(content, algorithm, grid.Constraints, grid.Metrics); }
public static byte[] ToBinary(string content, SudokuConvertionAlgorithm algorithm, SudokuGridConstraints constraints, SudokuGridMetrics metrics) { byte[] compressedContent; byte[] result; switch (algorithm) { case SudokuConvertionAlgorithm.Uniform: compressedContent = TextToBinaryUniform(content, metrics.MaximumNumber); result = new byte[(constraints == SudokuGridConstraints.Traditional) ? HEADER_UNIFORM_LENGTH : HEADER_UNIFORM_RESTRICTED_LENGTH + compressedContent.Length]; //if (constraints == SudokuGridConstraints.Traditional) //{ // result = new byte[HEADER_UNIFORM_LENGTH + compressedContent.Length]; //} //else //{ // result = new byte[HEADER_UNIFORM_RESTRICTED_LENGTH + compressedContent.Length]; //} result[0] = (byte)SudokuConvertionAlgorithm.Uniform; result[1] = (byte)constraints; result[2] = metrics.MaximumNumber; result[3] = metrics.BlockWidth; result[4] = metrics.BlockHeight; if (constraints != SudokuGridConstraints.Traditional) { throw new NotImplementedException(); //var restrictionsPos = (ushort)(); //result[5] = (byte)(); } Array.Copy(compressedContent, 0, result, (constraints == SudokuGridConstraints.Traditional) ? HEADER_UNIFORM_LENGTH : HEADER_UNIFORM_RESTRICTED_LENGTH, compressedContent.Length); return result; case SudokuConvertionAlgorithm.NonUniform: ushort zerosPairsPos; byte bitsPerPos; byte bitsPerZerosQuantity; compressedContent = TextToBinaryNonUniform(content, metrics.MaximumNumber, out zerosPairsPos, out bitsPerPos, out bitsPerZerosQuantity); result = new byte[(constraints == SudokuGridConstraints.Traditional) ? HEADER_NONUNIFORM_LENGTH : HEADER_NONUNIFORM_RESTRICTED_LENGTH + compressedContent.Length]; result[0] = (byte)SudokuConvertionAlgorithm.NonUniform; result[1] = (byte)(zerosPairsPos >> 8); result[2] = (byte)zerosPairsPos; result[3] = (byte)(((bitsPerPos - 1) << 4) | (bitsPerZerosQuantity - 1)); result[4] = (byte)constraints; result[5] = metrics.MaximumNumber; result[6] = metrics.BlockWidth; result[7] = metrics.BlockHeight; Array.Copy(compressedContent, 0, result, (constraints == SudokuGridConstraints.Traditional) ? HEADER_NONUNIFORM_LENGTH : HEADER_NONUNIFORM_RESTRICTED_LENGTH, compressedContent.Length); //Array.Copy(compressedContent, 0, result, NONLINEAR_HEADER_LENGTH, compressedContent.Length); return result; default: return null; } }
public static byte[] ToBinary(SudokuGrid grid, SudokuConvertionAlgorithm algorithm) { var sb = new StringBuilder(grid.Metrics.CellsTotal); grid.IterateLinesXY((x, y) => sb.Append(grid.Cells[y, x].Number)); return ToBinary(sb.ToString(), algorithm, grid.Constraints, grid.Metrics); }