Skip to content

Commit 130b913

Browse files
committed
More javadoc comments were added to subset generator
1 parent d8b5a06 commit 130b913

File tree

3 files changed

+225
-138
lines changed

3 files changed

+225
-138
lines changed

src/org/paukov/combinatorics/subsets/SubSetGenerator.java

Lines changed: 113 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,124 @@
77

88
/**
99
* This generator generates all subsets of the specified set (core set)
10+
* <p>
11+
* A set A is a subset of a set B if A is "contained" inside B. A and B may
12+
* coincide. The relationship of one set being a subset of another is called
13+
* inclusion or sometimes containment.
14+
* <p>
15+
* Examples:
16+
* <ul>
17+
* <li>The set {1, 2} is a proper subset of {1, 2, 3}.
18+
* <li>Any set is a subset of itself, but not a proper subset.
19+
* <li>The empty set, denoted by {}, is also a subset of any given set X.
20+
* </ul>
21+
* <p>
22+
* All subsets of {1, 2, 3} are:
23+
* <ol>
24+
* <li>{}
25+
* <li>{1}
26+
* <li>{2}
27+
* <li>{1, 2}
28+
* <li>{3}
29+
* <li>{1, 3}
30+
* <li>{2, 3}
31+
* <li>{1, 2, 3}
32+
* </ol>
33+
* <p>
34+
* And code which generates all subsets of {one, two, three}
35+
* <p>
36+
* <blockquote>
1037
*
38+
* <pre>
39+
* // create array of initial items
40+
* ArrayList&lt;String&gt; array = new ArrayList&lt;String&gt;();
41+
* array.add(&quot;one&quot;);
42+
* array.add(&quot;two&quot;);
43+
* array.add(&quot;three&quot;);
44+
*
45+
* // create combinatorics vector
46+
* CombinatoricsVector&lt;String&gt; initialVector = new CombinatoricsVector&lt;String&gt;(
47+
* array);
48+
*
49+
* // create subset generator
50+
* Generator&lt;String&gt; gen = new SubSetGenerator&lt;String&gt;(initialVector);
51+
*
52+
* // create iterator
53+
* Iterator&lt;CombinatoricsVector&lt;String&gt;&gt; itr = gen.createIterator();
54+
* // print the number of subsets
55+
* System.out
56+
* .println(&quot;Number of subsets is: &quot; + gen.getNumberOfGeneratedObjects());
57+
*
58+
* // go through the iterator
59+
* while (itr.hasNext()) {
60+
* CombinatoricsVector&lt;String&gt; subSet = itr.next();
61+
* System.out.println(subSet);
62+
* }
63+
* </pre>
64+
*
65+
* </blockquote>
66+
* <p>
67+
* And the result
68+
* <p>
69+
* <blockquote>
70+
*
71+
* <pre>
72+
* Number of subsets is: 8
73+
* SubSetIterator=[#1, CombinatoricsVector=[[]], size=0]]
74+
* SubSetIterator=[#2, CombinatoricsVector=[[one]], size=1]]
75+
* SubSetIterator=[#3, CombinatoricsVector=[[two]], size=1]]
76+
* SubSetIterator=[#4, CombinatoricsVector=[[one, two]], size=2]]
77+
* SubSetIterator=[#5, CombinatoricsVector=[[three]], size=1]]
78+
* SubSetIterator=[#6, CombinatoricsVector=[[one, three]], size=2]]
79+
* SubSetIterator=[#7, CombinatoricsVector=[[two, three]], size=2]]
80+
* SubSetIterator=[#8, CombinatoricsVector=[[one, two, three]], size=3]]
81+
* </pre>
82+
*
83+
* </blockquote>
84+
* <p>
85+
*
86+
* @author Dmytro.Paukov
87+
* @see CombinatoricsVector
88+
* @see SubSetIterator
1189
* @param <T>
1290
* Type of elements in the set
1391
*/
1492
public class SubSetGenerator<T> extends Generator<T> {
1593

16-
/**
17-
* Core set
18-
*/
19-
protected final CombinatoricsVector<T> _coreSet;
20-
21-
/**
22-
* Constructor
23-
*
24-
* @param coreSet
25-
* Core set
26-
*/
27-
public SubSetGenerator(CombinatoricsVector<T> coreSet) {
28-
_coreSet = new CombinatoricsVector<T>(coreSet);
29-
}
30-
31-
/**
32-
* Returns the core set
33-
*/
34-
public CombinatoricsVector<T> getCoreObject() {
35-
return _coreSet;
36-
}
37-
38-
/**
39-
* Returns the number of the subsets
40-
*/
41-
public long getNumberOfGeneratedObjects() {
42-
return Util.pow2(_coreSet.getSize());
43-
}
44-
45-
/**
46-
* Creates the iterator over the all subsets
47-
*/
48-
public Iterator<CombinatoricsVector<T>> createIterator() {
49-
return new SubSetIterator<T>(this);
50-
}
94+
/**
95+
* Core set
96+
*/
97+
protected final CombinatoricsVector<T> _coreSet;
98+
99+
/**
100+
* Constructor
101+
*
102+
* @param coreSet
103+
* Core set
104+
*/
105+
public SubSetGenerator(CombinatoricsVector<T> coreSet) {
106+
_coreSet = new CombinatoricsVector<T>(coreSet);
107+
}
108+
109+
/**
110+
* Returns the core set
111+
*/
112+
public CombinatoricsVector<T> getCoreObject() {
113+
return _coreSet;
114+
}
115+
116+
/**
117+
* Returns the number of the subsets
118+
*/
119+
public long getNumberOfGeneratedObjects() {
120+
return Util.pow2(_coreSet.getSize());
121+
}
122+
123+
/**
124+
* Creates the iterator over the all subsets
125+
*/
126+
public Iterator<CombinatoricsVector<T>> createIterator() {
127+
return new SubSetIterator<T>(this);
128+
}
51129

52130
}

