Alternative DynArray Find() function

Moderator: GZDoom Developers

Post Reply
User avatar
Major Cooke
Posts: 8175
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Alternative DynArray Find() function

Post by Major Cooke »

I suggest a different find function, such as Find2 or Search, which returns -1 instead of the array's size.

Code: Select all

int s = LargeKids.Find(who);
if (LargeKids.Size() > 0 && s < LargeKids.Size())
It'd allow reduction of this code to just this:

Code: Select all

if (LargeKids.Search(who) >= 0)
_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: Alternative DynArray Find() function

Post by _mental_ »

Why don't you write it like this?

Code: Select all

if (LargeKids.Find(who) != LargeKids.Size())
User avatar
Marisa the Magician
Posts: 3886
Joined: Fri Feb 08, 2008 9:15 am
Preferred Pronouns: She/Her
Operating System Version (Optional): (btw I use) Arch
Graphics Processor: nVidia with Vulkan support
Location: Vigo, Galicia
Contact:

Re: Alternative DynArray Find() function

Post by Marisa the Magician »

Because it's one function call less?
Blue Shadow
Posts: 4949
Joined: Sun Nov 14, 2010 12:59 am

Re: Alternative DynArray Find() function

Post by Blue Shadow »

This just feels more like laziness if you can't be bothered to do one extra function call.
User avatar
Xaser
 
 
Posts: 10772
Joined: Sun Jul 20, 2003 12:15 pm
Contact:

Re: Alternative DynArray Find() function

Post by Xaser »

Find() returns Size()? My first reaction to that is "wait WTF?"

-1 would be preferable if we had a time macine, but at this point the damage has been done because adding a Find2 would just change the gotcha from "remember to compare against Size()" to "remember to use Find2()" -- i.e. it's not any less confusing.
User avatar
Major Cooke
Posts: 8175
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Alternative DynArray Find() function

Post by Major Cooke »

Blue Shadow wrote:This just feels more like laziness if you can't be bothered to do one extra function call.
It's not laziness. It's a massive WTF factor like Xaser said.

And I use this on tons of arrays. The amount of overhead made can be quite ridiculous.
_mental_ wrote:Why don't you write it like this?

Code: Select all

if (LargeKids.Find(who) != LargeKids.Size())
When I did long ago, it had a VM abort. I see it's been fixed since, when performed on empty arrays.

Still two function calls that can be reduced down to one though, IMO.
Last edited by Major Cooke on Tue Mar 27, 2018 11:42 am, edited 1 time in total.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Alternative DynArray Find() function

Post by Graf Zahl »

Marisa Kirisame wrote:Because it's one function call less?

Size() is no function call, it's an intrinsic.
User avatar
Major Cooke
Posts: 8175
Joined: Sun Jan 28, 2007 3:55 pm
Preferred Pronouns: He/Him
Location: QZDoom Maintenance Team

Re: Alternative DynArray Find() function

Post by Major Cooke »

OH, that's right... I forgot about that. Which means its actually faster than a function call isn't it?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Alternative DynArray Find() function

Post by Graf Zahl »

Depending on where the array comes from it's like accessing a member variable in a class or reading a field from a local struct.

I have no idea why Randi implemented it this way. Had I written this function it'd have returned UINT_MAX, which is a lot less problematic to check.
User avatar
Marisa the Magician
Posts: 3886
Joined: Fri Feb 08, 2008 9:15 am
Preferred Pronouns: She/Her
Operating System Version (Optional): (btw I use) Arch
Graphics Processor: nVidia with Vulkan support
Location: Vigo, Galicia
Contact:

Re: Alternative DynArray Find() function

Post by Marisa the Magician »

But isn't UINT_MAX pretty much equal to -1 for signed int? :P
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Alternative DynArray Find() function

Post by Graf Zahl »

Depends on what compiler you use.
Ask the C standards guys and they tell you it's undefined.
Post Reply

Return to “Closed Feature Suggestions [GZDoom]”