Jump to content

[Suggestion] New scriptcommand: search_array


Recommended Posts

Posted

Can be interesting but I see some problems with the code.

First, maybe you should return SCRIPT_CMD_FAILURE instead of 1 (value subject to change ?)

Secondly the command does not return any value if there is no elements in the array. Should at least return the default not found value.

Third, I think (personal through) the default value if not found should be -1 instead of 128.

Posted

Thanks ^^

 

I changed the code above. I really like your solution returning -1 instead of SCRIPT_MAX_ARRAYSIZE :>

Posted

Do you think it'd be more useful to have an array sorting command instead? I imagine most applications of a search being repeated multiple times, so it'd be more effective to search through sorted arrays. Opinions?

Posted (edited)

Do you think it'd be more useful to have an array sorting command instead?

merge sort algorithm !

http://www.eathena.ws/board/index.php?s=&showtopic=180080&view=findpost&p=1293102

until today I still haven't crack @KeyWorld's merge sort algorithm <3

 

I imagine most applications of a search being repeated multiple times, so it'd be more effective to search through sorted arrays. Opinions?

no ... actually there is a problem ...

when search through an index, the script usually already has 2 or more array being called

example like

getinventorylist

@inventorylist_id[] ...

@inventorylist_amount[]

if you want to sort @inventorylist_id array, the @inventorylist_amount array needs to sort as well

I use sorting algorithm only once in this topic -> http://rathena.org/board/topic/72989-capture-the-flag-woe-script/?p=150735

which I sort the maximum points, along with the guild ID that has the maximum points

the script announce the getguildnname( guild ID ), not the maximum points ... not the array the being sorted

@Jey

why not just use ARR_FIND ?

https://github.com/rathena/rathena/blob/master/src/common/db.h#L896

can probably shorten the code by one-third

erm ... maybe its not compatible to use source command in a script command ..

Edited by AnnieRuru
  • 1 month later...
Posted (edited)

Do you think it'd be more useful to have an array sorting command instead? I imagine most applications of a search being repeated multiple times, so it'd be more effective to search through sorted arrays. Opinions?

Yes, it is more effective.

But first as AnnieRuru mentioned some arrays shouldn't be sorted.

And SCRIPT_MAX_ARRAYSIZE is 128. So the very most arrays in rathena have less than 100 elements.

So it should not be that critical to walk through all elements. O(n)

If you need to make sure, that the array is sorted (quicksort - O(n log n)) and search through it (binary search O(log n)) it could be less efficient (if the array changes often) and it would be very complex.

So I would recommend as a first step just a single array_search command.

If someone really need that little speed boost he's free to make another suggestion xD

But I don't think that scripts will become appreciable quicker by that script command.

I didn't tested it yet ^^

I use array_search espacially for cleaner coding. For instance in my Warper to check if MapUserCount should be deactivated on the specific map.

Or in my Disguise Event to check if the monster is on the black list... and so on.

It justs saves a loop and an if statement :P

 

ps: The Scriptcommand works fine for more than 1 month ^^

Edited by Jey

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...