2 * Copyright (c) 2003-2005 The BISON Project
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 import java.io.PrintStream;
23 //XXX This implementation is very restricted, to be made more flexible
26 * A class that can collect frequency information on integer input.
27 * right now it can handle only unsigned input. It simply ignores negative
30 public class IncrementalFreq implements Cloneable {
33 // ===================== fields ========================================
34 // =====================================================================
36 /** The number of items inserted. */
39 /** freq[i] holds the frequency of i. primitive implementation, to be changed */
40 private int[] freq = null;
43 * The capacity, if larger than 0. Added values larger than or equal to
44 * this one will be ignored.
49 // ====================== initialization ==============================
50 // ====================================================================
54 * @param maxvalue Values in the input set larger than this one will be ignored.
55 * However, if it is negative, no values are ignored.
57 public IncrementalFreq(int maxvalue) {
63 // --------------------------------------------------------------------
65 /** Calls <code>this(-1)</code>, that is, no values will be ignored.
66 * @see #IncrementalFreq(int) */
67 public IncrementalFreq() {
72 // --------------------------------------------------------------------
74 /** Reset the state of the object. After calling this, all public methods
75 * behave the same as they did after constructing the object.
79 if( freq==null || N==0 ) freq = new int[0];
80 else for(int i=0; i<freq.length; ++i) freq[i]=0;
85 // ======================== methods ===================================
86 // ====================================================================
89 * Adds item <code>i</code> to the input set.
90 * It calls <code>add(i,1)</code>.
93 public final void add( int i ) { add(i,1); }
96 // --------------------------------------------------------------------
99 * Adds item <code>i</code> to the input set <code>k</code> times.
100 * That is, it increments counter <code>i</code> by <code>k</code>.
101 * If, however, <code>i</code> is negative, or larger than the maximum defined
102 * at construction time (if a maximum was set at all) the operation is ignored.
104 public void add( int i, int k ) {
106 if( N>0 && i>=N ) return;
107 if( i<0 || k<=0 ) return;
109 // Increase number of items by k.
112 // If index i is out of bounds for the current array of counters,
113 // increase the size of the array to i+1.
116 int tmp[] = new int[i+1];
117 System.arraycopy(freq, 0, tmp, 0, freq.length);
121 // Finally, increase counter i by k.
125 // --------------------------------------------------------------------
127 /** Returns number of processed data items.
128 * This is the number of items over which the class holds statistics.
130 public int getN() { return n; }
132 // --------------------------------------------------------------------
134 /** Returns the number of occurrences of the given integer. */
135 public int getFreq(int i) {
137 if( i>=0 && i<freq.length ) return freq[i];
141 // --------------------------------------------------------------------
145 * Performs an element-by-element vector subtraction of the
146 * frequency vectors. If <code>strict</code> is true, it
147 * throws an IllegalArgumentException if <code>this</code> is
148 * not strictly larger than <code>other</code> (element by element)
149 * (Note that both frequency vectors are positive.)
150 * Otherwise just sets those elements in <code>this</code> to zero
151 * that are smaller than those of <code>other</code>.
152 * @param other The instance of IncrementalFreq to subtract
153 * @param strict See above explanation
155 public void remove(IncrementalFreq other, boolean strict) {
157 // check if other has non-zero elements in non-overlapping part
158 if(strict && other.freq.length>freq.length)
160 for(int i=other.freq.length-1; i>=freq.length; --i)
162 if (other.freq[i]!=0)
163 throw new IllegalArgumentException();
167 final int minLength = Math.min(other.freq.length, freq.length);
168 for (int i=minLength-1; i>=0; i--)
170 if (strict && freq[i] < other.freq[i])
171 throw new IllegalArgumentException();
172 final int remove = Math.min(other.freq[i], freq[i]);
178 // ---------------------------------------------------------------------
181 * Prints current frequency information. Prints a separate line for
182 * all values from 0 to the capacity of the internal representation using the
187 * That is, numbers with zero occurrences will also be printed.
189 public void printAll( PrintStream out ) {
191 for(int i=0; i<freq.length; ++i)
193 out.println(i+" "+freq[i]);
197 // ---------------------------------------------------------------------
200 * Prints current frequency information. Prints a separate line for
201 * all values that have a number of occurrences different from zero using the
207 public void print( PrintStream out ) {
209 for(int i=0; i<freq.length; ++i)
211 if(freq[i]!=0) out.println(i+" "+freq[i]);
215 // ---------------------------------------------------------------------
217 public String toString() {
219 StringBuilder result=new StringBuilder("");
220 for(int i=0; i<freq.length; ++i)
223 result.append(" (").append(i).append(","
224 ).append(freq[i]).append(")");
226 return result.toString();
229 // ---------------------------------------------------------------------
231 /** An alternative method to convert the object to String */
232 public String toArithmeticExpression() {
234 StringBuilder result=new StringBuilder("");
235 for (int i=freq.length-1; i>=0; i--)
238 result.append(freq[i]).append(
239 "*").append(i).append("+");
242 if (result.length()==0)
245 return result.substring(0, result.length()-1);
248 // ---------------------------------------------------------------------
250 public Object clone() throws CloneNotSupportedException {
252 IncrementalFreq result = (IncrementalFreq)super.clone();
253 if( freq != null ) result.freq = freq.clone();
257 // ---------------------------------------------------------------------
260 * Tests equality between two IncrementalFreq instances.
261 * Two objects are equal if both hold the same set of numbers that have
262 * occurred non-zero times and the number of occurrences is also equal for
265 public boolean equals(Object obj) {
267 if( !( obj instanceof IncrementalFreq) ) return false;
268 IncrementalFreq other = (IncrementalFreq)obj;
269 final int minlength = Math.min(other.freq.length, freq.length);
271 for (int i=minlength-1; i>=0; i--)
272 if (freq[i] != other.freq[i])
275 if( freq.length > minlength ) other=this;
276 for (int i=minlength; i<other.freq.length; i++)
277 if( other.freq[i] != 0 )
283 // ---------------------------------------------------------------------
286 * Hashcode (consistent with {@link #equals}). Probably you will never want to
287 * use this, but since we have {@link #equals}, we must implement it.
289 public int hashCode() {
292 for(int i=0; i<freq.length; ++i) sum += freq[i]*i;
296 // ---------------------------------------------------------------------
299 public static void main(String[] pars) {
301 IncrementalFreq ifq = new IncrementalFreq(Integer.parseInt(pars[0]));
302 for(int i=1; i<pars.length; ++i)
304 ifq.add(Integer.parseInt(pars[i]));
306 ifq.print(System.out);
307 System.out.println(ifq);