-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.cs
276 lines (257 loc) · 7.69 KB
/
Storage.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System;
using System.IO;
using System.Linq;
using System.Runtime.Remoting;
namespace oop_7 {
public class Storage<T> where T : Shape {
public class Node {
public bool isDistinguishVertex = false;
public Node next;
public Node Next {
get { return next; }
set { next = value; }
}
public Node prev;
public Node Prev {
get { return prev; }
set { prev = value; }
}
public T data;
public T Data {
get { return data; }
set { data = value; }
}
public Node() {
next = prev = null;
}
public Node(T tdata) {
data = tdata;
next = null;
}
}
private int capacity;
private Node head;
private Node current;
private Node iterator;
bool iFlag;
public int GetCapacity() {
return capacity;
}
public Node Current {
get {
return current;
}
set {
current = value;
}
}
public void IFirst() {
iterator = head;
iFlag = false;
}
public Node IObject {
get {
return iterator;
}
set {
iterator = value;
}
}
public void INext() {
iFlag = true;
iterator = iterator.next;
}
public bool IIsEOL() {
return iterator == head && iFlag;
}
public Node GetDistinguished() {
if (capacity == 0)
return null;
Node tnode = head;
int count = 0;
bool flag = head.isDistinguishVertex;
while (!tnode.isDistinguishVertex && count != (capacity + 1)) {
tnode = tnode.next;
count++;
if (tnode.isDistinguishVertex) {
flag = true;
break;
}
}
if (flag)
return tnode;
else
return null;
}
public void DeleteDistinguished() {
bool hasDistinguished = false;
Node tempNode = head;
for (int i = 0; i < capacity; i++) {
if (tempNode.isDistinguishVertex) {
hasDistinguished = true;
break;
}
tempNode = tempNode.next;
}
if (!hasDistinguished) {
DeleteCurrent();
}
if (capacity == 0)
return;
tempNode = head.next;
Node newCurrent = current;
int count = 0;
while (newCurrent.isDistinguishVertex && (count++) < (capacity + 1))
newCurrent = newCurrent.prev;
while ((tempNode = GetDistinguished()) != null) {
current = tempNode;
DeleteCurrent();
}
current = newCurrent;
}
public Storage() {
current = head = null;
}
public void DeleteCurrent() {
if (capacity == 1)
{
capacity = 0;
head = current = null;
}
if (capacity > 0)
{
capacity--;
Node tnode = current.prev;
if (current == head)
head = tnode;
current.prev.next = current.next;
current.next.prev = current.prev;
current = tnode;
}
}
public void Clear() {
Node t1 = head;
Node t2;
for (int i = 0; i < capacity; i++) {
t2 = t1.next;
t1.next = null;
t1.prev = null;
t1 = t2;
}
head = current = null;
capacity = 0;
}
public void goFront() {
if (capacity > 0)
current = current.next;
}
public void goBack() {
if (capacity > 0)
current = current.prev;
}
public void goFirst() {
current = head;
}
public void goTail() {
if (head != null)
current = head.prev;
}
public Node Tail {
get {
if (capacity == 0)
return null;
return head.prev;
}
}
public void Add(T tdata) {
Node tnode = new Node(tdata);
capacity++;
if (head == null) {
head = current = tnode;
head.next = head;
head.prev = head;
}
else {
tnode.prev = head.prev;
tnode.next = head;
head.prev.next = tnode;
head.prev = tnode;
}
}
public void PrintAll()
{
Node tnode = head;
for (int i = 0; i < capacity; i++)
{
Console.Write(String.Format("{0,2:0}", 1 + i));
if (tnode == current)
Console.Write(" >");
else
Console.Write(" ");
tnode.Data.Print();
tnode = tnode.next;
}
if (capacity == 0)
Console.WriteLine("Empty");
Console.WriteLine("Capacity: " + capacity);
}
public Node GetTail()
{
return head.prev;
}
public void Save(string filename)
{
StreamWriter writer = new StreamWriter(filename);
Node tnode = head;
for (int i = 0; i < capacity; i++)
{
writer.Write(tnode.data.GetData() + "," + tnode.isDistinguishVertex + "," + (tnode == current));
writer.WriteLine();
tnode = tnode.next;
}
writer.Close();
}
public bool Load(string filename, string namesp)
{
StreamReader s;
try
{
s = new StreamReader(filename);
}
catch (FileNotFoundException)
{
Console.WriteLine("Ôàéë íå íàéäåí");
return false;
}
using (s)
{
while (!s.EndOfStream)
{
var arrStr = s.ReadLine().Split(',');
var ttype = Type.GetType(namesp + "." + arrStr[0], false, true);
var types = new object[arrStr.Length - 3];
for (int l = 0; l < types.Length; l++)
types[l] = int.Parse(arrStr[l + 1]);
object obj = new Shape();
var constructors = ttype.GetConstructors();
foreach (var t in constructors)
{
try
{
obj = t.Invoke(types);
}
catch (Exception)
{
//continue;
}
}
Add((T)obj);
GetTail().isDistinguishVertex = bool.Parse(arrStr[arrStr.Length - 2]);
if (bool.Parse(arrStr[arrStr.Length - 1]))
current = GetTail();
}
}
return true;
}
}
}