[Gambas-user] New feature in the Array classes

Bruce Steers bsteers4 at gmail.com
Tue Feb 9 21:01:41 CET 2021


On Tue, 9 Feb 2021 at 19:56, Benoît Minisini <g4mba5 at gmail.com> wrote:

> Le 09/02/2021 à 20:51, Bruce Steers a écrit :
> >
> > On Tue, 9 Feb 2021 at 19:37, Benoît Minisini <g4mba5 at gmail.com
> > <mailto:g4mba5 at gmail.com>> wrote:
> >
> >     Hi,
> >
> >     Following the recent optimizations on the array classes (that
> >     apparently
> >     made array access about 15% faster), I added a new property named
> >     "Sorted" to the array classes.
> >
> >     If you set that property to TRUE, it will tell the interpreter that
> the
> >     array is sorted.
> >
> >     Then, the Find() and Exist() method will use a faster binary search
> >     algorithm instead of a sequential one.
> >
> >     But it's up to you to maintain the array sorted. If Array.Sorted is
> >     TRUE, and the array is not actually sorted, the result of Find() and
> >     Exist() will be false.
> >
> >     If Find() does not find the element, instead of returning -1, it will
> >     return a negative value indicating where the unfound element must be
> >     inserted in the array to keep it sorted.
> >
> >     If Find() returns the value -N, then the unfound element must be
> >     inserted at position is (N - 1).
> >
> >     Enjoy!
> >
> >
> > Thank you :)
> >
> > Does Array.Add() support negative values?
>
> Of course not. Reread the last lines of my mail.
>
> > A quick...
> > *myArray.Add(sText , myArray.Find(sText))*
> > to keep things sorted.
> > or might a new
> > *myArray.AddSorted(sText)* command be possible for us?
>
> You usually first search for an element, and then insert it if it does
> not exist. So I find cumbersome to add a new Add() method in each array
> class for that.
>

Aaah yes of course.

I was just looking for the easiest way to implement a "must be" sorted
array utilising the new Find() return value.

Many thanks
BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210209/b5d2c900/attachment.htm>


More information about the User mailing list