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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Collection.Map;
interface
uses
Comparator,
Enumerator,
Integer._32,
Iterator,
Projector,
Reference.Manager;
type
TMapEntry<TKey, TValue> = class(TObject)
public
Key: TKey;
Value: TValue;
constructor Create(Key: TKey; Value: TValue); virtual;
public
type
TKeyProjector = class(TProjector<TMapEntry<TKey, TValue>, TKey>)
public
function Project(const Entry: TMapEntry<TKey, TValue>): TKey; override;
end;
end;
TMap<TKey, TValue> = class(TObject)
private
FValueManager: TReferenceManager<TValue>;
FKeyManager: TReferenceManager<TKey>;
FKeyComparator: TComparator<TKey>;
procedure SetKeyManager(const Value: TReferenceManager<TKey>);
procedure SetValueManager(const Value: TReferenceManager<TValue>);
procedure SetKeyComparator(const Value: TComparator<TKey>);
protected
function GetCount(): TInteger32; virtual; abstract;
function GetValues(Key: TKey): TValue; virtual; abstract;
procedure SetValues(Key: TKey; const Value: TValue); virtual; abstract;
public
Unassigned: TValue;
constructor Create(); virtual;
destructor Destroy(); override;
procedure Clear(); virtual; abstract;
function Extract(Key: TKey): TValue; virtual; abstract;
procedure Remove(Key: TKey); virtual; abstract;
{$IF not Defined(FPC)}
function CreateIterator(): TIterator<TMapEntry<TKey, TValue>>; virtual; abstract;
function GetEnumerator(): TEnumerator<TMapEntry<TKey, TValue>>; virtual;
{$ENDIF}
property Count: TInteger32 read GetCount;
property Values[Key: TKey]: TValue read GetValues write SetValues; default;
property KeyManager: TReferenceManager<TKey> read FKeyManager write SetKeyManager;
property ValueManager: TReferenceManager<TValue> read FValueManager write SetValueManager;
property KeyComparator: TComparator<TKey> read FKeyComparator write SetKeyComparator;
end;
implementation
uses
Enumerator.Iterator,
Reference.User;
{ TMapEntry<TKey, TValue> }
constructor TMapEntry<TKey, TValue>.Create(Key: TKey; Value: TValue);
begin
inherited Create();
Self.Key := Key;
Self.Value := Value;
end;
{ TMap<TKey, TValue> }
constructor TMap<TKey, TValue>.Create();
begin
inherited Create();
KeyManager := TReferenceUser<TKey>.Create();
ValueManager := TReferenceUser<TValue>.Create();
end;
destructor TMap<TKey, TValue>.Destroy();
begin
KeyManager.Free();
ValueManager.Free();
KeyComparator.ReleaseReference();
inherited Destroy();
end;
function TMap<TKey, TValue>.GetEnumerator(): TEnumerator<TMapEntry<TKey, TValue>>;
begin
Result := TIteratorEnumerator<TMapEntry<TKey, TValue>>.Create(CreateIterator());
end;
procedure TMap<TKey, TValue>.SetKeyComparator(const Value: TComparator<TKey>);
begin
if Value <> KeyComparator then
begin
Value.AddReference();
KeyComparator.ReleaseReference();
FKeyComparator := Value;
end;
end;
procedure TMap<TKey, TValue>.SetKeyManager(const Value: TReferenceManager<TKey>);
begin
if Value <> KeyManager then
begin
KeyManager.Free();
FKeyManager := Value;
end;
end;
procedure TMap<TKey, TValue>.SetValueManager(const Value: TReferenceManager<TValue>);
begin
if Value <> ValueManager then
begin
ValueManager.Free();
FValueManager := Value;
end;
end;
{ TMapEntry<TKey, TValue>.TKeyProjector }
function TMapEntry<TKey, TValue>.TKeyProjector.Project(const Entry: TMapEntry<TKey, TValue>): TKey;
begin
Result := Entry.Key;
end;
end.
|