-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Add a BitMap struct #2710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add a BitMap struct #2710
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
69e7005
Add a BitMap struct
Amxx 4df1b0e
improve bitmap testing with consecutive keys
Amxx 34dca58
minor clarity and gas optimization
Amxx b43bbef
rename Bitmap.UintBitMap to Bitmaps.Bitmap
Amxx 65c6296
add changelog and document entries
Amxx 22ea07c
cleanup BitMaps code
Amxx 76c3676
Apply suggestions from code review
Amxx 1a3aee9
Merge branch 'master' into feature/structs/bitmap
Amxx e70b191
add a BitMap.setTo function
Amxx 7ac884e
fix lint
Amxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "../utils/structs/BitMaps.sol"; | ||
|
||
contract BitMapMock { | ||
using BitMaps for BitMaps.BitMap; | ||
|
||
BitMaps.BitMap private _bitmap; | ||
|
||
function get(uint256 index) public view returns (bool) { | ||
return _bitmap.get(index); | ||
} | ||
|
||
function setTo(uint256 index, bool value) public { | ||
_bitmap.setTo(index, value); | ||
} | ||
|
||
function set(uint256 index) public { | ||
_bitmap.set(index); | ||
} | ||
|
||
function unset(uint256 index) public { | ||
_bitmap.unset(index); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @dev Library for managing uint256 to bool mapping in a compact and efficient way, providing the keys are sequential. | ||
* Largelly 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 / 256; | ||
uint256 mask = 1 << (index % 256); | ||
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 { | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint256 bucket = index / 256; | ||
uint256 mask = 1 << (index % 256); | ||
bitmap._data[bucket] |= mask; | ||
} | ||
|
||
/** | ||
* @dev Unsets the bit at `index`. | ||
*/ | ||
function unset(BitMap storage bitmap, uint256 index) internal { | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint256 bucket = index / 256; | ||
uint256 mask = 1 << (index % 256); | ||
bitmap._data[bucket] &= ~mask; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
const { BN } = require('@openzeppelin/test-helpers'); | ||
const { expect } = require('chai'); | ||
|
||
const BitMap = artifacts.require('BitMapMock'); | ||
|
||
contract('BitMap', function (accounts) { | ||
const keyA = new BN('7891'); | ||
const keyB = new BN('451'); | ||
const keyC = new BN('9592328'); | ||
|
||
beforeEach(async function () { | ||
this.bitmap = await BitMap.new(); | ||
}); | ||
|
||
it('starts empty', async function () { | ||
expect(await this.bitmap.get(keyA)).to.equal(false); | ||
expect(await this.bitmap.get(keyB)).to.equal(false); | ||
expect(await this.bitmap.get(keyC)).to.equal(false); | ||
}); | ||
|
||
describe('setTo', function () { | ||
it('set a key to true', async function () { | ||
await this.bitmap.setTo(keyA, true); | ||
expect(await this.bitmap.get(keyA)).to.equal(true); | ||
expect(await this.bitmap.get(keyB)).to.equal(false); | ||
expect(await this.bitmap.get(keyC)).to.equal(false); | ||
}); | ||
|
||
it('set a key to false', async function () { | ||
await this.bitmap.setTo(keyA, true); | ||
await this.bitmap.setTo(keyA, false); | ||
expect(await this.bitmap.get(keyA)).to.equal(false); | ||
expect(await this.bitmap.get(keyB)).to.equal(false); | ||
expect(await this.bitmap.get(keyC)).to.equal(false); | ||
}); | ||
|
||
it('set several consecutive keys', async function () { | ||
await this.bitmap.setTo(keyA.addn(0), true); | ||
await this.bitmap.setTo(keyA.addn(1), true); | ||
await this.bitmap.setTo(keyA.addn(2), true); | ||
await this.bitmap.setTo(keyA.addn(3), true); | ||
await this.bitmap.setTo(keyA.addn(4), true); | ||
await this.bitmap.setTo(keyA.addn(2), false); | ||
await this.bitmap.setTo(keyA.addn(4), false); | ||
expect(await this.bitmap.get(keyA.addn(0))).to.equal(true); | ||
expect(await this.bitmap.get(keyA.addn(1))).to.equal(true); | ||
expect(await this.bitmap.get(keyA.addn(2))).to.equal(false); | ||
expect(await this.bitmap.get(keyA.addn(3))).to.equal(true); | ||
expect(await this.bitmap.get(keyA.addn(4))).to.equal(false); | ||
}); | ||
}); | ||
|
||
describe('set', function () { | ||
it('adds a key', async function () { | ||
await this.bitmap.set(keyA); | ||
expect(await this.bitmap.get(keyA)).to.equal(true); | ||
expect(await this.bitmap.get(keyB)).to.equal(false); | ||
expect(await this.bitmap.get(keyC)).to.equal(false); | ||
}); | ||
|
||
it('adds several keys', async function () { | ||
await this.bitmap.set(keyA); | ||
await this.bitmap.set(keyB); | ||
expect(await this.bitmap.get(keyA)).to.equal(true); | ||
expect(await this.bitmap.get(keyB)).to.equal(true); | ||
expect(await this.bitmap.get(keyC)).to.equal(false); | ||
}); | ||
|
||
it('adds several consecutive keys', async function () { | ||
await this.bitmap.set(keyA.addn(0)); | ||
await this.bitmap.set(keyA.addn(1)); | ||
await this.bitmap.set(keyA.addn(3)); | ||
expect(await this.bitmap.get(keyA.addn(0))).to.equal(true); | ||
expect(await this.bitmap.get(keyA.addn(1))).to.equal(true); | ||
expect(await this.bitmap.get(keyA.addn(2))).to.equal(false); | ||
expect(await this.bitmap.get(keyA.addn(3))).to.equal(true); | ||
expect(await this.bitmap.get(keyA.addn(4))).to.equal(false); | ||
}); | ||
}); | ||
|
||
describe('unset', function () { | ||
it('removes added keys', async function () { | ||
await this.bitmap.set(keyA); | ||
await this.bitmap.set(keyB); | ||
await this.bitmap.unset(keyA); | ||
expect(await this.bitmap.get(keyA)).to.equal(false); | ||
expect(await this.bitmap.get(keyB)).to.equal(true); | ||
expect(await this.bitmap.get(keyC)).to.equal(false); | ||
}); | ||
|
||
it('removes consecutive added keys', async function () { | ||
await this.bitmap.set(keyA.addn(0)); | ||
await this.bitmap.set(keyA.addn(1)); | ||
await this.bitmap.set(keyA.addn(3)); | ||
await this.bitmap.unset(keyA.addn(1)); | ||
expect(await this.bitmap.get(keyA.addn(0))).to.equal(true); | ||
expect(await this.bitmap.get(keyA.addn(1))).to.equal(false); | ||
expect(await this.bitmap.get(keyA.addn(2))).to.equal(false); | ||
expect(await this.bitmap.get(keyA.addn(3))).to.equal(true); | ||
expect(await this.bitmap.get(keyA.addn(4))).to.equal(false); | ||
}); | ||
|
||
it('adds and removes multiple keys', async function () { | ||
// [] | ||
|
||
await this.bitmap.set(keyA); | ||
await this.bitmap.set(keyC); | ||
|
||
// [A, C] | ||
|
||
await this.bitmap.unset(keyA); | ||
await this.bitmap.unset(keyB); | ||
|
||
// [C] | ||
|
||
await this.bitmap.set(keyB); | ||
|
||
// [C, B] | ||
|
||
await this.bitmap.set(keyA); | ||
await this.bitmap.unset(keyC); | ||
|
||
// [A, B] | ||
|
||
await this.bitmap.set(keyA); | ||
await this.bitmap.set(keyB); | ||
|
||
// [A, B] | ||
|
||
await this.bitmap.set(keyC); | ||
await this.bitmap.unset(keyA); | ||
|
||
// [B, C] | ||
|
||
await this.bitmap.set(keyA); | ||
await this.bitmap.unset(keyB); | ||
|
||
// [A, C] | ||
|
||
expect(await this.bitmap.get(keyA)).to.equal(true); | ||
expect(await this.bitmap.get(keyB)).to.equal(false); | ||
expect(await this.bitmap.get(keyC)).to.equal(true); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.