Написал реализацию списка на базе динамического массива:
Pascal |
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
| type
TDynamicArray<TValue> = class
private
_A: array of TValue;
_Count, _Capacity: int64;
procedure TryRaiseIndexOutOfRangeException(i: int64);
begin
if (i >= _Count) or (i < 0) then
raise new System.IndexOutOfRangeException();
end;
procedure TryRaiseZeroCountException();
begin
if _Count = 0 then
raise new Exception('Список пуст.');
end;
function GetItem(i: int64): TValue;
begin
TryRaiseIndexOutOfRangeException(i);
Result := _A[i];
end;
procedure SetItem(i: int64; v: TValue);
begin
TryRaiseIndexOutOfRangeException(i);
_A[i] := v;
end;
procedure TryResize();
begin
Inc(_Count);
if _Count > _Capacity then
begin
_Capacity *= 2;
SetLength(_A, _Capacity);
end;
end;
public
property Items[i: int64]: TValue read GetItem write SetItem;default;
property Count: int64 read _Count;
property Capacity: int64 read _Capacity;
constructor();
begin
_Capacity := 2;
SetLength(_A, _Capacity);
end;
procedure AddLast(v: TValue);
begin
TryResize();
_A[Pred(_Count)] := v;
end;
procedure RemoveLast();
begin
TryRaiseZeroCountException();
Dec(_Count);
end;
procedure AddFirst(v: TValue);
begin
TryResize();
for var i := Pred(_Count) downto 1 do
_A[i] := _A[Pred(i)];
_A[0] := v;
end;
procedure RemoveFirst();
begin
TryRaiseZeroCountException();
for var i := 0 to _Count - 2 do
_A[i] := _A[Succ(i)];
end;
function Seach(v1: TValue): int64;
begin
Result := 0;
while (Result < Pred(_Count)) and (_A[Result] <> v1) do Inc(Result);
TryRaiseIndexOutOfRangeException(Result);
end;
procedure Insert(v1, v2: TValue);
begin
TryResize();
var j := Seach(v1);
for var i := Pred(_Count) downto Succ(j) do
_A[i] := _A[Pred(i)];
_A[j] := v2;
end;
procedure Remove(v1: TValue);
begin
TryRaiseZeroCountException();
var j := Seach(v1);
for var i := j to _Count - 2 do
_A[i] := _A[Succ(i)];
Dec(_Count);
end;
procedure Shuffle();
begin
for var i := 0 to _Count - 2 do
Swap(_A[i], _A[Random(Succ(i), Pred(_Count))]);
end;
procedure Println();
begin
for var i := 0 to Pred(_Count) do
Write(_A[i].ToString():4);
Writeln();
end;
function Copy(): TDynamicArray<TValue>;
begin
Result := new TDynamicArray<TValue>();
for var i := 0 to Pred(_Count) do
Result.AddLast(_A[i]);
end;
procedure Append(a: TDynamicArray<TValue>);
begin
for var i := 0 to Pred(a.Count) do
AddLast(a[i]);
end;
class function operator*(a: TDynamicArray<TValue>; c: integer): TDynamicArray<TValue>;
begin
Result := new TDynamicArray<TValue>();
for var i := 1 to c do
Result.Append(a);
end;
class function operator+(a: TDynamicArray<TValue>; v1: TValue): TDynamicArray<TValue>;
begin
Result := a.Copy();
Result.AddLast(v1);
end;
class function operator+(v1: TValue; a: TDynamicArray<TValue>): TDynamicArray<TValue>;
begin
Result := a.Copy();
Result.AddFirst(v1);
end;
class function operator+(a, b: TDynamicArray<TValue>): TDynamicArray<TValue>;
begin
Result := a.Copy();
Result.Append(b);
end;
class function operator-(a: TDynamicArray<TValue>; v1: TValue): TDynamicArray<TValue>;
begin
Result := a.Copy();
Result.Remove(v1);
end;
class function operator-(a, b: TDynamicArray<TValue>): TDynamicArray<TValue>;
begin
Result := a.Copy();
for var i := 0 to Pred(b.Count) do
Result.Remove(b[i]);
end;
end;
begin
var L := new TDynamicArray<integer>();
for var i := 0 to 2 do
L := i + L;
L.Println();
L.Shuffle();
L.Println();
(4 + L * 2 + 5).Println();
Writeln(L.Capacity);
end. |
|
Насколько она нормальная? И да, нужно ли выбрасывать исключение, если не нашлось нужного элемента в Seach?