Submission e6d57323...

ChallengeRemove duplicate elements
Submitterwicketh.eth
Submitted at2018-51-27
Gas used122069
/**
 * This file is part of the 1st Solidity Gas Golfing Contest.
 *
 * This work is licensed under Creative Commons Attribution ShareAlike 3.0.
 * https://creativecommons.org/licenses/by-sa/3.0/
 */

pragma solidity 0.4.24;

contract Unique {
    
    function () external payable { assembly {
        
        // @author Remco Bloemen <[email protected]>
        mstore(0x40, 0)
        
        let i
        let ptr
        let iv
        let index1
        let last2 := 0
        let last1 := 0
        let vhash

        i := calldataload(36)
        jumpi(main512, gt(i, 128))
        jumpi(main128, gt(i, 1))
        jumpi(main1, gt(i, 0))
        
    main0:
        // Empty list
        mstore(0, 32)
        mstore(32, 0)
        return(0, 64)
        
    main1:
        // Singleton
        mstore(0, 32)
        mstore(32, 1)
        mstore(64, calldataload(68))
        return(0, 96)
        
    main128:
        // Table size 97
        ptr := 0xC60
        i := 68
        
    oloop128:
        // Read value
        vhash := not(calldataload(i))
        
        // Check recent values
        jumpi(seen128, or(eq(vhash, last1), eq(vhash, last2)))
        last2 := last1
        last1 := vhash
        
        // Compute index 1
        index1 := mul(mod(vhash, 97), 32)
        
        // Read index 1
        iv := mload(index1)
        jumpi(unique1128, iszero(iv))
        jumpi(seen128, eq(iv, vhash))
        
    iloop128:
    
        // Increment and test index 1
        index1 := mod(add(index1, 32), 0xC20)
        iv := mload(index1)
        jumpi(unique1128, iszero(iv))
        jumpi(seen128, eq(iv, vhash))
        
        // Increment and test index 1
        index1 := mod(add(index1, 32), 0xC20)
        iv := mload(index1)
        jumpi(unique1128, iszero(iv))
        jumpi(seen128, eq(iv, vhash))
        
        // Increment and test index 1
        index1 := mod(add(index1, 32), 0xC20)
        iv := mload(index1)
        jumpi(unique1128, iszero(iv))
        jumpi(seen128, eq(iv, vhash))
        
        // Loop
        jump(iloop128)
        
    seen128:
        // Resume loop
        i := add(i, 32)
        jumpi(oloop128, lt(i, calldatasize))
        jump(oloop_end128)
        
    unique1128:
        // Add to the hash table
        mstore(index1, vhash)
        
        // Add to start of list
        mstore(ptr, not(vhash))
        ptr := add(ptr, 32)
        
        // Resume loop
        i := add(i, 32)
        jumpi(oloop128, lt(i, calldatasize))
    
    oloop_end128:
        mstore(0xC20, 32)
        mstore(0xC40, div(sub(ptr, 0xC60), 32))
        return(0xC20, sub(ptr, 0xC20))

    main512:
        // Table size 331
        ptr := 0x29A0
        i := 68
        
        // Write first value
        vhash := calldataload(i)
        mstore(ptr, vhash)
        ptr := add(ptr, 32)
        last1 := vhash
        
    repeat5121:
        i := add(i, 32)
        vhash := calldataload(i)
        jumpi(repeat5121, eq(vhash, last1))
        jumpi(oloop_end512, eq(i, calldatasize))
        
        // Write second value
        mstore(ptr, vhash)
        ptr := add(ptr, 32)
        last2 := last1
        last1 := vhash
        
    repeat5122:
        i := add(i, 32)
        vhash := calldataload(i)
        jumpi(repeat5122, or(eq(vhash, last1), eq(vhash, last2)))
        jumpi(oloop_end512, eq(i, calldatasize))
        
        // Write third value
        mstore(ptr, vhash)
        ptr := add(ptr, 32)
        
        // Convert to main loop
        vhash := not(vhash)
        last1 := not(last1)
        last2 := not(last2)
        // Assume no collisions in first three unique values :/
        mstore(mul(mod(vhash, 331), 32), vhash)
        mstore(mul(mod(last1, 331), 32), last1)
        mstore(mul(mod(last2, 331), 32), last2)
        
        i := add(i, 32)
        
    oloop512:
        // Read value
        vhash := not(calldataload(i))
        
        // Compute index 1
        index1 := mul(mod(vhash, 331), 32)
        
        // Read index 1
        iv := mload(index1)
        jumpi(unique1512, iszero(iv))
        jumpi(seen512, eq(iv, vhash))
        
    iloop512:
        // Increment and test index 1
        index1 := mod(add(index1, 32), 0x2960)
        iv := mload(index1)
        jumpi(unique1512, iszero(iv))
        jumpi(seen512, eq(iv, vhash))
        
        // Increment and test index 1
        index1 := mod(add(index1, 32), 0x2960)
        iv := mload(index1)
        jumpi(unique1512, iszero(iv))
        jumpi(seen512, eq(iv, vhash))
        
        // Increment and test index 1
        index1 := mod(add(index1, 32), 0x2960)
        iv := mload(index1)
        jumpi(unique1512, iszero(iv))
        jumpi(seen512, eq(iv, vhash))
        
        // Loop
        jump(iloop512)
        
    seen512:
        // Resume loop
        i := add(i, 32)
        jumpi(oloop512, lt(i, calldatasize))
        jump(oloop_end512)
        
    unique1512:
        // Add to the hash table
        mstore(index1, vhash)
        
        // Add to start of list
        mstore(ptr, not(vhash))
        ptr := add(ptr, 32)
        
        // Resume loop
        i := add(i, 32)
        jumpi(oloop512, lt(i, calldatasize))
    
    oloop_end512:
        mstore(0x2960, 32)
        mstore(0x2980, div(sub(ptr, 0x29A0), 32))
        return(0x2960, sub(ptr, 0x2960))


    }}
    
}