Created
April 21, 2023 19:15
-
-
Save 0xheartcode/3953f7dee1a4eee00e29ad39a1291ac1 to your computer and use it in GitHub Desktop.
This file contains 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
function findPositionInArrayForLoop(uint _item, uint[] memory _array) pure internal returns (uint i) { // requirements should come before. | |
uint[] memory tmpArray = _array; | |
uint arrayLength = tmpArray.length; | |
for (i = 0; i < arrayLength;) { | |
if (tmpArray[i] != _item) { | |
unchecked { | |
i++; | |
} | |
continue; | |
} else { | |
return i; | |
} | |
} | |
} | |
function findPositionInArrayAssembly(uint _item, uint[] calldata _array) pure internal returns (uint tokenPosition) { // i've made this on a whim. My first Yul baby <(.3.)>. Will be used later to reduce code more. | |
assembly { | |
let guard := add(1, calldatasize()) | |
for {let offset := _array.offset} | |
lt(offset, guard) | |
{offset := add(offset, 32)} { | |
if eq(calldataload(offset), _item) { | |
tokenPosition := sub(div(offset, 32), 3) // could potentially comment out the lines below. | |
mstore(0, sub(div(offset, 32), 3)) | |
return(0, 32) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment