Hi,
I have a method where I try to complete a partial function. The
function consists of a hash map mapping a self-defined tuple class to
states (another class of mine).
To do the completion, I have to build the product set of all possible
inputs, check each of them, and if there is no value defined for that
input yet, add an entry into the hash map that maps the input to a given
default value.
The input class consists of a symbol and an array of states. The length
of the array can be read from the symbol´s arity.
So first I gather all symbols together in a set, and all states that can
be used as input. I´m pretty sure this works fine. Then I start
building the product set as follows:
- loop over all possible symbols with a foreach
- now the real problem: go through each possible array of states in the
given set with aritty symbol.getArity().
I thought of doing this as follows: define an array of iterators over
the set of states, then advance the first iterator till the end, then
for each step of the second iterator, loop through the first one
entirely, then for each step of the third, loop through the second one
entirely, thereby looping through the first one for every step...
I implemented this with a recursive function which does the step and
calls the previous iterator to do its loop. Now formally, this seems to
be correct, but the result is not. After some debugging, I figured out
that the iterators in this array are not independent! So if I have two
iterators over the same set (standard HashSet<State>), they do not
iterate independently!!
I see a possible solution in taking a copy of the set and asking for its
iterator for every array element, but this seems very inefficient,
creating a lot of new set objects (in my tests, these sets are small,
but they can and probably will be very big in the end program (thinking
of millions here), so this is not an option).
==> TWO QUESTIONS:
- How can I get those iterators to work independantly.
- Does anybody know of a better method to achieve my goal.
I can post some example code if needed, but it is more of a theoretical
question. Anyway, I posted the whole code yesterday, asking for some
comments, but I suppose it was too long, there was no response.
Many thanks, H.

Signature
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org
Mark Thomas - 09 Dec 2005 13:49 GMT
><snip>
> I implemented this with a recursive function which does the step and
[quoted text clipped - 4 lines]
> iterate independently!!
><snip>
Are you sure Hendrik? I knocked up a quick test which indicates to me
that they are independent - have a look and see if I've made an
incorrect assumption:
import java.util.*;
public class Test {
public static void main(String args[]) {
String[] letters =
{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};
HashSet<String> set = new HashSet<String>(Arrays.asList(letters));
Iterator it1 = set.iterator();
Iterator it2 = set.iterator();
while(it1.hasNext()&&it2.hasNext()) {
System.out.print(it1.next());
System.out.print(it2.next());
}
}
}
Gives:
ooddkkhhccllggbbiimmaaffjjnnee
- showing each iterator getting it's own 'next'.
Mark
Hendrik Maryns - 09 Dec 2005 14:11 GMT
Mark Thomas schreef:
>> <snip> I implemented this with a recursive function which does the
>> step and calls the previous iterator to do its loop. Now formally,
[quoted text clipped - 30 lines]
>
> - showing each iterator getting it's own 'next'.
Indeed, once again I posted too quickly, and found my mistake just after
hitting the send button...
The thing was I initialised that array with Arrays.fill, which of course
assigns the *same* iterator to every slot. Stupid. I tried to cancel
my post, but once again, the servers were too quick!
Thanks.
If anybody has suggestions about better ways to implement the cross
product, still welcome!
Cheers, H.

Signature
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org