-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cs
More file actions
133 lines (119 loc) · 3.96 KB
/
Copy pathTable.cs
File metadata and controls
133 lines (119 loc) · 3.96 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Linq;
using ArgumentException = System.ArgumentException;
namespace Generics.Tables
{
public class Cell<T1, T2, T3>
{
public T1 Row;
public T2 Column;
public T3 Value;
public Cell(T1 row)
{
Row = row;
}
public Cell(T2 column)
{
Column = column;
}
public Cell(T1 row, T2 column, T3 value)
{
Row = row;
Column = column;
Value = value;
}
}
public class Indexer<T1, T2, T3>
{
public Indexer(Table<T1, T2, T3> table)
{
_table = table;
}
private readonly Table<T1, T2, T3> _table;
public T3 this[T1 x, T2 y]
{
get
{
switch (this)
{
case OpenTableIndexer<T1, T2, T3> _:
return _table.Cells.FirstOrDefault(c =>
c.Row != null && c.Row.Equals(x) && c.Column != null && c.Column.Equals(y)) == null
? default
: _table.Cells.First(c => c.Row.Equals(x) && c.Column.Equals(y)).Value;
case ExistedTableIndexer<T1, T2, T3> _:
return _table.Cells.FirstOrDefault(c =>
c.Row != null && c.Row.Equals(x) && c.Column != null && c.Column.Equals(y)) == null
? throw new ArgumentException()
: _table.Cells.First(c => c.Row.Equals(x) && c.Column.Equals(y)).Value;
}
throw new ArgumentNullException();
}
set
{
var cell = _table.Cells.FirstOrDefault(c =>
c.Row != null && c.Row.Equals(x) && c.Column != null && c.Column.Equals(y));
if (cell == null)
{
switch (this)
{
case OpenTableIndexer<T1, T2, T3> _:
_table.Cells.Add(new Cell<T1, T2, T3>(x, y, value));
_table.Rows.Add(x);
_table.Columns.Add(y);
break;
case ExistedTableIndexer<T1, T2, T3> _:
throw new ArgumentException();
}
}
else cell.Value = value;
}
}
}
public class OpenTableIndexer<T1, T2, T3> : Indexer<T1, T2, T3>
{
public OpenTableIndexer(Table<T1, T2, T3> table) : base(table)
{
}
}
public class ExistedTableIndexer<T1, T2, T3> : Indexer<T1, T2, T3>
{
public ExistedTableIndexer(Table<T1, T2, T3> table) : base(table)
{
}
}
public class Table<T1, T2, T3>
{
public Table()
{
Open = new OpenTableIndexer<T1, T2, T3>(this);
Existed = new ExistedTableIndexer<T1, T2, T3>(this);
}
public HashSet<T1> Rows { get; } = new HashSet<T1>();
public HashSet<T2> Columns { get; } = new HashSet<T2>();
public HashSet<Cell<T1, T2, T3>> Cells { get; } = new HashSet<Cell<T1, T2, T3>>();
public Indexer<T1, T2, T3> Open { get; }
public Indexer<T1, T2, T3> Existed { get; }
public void AddRow(T1 row)
{
Rows.Add(row);
if (Cells.Count != 0 && !Cells.Last().Column.Equals(default(T2)))
{
Cells.Last().Row = row;
return;
}
Cells.Add(new Cell<T1, T2, T3>(row));
}
public void AddColumn(T2 column)
{
Columns.Add(column);
if (Cells.Count != 0 && !Cells.Last().Row.Equals(default(T1)))
{
Cells.Last().Column = column;
return;
}
Cells.Add(new Cell<T1, T2, T3>(column));
}
}
}