collection-strong.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. 'use strict';
  2. var defineProperty = require('../internals/object-define-property').f;
  3. var create = require('../internals/object-create');
  4. var redefineAll = require('../internals/redefine-all');
  5. var bind = require('../internals/function-bind-context');
  6. var anInstance = require('../internals/an-instance');
  7. var iterate = require('../internals/iterate');
  8. var defineIterator = require('../internals/define-iterator');
  9. var setSpecies = require('../internals/set-species');
  10. var DESCRIPTORS = require('../internals/descriptors');
  11. var fastKey = require('../internals/internal-metadata').fastKey;
  12. var InternalStateModule = require('../internals/internal-state');
  13. var setInternalState = InternalStateModule.set;
  14. var internalStateGetterFor = InternalStateModule.getterFor;
  15. module.exports = {
  16. getConstructor: function (wrapper, CONSTRUCTOR_NAME, IS_MAP, ADDER) {
  17. var Constructor = wrapper(function (that, iterable) {
  18. anInstance(that, Prototype);
  19. setInternalState(that, {
  20. type: CONSTRUCTOR_NAME,
  21. index: create(null),
  22. first: undefined,
  23. last: undefined,
  24. size: 0
  25. });
  26. if (!DESCRIPTORS) that.size = 0;
  27. if (iterable != undefined) iterate(iterable, that[ADDER], { that: that, AS_ENTRIES: IS_MAP });
  28. });
  29. var Prototype = Constructor.prototype;
  30. var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);
  31. var define = function (that, key, value) {
  32. var state = getInternalState(that);
  33. var entry = getEntry(that, key);
  34. var previous, index;
  35. // change existing entry
  36. if (entry) {
  37. entry.value = value;
  38. // create new entry
  39. } else {
  40. state.last = entry = {
  41. index: index = fastKey(key, true),
  42. key: key,
  43. value: value,
  44. previous: previous = state.last,
  45. next: undefined,
  46. removed: false
  47. };
  48. if (!state.first) state.first = entry;
  49. if (previous) previous.next = entry;
  50. if (DESCRIPTORS) state.size++;
  51. else that.size++;
  52. // add to index
  53. if (index !== 'F') state.index[index] = entry;
  54. } return that;
  55. };
  56. var getEntry = function (that, key) {
  57. var state = getInternalState(that);
  58. // fast case
  59. var index = fastKey(key);
  60. var entry;
  61. if (index !== 'F') return state.index[index];
  62. // frozen object case
  63. for (entry = state.first; entry; entry = entry.next) {
  64. if (entry.key == key) return entry;
  65. }
  66. };
  67. redefineAll(Prototype, {
  68. // `{ Map, Set }.prototype.clear()` methods
  69. // https://tc39.es/ecma262/#sec-map.prototype.clear
  70. // https://tc39.es/ecma262/#sec-set.prototype.clear
  71. clear: function clear() {
  72. var that = this;
  73. var state = getInternalState(that);
  74. var data = state.index;
  75. var entry = state.first;
  76. while (entry) {
  77. entry.removed = true;
  78. if (entry.previous) entry.previous = entry.previous.next = undefined;
  79. delete data[entry.index];
  80. entry = entry.next;
  81. }
  82. state.first = state.last = undefined;
  83. if (DESCRIPTORS) state.size = 0;
  84. else that.size = 0;
  85. },
  86. // `{ Map, Set }.prototype.delete(key)` methods
  87. // https://tc39.es/ecma262/#sec-map.prototype.delete
  88. // https://tc39.es/ecma262/#sec-set.prototype.delete
  89. 'delete': function (key) {
  90. var that = this;
  91. var state = getInternalState(that);
  92. var entry = getEntry(that, key);
  93. if (entry) {
  94. var next = entry.next;
  95. var prev = entry.previous;
  96. delete state.index[entry.index];
  97. entry.removed = true;
  98. if (prev) prev.next = next;
  99. if (next) next.previous = prev;
  100. if (state.first == entry) state.first = next;
  101. if (state.last == entry) state.last = prev;
  102. if (DESCRIPTORS) state.size--;
  103. else that.size--;
  104. } return !!entry;
  105. },
  106. // `{ Map, Set }.prototype.forEach(callbackfn, thisArg = undefined)` methods
  107. // https://tc39.es/ecma262/#sec-map.prototype.foreach
  108. // https://tc39.es/ecma262/#sec-set.prototype.foreach
  109. forEach: function forEach(callbackfn /* , that = undefined */) {
  110. var state = getInternalState(this);
  111. var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined);
  112. var entry;
  113. while (entry = entry ? entry.next : state.first) {
  114. boundFunction(entry.value, entry.key, this);
  115. // revert to the last existing entry
  116. while (entry && entry.removed) entry = entry.previous;
  117. }
  118. },
  119. // `{ Map, Set}.prototype.has(key)` methods
  120. // https://tc39.es/ecma262/#sec-map.prototype.has
  121. // https://tc39.es/ecma262/#sec-set.prototype.has
  122. has: function has(key) {
  123. return !!getEntry(this, key);
  124. }
  125. });
  126. redefineAll(Prototype, IS_MAP ? {
  127. // `Map.prototype.get(key)` method
  128. // https://tc39.es/ecma262/#sec-map.prototype.get
  129. get: function get(key) {
  130. var entry = getEntry(this, key);
  131. return entry && entry.value;
  132. },
  133. // `Map.prototype.set(key, value)` method
  134. // https://tc39.es/ecma262/#sec-map.prototype.set
  135. set: function set(key, value) {
  136. return define(this, key === 0 ? 0 : key, value);
  137. }
  138. } : {
  139. // `Set.prototype.add(value)` method
  140. // https://tc39.es/ecma262/#sec-set.prototype.add
  141. add: function add(value) {
  142. return define(this, value = value === 0 ? 0 : value, value);
  143. }
  144. });
  145. if (DESCRIPTORS) defineProperty(Prototype, 'size', {
  146. get: function () {
  147. return getInternalState(this).size;
  148. }
  149. });
  150. return Constructor;
  151. },
  152. setStrong: function (Constructor, CONSTRUCTOR_NAME, IS_MAP) {
  153. var ITERATOR_NAME = CONSTRUCTOR_NAME + ' Iterator';
  154. var getInternalCollectionState = internalStateGetterFor(CONSTRUCTOR_NAME);
  155. var getInternalIteratorState = internalStateGetterFor(ITERATOR_NAME);
  156. // `{ Map, Set }.prototype.{ keys, values, entries, @@iterator }()` methods
  157. // https://tc39.es/ecma262/#sec-map.prototype.entries
  158. // https://tc39.es/ecma262/#sec-map.prototype.keys
  159. // https://tc39.es/ecma262/#sec-map.prototype.values
  160. // https://tc39.es/ecma262/#sec-map.prototype-@@iterator
  161. // https://tc39.es/ecma262/#sec-set.prototype.entries
  162. // https://tc39.es/ecma262/#sec-set.prototype.keys
  163. // https://tc39.es/ecma262/#sec-set.prototype.values
  164. // https://tc39.es/ecma262/#sec-set.prototype-@@iterator
  165. defineIterator(Constructor, CONSTRUCTOR_NAME, function (iterated, kind) {
  166. setInternalState(this, {
  167. type: ITERATOR_NAME,
  168. target: iterated,
  169. state: getInternalCollectionState(iterated),
  170. kind: kind,
  171. last: undefined
  172. });
  173. }, function () {
  174. var state = getInternalIteratorState(this);
  175. var kind = state.kind;
  176. var entry = state.last;
  177. // revert to the last existing entry
  178. while (entry && entry.removed) entry = entry.previous;
  179. // get next entry
  180. if (!state.target || !(state.last = entry = entry ? entry.next : state.state.first)) {
  181. // or finish the iteration
  182. state.target = undefined;
  183. return { value: undefined, done: true };
  184. }
  185. // return step by kind
  186. if (kind == 'keys') return { value: entry.key, done: false };
  187. if (kind == 'values') return { value: entry.value, done: false };
  188. return { value: [entry.key, entry.value], done: false };
  189. }, IS_MAP ? 'entries' : 'values', !IS_MAP, true);
  190. // `{ Map, Set }.prototype[@@species]` accessors
  191. // https://tc39.es/ecma262/#sec-get-map-@@species
  192. // https://tc39.es/ecma262/#sec-get-set-@@species
  193. setSpecies(CONSTRUCTOR_NAME);
  194. }
  195. };