[Gambas-user] Accessing the nth item in a collection

B Bruen bbruen at ...2308...
Thu Jun 5 04:55:34 CEST 2014


Thanks to all for their interest an input here.  At the very least it has helped me to crystallize what my problem is.

I may have over generalised my problem. So I'll try to explain.

The issue is, as some have realised, the temporal order in which things are added to the collection, not some order imposed by information represented by the key or some other value of the object. Primarily, I need to find out what is the key of the last item added to the collection. Secondly, there is sometimes a need to find out what is the key of the first item that was added to the collection since the last time it had zero members.  I don't really (yet) have a need to find the key of the nth item that was added, but I can foresee given the current goals of the project that this need will come up in the future. I'll just talk about the primary goal here.

A semi-optimal solution to my particular situation would be if the collection were to raise an "Added(Key As String)" event when a new item was added.  It is only semi-optimal because that would mean processing would be invoked for each and every addition to the set.

What I have at the moment is an asynchronous "process" (actually it is a Timer event handler) that uses For Each to find the last added  item. Now, the comments that the For Each iteration in insertion order is not necessarily to be trusted are exactly the reason I started this thread. Within the "time quantum" defined by the Timer, zero one or more items could have been added.  It is only necessary for me to process the last added item, hence that is why the event driven idea above is only semi optimal.

Again, the fact that this is a collection and not an array is (unfortunately) a "given".  It is a collection and there is no reasonably economical solution, it would involve re-writing an application suite of over three years generation.

Bruce


On Wed, 4 Jun 2014 11:13:13 -0700
Randall Morgan <rmorgan62 at ...626...> wrote:

> In some languages, FOR EACH doesn't guaranty the order of the items
> processed. It only guaranties you will process each item in the collection.
> I think it is a bad habit to use For Each when order of processing is
> important. Unless you are using an array or other data structure that is
> known to maintain order. A linked list or array would be a better choice. I
> haven't followed all this thread but I am sure that has already been
> suggested... The other option is to implement your own ordered collection.
> But wouldn't an array of object be easier?
> 
> 
> On Wed, Jun 4, 2014 at 7:51 AM, Jussi Lahtinen <jussi.lahtinen at ...626...>
> wrote:
> 
> > OK, thanks for the answer! But can you elaborate bit further? If the
> > insertion order is no longer kept, then what dictates the order "For Each"
> > will enumerate the items? Order of memory addresses?
> >
> > I mean would the enumeration order be unchanging, but when new item is
> > added it would be inserted in random (practically from user perspective)
> > position of the enumeration?
> >
> >
> > Jussi
> >
> >
> > On Wed, Jun 4, 2014 at 3:51 PM, Benoît Minisini <
> > gambas at ...1...> wrote:
> >
> > > Le 04/06/2014 14:44, Jussi Lahtinen a écrit :
> > > >> AFAIK, Collection is a hash table and of course, you cannot assume a
> > > >> particular order of keys in a hash table.
> > > >>
> > > >
> > > > That is my understanding also. But they always seems to be in order
> > they
> > > > have been added, when you iterate them with "For Each ...".
> > > >
> > > > What would cause the disorder?
> > > >
> > >
> > > There are in order by design (the hash table elements are linked both by
> > > hash slot *and* insertion order).
> > >
> > > But I don't guarantee that this non-official feature won't be removed.
> > >
> > > Regards,
> > >
> > > --
> > > Benoît Minisini
> > >
> > >
> > >
> > ------------------------------------------------------------------------------
> > > Learn Graph Databases - Download FREE O'Reilly Book
> > > "Graph Databases" is the definitive new guide to graph databases and
> > their
> > > applications. Written by three acclaimed leaders in the field,
> > > this first edition is now available. Download your free book today!
> > > http://p.sf.net/sfu/NeoTech
> > > _______________________________________________
> > > Gambas-user mailing list
> > > Gambas-user at lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> > >
> >
> > ------------------------------------------------------------------------------
> > Learn Graph Databases - Download FREE O'Reilly Book
> > "Graph Databases" is the definitive new guide to graph databases and their
> > applications. Written by three acclaimed leaders in the field,
> > this first edition is now available. Download your free book today!
> > http://p.sf.net/sfu/NeoTech
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >
> 
> 
> 
> -- 
> If you ask me if it can be done. The answer is YES, it can always be done.
> The correct questions however are... What will it cost, and how long will
> it take?
> ------------------------------------------------------------------------------
> Learn Graph Databases - Download FREE O'Reilly Book
> "Graph Databases" is the definitive new guide to graph databases and their 
> applications. Written by three acclaimed leaders in the field, 
> this first edition is now available. Download your free book today!
> http://p.sf.net/sfu/NeoTech
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user


-- 
B Bruen <bbruen at ...2308...>




More information about the User mailing list