This repository has been archived by the owner on Jan 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.cs
73 lines (67 loc) · 2.29 KB
/
Editor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2024 RFull Development
// This source code is managed under the MIT license. See LICENSE in the project root.
using Microsoft.VisualBasic.FileIO;
namespace MatrixRotator
{
public class Editor(string delimiter = ",", string? newline = null)
{
public string[,] Data { get; private set; } = { };
private readonly string _delimiter = delimiter;
private readonly string _newline = newline ?? Environment.NewLine;
public void Load(StreamReader reader)
{
using TextFieldParser parser = new(reader)
{
TextFieldType = FieldType.Delimited
};
parser.SetDelimiters(_delimiter);
List<string[]> list = [];
while (!parser.EndOfData)
{
string[]? fields = parser.ReadFields();
if (fields == null)
{
continue;
}
list.Add(fields);
}
int rowCount = list.Count;
int columnCount = list[0].Length;
string[,] newData = new string[rowCount, columnCount];
Parallel.For(0, rowCount, i =>
{
Parallel.For(0, columnCount, j =>
{
newData[i, j] = list[i][j];
});
});
Data = newData;
}
public void Save(StreamWriter writer)
{
int rowCount = Data.GetLength(0);
int columnCount = Data.GetLength(1);
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
var row = Enumerable.Range(0, columnCount)
.Select(columnIndex => Data[rowIndex, columnIndex]);
string line = string.Join(_delimiter, row);
writer.WriteLine(line);
}
}
public void Rotate()
{
int rowCount = Data.GetLength(0);
int columnCount = Data.GetLength(1);
string[,] newData = new string[columnCount, rowCount];
Parallel.For(0, rowCount, i =>
{
Parallel.For(0, columnCount, j =>
{
newData[j, i] = Data[i, j];
});
});
Data = newData;
}
}
}