Set.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.collections.Set"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.collections.Set"] = true;
  8. dojo.provide("dojox.collections.Set");
  9. dojo.require("dojox.collections.ArrayList");
  10. (function(){
  11. var dxc=dojox.collections;
  12. dxc.Set=new (function(){
  13. function conv(arr){
  14. if(arr.constructor==Array){
  15. return new dojox.collections.ArrayList(arr); // dojox.collections.ArrayList
  16. }
  17. return arr; // dojox.collections.ArrayList
  18. }
  19. this.union = function(/* array */setA, /* array */setB){
  20. // summary
  21. // Return the union of the two passed sets.
  22. setA=conv(setA);
  23. setB=conv(setB);
  24. var result = new dojox.collections.ArrayList(setA.toArray());
  25. var e = setB.getIterator();
  26. while(!e.atEnd()){
  27. var item=e.get();
  28. if(!result.contains(item)){
  29. result.add(item);
  30. }
  31. }
  32. return result; // dojox.collections.ArrayList
  33. };
  34. this.intersection = function(/* array */setA, /* array */setB){
  35. // summary
  36. // Return the intersection of the two passed sets.
  37. setA=conv(setA);
  38. setB=conv(setB);
  39. var result = new dojox.collections.ArrayList();
  40. var e = setB.getIterator();
  41. while(!e.atEnd()){
  42. var item=e.get();
  43. if(setA.contains(item)){
  44. result.add(item);
  45. }
  46. }
  47. return result; // dojox.collections.ArrayList
  48. };
  49. this.difference = function(/* array */setA, /* array */setB){
  50. // summary
  51. // Returns everything in setA that is not in setB.
  52. setA=conv(setA);
  53. setB=conv(setB);
  54. var result = new dojox.collections.ArrayList();
  55. var e=setA.getIterator();
  56. while(!e.atEnd()){
  57. var item=e.get();
  58. if(!setB.contains(item)){
  59. result.add(item);
  60. }
  61. }
  62. return result; // dojox.collections.ArrayList
  63. };
  64. this.isSubSet = function(/* array */setA, /* array */setB) {
  65. // summary
  66. // Returns if set B is a subset of set A.
  67. setA=conv(setA);
  68. setB=conv(setB);
  69. var e = setA.getIterator();
  70. while(!e.atEnd()){
  71. if(!setB.contains(e.get())){
  72. return false; // boolean
  73. }
  74. }
  75. return true; // boolean
  76. };
  77. this.isSuperSet = function(/* array */setA, /* array */setB){
  78. // summary
  79. // Returns if set B is a superset of set A.
  80. setA=conv(setA);
  81. setB=conv(setB);
  82. var e = setB.getIterator();
  83. while(!e.atEnd()){
  84. if(!setA.contains(e.get())){
  85. return false; // boolean
  86. }
  87. }
  88. return true; // boolean
  89. };
  90. })();
  91. })();
  92. }