EnumerableSet.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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: This function has an unbounded cost that scales with set size. Developers should keep in mind that
  119. * using it may render the function uncallable if the set grows to the point where clearing it consumes too much
  120. * gas to fit in a block.
  121. */
  122. function _clear(Set storage set) private {
  123. uint256 len = _length(set);
  124. for (uint256 i = 0; i < len; ++i) {
  125. delete set._positions[set._values[i]];
  126. }
  127. Arrays.unsafeSetLength(set._values, 0);
  128. }
  129. /**
  130. * @dev Returns true if the value is in the set. O(1).
  131. */
  132. function _contains(Set storage set, bytes32 value) private view returns (bool) {
  133. return set._positions[value] != 0;
  134. }
  135. /**
  136. * @dev Returns the number of values on the set. O(1).
  137. */
  138. function _length(Set storage set) private view returns (uint256) {
  139. return set._values.length;
  140. }
  141. /**
  142. * @dev Returns the value stored at position \`index\` in the set. O(1).
  143. *
  144. * Note that there are no guarantees on the ordering of values inside the
  145. * array, and it may change when more values are added or removed.
  146. *
  147. * Requirements:
  148. *
  149. * - \`index\` must be strictly less than {length}.
  150. */
  151. function _at(Set storage set, uint256 index) private view returns (bytes32) {
  152. return set._values[index];
  153. }
  154. /**
  155. * @dev Return the entire set in an array
  156. *
  157. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  158. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  159. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  160. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  161. */
  162. function _values(Set storage set) private view returns (bytes32[] memory) {
  163. return set._values;
  164. }
  165. /**
  166. * @dev Return a slice of the set in an array
  167. *
  168. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  169. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  170. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  171. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  172. */
  173. function _values(Set storage set, uint256 start, uint256 end) private view returns (bytes32[] memory) {
  174. unchecked {
  175. end = Math.min(end, _length(set));
  176. start = Math.min(start, end);
  177. uint256 len = end - start;
  178. bytes32[] memory result = new bytes32[](len);
  179. for (uint256 i = 0; i < len; ++i) {
  180. result[i] = Arrays.unsafeAccess(set._values, start + i).value;
  181. }
  182. return result;
  183. }
  184. }
  185. `;
  186. // NOTE: this should be deprecated in favor of a more native construction in v6.0
  187. const customSet = ({ name, value: { type } }) => `\
  188. // ${name}
  189. struct ${name} {
  190. Set _inner;
  191. }
  192. /**
  193. * @dev Add a value to a set. O(1).
  194. *
  195. * Returns true if the value was added to the set, that is if it was not
  196. * already present.
  197. */
  198. function add(${name} storage set, ${type} value) internal returns (bool) {
  199. return _add(set._inner, ${toBytes32(type, 'value')});
  200. }
  201. /**
  202. * @dev Removes a value from a set. O(1).
  203. *
  204. * Returns true if the value was removed from the set, that is if it was
  205. * present.
  206. */
  207. function remove(${name} storage set, ${type} value) internal returns (bool) {
  208. return _remove(set._inner, ${toBytes32(type, 'value')});
  209. }
  210. /**
  211. * @dev Removes all the values from a set. O(n).
  212. *
  213. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  214. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  215. */
  216. function clear(${name} storage set) internal {
  217. _clear(set._inner);
  218. }
  219. /**
  220. * @dev Returns true if the value is in the set. O(1).
  221. */
  222. function contains(${name} storage set, ${type} value) internal view returns (bool) {
  223. return _contains(set._inner, ${toBytes32(type, 'value')});
  224. }
  225. /**
  226. * @dev Returns the number of values in the set. O(1).
  227. */
  228. function length(${name} storage set) internal view returns (uint256) {
  229. return _length(set._inner);
  230. }
  231. /**
  232. * @dev Returns the value stored at position \`index\` in the set. O(1).
  233. *
  234. * Note that there are no guarantees on the ordering of values inside the
  235. * array, and it may change when more values are added or removed.
  236. *
  237. * Requirements:
  238. *
  239. * - \`index\` must be strictly less than {length}.
  240. */
  241. function at(${name} storage set, uint256 index) internal view returns (${type}) {
  242. return ${fromBytes32(type, '_at(set._inner, index)')};
  243. }
  244. /**
  245. * @dev Return the entire set in an array
  246. *
  247. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  248. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  249. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  250. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  251. */
  252. function values(${name} storage set) internal view returns (${type}[] memory) {
  253. bytes32[] memory store = _values(set._inner);
  254. ${type}[] memory result;
  255. assembly ("memory-safe") {
  256. result := store
  257. }
  258. return result;
  259. }
  260. /**
  261. * @dev Return a slice of the set in an array
  262. *
  263. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  264. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  265. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  266. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  267. */
  268. function values(${name} storage set, uint256 start, uint256 end) internal view returns (${type}[] memory) {
  269. bytes32[] memory store = _values(set._inner, start, end);
  270. ${type}[] memory result;
  271. assembly ("memory-safe") {
  272. result := store
  273. }
  274. return result;
  275. }
  276. `;
  277. const memorySet = ({ name, value }) => `\
  278. struct ${name} {
  279. // Storage of set values
  280. ${value.type}[] _values;
  281. // Position is the index of the value in the \`values\` array plus 1.
  282. // Position 0 is used to mean a value is not in the set.
  283. mapping(${value.type} value => uint256) _positions;
  284. }
  285. /**
  286. * @dev Add a value to a set. O(1).
  287. *
  288. * Returns true if the value was added to the set, that is if it was not
  289. * already present.
  290. */
  291. function add(${name} storage set, ${value.type} memory value) internal returns (bool) {
  292. if (!contains(set, value)) {
  293. set._values.push(value);
  294. // The value is stored at length-1, but we add 1 to all indexes
  295. // and use 0 as a sentinel value
  296. set._positions[value] = set._values.length;
  297. return true;
  298. } else {
  299. return false;
  300. }
  301. }
  302. /**
  303. * @dev Removes a value from a set. O(1).
  304. *
  305. * Returns true if the value was removed from the set, that is if it was
  306. * present.
  307. */
  308. function remove(${name} storage set, ${value.type} memory value) internal returns (bool) {
  309. // We cache the value's position to prevent multiple reads from the same storage slot
  310. uint256 position = set._positions[value];
  311. if (position != 0) {
  312. // Equivalent to contains(set, value)
  313. // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
  314. // the array, and then remove the last element (sometimes called as 'swap and pop').
  315. // This modifies the order of the array, as noted in {at}.
  316. uint256 valueIndex = position - 1;
  317. uint256 lastIndex = set._values.length - 1;
  318. if (valueIndex != lastIndex) {
  319. ${value.type} memory lastValue = set._values[lastIndex];
  320. // Move the lastValue to the index where the value to delete is
  321. set._values[valueIndex] = lastValue;
  322. // Update the tracked position of the lastValue (that was just moved)
  323. set._positions[lastValue] = position;
  324. }
  325. // Delete the slot where the moved value was stored
  326. set._values.pop();
  327. // Delete the tracked position for the deleted slot
  328. delete set._positions[value];
  329. return true;
  330. } else {
  331. return false;
  332. }
  333. }
  334. /**
  335. * @dev Removes all the values from a set. O(n).
  336. *
  337. * WARNING: Developers should keep in mind that this function has an unbounded cost and using it may render the
  338. * function uncallable if the set grows to the point where clearing it consumes too much gas to fit in a block.
  339. */
  340. function clear(${name} storage set) internal {
  341. uint256 len = length(set);
  342. for (uint256 i = 0; i < len; ++i) {
  343. delete set._positions[set._values[i]];
  344. }
  345. Arrays.unsafeSetLength(set._values, 0);
  346. }
  347. /**
  348. * @dev Returns true if the value is in the set. O(1).
  349. */
  350. function contains(${name} storage set, ${value.type} memory value) internal view returns (bool) {
  351. return set._positions[value] != 0;
  352. }
  353. /**
  354. * @dev Returns the number of values on the set. O(1).
  355. */
  356. function length(${name} storage set) internal view returns (uint256) {
  357. return set._values.length;
  358. }
  359. /**
  360. * @dev Returns the value stored at position \`index\` in the set. O(1).
  361. *
  362. * Note that there are no guarantees on the ordering of values inside the
  363. * array, and it may change when more values are added or removed.
  364. *
  365. * Requirements:
  366. *
  367. * - \`index\` must be strictly less than {length}.
  368. */
  369. function at(${name} storage set, uint256 index) internal view returns (${value.type} memory) {
  370. return set._values[index];
  371. }
  372. /**
  373. * @dev Return the entire set in an array
  374. *
  375. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  376. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  377. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  378. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  379. */
  380. function values(${name} storage set) internal view returns (${value.type}[] memory) {
  381. return set._values;
  382. }
  383. /**
  384. * @dev Return a slice of the set in an array
  385. *
  386. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  387. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  388. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  389. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  390. */
  391. function values(${name} storage set, uint256 start, uint256 end) internal view returns (${value.type}[] memory) {
  392. unchecked {
  393. end = Math.min(end, length(set));
  394. start = Math.min(start, end);
  395. uint256 len = end - start;
  396. ${value.type}[] memory result = new ${value.type}[](len);
  397. for (uint256 i = 0; i < len; ++i) {
  398. result[i] = Arrays.unsafeAccess(set._values, start + i).value;
  399. }
  400. return result;
  401. }
  402. }
  403. `;
  404. // GENERATE
  405. module.exports = format(
  406. header.trimEnd(),
  407. 'library EnumerableSet {',
  408. format(
  409. [].concat(
  410. defaultSet,
  411. SET_TYPES.filter(({ value }) => !value.memory).map(customSet),
  412. SET_TYPES.filter(({ value }) => value.memory).map(memorySet),
  413. ),
  414. ).trimEnd(),
  415. '}',
  416. );