bcds.tools
Class FreqTable<E>

java.lang.Object
  extended by bcds.tools.FreqTable<E>

public class FreqTable<E>
extends java.lang.Object

Implements a simple frequency table of elements of the parameterized type E. This is a simple frequency builder; there is no support for classes, or lower/upper outliers, for example.

Author:
Juan Segovia S.

Constructor Summary
FreqTable()
          Default constructor.
 
Method Summary
 void clear()
          Resets the table.
 boolean contains(E e)
          Returns true if element e exists in the table.
 int getCount(E e)
          Returns the frequency (or counter) of the element e, or zero if that element does not exist.
 java.util.Collection<java.lang.Integer> getCounters()
          Returns an list containing the frequency of each element, in no particular order.
 long getTotal()
          Returns the number of occurrences registered in the table (not the number of elements), that is, the sum of the frequencies of all the elements in the table.
 void inc(E e)
          Calls incr(e, 1).
 void inc(E e, int incr)
          Performs the equivalent of T[e] += incr.
 java.util.Set<E> keySet()
          Returns the elements in the table.
static
<E> FreqTable<E>
make()
          Syntactic sugar.
 void remove(E e)
          Removes the element e.
 java.util.List<E> sortedByCounter()
          Returns a new list containing the elements of the table, sorted by their frequency in ascending order.
 java.util.List<E> sortedByCounterDesc()
          Returns a new list containing the elements of the table, sorted by their frequency in descending order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FreqTable

public FreqTable()
Default constructor.

Method Detail

make

public static <E> FreqTable<E> make()
Syntactic sugar.


inc

public void inc(E e,
                int incr)
Performs the equivalent of T[e] += incr.

Parameters:
e - the element whose frequency will be incremented. If it does not exist, it is inserted with intial count set to incr.
incr - the increment. It can be any value, including negatives. No check is performed to guarantee that the frequency remains positive.

inc

public void inc(E e)
Calls incr(e, 1).


getTotal

public long getTotal()
Returns the number of occurrences registered in the table (not the number of elements), that is, the sum of the frequencies of all the elements in the table.


getCount

public int getCount(E e)
Returns the frequency (or counter) of the element e, or zero if that element does not exist.


contains

public boolean contains(E e)
Returns true if element e exists in the table.


clear

public void clear()
Resets the table. All elements are discarded.


getCounters

public java.util.Collection<java.lang.Integer> getCounters()
Returns an list containing the frequency of each element, in no particular order. Beware that a temporary list is created in each invocation of this method, to which individual element counters are added one by one.


sortedByCounter

public java.util.List<E> sortedByCounter()
Returns a new list containing the elements of the table, sorted by their frequency in ascending order.


sortedByCounterDesc

public java.util.List<E> sortedByCounterDesc()
Returns a new list containing the elements of the table, sorted by their frequency in descending order.


keySet

public java.util.Set<E> keySet()
Returns the elements in the table. It is called "keySet" for consistency with other "collections".


remove

public void remove(E e)
Removes the element e. It does nothing if the element does not exist.