-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathHashSet.pas
More file actions
168 lines (140 loc) · 3.96 KB
/
Copy pathHashSet.pas
File metadata and controls
168 lines (140 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
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
namespace Sugar.Collections;
interface
type
HashSet<T> = public class mapped to {$IF COOPER}java.util.HashSet<T>{$ELSEIF ECHOES}System.Collections.Generic.HashSet<T>{$ELSEIF TOFFEE}Foundation.NSMutableSet{$ENDIF}
public
constructor; mapped to constructor();
constructor(&Set: HashSet<T>);
method &Add(Item: T): Boolean;
method Clear; mapped to {$IF COOPER OR ECHOES}Clear{$ELSE}removeAllObjects{$ENDIF};
method Contains(Item: T): Boolean;
method &Remove(Item: T): Boolean;
method ForEach(Action: Action<T>);
method ToArray(); mapped to {$IF COOPER OR ECHOES}ToArray{$ELSE}objectEnumerator().allObjects{$ENDIF};
method Intersect(&Set: HashSet<T>);
method &Union(&Set: HashSet<T>);
method IsSubsetOf(&Set: HashSet<T>): Boolean;
method IsSupersetOf(&Set: HashSet<T>): Boolean;
method SetEquals(&Set: HashSet<T>): Boolean;
property Count: Integer read {$IF ECHOES OR TOFFEE}mapped.Count{$ELSE}mapped.size{$ENDIF};
{$IF TOFFEE}
operator Implicit(aSet: NSSet<T>): HashSet<T>;
{$ENDIF}
end;
HashsetHelpers = public class
private
public
class method Foreach<T>(aSelf: HashSet<T>; aAction: Action<T>);
class method IsSubsetOf<T>(aSelf, aSet: HashSet<T>): Boolean;
end;
implementation
{ HashSet }
constructor HashSet<T>(&Set: HashSet<T>);
begin
{$IF COOPER}
exit new java.util.HashSet<T>(&Set);
{$ELSEIF ECHOES}
exit new System.Collections.Generic.HashSet<T>(&Set);
{$ELSEIF TOFFEE}
var NewSet := new Foundation.NSMutableSet();
NewSet.setSet(&Set);
exit NewSet;
{$ENDIF}
end;
method HashSet<T>.&Add(Item: T): Boolean;
begin
{$IF COOPER OR ECHOES}
exit mapped.Add(Item);
{$ELSEIF TOFFEE}
var lSize := mapped.count;
mapped.addObject(NullHelper.ValueOf(Item));
exit lSize < mapped.count;
{$ENDIF}
end;
method HashSet<T>.Contains(Item: T): Boolean;
begin
{$IF COOPER OR ECHOES}
exit mapped.Contains(Item);
{$ELSEIF TOFFEE}
exit mapped.containsObject(NullHelper.ValueOf(Item));
{$ENDIF}
end;
method HashSet<T>.&Remove(Item: T): Boolean;
begin
{$IF COOPER OR ECHOES}
exit mapped.Remove(Item);
{$ELSEIF TOFFEE}
var lSize := mapped.count;
mapped.removeObject(NullHelper.ValueOf(Item));
exit lSize > mapped.count;
{$ENDIF}
end;
method HashSet<T>.ForEach(Action: Action<T>);
begin
HashsetHelpers.Foreach(self, Action);
end;
method HashSet<T>.Intersect(&Set: HashSet<T>);
begin
{$IF COOPER}
mapped.retainAll(&Set);
{$ELSEIF ECHOES}
mapped.IntersectWith(&Set);
{$ELSEIF TOFFEE}
mapped.intersectSet(&Set);
{$ENDIF}
end;
method HashSet<T>.&Union(&Set: HashSet<T>);
begin
{$IF COOPER}
mapped.addAll(&Set);
{$ELSEIF ECHOES}
mapped.UnionWith(&Set);
{$ELSEIF TOFFEE}
mapped.unionSet(&Set);
{$ENDIF}
end;
method HashSet<T>.SetEquals(&Set: HashSet<T>): Boolean;
begin
{$IF COOPER}
exit mapped.equals(&Set);
{$ELSEIF ECHOES}
exit mapped.SetEquals(&Set);
{$ELSEIF TOFFEE}
exit mapped.isEqualToSet(&Set);
{$ENDIF}
end;
method HashSet<T>.IsSupersetOf(&Set: HashSet<T>): Boolean;
begin
exit HashsetHelpers.IsSubsetOf(&Set, self);
end;
method HashSet<T>.IsSubsetOf(&Set: HashSet<T>): Boolean;
begin
exit HashsetHelpers.IsSubsetOf(self, &Set);
end;
{$IF TOFFEE}
operator HashSet<T>.Implicit(aSet: NSSet<T>): HashSet<T>;
begin
if aSet is NSMutableArray then
result := HashSet<T>(aSet)
else
result := HashSet<T>(aSet:mutableCopy);
end;
{$ENDIF}
{ HashsetHelpers }
class method HashsetHelpers.Foreach<T>(aSelf: HashSet<T>; aAction: Action<T>);
begin
for each el in aSelf do
aAction(el);
end;
class method HashsetHelpers.IsSubsetOf<T>(aSelf, aSet: HashSet<T>): Boolean;
begin
if aSelf.Count = 0 then
exit true;
if aSelf.Count > aSet.Count then
exit false;
for each el in aSelf do
if not aSet.Contains(el) then
exit false;
exit true;
end;
end.