Page 1 of 1

List and Map iterators

Posted: Mon Aug 02, 2010 7:34 pm
by Demivec
PureBasic currently has a limitation of only being able to have one current element for each List or Map.

I propose the possibility of creating a separate iterator type that can be used with lists or maps.

Similar solutions were made in the past when v4.00 of PureBasic was created. Remember the UseImage(), UseFile(), etc. that were removed in v4.00. Those changes made to v4.00 made it possible to access more than one object in those respective libraries at the same time. Something similar is needed for the LinkedList and Map librairies.

An iterator solution could take this form:

Code: Select all

NewList a.i()
AddElement(a()): a() = 1
AddElement(a()): a() = 5
AddElement(a()): a() = 3

;create an iterator which creates a pointer to a header for a list or map of a given type, but with its own current element
NewListIterator x.i() ;or NewMapIterator

;initialize the iterator to point to a specific list or map, this allows it to service more than one list or map of the same type
SetListIterator(x(), a()) ;or SetMapIterator

ResetList(x()) ;this would reset the current element in x(), not a()
While NextListElement(x())
  Debug a() + x() ;would display results for 3 + 1, 3 + 5, 3 + 3
Wend

;another example
ForEach x()
  ForEach a()
    If a() = x()
      Debug a() ;should display 1, 5, 3
    EndIf 
  Next
Next
Commands such as AddElement() or DeleteElement() would affect the original list or map header which the inerator is set to point to, but would affect only the current element stored in the iterator.

@Edit: corrected spelling of iterator. :P

Re: List and Map itinerators

Posted: Tue Aug 03, 2010 4:51 am
by akj
@Demivec:

Presumably you mean iterator, not itinerator

Re: List and Map iterators

Posted: Tue Aug 03, 2010 8:56 am
by Demivec
akj wrote:@Demivec:

Presumably you mean iterator, not itinerator
I do, I tried to spell it about 15 different ways and had settled on the one I thought was the least incorrect. :D Thanks for the correction, I've updated the first posting and topic title.