001 /* Copyright 2004-2006 Stefan Ram. 002 This program is free software; you can redistribute it and/or modify 003 it under the terms of the GNU General Public License as published by 004 the Free Software Foundation; either version 2 of the License, or 005 (at your option) any later version. 006 This program is distributed in the hope that it will be useful, 007 but WITHOUT ANY WARRANTY; without even the implied warranty of 008 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 009 GNU General Public License for more details. 010 You should have received a copy of the GNU General Public License 011 along with this program; if not, write to the Free Software 012 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 013 package de.dclj.ram.system.iteration; 014 015 /** Enumerate all combinations of an array. 016 <pre><code> 017 import java.lang.String; 018 import java.lang.Object; 019 import de.dclj.ram.system.iteration.Combinations; 020 import static java.lang.System.out; 021 import java.util.Arrays; 022 // 2-combinations of "array" without repetition 023 // a combination of n things, taken t at a time 024 // i.e., select a subset of size t, t=subset size 025 // Kombinationen von "array" ohne Wiederholung zur Klasse 2. 026 public class Main 027 { public static void main( final String[] args ) 028 { final Object[] array = new Object[]{ "T", "H", "E" }; 029 for( final java.lang.Object[] o : new Combinations( 2, array )) 030 out.print( Arrays.toString( o )); }} 031 </code></pre> 032 <pre><code> 033 [T, H][T, E][H, E] 034 </code></pre> 035 @version slr@2008-04-25T05:47:49+02:00 036 @since slr@2007-06-05T08:12:16+02:00 037 @see <a HREF="http://www.purl.org/stefan_ram/java/de/dclj/ram/system/iteration/Combinations.java">source code</a> 038 @see <a HREF="http://www.purl.org/stefan_ram/html/ram.jar/de/dclj/ram/system/iteration/Combinations.html">documentation</a> 039 @see "<a href='http://www.purl.org/stefan_ram/pub/ram-jar'>Library homepage</a> (Must be opened in a <i>new window</i>, outside of frames, to avoid a 403 status code.)" */ 040 @de.dclj.ram.meta.quality.Cleaned(2) 041 @de.dclj.ram.meta.description.Copyright("Copright 2006 Stefan Ram") 042 @de.dclj.ram.meta.description.TypePath("de.dclj.ram.ram.system.iteration.Combinations") 043 @de.dclj.ram.meta.description.Cleared("slr@2008-04-25T05:47:49+02:00") 044 public class Combinations /* de.dclj.ram.system.iteration.Combinations */ 045 implements de.dclj.ram.system.iteration.Iteration<java.lang.Object[]> { 046 047 public final int zzSize; 048 public final java.lang.Object[] zzBase; 049 public final int zzLength; 050 public final int[] zzC; 051 public final java.lang.Object[] zzResult; 052 public boolean zzHasNext = true; 053 054 public Combinations(final int size, final java.lang.Object[] base) { 055 this.zzSize = size; 056 assert this.zzSize >= 0; 057 this.zzBase = base; 058 this.zzLength = this.zzBase.length; 059 assert this.zzLength >= this.zzSize; 060 this.zzResult = new java.lang.Object[this.zzSize]; 061 zzC = new int[this.zzSize + 3]; 062 { 063 int j; 064 065 for (j = 1; j <= this.zzSize; ++j) { 066 zzC[j] = j - 1; 067 } 068 zzC[this.zzSize + 1] = this.zzLength; 069 zzC[this.zzSize + 2] = 0; 070 } 071 advance(); 072 } 073 074 public final boolean hasNext() { 075 return this.zzHasNext; 076 } 077 078 public void advance() { 079 for (int i = 1; i <= this.zzSize; ++i) { 080 this.zzResult[i - 1] = this.zzBase[this.zzC[i]]; 081 } 082 } 083 084 public final java.lang.Object[] next() { 085 advance(); 086 if (this.zzHasNext) { 087 int j; 088 { 089 for (j = 1; this.zzC[j] + 1 == this.zzC[j + 1]; ++j) { 090 this.zzC[j] = j - 1; 091 } 092 } 093 if (j > this.zzSize) { 094 this.zzHasNext = false; 095 } else { 096 this.zzC[j] = this.zzC[j] + 1; 097 } 098 } else { 099 throw new java.util.NoSuchElementException(); 100 } 101 return this.zzResult; 102 } 103 104 public void remove() { 105 throw new java.lang.UnsupportedOperationException(); 106 } 107 108 public java.util.Iterator<java.lang.Object[]> iterator() { 109 return this; 110 } 111 }