src/org/paukov/combinatorics/subsets/SubSetIterator.java

Lines changed: 107 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -7,117 +7,121 @@
77
/**
88
* Iterator over the all subsets
99
*
10+
* @author Dmytro.Paukov
11+
* @see CombinatoricsVector
12+
* @see SubSetGenerator
13+
*
1014
* @param <T>
1115
* Type of elements of subset
1216
*/
1317
public class SubSetIterator<T> extends Iterator<CombinatoricsVector<T>> {
1418

15-
/**
16-
* Subset generator
17-
*/
18-
protected final Generator<T> _generator;
19-
20-
/**
21-
* Current subset
22-
*/
23-
protected CombinatoricsVector<T> _currentSubSet = null;
24-
25-
/**
26-
* Index of the current subset
27-
*/
28-
protected long _currentIndex = 0;
29-
30-
/**
31-
* Size of the subset
32-
*/
33-
protected final int _length;
34-
35-
/**
36-
* internal bit vector, representing the subset
37-
*/
38-
private int[] _bitVector = null;
39-
40-
/**
41-
* Constructor
42-
*
43-
* @param generator
44-
* The subset generator
45-
*/
46-
public SubSetIterator(Generator<T> generator) {
47-
_generator = generator;
48-
_length = generator.getCoreObject().getSize();
49-
_currentSubSet = new CombinatoricsVector<T>();
50-
_bitVector = new int[_length + 2];
51-
init();
52-
}
53-
54-
/**
55-
* initialize the iterator
56-
*
57-
* @see org.paukov.combinatorics.iterator.Iterator#first()
58-
*/
59-
private void init() {
60-
_currentIndex = 0;
61-
for (int i = 0; i <= _length + 1; i++) {
62-
_bitVector[i] = 0;
19+
/**
20+
* Subset generator
21+
*/
22+
protected final Generator<T> _generator;
23+
24+
/**
25+
* Current subset
26+
*/
27+
protected CombinatoricsVector<T> _currentSubSet = null;
28+
29+
/**
30+
* Index of the current subset
31+
*/
32+
protected long _currentIndex = 0;
33+
34+
/**
35+
* Size of the subset
36+
*/
37+
protected final int _length;
38+
39+
/**
40+
* internal bit vector, representing the subset
41+
*/
42+
private int[] _bitVector = null;
43+
44+
/**
45+
* Constructor
46+
*
47+
* @param generator
48+
* The subset generator
49+
*/
50+
public SubSetIterator(Generator<T> generator) {
51+
_generator = generator;
52+
_length = generator.getCoreObject().getSize();
53+
_currentSubSet = new CombinatoricsVector<T>();
54+
_bitVector = new int[_length + 2];
55+
init();
6356
}
6457

65-
}
66-
67-
/**
68-
* Returns the current subset
69-
*
70-
* @see org.paukov.combinatorics.iterator.Iterator#getCurrentItem()
71-
*/
72-
@Override
73-
public CombinatoricsVector<T> getCurrentItem() {
74-
return _currentSubSet;
75-
}
76-
77-
/**
78-
* Returns true if iteration is done, otherwise false
79-
*
80-
* @see org.paukov.combinatorics.iterator.Iterator#isDone()
81-
*/
82-
@Override
83-
public boolean isDone() {
84-
return _bitVector[_length + 1] == 1;
85-
}
86-
87-
/**
88-
* Returns the next subset if it is available
89-
*
90-
* @see org.paukov.combinatorics.iterator.Iterator#next()
91-
*/
92-
@Override
93-
public CombinatoricsVector<T> next() {
94-
_currentIndex++;
95-
_currentSubSet.getVector().clear();
96-
for (int index = 1; index <= _length; index++) {
97-
if (_bitVector[index] == 1) {
98-
T value = _generator.getCoreObject().getValue(index - 1);
99-
_currentSubSet.getVector().add(value);
100-
}
58+
/**
59+
* initialize the iterator
60+
*
61+
* @see org.paukov.combinatorics.iterator.Iterator#first()
62+
*/
63+
private void init() {
64+
_currentIndex = 0;
65+
for (int i = 0; i <= _length + 1; i++) {
66+
_bitVector[i] = 0;
67+
}
68+
10169
}
102-
int i = 1;
103-
while (_bitVector[i] == 1) {
104-
_bitVector[i] = 0;
105-
i++;
70+
71+
/**
72+
* Returns the current subset
73+
*
74+
* @see org.paukov.combinatorics.iterator.Iterator#getCurrentItem()
75+
*/
76+
@Override
77+
public CombinatoricsVector<T> getCurrentItem() {
78+
return _currentSubSet;
79+
}
80+
81+
/**
82+
* Returns true if iteration is done, otherwise false
83+
*
84+
* @see org.paukov.combinatorics.iterator.Iterator#isDone()
85+
*/
86+
@Override
87+
public boolean isDone() {
88+
return _bitVector[_length + 1] == 1;
89+
}
90+
91+
/**
92+
* Returns the next subset if it is available
93+
*
94+
* @see org.paukov.combinatorics.iterator.Iterator#next()
95+
*/
96+
@Override
97+
public CombinatoricsVector<T> next() {
98+
_currentIndex++;
99+
_currentSubSet.getVector().clear();
100+
for (int index = 1; index <= _length; index++) {
101+
if (_bitVector[index] == 1) {
102+
T value = _generator.getCoreObject().getValue(index - 1);
103+
_currentSubSet.getVector().add(value);
104+
}
105+
}
106+
int i = 1;
107+
while (_bitVector[i] == 1) {
108+
_bitVector[i] = 0;
109+
i++;
110+
}
111+
_bitVector[i] = 1;
112+
113+
return getCurrentItem();
114+
}
115+
116+
/**
117+
* Convert
118+
*
119+
* @see java.lang.Object#toString()
120+
*/
121+
@Override
122+
public String toString() {
123+
return "SubSetIterator=[#" + _currentIndex + ", " + _currentSubSet
124+
+ "]";
106125
}
107-
_bitVector[i] = 1;
108-
109-
return getCurrentItem();
110-
}
111-
112-
/**
113-
* Convert
114-
*
115-
* @see java.lang.Object#toString()
116-
*/
117-
@Override
118-
public String toString() {
119-
return "SubSetIterator=[#" + _currentIndex + ", " + _currentSubSet
120-
+ "]";
121-
}
122126

123127
}

src/org/paukov/combinatorics/util/Util.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
import java.awt.geom.Point2D;
55
import java.math.BigDecimal;
66

7+
import org.paukov.combinatorics.CombinatoricsVector;
8+
79
/**
810
* Utility class for combinatorial package
11+
*
12+
* @author Dmytro.Paukov
13+
* @see CombinatoricsVector
914
*/
1015
public class Util {
1116

0 commit comments

Comments
 (0)