|
1 |
| -using System; |
| 1 | +using System; |
2 | 2 | using System.Linq;
|
3 | 3 |
|
4 |
| -namespace Algorithms.DataCompression |
| 4 | +namespace Algorithms.DataCompression; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// The Burrows–Wheeler transform (BWT) rearranges a character string into runs of similar characters. |
| 8 | +/// This is useful for compression, since it tends to be easy to compress a string that has runs of repeated |
| 9 | +/// characters. |
| 10 | +/// See <a href="https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform">here</a> for more info. |
| 11 | +/// </summary> |
| 12 | +public class BurrowsWheelerTransform |
5 | 13 | {
|
6 | 14 | /// <summary>
|
7 |
| - /// The Burrows–Wheeler transform (BWT) rearranges a character string into runs of similar characters. |
8 |
| - /// This is useful for compression, since it tends to be easy to compress a string that has runs of repeated |
9 |
| - /// characters. |
10 |
| - /// See <a href="https://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform">here</a> for more info. |
| 15 | + /// Encodes the input string using BWT and returns encoded string and the index of original string in the sorted |
| 16 | + /// rotation matrix. |
11 | 17 | /// </summary>
|
12 |
| - public class BurrowsWheelerTransform |
| 18 | + /// <param name="s">Input string.</param> |
| 19 | + public (string encoded, int index) Encode(string s) |
13 | 20 | {
|
14 |
| - /// <summary> |
15 |
| - /// Encodes the input string using BWT and returns encoded string and the index of original string in the sorted |
16 |
| - /// rotation matrix. |
17 |
| - /// </summary> |
18 |
| - /// <param name="s">Input string.</param> |
19 |
| - public (string encoded, int index) Encode(string s) |
| 21 | + if (s.Length == 0) |
20 | 22 | {
|
21 |
| - if (s.Length == 0) |
22 |
| - { |
23 |
| - return (string.Empty, 0); |
24 |
| - } |
25 |
| - |
26 |
| - var rotations = GetRotations(s); |
27 |
| - Array.Sort(rotations, StringComparer.Ordinal); |
28 |
| - var lastColumn = rotations |
29 |
| - .Select(x => x[^1]) |
30 |
| - .ToArray(); |
31 |
| - var encoded = new string(lastColumn); |
32 |
| - return (encoded, Array.IndexOf(rotations, s)); |
| 23 | + return (string.Empty, 0); |
33 | 24 | }
|
34 | 25 |
|
35 |
| - /// <summary> |
36 |
| - /// Decodes the input string and returns original string. |
37 |
| - /// </summary> |
38 |
| - /// <param name="s">Encoded string.</param> |
39 |
| - /// <param name="index">Index of original string in the sorted rotation matrix.</param> |
40 |
| - public string Decode(string s, int index) |
| 26 | + var rotations = GetRotations(s); |
| 27 | + Array.Sort(rotations, StringComparer.Ordinal); |
| 28 | + var lastColumn = rotations |
| 29 | + .Select(x => x[^1]) |
| 30 | + .ToArray(); |
| 31 | + var encoded = new string(lastColumn); |
| 32 | + return (encoded, Array.IndexOf(rotations, s)); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Decodes the input string and returns original string. |
| 37 | + /// </summary> |
| 38 | + /// <param name="s">Encoded string.</param> |
| 39 | + /// <param name="index">Index of original string in the sorted rotation matrix.</param> |
| 40 | + public string Decode(string s, int index) |
| 41 | + { |
| 42 | + if (s.Length == 0) |
41 | 43 | {
|
42 |
| - if (s.Length == 0) |
43 |
| - { |
44 |
| - return string.Empty; |
45 |
| - } |
| 44 | + return string.Empty; |
| 45 | + } |
46 | 46 |
|
47 |
| - var rotations = new string[s.Length]; |
| 47 | + var rotations = new string[s.Length]; |
48 | 48 |
|
49 |
| - for (var i = 0; i < s.Length; i++) |
| 49 | + for (var i = 0; i < s.Length; i++) |
| 50 | + { |
| 51 | + for (var j = 0; j < s.Length; j++) |
50 | 52 | {
|
51 |
| - for (var j = 0; j < s.Length; j++) |
52 |
| - { |
53 |
| - rotations[j] = s[j] + rotations[j]; |
54 |
| - } |
55 |
| - |
56 |
| - Array.Sort(rotations, StringComparer.Ordinal); |
| 53 | + rotations[j] = s[j] + rotations[j]; |
57 | 54 | }
|
58 | 55 |
|
59 |
| - return rotations[index]; |
| 56 | + Array.Sort(rotations, StringComparer.Ordinal); |
60 | 57 | }
|
61 | 58 |
|
62 |
| - private string[] GetRotations(string s) |
63 |
| - { |
64 |
| - var result = new string[s.Length]; |
| 59 | + return rotations[index]; |
| 60 | + } |
65 | 61 |
|
66 |
| - for (var i = 0; i < s.Length; i++) |
67 |
| - { |
68 |
| - result[i] = s.Substring(i) + s.Substring(0, i); |
69 |
| - } |
| 62 | + private string[] GetRotations(string s) |
| 63 | + { |
| 64 | + var result = new string[s.Length]; |
70 | 65 |
|
71 |
| - return result; |
| 66 | + for (var i = 0; i < s.Length; i++) |
| 67 | + { |
| 68 | + result[i] = s.Substring(i) + s.Substring(0, i); |
72 | 69 | }
|
| 70 | + |
| 71 | + return result; |
73 | 72 | }
|
74 | 73 | }
|
0 commit comments