EnumerableSet.js 13 KB

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