/*
 * @(#)BooleanStateArray.java    2.5.0   26 April 2006
 *
 * Copyright 2006
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.quick;

import java.util.*;

/**
 * The class <code>BooleanStateArray</code> tracks a
 * boolean state corresponding to a list of int keys
 * provided in the class constructor.
 *
 * @author  Richard Rasala
 * @version 2.5.0
 * @since   2.5.0
 */
public class BooleanStateArray
{
    /** The array of int keys. */
    private int[] keys = null;
    
    /** The corresponding array of boolean states. */
    private boolean[] states = null;
    
    
    /**
     * </p>Initializes the BooleanStateArray with the list
     * of keys whose boolean state will be maintained.</p>
     * 
     * <p>This list cannot be changed after construction.</p>
     * 
     * <p>If the given list contains duplicate int entries,
     * such duplicates will be removed in the internal data
     * structure.</p>
     * 
     * @param list the list of keys that will be associated
     *     with boolean states
     */
    public BooleanStateArray(int[] list) {
        int N = 0;
        
        if (list == null)
            keys = new int[0];
        else {
            N = list.length;
            
            keys = new int[N];
            
            for (int i = 0; i < N; i++)
                keys[i] = list[i];
        }
        
        Arrays.sort(keys);
        
        int duplicates = 0;
        
        for (int i = 1; i < N; i++)
            if (keys[i] == keys[i-1])
                duplicates++;
        
        if (duplicates > 0) {
            int M = N - duplicates;
            int[] copy = new int[M];
            
            copy[0] = keys[0];
            
            int k = 1;
            
            for (int i = 1; i < N; i++) {
                if (keys[i] != keys[i-1]) {
                    copy[k] = keys[i];
                    k++;
                }
            }
            
            N = M;
            keys = copy;
        }
        
        states = new boolean[N];
    }
    
    
    /**
     * <p>Returns the boolean state of the given key.</p>
     * 
     * <p>Returns false if the key was not given in
     * the list provided to the constructor.</p>
     * 
     * @param key the key whose state is desired
     */
    public boolean getState(int key) {
        int index = Arrays.binarySearch(keys, key);
        
        if (index >= 0)
            return states[index];
        else
            return false;
    }
    
    
    /**
     * <p>Sets the boolean state of the given key.</p>
     * 
     * <p>Does nothing if the key was not given in
     * the list provided to the constructor.</p>
     * 
     * @param key the key whose state is to be set
     * @param state the state to set
     */
    public void setState(int key, boolean state) {
        int index = Arrays.binarySearch(keys, key);
        
        if (index >= 0)
            states[index] = state;
    }
    
    /**
     * <p>Sets the states in the given list of keys
     * to the given state.</p>
     * 
     * @param list the list of keys whose state is to be set
     * @param state the state to set
     */
    public void setState(int[] list, boolean state) {
        if (list == null)
            return;
        
        int N = list.length;
        
        for (int i = 0; i < N; i++)
            setState(list[i], state);
    }
    
    
    /**
     * <p>Sets all states to the given state.</p>
     * 
     * @param state the state to set
     */
    public void setAllStates(boolean state) {
        int N = states.length;
        
        for (int i = 0; i < N; i++)
            states[i] = state;
    }
    
}
