随笔-341  评论-2670  文章-0  trackbacks-0
    自从可以重载除了赋值以外的所有操作符以后,我突然发现原来【将自己写的容器跟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                 Items[Index]=Value;
 415             };
 416 
 417             return multifunc
 418             {
 419                 func();
 420                 func({array}Array)
 421                 {
 422                     Items=Array[0:#Array];
 423                 }
 424             };
 425         }();
 426     };
 427 
 428 /**********************************************************
 429 SortedList
 430 **********************************************************/
 431 
 432     fixed SortedList=class()
 433     {
 434         local Data=null;
 435         local Add=null;
 436         local AddDistinct=null;
 437         local AddArray=null;
 438         local AddArrayDistinct=null;
 439         local Remove=null;
 440         local RemoveIf=null;
 441         local RemoveObject=null;
 442         local RemoveObjectAll=null;
 443         local RemoveObjects=null;
 444         local RemoveObjectsAll=null;
 445         local Clear=null;
 446         local IndexOf=null;
 447         local IndicesOf=null;
 448         local Exists=null;
 449         local __getelem__=null;
 450         local __len__=null;
 451 
 452         local constructor=func()
 453         {
 454             local Items=null;
 455 
 456             Data=func()
 457             {
 458                 return Items.Data();
 459             };
 460 
 461             Add=func(Object)
 462             {
 463                 for(i in 0 to #Items-1)
 464                     if(Items[i]>=Object)
 465                     {
 466                         Items.Insert(i,Object);
 467                         return this;
 468                     }
 469                 Items.Add(Object);
 470                 return this;
 471             };
 472 
 473             AddDistinct=func(Object)
 474             {
 475                 if(!Items.Exists(Object))
 476                     Add(Object);
 477                 return this;
 478             };
 479 
 480             AddArray=func({array}Array)
 481             {
 482                 for(i in Array)
 483                     Add(i);
 484                 return this;
 485             };
 486 
 487             AddArrayDistinct=func({array}Array)
 488             {
 489                 for(i in Array)
 490                     AddDistinct(i);
 491                 return this;
 492             };
 493 
 494             Remove=multifunc
 495             {
 496                 func({int}Index)
 497                 {
 498                     Items.Remove(Index);
 499                     return this;
 500                 }
 501                 func({array}Indices)
 502                 {
 503                     Items.Remove(Indices);
 504                     return this;
 505                 }
 506                 func({int}Index,{int}Count)
 507                 {
 508                     Items.Remove(Index,Count);
 509                     return this;
 510                 }
 511             };
 512 
 513             RemoveIf=func(Predicate)
 514             {
 515                 Items.RemoveIf(Predicate);
 516                 return this;
 517             };
 518 
 519             RemoveObject=func(Object)
 520             {
 521                 Items.RemoveObject(Object);
 522                 return this;
 523             };
 524 
 525             RemoveObjectAll=func(Object)
 526             {
 527                 Items.RemoveObjectAll(Object);
 528                 return this;
 529             };
 530 
 531             RemoveObjects=func({array}Array)
 532             {
 533                 Items.RemoveObjects(Array);
 534                 return this;
 535             };
 536 
 537             RemoveObjectsAll=func({array}Array)
 538             {
 539                 Items.RemoveObjectsAll(Array);
 540                 return this;
 541             };
 542 
 543             Clear=func()
 544             {
 545                 Items.Clear();
 546                 return this;
 547             };
 548 
 549             IndexOf=func(Object)
 550             {
 551                 return Items.IndexOf(Object);
 552             };
 553 
 554             IndicesOf=func(Object)
 555             {
 556                 return Items.IndicesOf(Object);
 557             };
 558 
 559             Exists=func(Object)
 560             {
 561                 return Items.Exists(Object);
 562             };
 563 
 564             __getelem__=func(Object)
 565             {
 566                 return Items[Object];
 567             };
 568 
 569             __len__=func(me)
 570             {
 571                 return #Items;
 572             };
 573 
 574             return multifunc
 575             {
 576                 func()
 577                 {
 578                     Items=List.new();
 579                 }
 580                 func({array}Array)
 581                 {
 582                     Items=List.new();
 583                     for(i in Array)
 584                         Add(i);
 585                 }
 586             };
 587         }();
 588     };
 589 
 590 /**********************************************************
 591 Stack
 592 **********************************************************/
 593 
 594     fixed Stack=class()
 595     {
 596         local Data=null;
 597         local Push=null;
 598         local Pop=null;
 599         local Top=null;
 600         local Clear=null;
 601         local IndexOf=null;
 602         local IndicesOf=null;
 603         local Exists=null;
 604         local __getelem__=null;
 605         local __len__=null;
 606 
 607         local constructor=func()
 608         {
 609             local Items=null;
 610 
 611             Data=func()
 612             {
 613                 return Items.Data();
 614             };
 615 
 616             Push=func(Object)
 617             {
 618                 Items.Add(Object);
 619                 return this;
 620             };
 621 
 622             Pop=func()
 623             {
 624                 if(#Items==0)
 625                     throw(EmptyContainerException.new("空堆栈不能弹出对象。"));
 626                 Items.Remove(#Items-1);
 627                 return this;
 628             };
 629 
 630             Top=func()
 631             {
 632                 if(#Items==0)
 633                     throw(EmptyContainerException.new("空堆栈不能获得列顶对象。"));
 634                 return Items[#Items-1];
 635             };
 636 
 637             Clear=func()
 638             {
 639                 Items.Clear();
 640                 return this;
 641             };
 642 
 643             IndexOf=func(Object)
 644             {
 645                 return Items.IndexOf(Object);
 646             };
 647 
 648             IndicesOf=func(Object)
 649             {
 650                 return Items.IndicesOf(Object);
 651             };
 652 
 653             Exists=func(Object)
 654             {
 655                 return Items.Exists(Object);
 656             };
 657 
 658             __getelem__=func(Object)
 659             {
 660                 return Items[Object];
 661             };
 662 
 663             __len__=func(me)
 664             {
 665                 return #Items;
 666             };
 667 
 668             return multifunc
 669             {
 670                 func()
 671                 {
 672                     Items=List.new();
 673                 }
 674                 func({array}Array)
 675                 {
 676                     Items=List.new(Array);
 677                 }
 678             };
 679         }();
 680     };
 681 
 682 /**********************************************************
 683 Queue
 684 **********************************************************/
 685 
 686     fixed Queue=class()
 687     {
 688         local Data=null;
 689         local Enqueue=null;
 690         local Dequeue=null;
 691         local Top=null;
 692         local Clear=null;
 693         local IndexOf=null;
 694         local IndicesOf=null;
 695         local Exists=null;
 696         local __getelem__=null;
 697         local __len__=null;
 698 
 699         local constructor=func()
 700         {
 701             local Items=null;
 702 
 703             Data=func()
 704             {
 705                 return Items.Data();
 706             };
 707 
 708             Enqueue=func(Object)
 709             {
 710                 Items.Add(Object);
 711                 return this;
 712             };
 713 
 714             Dequeue=func()
 715             {
 716                 if(#Items==0)
 717                     throw(EmptyContainerException.new("空队列不能弹出对象。"));
 718                 Items.Remove(0);
 719                 return this;
 720             };
 721 
 722             Top=func()
 723             {
 724                 if(#Items==0)
 725                     throw(EmptyContainerException.new("空队列不能获得列顶对象。"));
 726                 return Items[0];
 727             };
 728 
 729             Clear=func()
 730             {
 731                 Items.Clear();
 732                 return this;
 733             };
 734 
 735             IndexOf=func(Object)
 736             {
 737                 return Items.IndexOf(Object);
 738             };
 739 
 740             IndicesOf=func(Object)
 741             {
 742                 return Items.IndicesOf(Object);
 743             };
 744 
 745             Exists=func(Object)
 746             {
 747                 return Items.Exists(Object);
 748             };
 749 
 750             __getelem__=func(Object)
 751             {
 752                 return Items[Object];
 753             };
 754 
 755             __len__=func(me)
 756             {
 757                 return #Items;
 758             };
 759 
 760             return multifunc
 761             {
 762                 func()
 763                 {
 764                     Items=List.new();
 765                 }
 766                 func({array}Array)
 767                 {
 768                     Items=List.new(Array);
 769                 }
 770             };
 771         }();
 772     };
 773 
 774 /**********************************************************
 775 Set
 776 **********************************************************/
 777 
 778     fixed Set=class()
 779     {
 780         local Data=null;
 781         local Add=null;
 782         local AddArray=null;
 783         local Remove=null;
 784         local RemoveIf=null;
 785         local Clear=null;
 786         local Exists=null;
 787         local __len__=null;
 788         local __getelem__=null;
 789         local __add__=null;
 790         local __mul__=null;
 791         local __sub__=null;
 792         local __lg__=null;
 793         local __sm__=null;
 794         local __elg__=null;
 795         local __esm__=null;
 796         local __equ__=null;
 797         local __neq__=null;
 798 
 799         local constructor=func()
 800         {
 801             local Items=null;
 802 
 803             Data=func()
 804             {
 805                 return Items.Data();
 806             };
 807 
 808             Add=func(Object)
 809             {
 810                 Items.AddDistinct(Object);
 811                 return this;
 812             };
 813 
 814             AddArray=func({array}Array)
 815             {
 816                 Items.AddArrayDistinct(Array);
 817                 return this;
 818             };
 819 
 820             Remove=func(Object)
 821             {
 822                 Items.Remove(Object);
 823                 return this;
 824             };
 825 
 826             RemoveIf=func(Predicate)
 827             {
 828                 Items.RemoveIf(Predicate);
 829                 return this;
 830             };
 831 
 832             Clear=func(Object)
 833             {
 834                 Items.Clear();
 835                 return this;
 836             };
 837 
 838             Exists=func(Object)
 839             {
 840                 return Items.Exists(Object);
 841             };
 842 
 843             __getelem__=func(Object)
 844             {
 845                 return Items[Object];
 846             };
 847 
 848             __len__=func(me)
 849             {
 850                 return #Items;
 851             };
 852 
 853             __add__=func({Set}a,{Set}b)
 854             {
 855                 return Set.new(a.Data()).AddArray(b.Data());
 856             };
 857 
 858             __sub__=func({Set}a,{Set}b)
 859             {
 860                 NewSet=Set.new(a.Data());
 861                 NewSet.RemoveIf(func(i)return b.Exists(i););
 862                 return NewSet;
 863             };
 864 
 865             __mul__=func({Set}a,{Set}b)
 866             {
 867                 NewSet=Set.new(a.Data());
 868                 NewSet.RemoveIf(func(i)return !b.Exists(i););
 869                 return NewSet;
 870             };
 871 
 872             __lg__=func({Set}a,{Set}b)
 873             {
 874                 return __elg__(a,b) && #a!=#b;
 875             };
 876 
 877             __sm__=func({Set}a,{Set}b)
 878             {
 879                 return __esm__(a,b) && #a!=#b;
 880             };
 881 
 882             __elg__=func({Set}a,{Set}b)
 883             {
 884                 for(i in b.Data())
 885                     if(!a.Exists(i))
 886                         return false;
 887                 return true;
 888             };
 889 
 890             __esm__=func({Set}a,{Set}b)
 891             {
 892                 for(i in a.Data())
 893                     if(!b.Exists(i))
 894                         return false;
 895                 return true;
 896             };
 897 
 898             __equ__=func({Set}a,{Set}b)
 899             {
 900                 if(#a==#b)
 901                 {
 902                     for(i in 0 to #a-1)
 903                         if(a[i]!=b[i])
 904                             return false;
 905                     return true;
 906                 }
 907                 else
 908                     return false;
 909             };
 910 
 911             __neq__=func({Set}a,{Set}b)
 912             {
 913                 if(#a==#b)
 914                 {
 915                     for(i in 0 to #a-1)
 916                         if(a[i]!=b[i])
 917                             return true;
 918                     return false;
 919                 }
 920                 else
 921                     return true;
 922             };
 923 
 924             return multifunc
 925             {
 926                 func()
 927                 {
 928                     Items=SortedList.new();
 929                 }
 930                 func({array}Array)
 931                 {
 932                     Items=SortedList.new(Array);
 933                 }
 934             };
 935         }();
 936     };
 937 
 938 /**********************************************************
 939 ReadonlyList
 940 **********************************************************/
 941 
 942     fixed ReadonlyList=class()
 943     {
 944         local Container=null;
 945         local Internal=null;
 946         local Data=null;
 947         local IndexOf=null;
 948         local IndicesOf=null;
 949         local Exists=null;
 950         local __getelem__=null;
 951         local __len__=null;
 952         local Clone=null;
 953         local Map=null;
 954         local Filter=null;
 955         local All=null;
 956         local Any=null;
 957         local Find=null;
 958         local Get=null;
 959         local FoldLeft=null;
 960         local FoldRight=null;
 961 
 962         local constructor=func()
 963         {
 964             local Items=null;
 965             local IsFromArray=false;
 966 
 967             Container=func()
 968             {
 969                 return Items;
 970             };
 971 
 972             Internal=func()
 973             {
 974                 if(IsFromArray)
 975                     return Items.Data();
 976                 else
 977                     return Items;
 978             };
 979 
 980             Data=func()
 981             {
 982                 return Items.Data();
 983             };
 984 
 985             IndexOf=func(Object)
 986             {
 987                 for(i in 0 to #Items-1)
 988                     if(Items[i]==Object)
 989                         return i;
 990                 return -1;
 991             };
 992 
 993             IndicesOf=func(Object)
 994             {
 995                 local Indices=[];
 996                 for(i in 0 to #Items-1)
 997                     if(Items[i]==Object)
 998                         Indices[#Indices:0]=[i];
 999                 return Indices;
1000             };
1001 
1002             Exists=func(Object)
1003             {
1004                 return IndexOf(Object)>=0;
1005             };
1006 
1007             __getelem__=func({int}Index)
1008             {
1009                 return Items[Index];
1010             };
1011 
1012             __len__=func(me)
1013             {
1014                 return #Items;
1015             };
1016 
1017             Internal_Create=func(Array)
1018             {
1019                 if(IsFromArray)
1020                     return ctor.new(Array);
1021                 else
1022                     return ctor.new(Items.ctor.new(Array));
1023             };
1024 
1025             Clone=func()
1026             {
1027                 if(IsFromArray)
1028                     return ctor.new(Items.Data());
1029                 else
1030                     return ctor.new(Items);
1031             };
1032 
1033             Map=func(Transformer)
1034             {
1035                 local OldItems=Items.Data();
1036                 local NewItems=OldItems[0:#OldItems];
1037                 for(i in 0 to #NewItems-1)
1038                     NewItems[i]=Transformer(NewItems[i]);
1039                 return Internal_Create(NewItems);
1040             };
1041 
1042             Filter=func(Predicate)
1043             {
1044                 local OldItems=Items.Data();
1045                 local NewItems=OldItems[0:#OldItems];
1046                 for(i in #NewItems-1 downto 0)
1047                     if(!Predicate(NewItems[i]))
1048                         NewItems[i:1]=[];
1049                 return Internal_Create(NewItems);
1050             };
1051 
1052             All=func(Predicate)
1053             {
1054                 for(i in Items)
1055                     if(!Predicate(i))
1056                         return false;
1057                 return true;
1058             };
1059 
1060             Any=func(Predicate)
1061             {
1062                 for(i in Items)
1063                     if(Predicate(i))
1064                         return true;
1065                 return false;
1066             };
1067 
1068             Find=func(Predicate)
1069             {
1070                 local Indices=[null]**#Items;
1071                 local Count=0;
1072                 for(i in 0 to #Items-1)
1073                     if(Predicate(Items[i]))
1074                     {
1075                         Indices[Count]=i;
1076                         Count=Count+1;
1077                     }
1078                 return Indices[0:Count];
1079             };
1080 
1081             Get=func({array}Indices)
1082             {
1083                 local NewItems=[null]**#Indices;
1084                 for(i in 0 to #Indices-1)
1085                     NewItems[i]=Items[Indices[i]];
1086                 return Internal_Create(NewItems);
1087             };
1088 
1089             FoldLeft=func(Operand,Operator)
1090             {
1091                 for(i in 0 to #Items-1)
1092                     Operand=Operator(Operand,Items[i]);
1093                 return Operand;
1094             };
1095 
1096             FoldRight=func(Operand,Operator)
1097             {
1098                 for(i in #Items-1 downto 0)
1099                     Operand=Operator(Items[i],Operand);
1100                 return Operand;
1101             };
1102 
1103             return multifunc
1104             {
1105                 func({List}items)        Items=items;
1106                 func({SortedList}items)        Items=items;
1107                 func({Queue}items)        Items=items;
1108                 func({Stack}items)        Items=items;
1109                 func({Set}items)        Items=items;
1110                 func({ReadonlyList}items)    Items=items.Container();
1111                 func({array}items)
1112                 {
1113                     Items=List.new(items);
1114                     IsFromArray=true;
1115                 }
1116             };
1117         }();
1118     };
1119 
1120 /**********************************************************
1121 MapPair
1122 **********************************************************/
1123 
1124     fixed MapPair=class()
1125     {
1126         local Key=null;
1127         local Value=null;
1128     };
1129 
1130 /**********************************************************
1131 Map
1132 **********************************************************/
1133 
1134     fixed Map=class()
1135     {
1136         local Keys=null;
1137         local Values=null;
1138         local Pairs=null;
1139         local Add=null;
1140         local Remove=null;
1141         local RemoveIf=null;
1142         local Clear=null;
1143         local Exists=null;
1144         local __getelem__=null;
1145 
1146         local constructor=func()
1147         {
1148             local K=SortedList.new();
1149             local V=List.new();
1150 
1151             Keys=func()
1152             {
1153                 return K;
1154             };
1155 
1156             Values=func()
1157             {
1158                 return V;
1159             };
1160 
1161             Pairs=func()
1162             {
1163                 local ps=[null]**#K;
1164                 for(i in 0 to #K-1)
1165                 {
1166                     local p=MapPair.new();
1167                     p.Key=K[i];
1168                     p.Value=V[i];
1169                     ps[i]=p;
1170                 }
1171                 return List.new(ps);
1172             };
1173 
1174             Add=func(Key,Value)
1175             {
1176                 if(K.Exists(Key))
1177                 {
1178                     V[K.IndexOf(Key)]=Value;
1179                 }
1180                 else
1181                 {
1182                     K.Add(Key);
1183                     V.Insert(K.IndexOf(Key),Value);
1184                 }
1185                 return this;
1186             };
1187 
1188             Remove=func(Key)
1189             {
1190                 if(K.Exists(Key))
1191                 {
1192                     local Index=K.IndexOf(Key);
1193                     K.Remove(Index);
1194                     V.Remove(Index);
1195                 }
1196                 return this;
1197             };
1198 
1199             RemoveIf=func(Predicate)
1200             {
1201                 for(i in #K-1 downto 0)
1202                 {
1203                     local p=MapPair.new();
1204                     p.Key=K[i];
1205                     p.Value=V[i];
1206                     if(Predicate(p))
1207                     {
1208                         K.Remove(i);
1209                         V.Remove(i);
1210                     }
1211                 }
1212                 return this;
1213             };
1214 
1215             Clear=func()
1216             {
1217                 K.Clear();
1218                 V.Clear();
1219                 return this;
1220             };
1221 
1222             Exists=func(Key)
1223             {
1224                 return K.Exists(Key);
1225             };
1226 
1227             __getelem__=func(Key)
1228             {
1229                 local Index=K.IndexOf(Key);
1230                 if(Index>=0)
1231                     return V[Index];
1232                 else
1233                     throw(KeyNotFoundException.new("指定的键不存在。"));
1234             };
1235 
1236             return multifunc
1237             {
1238                 func();
1239                 func({List}Pairs)
1240                 {
1241                     for(p in Pairs)
1242                     {
1243                         Add(p.Key,p.Value);
1244                     }
1245                 }
1246             };
1247         }();
1248     };
1249 
1250 /**********************************************************
1251 MultiMap
1252 **********************************************************/
1253 
1254     fixed MultiMap=class()
1255     {
1256         local Keys=null;
1257         local Values=null;
1258         local Pairs=null;
1259         local Add=null;
1260         local AddDistinct=null;
1261         local Remove=null;
1262         local RemovePair=null;
1263         local RemovePairAll=null;
1264         local RemoveIf=null;
1265         local Clear=null;
1266         local Exists=null;
1267         local __getelem__=null;
1268 
1269         local constructor=func()
1270         {
1271             local M=Map.new();
1272 
1273             Keys=func()
1274             {
1275                 return M.Keys();
1276             };
1277 
1278             Values=func()
1279             {
1280                 return M.Values();
1281             };
1282 
1283             Pairs=func()
1284             {
1285                 local Count=0;
1286                 for(v in M.Values())
1287                     Count=Count+#v;
1288                 local ps=[null]**Count;
1289                 Count=0;
1290                 for(k in M.Keys())
1291                     for(v in M[k])
1292                     {
1293                         local p=MapPair.new();
1294                         p.Key=k;
1295                         p.Value=v;
1296                         ps[Count]=p;
1297                         Count=Count+1;
1298                     }
1299                 return List.new(ps);
1300             };
1301 
1302             Add=func(Key,Value)
1303             {
1304                 if(M.Exists(Key))
1305                     M[Key].Add(Value);
1306                 else
1307                     M.Add(Key,List.new([Value]));
1308                 return this;
1309             };
1310 
1311             AddDistinct=func(Key,Value)
1312             {
1313                 if(M.Exists(Key))
1314                     M[Key].AddDistinct(Value);
1315                 else
1316                     M.Add(Key,List.new([Value]));
1317                 return this;
1318             };
1319 
1320             Remove=func(Key)
1321             {
1322                 M.Remove(Key);
1323                 return this;
1324             };
1325 
1326             RemovePair=func(Key,Value)
1327             {
1328                 if(M.Exists(Key))
1329                 {
1330                     local V=M[Key];
1331                     V.RemoveObject(Value);
1332                     if(#V==0)
1333                         M.Remove(Key);
1334                 }
1335                 return this;
1336             };
1337 
1338             RemovePairAll=func(Key,Value)
1339             {
1340                 if(M.Exists(Key))
1341                 {
1342                     local V=M[Key];
1343                     V.RemoveObjectAll(Value);
1344                     if(#V==0)
1345                         M.Remove(Key);
1346                 }
1347                 return this;
1348             };
1349 
1350             RemoveIf=func(Predicate)
1351             {
1352                 local K=M.Keys();
1353                 for(i in #K-1 downto 0)
1354                 {
1355                     local V=M[K[i]];
1356                     V.RemoveIf(func(Value)
1357                     {
1358                         local p=MapPair.new();
1359                         p.Key=K[i];
1360                         p.Value=Value;
1361                         return Predicate(p);
1362                     });
1363                     if(#V==0)
1364                         M.Remove(K[i]);
1365                 }
1366                 return this;
1367             };
1368 
1369             Clear=func()
1370             {
1371                 M.Clear();
1372                 return this;
1373             };
1374 
1375             Exists=multifunc
1376             {
1377                 func(Key)
1378                 {
1379                     return M.Exists(Key);
1380                 }
1381                 func(Key,Value)
1382                 {
1383                     if(M.Exists(Key))
1384                         return M[Key].Exists(Value);
1385                     else
1386                         return false;
1387                 }
1388             };
1389 
1390             __getelem__=func(Key)
1391             {
1392                 return M[Key];
1393             };
1394 
1395             return multifunc
1396             {
1397                 func();
1398                 func({List}Pairs)
1399                 {
1400                     for(p in Pairs)
1401                     {
1402                         Add(p.Key,p.Value);
1403                     }
1404                 }
1405             };
1406         }();
1407     };
1408 
1409 /**********************************************************
1410 FreeMap
1411 **********************************************************/
1412 
1413     fixed FreeMap=class()
1414     {
1415         local __get__=null;
1416         local __set__=null;
1417         local __Keys=null;
1418         local __Exists=null;
1419         local __Remove=null;
1420 
1421         local constructor=func()
1422         {
1423             local Names=SortedList.new();
1424             local Keys=ReadonlyList.new(Names);
1425 
1426             __get__=func({value}Name)
1427             {
1428                 local NewMap=ctor.new();
1429                 _setvar(this,Name,NewMap);
1430                 Names.Add(Name);
1431                 return NewMap;
1432             };
1433 
1434             __set__=func({value}Name,Value)
1435             {
1436                 _setvar(this,Name,Value);
1437                 Names.Add(Name);
1438             };
1439 
1440             __Keys=func()
1441             {
1442                 return Keys;
1443             };
1444 
1445             __Exists=func({value}Name)
1446             {
1447                 return Names.Exists(Name);
1448             };
1449 
1450             __Remove=func({value}Name)
1451             {
1452                 if(Names.Exists(Name))
1453                 {
1454                     _rmvar(this,Name);
1455                     Names.RemoveObject(Name);
1456                 }
1457             };
1458 
1459             return func(){};
1460 
1461         }();
1462     };
1463 };
1464 
posted on 2008-05-18 06:42 陈梓瀚(vczh) 阅读(1670) 评论(3)  编辑 收藏 引用 所属分类: Vczh Free Script

评论:
# re: 今天使用新的feature为Vczh Free Script 2.0写了一个collection库[未登录] 2008-05-18 07:13 | foxtail
沙发  回复  更多评论
  
# re: 今天使用新的feature为Vczh Free Script 2.0写了一个collection库 2008-05-18 07:56 | nn
嘿嘿  回复  更多评论
  
# re: 今天使用新的feature为Vczh Free Script 2.0写了一个collection库 2008-05-18 21:30 | Gohan
挺漂亮的script  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理