自从可以重载除了赋值以外的所有操作符以后,我突然发现原来【将自己写的容器跟foreach语句结合】这个目标自动实现了。这次写了一些东西,先贴个使用的代码。
Collection库里面的所有容器都实现了操作符重载,Set容器更是重载了+、-、*、>、<、>=、<=、==和!=。所有容器都重载了#(用于获取长度的单目前缀操作符)和[](类似于C++的operator[])。
这份代码使用Collection.FreeMap弄了一个可以自动添加类成员的表,可以大大简化一些操作。虽然实际上可能并不是很常用,整个Collection里面,只有FreeMap是用来做广告的。嘿嘿。
1 _run(_global,readfile(apppath++"Collections.free"));
2 using Collections;
3
4 Print=func(m)
5 {
6 internal=multifunc
7 {
8 func({value}name,{value}v,prefix)
9 {
10 writeln(prefix,name,"=",v);
11 }
12 func({value}name,{FreeMap}m,prefix)
13 {
14 writeln(prefix,name," {");
15 for(k in m.__Keys())
16 internal(k,_getvar(m,k),prefix++" ");
17 writeln(prefix,"}");
18 }
19 func(name,unknown,prefix)
20 {
21 writeln(prefix,name,"=<UNKNOWN>");
22 }
23 };
24 internal("a",m,"");
25 };
26
27 a=FreeMap.new();
28 a.Name="IDE Set";
29 a.VC.Language="C++";
30 a.VC.Owner="Microsoft";
31 a.Delphi.Language="Object Pascal";
32 a.Delphi.Owner="Codegear";
33 a.Print=func(this)
34 {
35 return func()
36 {
37 Print(this);
38 };
39 }(a);
40
41 a.Print();
以下是输出的结果:
1 a {
2 Delphi {
3 Language=Object Pascal
4 Owner=Codegear
5 }
6 Name=IDE Set
7 Print=<UNKNOWN>
8 VC {
9 Language=C++
10 Owner=Microsoft
11 }
12 }
最后就是整个Collection库的代码了,一共1463行,全部使用Vczh Free Script 2.0实现。
1 /**********************************************************
2 异常:
3 OutOfRangeExpcetion :越界
4 KeyNotFoundException :键不存在
5 EmptyContainerExpcetion :空容器异常
6 List:列表
7 constructor() :构造空列表
8 constructor({array}Array) :根据数组构造列表
9 Data() :获得内部数组
10 Add(Object) :添加对象
11 AddDistinct(Object) :添加不重复的对象
12 AddArray({array}Array) :添加数组中的对象
13 AddArrayDistinct({array}Array) :添加数组中的对象
14 Insert(Index,Object) :插入对象
15 InsertDistinct(Index,Object) :插入不重复的对象
16 InsertArray(Index,{array}Array) :添加数组中的对象
17 InsertArrayDistinct(Index,{array}Array) :添加数组中的对象
18 Remove(Index) :删除指定位置的对象
19 Remove({array}Indices) :删除指定位置的一些对象
20 Remove(Index,Count) :删除指定部分的对象
21 RemoveIf(Predicate) :删除满足条件的对象
22 RemoveObject(Object) :删除对象
23 RemoveObjectAll(Object) :删除指定对象的等价对象
24 RemoveObjects({array}Array) :删除一些对象
25 RemoveObjectsAll({array}Array) :删除指定的对象数组的等价对象
26 Clear() :删除全部对象
27 IndexOf(Object) :寻找对象的第一个
28 IndicesOf(Object) :寻找对象的所有位置
29 Exists(Object) :检查对象是否存在
30 __getelem__(Index) :根据位置获得对象
31 __setelem__(Index,Value) :根据位置设置对象
32 __len__() :求长度
33 SortedList:有序列表
34 constructor() :构造空列表
35 constructor({array}Array) :根据数组构造列表
36 Data() :获得内部数组
37 Add(Object) :添加对象
38 AddDistinct(Object) :添加不重复的对象
39 AddArray({array}Array) :添加数组中的对象
40 AddArrayDistinct({array}Array) :添加数组中的对象
41 Remove(Index) :删除指定位置的对象
42 Remove({array}Indices) :删除指定位置的一些对象
43 Remove(Index,Count) :删除指定部分的对象
44 RemoveIf(Predicate) :删除满足条件的对象
45 RemoveObject(Object) :删除对象
46 RemoveObjectAll(Object) :删除指定对象的等价对象
47 RemoveObjects({array}Array) :删除一些对象
48 RemoveObjectsAll({array}Array) :删除指定的对象数组的等价对象
49 Clear() :删除全部对象
50 IndexOf(Object) :寻找对象的第一个
51 IndicesOf(Object) :寻找对象的所有位置
52 Exists(Object) :检查对象是否存在
53 __getelem__(Index) :根据位置获得对象
54 __len__() :求长度
55 Stack:堆栈
56 constructor() :构造空堆栈
57 constructor({array}Array) :根据数组构造堆栈
58 Data() :获得内部数组
59 Push(Object) :推入堆栈
60 Pop() :弹出堆栈
61 Top() :返回栈顶
62 Clear() :删除全部对象
63 IndexOf(Object) :寻找对象的第一个
64 IndicesOf(Object) :寻找对象的所有位置
65 Exists(Object) :检查对象是否存在
66 __getelem__(Index) :根据位置获得对象
67 __len__() :求长度
68 Queue:队列
69 constructor() :构造空列队
70 constructor({array}Array) :根据数组构造列队
71 Data() :获得内部数组
72 Enquque(Object) :推入列队
73 Dequque() :弹出列队
74 Top() :返回列队顶
75 Clear() :删除全部对象
76 IndexOf(Object) :寻找对象的第一个
77 IndicesOf(Object) :寻找对象的所有位置
78 Exists(Object) :检查对象是否存在
79 __getelem__(Index) :根据位置获得对象
80 __len__() :求长度
81 Set:集合
82 constructor() :构造空列表
83 constructor({array}Array) :根据数组构造列表
84 Data() :获得内部数组
85 Add(Value) :添加对象
86 AddArray({array}Array) :添加数组中的对象
87 Remove(Value) :删除对象
88 RemoveIf(Predicate) :删除满足条件的对象
89 Clear() :删除全部对象
90 Exists(Value) :检查对象是否存在
91 __len__() :求长度
92 __getelem__(Index) :根据位置获得对象
93 __add__({Set}a,{Set}b) :并
94 __mul__({Set}a,{Set}b) :交
95 __sub__({Set}a,{Set}b) :差
96 __lg__({Set}a,{Set}b) :a包含b
97 __sm__({Set}a,{Set}b) :b包含a
98 __elg__({Set}a,{Set}b) :a包含或等于b
99 __esm__({Set}a,{Set}b) :b包含或等于a
100 __equ__({Set}a,{Set}b) :a与b相等
101 __neq__({Set}a,{Set}b) :a与b不相等
102 ReadonlyList:只读列表
103 constructor({List}List) :构造只读表
104 constructor({SortedList}List) :构造只读表
105 constructor({ReadonlyList}List) :构造只读表
106 constructor({Stack}List) :构造只读表
107 constructor({Queue}List) :构造只读表
108 constructor({Set}List) :构造只读表
109 constructor({array}List) :构造只读表
110 Container() :内部容器
111 Internal() :内部数据,如果使用数组创建ReadonlyList的话则返回一个数组
112 Data() :内部数组
113 IndexOf(Object) :寻找对象的第一个
114 IndicesOf(Object) :寻找对象的所有位置
115 Exists(Object) :检查对象是否存在
116 __getelem__(Index) :根据位置获得对象
117 __len__() :求长度
118 Clone() :构造跟构造时的输入同类型的表的ReadonlyList包装
119 Map(Transformer) :Clone后把列表中的所有东西使用Transformer转换
120 Filter(Predicate) :Clone后保留通过Predicate的对象
121 All(Predicate) :看是否所有对象都能通过Predicate
122 Any(Predicate) :看是否存在能通过Predicate的对象
123 Find(Predicate) :返回所有能够通过Predicate的对象的位置
124 Get(Indices) :Clone后保留Indices中指定的对象
125 FoldLeft(Operand,Operator) :使用Operator对Operand++List进行左结合计算
126 FoldRight(Operand,Operator) :使用Operator对Operand++List进行右结合计算
127 MapPair:键值对
128 Map:表
129 constructor() :构造空表
130 constructor({List}Pairs) :通过MapPair列表构造表
131 Keys() :获得键表
132 Values() :获得值表
133 Pairs() :获得键值表
134 Add(Key,Value) :添加项
135 Remove(Key) :删除项
136 RemoveIf(Predicate) :删除满足条件的对象,使用MapPair进行验证
137 Clear() :删除全部对象
138 Exists(Key) :检查键是否存在
139 __getelem__(Key) :根据键获得值
140 MultiMap:多值表
141 constructor() :构造空表
142 constructor({List}Pairs) :通过MapPair列表构造表
143 Keys() :获得键表
144 Values() :获得值表
145 Pairs() :获得键值表
146 Add(Key,Value) :添加项
147 AddDistinct(Key,Value) :添加不重复项
148 Remove(Key) :删除值的所有项
149 RemovePair(Key,Value) :删除指定项
150 RemovePairAll(Key,Value) :删除与指定项等价的所有项
151 RemoveIf(Predicate) :删除满足条件的对象,使用MapPair进行验证
152 Clear() :删除全部对象
153 Exists(Key) :检查键是否存在
154 Exists(Key,Value) :检查键值是否存在
155 __getelem__(Key) :根据键获得值
156 FreeMap:自由对象,使用“.”而不是“[]”来获取表的对象
157 constructor() :构造空表
158 __get__(Name) :获取对象
159 __set__(Name,Value) :设置对象
160 __Keys() :获取对象名表
161 __Exists(Name) :检查对象是否存在
162 __Remove(Name) :删除对象
163 其他 :直接访问对象
164 **********************************************************/
165 Collections=namespace
166 {
167 fixed OutOfRangeException=class()
168 {
169 local Message="";
170 local constructor=func({value}m)
171 {
172 Message=m;
173 };
174 };
175 fixed KeyNotFoundException=class()
176 {
177 local Message="";
178 local constructor=func({value}m)
179 {
180 Message=m;
181 };
182 };
183 fixed EmptyContainerException=class()
184 {
185 local Message="";
186 local constructor=func({value}m)
187 {
188 Message=m;
189 };
190 };
191
192 /**********************************************************
193 List
194 **********************************************************/
195
196 fixed List=class()
197 {
198 local Data=null;
199 local Add=null;
200 local AddDistinct=null;
201 local AddArray=null;
202 local AddArrayDistinct=null;
203 local Insert=null;
204 local InsertDistinct=null;
205 local InsertArray=null;
206 local InsertArrayDistinct=null;
207 local Remove=null;
208 local RemoveIf=null;
209 local RemoveObject=null;
210 local RemoveObjectAll=null;
211 local RemoveObjects=null;
212 local RemoveObjectsAll=null;
213 local Clear=null;
214 local IndexOf=null;
215 local IndicesOf=null;
216 local Exists=null;
217 local __len__=null;
218 local __getelem__=null;
219 local __setelem__=null;
220
221 local constructor=func()
222 {
223 local Items=[];
224
225 Data=func()
226 {
227 return Items;
228 };
229
230 Add=func(Object)
231 {
232 Items[#Items:0]=[Object];
233 return this;
234 };
235
236 AddDistinct=func(Object)
237 {
238 if(!Exists(Object))
239 Add(Object);
240 return this;
241 };
242
243 AddArray=func({array}Array)
244 {
245 Items[#Items:0]=Array;
246 return this;
247 };
248
249 AddArrayDistinct=func({array}Array)
250 {
251 for(i in Array)
252 AddDistinct(i);
253 return this;
254 };
255
256 Insert=func({int}Index,Value)
257 {
258 if(Index<0 || Index>#Items)
259 throw(OutOfRangeException.new("下标越界,长度:"++#Items++",下标:"++Index++"。"));
260 Items[Index:0]=[Value];
261 return this;
262 };
263
264 InsertDistinct=func({int}Index,Value)
265 {
266 if(Index<0 || Index>#Items)
267 throw(OutOfRangeException.new("下标越界,长度:"++#Items++",下标:"++Index++"。"));
268 if(!Exists(Value))
269 Items[Index:0]=[Value];
270 return this;
271 };
272
273 InsertArray=func({int}Index,{array}Value)
274 {
275 if(Index<0 || Index>#Items)
276 throw(OutOfRangeException.new("下标越界,长度:"++#Items++",下标:"++Index++"。"));
277 Items[Index:0]=Value;
278 return this;
279 };
280
281 InsertArrayDistinct=func({int}Index,{array}Value)
282 {
283 if(Index<0 || Index>#Items)
284 throw(OutOfRangeException.new("下标越界,长度:"++#Items++",下标:"++Index++"。"));
285 for(i in Value)
286 if(!Exists(i))
287 {
288 Items[Index:0]=[i];
289 Index=Index+1;
290 }
291 return this;
292 };
293
294 Remove=multifunc
295 {
296 func({int}Index)
297 {
298 if(Index<0 || Index>=#Items)
299 throw(OutOfRangeException.new("下标越界,长度:"++#Items++",下标:"++Index++"。"));
300 Items[Index:1]=[];
301 return this;
302 }
303 func({array}Indices)
304 {
305 local Sorted=Indices[0:#Indices];
306 for(i in 0 to #Sorted-2)
307 for(j in #Sorted-2 downto i)
308 if(Sorted[j]<Sorted[j+1])
309 {
310 local t=Sorted[j];
311 Sorted[j]=Sorted[j+1];
312 Sorted[j+1]=t;
313 }
314 local Last=-1;
315 for(i in Sorted)
316 if(Last!=i)
317 {
318 Last=i;
319 Remove(i);
320 }
321 return this;
322 }
323 func({int}Index,{int}Count)
324 {
325 if(Index<0 || Index>#Items)
326 throw(OutOfRangeException.new("范围越界,长度:"++#Items++",位置:"++Index++",长度:"++Count++"。"));
327 if(Index+Count<0 || Index+Count>#Items)
328 throw(OutOfRangeException.new("范围越界,长度:"++#Items++",位置:"++Index++",长度:"++Count++"。"));
329 Items[Index:Count]=[];
330 return this;
331 }
332 };
333
334 RemoveIf=func(Predicate)
335 {
336 local Indices=[];
337 for(i in 0 to #Items-1)
338 if(Predicate(Items[i]))
339 Indices[#Indices:0]=[i];
340 return Remove(Indices);
341 };
342
343 RemoveObject=func(Object)
344 {
345 local Index=IndexOf(Object);
346 if(Index>=0)
347 Remove(Index);
348 return this;
349 };
350
351 RemoveObjectAll=func(Object)
352 {
353 Remove(IndicesOf(Object));
354 };
355
356 RemoveObjects=func({array}Array)
357 {
358 for(i in Array)
359 RemoveObject(i);
360 return this;
361 };
362
363 RemoveObjectsAll=func({array}Array)
364 {
365 for(i in Array)
366 RemoveObjectAll(i);
367 return this;
368 };
369
370 Clear=func()
371 {
372 Items=[];
373 return this;
374 };
375
376 IndexOf=func(Object)
377 {
378 for(i in 0 to #Items-1)
379 if(Items[i]==Object)
380 return i;
381 return -1;
382 };
383
384 IndicesOf=func(Object)
385 {
386 local Indices=[];
387 for(i in 0 to #Items-1)
388 if(Items[i]==Object)
389 Indices[#Indices:0]=[i];
390 return Indices;
391 };
392
393 Exists=func(Object)
394 {
395 return IndexOf(Object)>=0;
396 };
397
398 __len__=func(me)
399 {
400 return #Items;
401 };
402
403 __getelem__=func({int}Index)
404 {
405 if(Index<0 || Index>=#Items)
406 throw(OutOfRangeException.new("下标越界,长度:"++#Items++",下标:"++Index++"。"));
407 return Items[Index];
408 };
409
410 __setelem__=func({int}Index,Value)
411 {
412 if(Index<0 || Index>=#Items)
413 throw(OutOfRangeException.new("下标越界,长度:"++#Items++",下标:"++Index++"。"));
414