Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / .InsertSorted & .Find behavior

Author
Message
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 13th Apr 2023 00:40 Edited at: 13th Apr 2023 01:25
matching a top score (of 3, fewest attempts) in my current project recently yielded intriguing results where i expected the .InsertSorted(Score) to be the first of any that matched but was not:



running some initial tests with .InsertSorted(10) 10 times revealed the following behavior (@ nth time .InsertSorted):
(ascending evens followed by descending odds, then 0)

then, curious as to which index would be returned by .Find always returned 4.

full test code with .Find returning (roughly) "middle -1" behavior:

note the 2 Modes$ with Mode 2 order returning ascending indices presumably because they were .Insert'd in that order.
ie, reversing the .insert, the 2 sets (1 and 2):


point: this was unexpected for me while the docs never did state that .InsertSorted would set it to the top of same values nor does it state that .Find would find the first instance of the value.

the current functionality is still valuable in .find being lightning fast vs our own custom functions but users need to know what the results actually mean.

meanwhile , my desired (formerly anticipated) behavior will need to be accounted for manually. i can see using .find as a jump start then crawling up from there (which i think i do HERE tho saves are currently broken, it seems), for example.

finally, any related issues such as THIS need to be further explored with this new-found insight while feature requests such as .FindNext() that would rely upon the current .InsertSorted/.find behavior would be affected/should be considered.

IndexOf may also be affected?

please post any additional test code that might reveal additional insight.

Attachments

Login to view attachments
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 14th Apr 2023 02:07 Edited at: 14th Apr 2023 02:07
Easily fixed by creating a field at the beginning of the type that is a combination of value/date/time
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 14th Apr 2023 02:12 Edited at: 14th Apr 2023 02:16
yah, easily accounted for; just posting in case others had the same expectations that i've had for the past couple of years...

that AddScore() function is a new one for me where i thought i'd found a simpler way to maintain the top scores than i had in the past while the .remove() was sometimes removing the last instance of the same score and i thought i was misunderstanding .remove().

in the end, i was right (on that) and wrong (on .InsertSorted())
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 14th Apr 2023 02:14
It would be interesting to see what the underlying C++ functions are
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 14th Apr 2023 02:18 Edited at: 14th Apr 2023 02:19
(post above edited around the same time you responded)

i guess it's in here somewhere and i gave up on finding it.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 14th Apr 2023 03:53
Search for AGKI_ARRAY_FIND_STRING and AGKI_ARRAY_FIND in apps\interpreter\AGKCommonSwitch.h
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 14th Apr 2023 05:36 Edited at: 14th Apr 2023 05:36
i dont see how it produces the results i received; now i'm questioning my test code.

then, i wonder if there's some OS-specific issue involved like Round()

doesn't matter, i suppose. if i care enough, i'll code it myself
adambiser
AGK Developer
8
Years of Service
User Offline
Joined: 16th Sep 2015
Location: US
Posted: 14th Apr 2023 07:36 Edited at: 14th Apr 2023 07:40
This is the "find" algorithm: https://github.com/TheGameCreators/AGKTier2/blob/master/apps/interpreter/ProgramData.h#L141

It's a binary search (which would have been a better name for the function):

int high = m_iLength-1;
int low = 0;

starting point is: mid = (high+low)/2;
Your m_iLength is 10, so high = 9, low = 0, It starts checking at index (9-0)/2 = 4 (int division)

For insertsorted, every entry has the same key, so it's inserting in the middle of the list each time
Insert 0 when length = 0, so index 0 => 0
Insert 1 when length = 1, mid = 0 / 2, so index 0 => 1, 0
Insert 2 when length = 2, mid = 1 / 2, so index 0 => 2, 1, 0
Insert 3 when length = 3, mid = 2 / 2, so index 1 => 2, 3, 1, 0
Insert 4 when length = 4, mid = 3 / 2, so index 1 => 2, 4, 3, 1, 0
Insert 5 when length = 5, mid = 4 / 2, so index 2 => 2, 4, 5, 3, 1, 0
etc

Login to post a reply

Server time is: 2024-05-04 11:06:22
Your offset time is: 2024-05-04 11:06:22