package simpledb;

import java.util.*;

/**
 * The common interface for any class that can compute an aggregate over a
 * list of Tuples.
 */
public interface Aggregator {
    public enum Op {
        MIN, MAX, SUM, AVG, COUNT;

        /**
         * Interface to access operations by a string containing an integer
         * index for command-line convenience.
         *
         * @param s a string containing a valid integer Op index
         */
        public static Op getOp(String s) {
            return getOp(Integer.parseInt(s));
        }

        /**
         * Interface to access operations by integer value for command-line
         * convenience.
         *
         * @param i a valid integer Op index
         */
        public static Op getOp(int i) {
            switch(i) {
            case 0: return MIN;
            case 1: return MAX;
            case 2: return SUM;
            case 3: return AVG;
            case 4: return COUNT;
            }
            return null;
        }
    };

    /**
     * Merge a new tuple into the aggregate for a distinct group value;
     * creates a new group aggregate result if the group value has not yet
     * been encountered.
     *
     * @param tup the Tuple containing an aggregate field and a group-by field
     * @param gfield the 0-based index of the group-by field in the tuple
     * @param afield the 0-based index of the aggregate field in the tuple
     */
    public void merge(Tuple tup, int gfield, int afield);

    /**
     * Create a DbIterator over group aggregate results.
     */
    public DbIterator iterator();

    public TupleDesc getTupleDesc();

}
