question about JFormMap.nextKey()

39 views
Skip to first unread message

jimka...@gmail.com

unread,
Apr 10, 2015, 12:31:11 AM4/10/15
to jcontain...@googlegroups.com
If I am iterating over a JFormMap using the nextKey function, as described in the Papyrus source, and I make changes to the JFormMap during iteration, including adding or removing entries, will I reliably iterate over every entry that was a member of JFormMap at the point I started iterating (except, of course, for those members that were removed)? What happens if I call JFormMap.nextKey(map, key), where key is no longer in map?

silverice

unread,
Apr 10, 2015, 5:55:35 AM4/10/15
to jimka...@gmail.com, jcontain...@googlegroups.com
If you are modifying things during iteration you should expect surprises
During iteration a nextKey function returns sorted sequence of keys:
[kA,kB,kC,<current position>, kE, kF].
If you'll add 'k0' (before kA) you'll not visit it. If you'll add kJ (after kF) you'll visit it, if you will removed kJ -  you will not visit it


> What happens if I call JFormMap.nextKey(map, key), where key is no longer in map?

If the key is not in map the function returns 'endKey' value (i.e. exactly what you passed into nextKey func.)

On Fri, Apr 10, 2015 at 7:31 AM, <jimka...@gmail.com> wrote:
If I am iterating over a JFormMap using the nextKey function, as described in the Papyrus source, and I make changes to the JFormMap during iteration, including adding or removing entries, will I reliably iterate over every entry that was a member of JFormMap at the point I started iterating (except, of course, for those members that were removed)? What happens if I call JFormMap.nextKey(map, key), where key is no longer in map?

--
You received this message because you are subscribed to the Google Groups "jcontainers-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jcontainers-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jcontainers-users/aa3bf879-e420-45e6-bc23-cefaebe3a869%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kais...@mail.gvsu.edu

unread,
Apr 10, 2015, 10:13:34 AM4/10/15
to jcontain...@googlegroups.com, jimka...@gmail.com
OK, that is basically what I thought. What I want to do is iterate over the collection and, under certain circumstances, remove some keys as I find them. So, my algorithm is something like this:

Form key = JFormMap.nextKey(map)
Form nKey
while key
  nkey = JFormMap.nextKey(map, key)
  if readyToRemove(key)
    JFormMap.removeKey(map, key)
  endif
  key = nKey
endwhile

Based on your reply, I think this algorithm will work as expected. That is, every element should be iterated over, and the ones I want to remove will be removed. Do you agree?

I am also interested in what would happen if another thread added an element to the map while I am iterating over it. From your reply, I assume that, if it is sorted to a position before key (and possibly if it is just before nKey), it will not be iterated over. But, if it is sorted to a position after them, the algorithm will reach it. Is this correct?

One last question: am I correct that the order that elements are iterated over corresponds to their index, so that JFormMap.getNthKey(2) is the third element (indexing from 0) that I will reach using JFormMap.nextKey()?

Thanks again for your help!

silverice

unread,
Apr 10, 2015, 10:48:50 AM4/10/15
to kais...@mail.gvsu.edu, jcontain...@googlegroups.com, Jim Kaiser
Yeah, this should work, this is the same what some C++ programmers doing during tree iteration

> I am also interested in what would happen if another thread added an element to the map while I am iterating over it. From your reply, I assume that, if it is sorted to a position before key (and possibly if it is just before nKey), it will not be iterated over. But, if it is sorted to a position after them, the algorithm will reach it. Is this correct?

Right


> One last question: am I correct that the order that elements are iterated over corresponds to their index, so that JFormMap.getNthKey(2) is the third element (indexing from 0) that I will reach using JFormMap.nextKey()?

yeah, index 2 is the third element

> Thanks again for your help!

Not at all :)

Reply all
Reply to author
Forward
0 new messages