EnumerableSet.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. const format = require('../format-lines');
  2. const { fromBytes32, toBytes32 } = require('./conversion');
  3. const { SET_TYPES } = require('./Enumerable.opts');
  4. const header = `\
  5. pragma solidity ^0.8.20;
  6. import {Arrays} from "../Arrays.sol";
  7. import {Math} from "../math/Math.sol";
  8. /**
  9. * @dev Library for managing
  10. * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
  11. * types.
  12. *
  13. * Sets have the following properties:
  14. *
  15. * - Elements are added, removed, and checked for existence in constant time
  16. * (O(1)).
  17. * - Elements are enumerated in O(n). No guarantees are made on the ordering.
  18. * - Set can be cleared (all elements removed) in O(n).
  19. *
  20. * \`\`\`solidity
  21. * contract Example {
  22. * // Add the library methods
  23. * using EnumerableSet for EnumerableSet.AddressSet;
  24. *
  25. * // Declare a set state variable
  26. * EnumerableSet.AddressSet private mySet;
  27. * }
  28. * \`\`\`
  29. *
  30. * The following types are supported:
  31. *
  32. * - \`bytes32\` (\`Bytes32Set\`) since v3.3.0
  33. * - \`address\` (\`AddressSet\`) since v3.3.0
  34. * - \`uint256\` (\`UintSet\`) since v3.3.0
  35. * - \`string\` (\`StringSet\`) since v5.4.0
  36. * - \`bytes\` (\`BytesSet\`) since v5.4.0
  37. *
  38. * [WARNING]
  39. * ====
  40. * Trying to delete such a structure from storage will likely result in data corruption, rendering the structure
  41. * unusable.
  42. * See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
  43. *
  44. * In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an
  45. * array of EnumerableSet.
  46. * ====
  47. */
  48. `;
  49. // NOTE: this should be deprecated in favor of a more native construction in v6.0
  50. const defaultSet = `\
  51. // To implement this library for multiple types with as little code
  52. // repetition as possible, we write it in terms of a generic Set type with
  53. // bytes32 values.
  54. // The Set implementation uses private functions, and user-facing
  55. // implementations (such as AddressSet) are just wrappers around the
  56. // underlying Set.
  57. // This means that we can only create new EnumerableSets for types that fit
  58. // in bytes32.
  59. struct Set {
  60. // Storage of set values
  61. bytes32[] _values;
  62. // Position is the index of the value in the \`values\` array plus 1.
  63. // Position 0 is used to mean a value is not in the set.
  64. mapping(bytes32 value => uint256) _positions;
  65. }
  66. /**
  67. * @dev Add a value to a set. O(1).
  68. *
  69. * Returns true if the value was added to the set, that is if it was not
  70. * already present.
  71. */
  72. function _add(Set storage set, bytes32 value) private returns (bool) {
  73. if (!_contains(set, value)) {
  74. set._values.push(value);
  75. // The value is stored at length-1, but we add 1 to all indexes
  76. // and use 0 as a sentinel value
  77. set._positions[value] = set._values.length;
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. /**
  84. * @dev Removes a value from a set. O(1).
  85. *
  86. * Returns true if the value was removed from the set, that is if it was
  87. * present.
  88. */
  89. function _remove(Set storage set, bytes32 value) private returns (bool) {
  90. // We cache the value's position to prevent multiple reads from the same storage slot
  91. uint256 position = set._positions[value];
  92. if (position != 0) {
  93. // Equivalent to contains(set, value)
  94. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  95. // the array, and then remove the last element (sometimes called as 'swap and pop').
  96. // This modifies the order of the array, as noted in {at}.
  97. uint256 valueIndex = position - 1;
  98. uint256 lastIndex = set._values.length - 1;
  99. if (valueIndex != lastIndex) {
  100. bytes32 lastValue = set._values[lastIndex];
  101. // Move the lastValue to the index where the value to delete is
  102. set._values[valueIndex] = lastValue;
  103. // Update the tracked position of the lastValue (that was just moved)
  104. set._positions[lastValue] = position;
  105. }
  106. // Delete the slot where the moved value was stored
  107. set._values.pop();
  108. // Delete the tracked position for the deleted slot
  109. delete set._positions[value];
  110. return true;
  111. } else {
  112. return false;
  113. }
  114. }
  115. /**
  116. * @dev Removes all the values from a set. O(n).
  117. *
  118. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  119. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  120. */
  121. function _clear(Set storage set) private {
  122. uint256 len = _length(set);
  123. for (uint256 i = 0; i < len; ++i) {
  124. delete set._positions[set._values[i]];
  125. }
  126. Arrays.unsafeSetLength(set._values, 0);
  127. }
  128. /**
  129. * @dev Returns true if the value is in the set. O(1).
  130. */
  131. function _contains(Set storage set, bytes32 value) private view returns (bool) {
  132. return set._positions[value] != 0;
  133. }
  134. /**
  135. * @dev Returns the number of values on the set. O(1).
  136. */
  137. function _length(Set storage set) private view returns (uint256) {
  138. return set._values.length;
  139. }
  140. /**
  141. * @dev Returns the value stored at position \`index\` in the set. O(1).
  142. *
  143. * Note that there are no guarantees on the ordering of values inside the
  144. * array, and it may change when more values are added or removed.
  145. *
  146. * Requirements:
  147. *
  148. * - \`index\` must be strictly less than {length}.
  149. */
  150. function _at(Set storage set, uint256 index) private view returns (bytes32) {
  151. return set._values[index];
  152. }
  153. /**
  154. * @dev Return the entire set in an array
  155. *
  156. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  157. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  158. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  159. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  160. */
  161. function _values(Set storage set) private view returns (bytes32[] memory) {
  162. return set._values;
  163. }
  164. /**
  165. * @dev Return a slice of the set in an array
  166. *
  167. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  168. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  169. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  170. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  171. */
  172. function _values(Set storage set, uint256 start, uint256 end) private view returns (bytes32[] memory) {
  173. unchecked {
  174. end = Math.min(end, _length(set));
  175. start = Math.min(start, end);
  176. uint256 len = end - start;
  177. bytes32[] memory result = new bytes32[](len);
  178. for (uint256 i = 0; i < len; ++i) {
  179. result[i] = Arrays.unsafeAccess(set._values, start + i).value;
  180. }
  181. return result;
  182. }
  183. }
  184. `;
  185. // NOTE: this should be deprecated in favor of a more native construction in v6.0
  186. const customSet = ({ name, value: { type } }) => `\
  187. // ${name}
  188. struct ${name} {
  189. Set _inner;
  190. }
  191. /**
  192. * @dev Add a value to a set. O(1).
  193. *
  194. * Returns true if the value was added to the set, that is if it was not
  195. * already present.
  196. */
  197. function add(${name} storage set, ${type} value) internal returns (bool) {
  198. return _add(set._inner, ${toBytes32(type, 'value')});
  199. }
  200. /**
  201. * @dev Removes a value from a set. O(1).
  202. *
  203. * Returns true if the value was removed from the set, that is if it was
  204. * present.
  205. */
  206. function remove(${name} storage set, ${type} value) internal returns (bool) {
  207. return _remove(set._inner, ${toBytes32(type, 'value')});
  208. }
  209. /**
  210. * @dev Removes all the values from a set. O(n).
  211. *
  212. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  213. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  214. */
  215. function clear(${name} storage set) internal {
  216. _clear(set._inner);
  217. }
  218. /**
  219. * @dev Returns true if the value is in the set. O(1).
  220. */
  221. function contains(${name} storage set, ${type} value) internal view returns (bool) {
  222. return _contains(set._inner, ${toBytes32(type, 'value')});
  223. }
  224. /**
  225. * @dev Returns the number of values in the set. O(1).
  226. */
  227. function length(${name} storage set) internal view returns (uint256) {
  228. return _length(set._inner);
  229. }
  230. /**
  231. * @dev Returns the value stored at position \`index\` in the set. O(1).
  232. *
  233. * Note that there are no guarantees on the ordering of values inside the
  234. * array, and it may change when more values are added or removed.
  235. *
  236. * Requirements:
  237. *
  238. * - \`index\` must be strictly less than {length}.
  239. */
  240. function at(${name} storage set, uint256 index) internal view returns (${type}) {
  241. return ${fromBytes32(type, '_at(set._inner, index)')};
  242. }
  243. /**
  244. * @dev Return the entire set in an array
  245. *
  246. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  247. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  248. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  249. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  250. */
  251. function values(${name} storage set) internal view returns (${type}[] memory) {
  252. bytes32[] memory store = _values(set._inner);
  253. ${type}[] memory result;
  254. assembly ("memory-safe") {
  255. result := store
  256. }
  257. return result;
  258. }
  259. /**
  260. * @dev Return a slice of the set in an array
  261. *
  262. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  263. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  264. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  265. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  266. */
  267. function values(${name} storage set, uint256 start, uint256 end) internal view returns (${type}[] memory) {
  268. bytes32[] memory store = _values(set._inner, start, end);
  269. ${type}[] memory result;
  270. assembly ("memory-safe") {
  271. result := store
  272. }
  273. return result;
  274. }
  275. `;
  276. const memorySet = ({ name, value }) => `\
  277. struct ${name} {
  278. // Storage of set values
  279. ${value.type}[] _values;
  280. // Position is the index of the value in the \`values\` array plus 1.
  281. // Position 0 is used to mean a value is not in the set.
  282. mapping(${value.type} value => uint256) _positions;
  283. }
  284. /**
  285. * @dev Add a value to a set. O(1).
  286. *
  287. * Returns true if the value was added to the set, that is if it was not
  288. * already present.
  289. */
  290. function add(${name} storage self, ${value.type} memory value) internal returns (bool) {
  291. if (!contains(self, value)) {
  292. self._values.push(value);
  293. // The value is stored at length-1, but we add 1 to all indexes
  294. // and use 0 as a sentinel value
  295. self._positions[value] = self._values.length;
  296. return true;
  297. } else {
  298. return false;
  299. }
  300. }
  301. /**
  302. * @dev Removes a value from a set. O(1).
  303. *
  304. * Returns true if the value was removed from the set, that is if it was
  305. * present.
  306. */
  307. function remove(${name} storage self, ${value.type} memory value) internal returns (bool) {
  308. // We cache the value's position to prevent multiple reads from the same storage slot
  309. uint256 position = self._positions[value];
  310. if (position != 0) {
  311. // Equivalent to contains(self, value)
  312. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  313. // the array, and then remove the last element (sometimes called as 'swap and pop').
  314. // This modifies the order of the array, as noted in {at}.
  315. uint256 valueIndex = position - 1;
  316. uint256 lastIndex = self._values.length - 1;
  317. if (valueIndex != lastIndex) {
  318. ${value.type} memory lastValue = self._values[lastIndex];
  319. // Move the lastValue to the index where the value to delete is
  320. self._values[valueIndex] = lastValue;
  321. // Update the tracked position of the lastValue (that was just moved)
  322. self._positions[lastValue] = position;
  323. }
  324. // Delete the slot where the moved value was stored
  325. self._values.pop();
  326. // Delete the tracked position for the deleted slot
  327. delete self._positions[value];
  328. return true;
  329. } else {
  330. return false;
  331. }
  332. }
  333. /**
  334. * @dev Removes all the values from a set. O(n).
  335. *
  336. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  337. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  338. */
  339. function clear(${name} storage set) internal {
  340. uint256 len = length(set);
  341. for (uint256 i = 0; i < len; ++i) {
  342. delete set._positions[set._values[i]];
  343. }
  344. Arrays.unsafeSetLength(set._values, 0);
  345. }
  346. /**
  347. * @dev Returns true if the value is in the set. O(1).
  348. */
  349. function contains(${name} storage self, ${value.type} memory value) internal view returns (bool) {
  350. return self._positions[value] != 0;
  351. }
  352. /**
  353. * @dev Returns the number of values on the set. O(1).
  354. */
  355. function length(${name} storage self) internal view returns (uint256) {
  356. return self._values.length;
  357. }
  358. /**
  359. * @dev Returns the value stored at position \`index\` in the set. O(1).
  360. *
  361. * Note that there are no guarantees on the ordering of values inside the
  362. * array, and it may change when more values are added or removed.
  363. *
  364. * Requirements:
  365. *
  366. * - \`index\` must be strictly less than {length}.
  367. */
  368. function at(${name} storage self, uint256 index) internal view returns (${value.type} memory) {
  369. return self._values[index];
  370. }
  371. /**
  372. * @dev Return the entire set in an array
  373. *
  374. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  375. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  376. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  377. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  378. */
  379. function values(${name} storage self) internal view returns (${value.type}[] memory) {
  380. return self._values;
  381. }
  382. /**
  383. * @dev Return a slice of the set in an array
  384. *
  385. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  386. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  387. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  388. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  389. */
  390. function values(${name} storage set, uint256 start, uint256 end) internal view returns (${value.type}[] memory) {
  391. unchecked {
  392. end = Math.min(end, length(set));
  393. start = Math.min(start, end);
  394. uint256 len = end - start;
  395. ${value.type}[] memory result = new ${value.type}[](len);
  396. for (uint256 i = 0; i < len; ++i) {
  397. result[i] = Arrays.unsafeAccess(set._values, start + i).value;
  398. }
  399. return result;
  400. }
  401. }
  402. `;
  403. // GENERATE
  404. module.exports = format(
  405. header.trimEnd(),
  406. 'library EnumerableSet {',
  407. format(
  408. [].concat(
  409. defaultSet,
  410. SET_TYPES.filter(({ value }) => !value.memory).map(customSet),
  411. SET_TYPES.filter(({ value }) => value.memory).map(memorySet),
  412. ),
  413. ).trimEnd(),
  414. '}',
  415. );