Set.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js.lib;
  23. /**
  24. The `js.Set` object lets you store unique values of any type, whether
  25. primitive values or object references.
  26. Documentation [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set$history), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
  27. **/
  28. @:native("Set")
  29. extern class Set<T> {
  30. /**
  31. The number of values in the `js.Set` object.
  32. **/
  33. var size(default, null):Int;
  34. /**
  35. If an iterable object is passed, all of its elements will be added to
  36. the new `js.Set`.
  37. **/
  38. @:pure function new(?iterable:Any);
  39. /**
  40. Returns a boolean asserting whether an element is present with the given
  41. value in the `js.Set` object or not.
  42. **/
  43. @:pure function has(value:T):Bool;
  44. /**
  45. Appends a new element with the given value to the `js.Set` object.
  46. Returns the `js.Set` object.
  47. **/
  48. function add(value:T):Set<T>;
  49. /**
  50. Removes the element associated to the value and returns the value that
  51. `has(value)` would have previously returned.
  52. `has(value)` will return `false` afterwards.
  53. **/
  54. function delete(value:T):Bool;
  55. /**
  56. Removes all elements from the `js.Set` object.
  57. **/
  58. function clear():Void;
  59. /**
  60. Calls `callback` once for each key-value pair present in the `js.Set`
  61. object, in insertion order.
  62. If a `thisArg` parameter is provided to forEach, it will be used as the
  63. `this` value for each callback.
  64. **/
  65. function forEach(callback:(value:T, key:T, set:Set<T>) -> Void, ?thisArg:Any):Void;
  66. /**
  67. Returns a new `js.lib.Iterator` object that contains the keys for each element
  68. in the `js.Set` object in insertion order.
  69. **/
  70. function keys():js.lib.Iterator<T>;
  71. /**
  72. Returns a new `js.lib.Iterator` object that contains the values for each
  73. element in the `js.Set` object in insertion order.
  74. **/
  75. function values():js.lib.Iterator<T>;
  76. /**
  77. Returns a new `js.lib.Iterator` object that contains an array of
  78. `[value, value]` for each element in the `js.Set` object, in insertion
  79. order.
  80. This is kept similar to the `js.Map` object, so that each entry has the
  81. same value for its key and value here.
  82. **/
  83. function entries():js.lib.Iterator<KeyValue<T, T>>;
  84. inline function iterator():HaxeIterator<T> {
  85. return new HaxeIterator(this.values());
  86. }
  87. inline function keyValueIterator():SetKeyValueIterator<T> {
  88. return new SetKeyValueIterator(this);
  89. }
  90. }
  91. /**
  92. key => value iterator for js.lib.Set, tracking the entry index for the key to match the behavior of haxe.ds.List
  93. **/
  94. class SetKeyValueIterator<T> {
  95. final set:js.lib.Set<T>;
  96. final values:HaxeIterator<T>;
  97. var index = 0;
  98. public inline function new(set:js.lib.Set<T>) {
  99. this.set = set;
  100. this.values = new HaxeIterator(set.values());
  101. }
  102. public inline function hasNext():Bool {
  103. return values.hasNext();
  104. }
  105. public inline function next():{key:Int, value:T} {
  106. return {
  107. key: index++,
  108. value: values.next(),
  109. };
  110. }
  111. }