Skip to content

Commit 231167f

Browse files
authored
Switch to file-scoped namespaces (#429)
1 parent 821f492 commit 231167f

22 files changed

+1588
-1610
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,73 @@
1-
using System;
1+
using System;
22
using System.Linq;
33

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
513
{
614
/// <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.
1117
/// </summary>
12-
public class BurrowsWheelerTransform
18+
/// <param name="s">Input string.</param>
19+
public (string encoded, int index) Encode(string s)
1320
{
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)
2022
{
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);
3324
}
3425

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)
4143
{
42-
if (s.Length == 0)
43-
{
44-
return string.Empty;
45-
}
44+
return string.Empty;
45+
}
4646

47-
var rotations = new string[s.Length];
47+
var rotations = new string[s.Length];
4848

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++)
5052
{
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];
5754
}
5855

59-
return rotations[index];
56+
Array.Sort(rotations, StringComparer.Ordinal);
6057
}
6158

62-
private string[] GetRotations(string s)
63-
{
64-
var result = new string[s.Length];
59+
return rotations[index];
60+
}
6561

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];
7065

71-
return result;
66+
for (var i = 0; i < s.Length; i++)
67+
{
68+
result[i] = s.Substring(i) + s.Substring(0, i);
7269
}
70+
71+
return result;
7372
}
7473
}

0 commit comments

Comments
 (0)