12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v4.8.0-rc.0) (utils/structs/BitMaps.sol)
- pragma solidity ^0.8.0;
- /**
- * @dev Library for managing uint256 to bool mapping in a compact and efficient way, providing the keys are sequential.
- * Largely inspired by Uniswap's https://github.com/Uniswap/merkle-distributor/blob/master/contracts/MerkleDistributor.sol[merkle-distributor].
- */
- library BitMaps {
- struct BitMap {
- mapping(uint256 => uint256) _data;
- }
- /**
- * @dev Returns whether the bit at `index` is set.
- */
- function get(BitMap storage bitmap, uint256 index) internal view returns (bool) {
- uint256 bucket = index >> 8;
- uint256 mask = 1 << (index & 0xff);
- return bitmap._data[bucket] & mask != 0;
- }
- /**
- * @dev Sets the bit at `index` to the boolean `value`.
- */
- function setTo(
- BitMap storage bitmap,
- uint256 index,
- bool value
- ) internal {
- if (value) {
- set(bitmap, index);
- } else {
- unset(bitmap, index);
- }
- }
- /**
- * @dev Sets the bit at `index`.
- */
- function set(BitMap storage bitmap, uint256 index) internal {
- uint256 bucket = index >> 8;
- uint256 mask = 1 << (index & 0xff);
- bitmap._data[bucket] |= mask;
- }
- /**
- * @dev Unsets the bit at `index`.
- */
- function unset(BitMap storage bitmap, uint256 index) internal {
- uint256 bucket = index >> 8;
- uint256 mask = 1 << (index & 0xff);
- bitmap._data[bucket] &= ~mask;
- }
- }
|