Re: Standard library praise and challenge
- Posted by Insolor Mar 01, 2012
- 2097 views
Today I looked inside std/sequence.e and have seen this:
public function add_item(object needle, sequence haystack, integer pOrder = 1) if find(needle, haystack) then return haystack end if switch pOrder do case ADD_PREPEND then return prepend(haystack, needle) case ADD_APPEND then return append(haystack, needle) case ADD_SORT_UP then return stdsort:sort(append(haystack, needle)) case ADD_SORT_DOWN then return stdsort:sort(append(haystack, needle), stdsort:DESCENDING) case else error:crash("sequence.e:add_item() invalid Order argument '%d'", pOrder) end switch return haystack end function
Why not to replace this
stdsort:sort(append(haystack, needle))
with some sort of binary insertion function? This approach would be is less reliable (eg. function will not know if the sequence have a proper order, presorting will be up to a user), but it would be much faster on long sequences.
P.S. something like this:
eu:insert(haystack,needle,math:abs(search:binary_search(needle,haystack)))