I have a sorted linked list that contains around 1000 items. When I receive a new item my goal is to push the item to the list keeping the sorted order. So if I receive very small value, I am required to iterate all the list to find the tail of the list.
Constant function's goal is to return the index location for transaction function to push into.
Iteration(iterateListToFindPushIndex()) might look like this:
struct Interval {
uint32 num; //contained value.
uint32 next; //points to next node on the linkedlist.
}
prevNode = self.list[self.head]; //head of the list, largest value.
currentNode = self.list[prevNode.next];
while( true ) {
if( s > currentNode.num ){
//self.list.push(Interval( { num: s, next: prevNode.next }) );
//prevNode.next = uint32(self.list.length - 1);
return prevNode.next;
//returned indexed would be use in transaction function to insert intem into the correct location.
}
prevNode = currentNode;
currentNode = self.list[currentNode.next];
}
As we know when we call a constant function, it does not cost any gas and its free to use. So when I find the index location via a constant function, it won't cost me any gas.
function iterateListToFindPushIndex() constant returns(uint index){}
function func(intervalNode storage self) {
index = iterateListToFindPushIndex();
self.list.push(Interval( { num: s, next: index }) )
}
Inside a transaction function, when I iterate through my list even though I don't change any memory storage, it cost me additional gas.
So is it possible to call a constant function without spending any gas inside a transaction function.
Thank you for your valuable time and help.