-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjava_example.java
165 lines (140 loc) · 5.63 KB
/
java_example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import com.jordanzimmerman.KWayMerge;
import com.jordanzimmerman.KWayMergeIterator;
import com.jordanzimmerman.KWayMergeError;
import java.text.NumberFormat;
/**
* A contrived test of KWayMerge. emulates n number of arrays each with n sequential integers.
* Calls KWayMerge on the buckets as well as StandardMerge. Outputs the merged buckets and then
* reports on the number of comparisons. The ints are converted to Strings for comparison to better
* simulate real-world use. Standard int comparison is too fast to measure well.<p>
*
* A good test is 6 buckets of 10000 items per bucket
*/
public class test
{
public static void main(String[] args) throws Exception
{
if ( args.length != 2 )
{
System.out.println("test <number of buckets> <items in each bucket>");
return;
}
int bucketQty = Integer.parseInt(args[0]);
int itemQty = Integer.parseInt(args[1]);
test t = new test();
t.execute(bucketQty, itemQty);
}
private void execute(int bucketQty, int itemQty) throws KWayMergeError
{
int standardCount = doStandard(bucketQty, itemQty);
int kWayCount = doKWay(bucketQty, itemQty);
System.out.println("Standard: " + standardCount + " comparisons.");
System.out.println();
System.out.println("K-Way: " + kWayCount + " comparisons.");
System.out.println();
String winner = (kWayCount < standardCount) ? "K-Way" : "Standard";
int winnerCount = (kWayCount < standardCount) ? kWayCount : standardCount;
int loserCount = (kWayCount > standardCount) ? kWayCount : standardCount;
System.out.println(winner + " had " + (loserCount - winnerCount) + " fewer comparisons ~" + ((100 * (loserCount - winnerCount)) / loserCount) + "%");
}
private int doStandard(int bucketQty, int itemQty) throws KWayMergeError
{
testIterator[] iterators = new testIterator[bucketQty];
StandardMerge<testIterator> standard = new StandardMerge<testIterator>();
for ( int i = 0; i < bucketQty; ++i )
{
iterators[i] = new testIterator(itemQty, i * itemQty);
standard.add(iterators[i]);
}
while ( standard.advance() )
{
System.out.println(standard.current());
}
System.out.println();
int count = 0;
for ( testIterator iterator : iterators )
{
count += iterator.getCompareCount();
}
return count;
}
private int doKWay(int bucketQty, int itemQty) throws KWayMergeError
{
testIterator[] iterators = new testIterator[bucketQty];
KWayMerge<testIterator> merge = new KWayMerge<testIterator>();
for ( int i = 0; i < bucketQty; ++i )
{
iterators[i] = new testIterator(itemQty, i * itemQty);
merge.add(iterators[i]);
}
merge.build();
while ( merge.advance() )
{
System.out.println(merge.current());
}
System.out.println();
int count = 0;
for ( testIterator iterator : iterators )
{
count += iterator.getCompareCount();
}
return count;
}
private static final NumberFormat fIntegerInstance = NumberFormat.getIntegerInstance();
static
{
fIntegerInstance.setMinimumIntegerDigits(10);
fIntegerInstance.setGroupingUsed(false);
}
private class testIterator implements KWayMergeIterator<testIterator>
{
public testIterator(int itemQty, int startNumber)
{
fItemQty = itemQty;
fIndex = -1;
fStartNumber = startNumber;
fIsDone = false;
fCompareCount = 0;
}
public boolean isDone() throws KWayMergeError
{
return fIsDone;
}
public void advance() throws KWayMergeError
{
if ( !fIsDone)
{
if ( (fIndex + 1) >= fItemQty)
{
fIsDone = true;
}
else
{
++fIndex;
}
}
}
public testIterator compare(testIterator rhs) throws KWayMergeError
{
++fCompareCount;
return (currentValue().compareTo(rhs.currentValue()) <= 0) ? this : rhs;
}
public String toString()
{
return currentValue();
}
public int getCompareCount()
{
return fCompareCount;
}
private String currentValue()
{
return fIntegerInstance.format(fIndex * fStartNumber);
}
private int fItemQty;
private int fIndex;
private int fStartNumber;
private boolean fIsDone;
private int fCompareCount;
}
}