Re: A little algorithmic problem...
dushkin <dushkin@012.net.il> writes:
How can I effectivly generate a set of all string possibilities?
When replying to my post, please do not quote my complete
post, but only those parts you directly refer to.
Especially, please do not quote the complete source code.
Otherwise, I will refrain from posting source code to this group.
class Possibility implements
java.lang.Iterable<java.lang.Object>,
java.util.Iterator<java.lang.Object>
{ public Possibility( final java.lang.Object ... array )
{ this.array = array; this.offset = 0; }
public boolean hasNext(){ return this.offset < this.array.length; }
public java.lang.Object next()
{ if( !this.hasNext() )this.offset = 0;
return this.array[ this.offset++ ]; }
public java.util.Iterator<java.lang.Object>iterator(){ return this; }
public void remove()
{ throw new java.lang.UnsupportedOperationException(); }
private final java.lang.Object[] array;
private int offset; }
class Possibilities implements
java.lang.Iterable<java.lang.Object[]>,
java.util.Iterator<java.lang.Object[]>
{ public Possibilities( final Possibility ... array )
{ this.array = array; this.offset = 0;
this.hasNext = true;
this.length = this.array.length;
this.result = new java.lang.Object[ this.length ];
for( int i = 0; i < this.length; ++i )
{ if( this.array[ i ].hasNext() )result[ i ]= this.array[ i ].next();
else throw new java.lang.RuntimeException(); }}
public boolean hasNext(){ return this.hasNext; }
public java.lang.Object[] next()
{ final java.lang.Object[] result =
java.util.Arrays.copyOf( this.result, this.length );
this.advance();
return result; }
private void advance(){ this.advance( this.length - 1 ); }
private void advance( final int offset )
{ if( offset < 0 )
this.hasNext = false;
else
{ if( this.array[ offset ].hasNext() )
this.result[ offset ]= this.array[ offset ].next();
else
{ this.result[ offset ]= this.array[ offset ].next();
this.advance( offset - 1 ); }}}
public java.util.Iterator<java.lang.Object[]>iterator(){ return this; }
public void remove()
{ throw new java.lang.UnsupportedOperationException(); }
private java.lang.Object[] result;
private final Possibility[] array;
private int offset;
private final int length;
private boolean hasNext; }
public class Main
{ public static void main( final java.lang.String[] args )
{ for
( final java.lang.Object[] o :
new Possibilities
( new Possibility( "alpha", "beta" ),
new Possibility( "0", "1" ),
new Possibility( "x", "y", "z" )))
{ java.lang.System.out.printf( "%s-%s-%s%n", o[ 0 ], o[ 1 ], o[ 2 ]); }}}
alpha-0-x
alpha-0-y
alpha-0-z
alpha-1-x
alpha-1-y
alpha-1-z
beta-0-x
beta-0-y
beta-0-z
beta-1-x
beta-1-y
beta-1-z
When replying to my post, please do not quote my complete
post, but only those parts you directly refer to.
Especially, please do not quote the complete source code.
Otherwise, I will refrain from posting source code to this group.