set.js.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: set.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: set.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>/**
  20. * @constructor
  21. * @param {Array.&lt;Object>} S
  22. */
  23. Sk.builtin.set = function (S) {
  24. var it, i;
  25. var S_list;
  26. if (!(this instanceof Sk.builtin.set)) {
  27. return new Sk.builtin.set(S);
  28. }
  29. if (typeof(S) === "undefined") {
  30. S = [];
  31. }
  32. this.set_reset_();
  33. S_list = new Sk.builtin.list(S);
  34. // python sorts sets on init, but not thereafter.
  35. // Skulpt seems to init a new set each time you add/remove something
  36. //Sk.builtin.list.prototype['sort'].func_code(S);
  37. for (it = S_list.tp$iter(), i = it.tp$iternext(); i !== undefined; i = it.tp$iternext()) {
  38. Sk.builtin.set.prototype["add"].func_code(this, i);
  39. }
  40. this.__class__ = Sk.builtin.set;
  41. this["v"] = this.v;
  42. return this;
  43. };
  44. Sk.builtin.set.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj("set", Sk.builtin.set);
  45. Sk.builtin.set.prototype.set_iter_ = function () {
  46. return this["v"].tp$iter();
  47. };
  48. Sk.builtin.set.prototype.set_reset_ = function () {
  49. this.v = new Sk.builtin.dict([]);
  50. };
  51. Sk.builtin.set.prototype.tp$name = "set";
  52. Sk.builtin.set.prototype["$r"] = function () {
  53. var it, i;
  54. var ret = [];
  55. for (it = this.tp$iter(), i = it.tp$iternext(); i !== undefined; i = it.tp$iternext()) {
  56. ret.push(Sk.misceval.objectRepr(i).v);
  57. }
  58. if(Sk.python3) {
  59. return new Sk.builtin.str("{" + ret.join(", ") + "}");
  60. } else {
  61. return new Sk.builtin.str("set([" + ret.join(", ") + "])");
  62. }
  63. };
  64. Sk.builtin.set.prototype.tp$getattr = Sk.builtin.object.prototype.GenericGetAttr;
  65. // todo; you can't hash a set() -- what should this be?
  66. Sk.builtin.set.prototype.tp$hash = Sk.builtin.none.none$;
  67. Sk.builtin.set.prototype.tp$richcompare = function (w, op) {
  68. // todo; NotImplemented if either isn't a set
  69. var isSuper;
  70. var isSub;
  71. var wl;
  72. var vl;
  73. if (this === w &amp;&amp; Sk.misceval.opAllowsEquality(op)) {
  74. return true;
  75. }
  76. // w not a set
  77. if (!w.__class__ || w.__class__ != Sk.builtin.set) {
  78. // shortcuts for eq/not
  79. if (op === "Eq") {
  80. return false;
  81. }
  82. if (op === "NotEq") {
  83. return true;
  84. }
  85. // todo; other types should have an arbitrary order
  86. return false;
  87. }
  88. vl = this.sq$length();
  89. wl = w.sq$length();
  90. // easy short-cut
  91. if (wl !== vl) {
  92. if (op === "Eq") {
  93. return false;
  94. }
  95. if (op === "NotEq") {
  96. return true;
  97. }
  98. }
  99. // used quite a lot in comparisons.
  100. isSub = false;
  101. isSuper = false;
  102. // gather common info
  103. switch (op) {
  104. case "Lt":
  105. case "LtE":
  106. case "Eq":
  107. case "NotEq":
  108. isSub = Sk.builtin.set.prototype["issubset"].func_code(this, w).v;
  109. break;
  110. case "Gt":
  111. case "GtE":
  112. isSuper = Sk.builtin.set.prototype["issuperset"].func_code(this, w).v;
  113. break;
  114. default:
  115. goog.asserts.fail();
  116. }
  117. switch (op) {
  118. case "Lt":
  119. return vl &lt; wl &amp;&amp; isSub;
  120. case "LtE":
  121. case "Eq": // we already know that the lengths are equal
  122. return isSub;
  123. case "NotEq":
  124. return !isSub;
  125. case "Gt":
  126. return vl > wl &amp;&amp; isSuper;
  127. case "GtE":
  128. return isSuper;
  129. }
  130. };
  131. Sk.builtin.set.prototype.tp$iter = Sk.builtin.set.prototype.set_iter_;
  132. Sk.builtin.set.prototype.sq$length = function () {
  133. return this["v"].mp$length();
  134. };
  135. Sk.builtin.set.prototype.sq$contains = function(ob) {
  136. return this["v"].sq$contains(ob);
  137. };
  138. Sk.builtin.set.prototype["isdisjoint"] = new Sk.builtin.func(function (self, other) {
  139. // requires all items in self to not be in other
  140. var isIn;
  141. var it, item;
  142. for (it = self.tp$iter(), item = it.tp$iternext(); item !== undefined; item = it.tp$iternext()) {
  143. isIn = Sk.abstr.sequenceContains(other, item);
  144. if (isIn) {
  145. return Sk.builtin.bool.false$;
  146. }
  147. }
  148. return Sk.builtin.bool.true$;
  149. });
  150. Sk.builtin.set.prototype["issubset"] = new Sk.builtin.func(function (self, other) {
  151. var isIn;
  152. var it, item;
  153. var selfLength = self.sq$length();
  154. var otherLength = other.sq$length();
  155. if (selfLength > otherLength) {
  156. // every item in this set can't be in other if it's shorter!
  157. return Sk.builtin.bool.false$;
  158. }
  159. for (it = self.tp$iter(), item = it.tp$iternext(); item !== undefined; item = it.tp$iternext()) {
  160. isIn = Sk.abstr.sequenceContains(other, item);
  161. if (!isIn) {
  162. return Sk.builtin.bool.false$;
  163. }
  164. }
  165. return Sk.builtin.bool.true$;
  166. });
  167. Sk.builtin.set.prototype["issuperset"] = new Sk.builtin.func(function (self, other) {
  168. return Sk.builtin.set.prototype["issubset"].func_code(other, self);
  169. });
  170. Sk.builtin.set.prototype["union"] = new Sk.builtin.func(function (self) {
  171. var i;
  172. var S = new Sk.builtin.set(self);
  173. for (i = 1; i &lt; arguments.length; i++) {
  174. Sk.builtin.set.prototype["update"].func_code(S, arguments[i]);
  175. }
  176. return S;
  177. });
  178. Sk.builtin.set.prototype["intersection"] = new Sk.builtin.func(function (self) {
  179. var S = Sk.builtin.set.prototype["copy"].func_code(self),
  180. new_args = Array.prototype.slice.call(arguments); //copy array
  181. new_args[0] = S;
  182. Sk.builtin.set.prototype["intersection_update"].func_code.apply(null, new_args);
  183. return S;
  184. });
  185. Sk.builtin.set.prototype["difference"] = new Sk.builtin.func(function (self, other) {
  186. var S = Sk.builtin.set.prototype["copy"].func_code(self),
  187. new_args = Array.prototype.slice.call(arguments); //copy array
  188. new_args[0] = S;
  189. Sk.builtin.set.prototype["difference_update"].func_code.apply(null, new_args);
  190. return S;
  191. });
  192. Sk.builtin.set.prototype["symmetric_difference"] = new Sk.builtin.func(function (self, other) {
  193. var it, item;
  194. var S = Sk.builtin.set.prototype["union"].func_code(self, other);
  195. for (it = S.tp$iter(), item = it.tp$iternext(); item !== undefined; item = it.tp$iternext()) {
  196. if (Sk.abstr.sequenceContains(self, item) &amp;&amp; Sk.abstr.sequenceContains(other, item)) {
  197. Sk.builtin.set.prototype["discard"].func_code(S, item);
  198. }
  199. }
  200. return S;
  201. });
  202. Sk.builtin.set.prototype["copy"] = new Sk.builtin.func(function (self) {
  203. return new Sk.builtin.set(self);
  204. });
  205. Sk.builtin.set.prototype["update"] = new Sk.builtin.func(function (self, other) {
  206. var it, item;
  207. for (it = other.tp$iter(), item = it.tp$iternext(); item !== undefined; item = it.tp$iternext()) {
  208. Sk.builtin.set.prototype["add"].func_code(self, item);
  209. }
  210. return Sk.builtin.none.none$;
  211. });
  212. Sk.builtin.set.prototype["intersection_update"] = new Sk.builtin.func(function (self, other) {
  213. var i;
  214. var it, item;
  215. for (it = self.tp$iter(), item = it.tp$iternext(); item !== undefined; item = it.tp$iternext()) {
  216. for (i = 1; i &lt; arguments.length; i++) {
  217. if (!Sk.abstr.sequenceContains(arguments[i], item)) {
  218. Sk.builtin.set.prototype["discard"].func_code(self, item);
  219. break;
  220. }
  221. }
  222. }
  223. return Sk.builtin.none.none$;
  224. });
  225. Sk.builtin.set.prototype["difference_update"] = new Sk.builtin.func(function (self, other) {
  226. var i;
  227. var it, item;
  228. for (it = self.tp$iter(), item = it.tp$iternext(); item !== undefined; item = it.tp$iternext()) {
  229. for (i = 1; i &lt; arguments.length; i++) {
  230. if (Sk.abstr.sequenceContains(arguments[i], item)) {
  231. Sk.builtin.set.prototype["discard"].func_code(self, item);
  232. break;
  233. }
  234. }
  235. }
  236. return Sk.builtin.none.none$;
  237. });
  238. Sk.builtin.set.prototype["symmetric_difference_update"] = new Sk.builtin.func(function (self, other) {
  239. var sd = Sk.builtin.set.prototype["symmetric_difference"].func_code(self, other);
  240. self.set_reset_();
  241. Sk.builtin.set.prototype["update"].func_code(self, sd);
  242. return Sk.builtin.none.none$;
  243. });
  244. Sk.builtin.set.prototype["add"] = new Sk.builtin.func(function (self, item) {
  245. self.v.mp$ass_subscript(item, true);
  246. return Sk.builtin.none.none$;
  247. });
  248. Sk.builtin.set.prototype["discard"] = new Sk.builtin.func(function (self, item) {
  249. Sk.builtin.dict.prototype["pop"].func_code(self.v, item,
  250. Sk.builtin.none.none$);
  251. return Sk.builtin.none.none$;
  252. });
  253. Sk.builtin.set.prototype["pop"] = new Sk.builtin.func(function (self) {
  254. var it, item;
  255. if (self.sq$length() === 0) {
  256. throw new Sk.builtin.KeyError("pop from an empty set");
  257. }
  258. it = self.tp$iter();
  259. item = it.tp$iternext();
  260. Sk.builtin.set.prototype["discard"].func_code(self, item);
  261. return item;
  262. });
  263. Sk.builtin.set.prototype["remove"] = new Sk.builtin.func(function (self, item) {
  264. self.v.mp$del_subscript(item);
  265. return Sk.builtin.none.none$;
  266. });
  267. goog.exportSymbol("Sk.builtin.set", Sk.builtin.set);
  268. </code></pre>
  269. </article>
  270. </section>
  271. </div>
  272. <nav>
  273. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Sk.builtin.func.html">func</a></li><li><a href="Sk.builtin.set.html">set</a></li><li><a href="Sk.misceval.Suspension.html">Suspension</a></li></ul><h3>Namespaces</h3><ul><li><a href="Sk.html">Sk</a></li><li><a href="Sk.abstr.html">abstr</a></li><li><a href="Sk.builtin.html">builtin</a></li><li><a href="Sk.ffi.html">ffi</a></li><li><a href="Sk.misceval.html">misceval</a></li></ul>
  274. </nav>
  275. <br class="clear">
  276. <footer>
  277. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0</a> on Tue Jun 09 2015 08:38:16 GMT+0200 (CEST)
  278. </footer>
  279. <script> prettyPrint(); </script>
  280. <script src="scripts/linenumber.js"> </script>
  281. </body>
  282. </html>