3

I've got a quite complex contract with two arrays:

struct TransactionsLink {
    uint transactionId;
    uint creationTime;
    uint amount;
}

mapping( address => TransactionsLink[]) public transactionsFinder;


struct BillLink {
    uint billId;
    uint creationTime;
    string name;
}   
mapping( address => ResearchBillLink[]) public billsFinder;


function getBillsIndexBetweenTimes(uint _startTimestamp, uint _endTimestamp) constant returns(uint startIdx, uint endIdx){ if (billsFinder[0].creationTime) .... }
function getTransactionIndexBetweenTimes(uint _startTimestamp, uint _endTimestamp) constant returns(uint startIdx, uint endIdx){ if (transactionsFinder[0].creationTime) .... }

And I've developed a binary search algorithm inside the contract in order to get bills/transactions for someone from those arrays. However my code is quite big (range search) and I would like to reuse it for both arrays (in fact I've got 4).

Is there a way in which I can reuse my function without pasting all the code twice?

function getArrIndexesBetweenTimes(GenericStruct[] arr, uint _startTimestamp, uint _endTimestamp) constant returns(uint startIdx, uint endIdx){ if (transactionsFinder[0].creationTime) .... }
function getBillsIndexBetweenTimes(uint _startTimestamp, uint _endTimestamp) constant returns(uint startIdx, uint endIdx){return getArrIndexesBetweenTimes(billsFinder)}
function getTransactionIndexBetweenTimes(uint _startTimestamp, uint _endTimestamp) constant returns(uint startIdx, uint endIdx){ return getArrIndexesBetweenTimes(transactionsFinder) }

I would like to be able to do the second, but structs do not seems to support inheritance, I could try playing with how structs are mapped in memory and placing creationTime as first parameter, but I'm not sure that would be totally safe...

Thanks,

1

*I came to a temporary workaround not to duplicate a too much code, however any other suggestion would be great because its quite dirty and non-scalable :)

function getCreationTime(uint arrayType) private returns (uint ret) {
    if (arrayType==BFINDER_CONST) {
            ret=billsFinder[clientAddr].length;
    } else if (arrayType==TFINDER_CONST) {
            ret=transactionsFinder[clientAddr].length;
    }
    return ret;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.